ManaPlus
Public Types | Public Member Functions | Static Public Member Functions | Data Fields | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes
CompoundSprite Class Reference

#include <compoundsprite.h>

Inheritance diagram for CompoundSprite:
Sprite ActorSprite Being FloorItem LocalPlayer

Public Types

typedef std::vector< Sprite * >::iterator SpriteIterator
 
typedef std::vector< Sprite * >::const_iterator SpriteConstIterator
 

Public Member Functions

 CompoundSprite ()
 
virtual ~CompoundSprite ()
 
bool reset ()
 
bool play (const std::string &action)
 
bool update (const int time)
 
void drawSimple (Graphics *const graphics, const int posX, const int posY) const
 
int getWidth () const
 
int getHeight () const
 
const ImagegetImage () const
 
bool setSpriteDirection (const SpriteDirection::Type direction)
 
int getNumberOfLayers () const
 
unsigned int getCurrentFrame () const
 
unsigned int getFrameCount () const
 
void addSprite (Sprite *const sprite)
 
void setSprite (const size_t layer, Sprite *const sprite)
 
void removeSprite (const int layer)
 
void clear ()
 
void ensureSize (const size_t layerCount)
 
void drawSprites (Graphics *const graphics, const int posX, const int posY) const
 
virtual void drawSpritesSDL (Graphics *const graphics, const int posX, const int posY) const
 
void setAlpha (float alpha)
 
bool updateNumber (const unsigned num)
 
int getLastTime () const
 
int getStartTime () const
 
- Public Member Functions inherited from Sprite
virtual ~Sprite ()
 
virtual void draw (Graphics *const graphics, const int posX, const int posY) const =0
 
virtual float getAlpha () const
 
virtual const void * getHash () const
 
virtual const void * getHash2 () const
 

Static Public Member Functions

static void setEnableDelay (bool b)
 

Data Fields

std::vector< Sprite * > mSprites
 

Protected Types

typedef std::list< CompoundItem * > ImagesCache
 

Protected Member Functions

void redraw () const
 
void updateImages () const
 
bool updateFromCache () const
 
void initCurrentCacheItem () const
 
- Protected Member Functions inherited from Sprite
 Sprite ()
 

Protected Attributes

ImagesCache imagesCache
 
CompoundItemmCacheItem
 
ImagemImage
 
ImagemAlphaImage
 
int mOffsetX
 
int mOffsetY
 
int mStartTime
 
int mLastTime
 
int mNextRedrawTime
 
bool mNeedsRedraw
 
bool mEnableAlphaFix
 
bool mDisableAdvBeingCaching
 
bool mDisableBeingCaching
 
- Protected Attributes inherited from Sprite
float mAlpha
 

Static Protected Attributes

static bool mEnableDelay = true
 

Detailed Description

Definition at line 36 of file compoundsprite.h.

Member Typedef Documentation

◆ ImagesCache

typedef std::list<CompoundItem*> CompoundSprite::ImagesCache
protected

Definition at line 121 of file compoundsprite.h.

◆ SpriteConstIterator

typedef std::vector<Sprite*>::const_iterator CompoundSprite::SpriteConstIterator

Definition at line 40 of file compoundsprite.h.

◆ SpriteIterator

typedef std::vector<Sprite*>::iterator CompoundSprite::SpriteIterator

Definition at line 39 of file compoundsprite.h.

Constructor & Destructor Documentation

◆ CompoundSprite()

CompoundSprite::CompoundSprite ( )

Definition at line 71 of file compoundsprite.cpp.

References Sprite::mAlpha.

71  :
72  Sprite(),
73  mSprites(),
74  imagesCache(),
75  mCacheItem(nullptr),
76  mImage(nullptr),
77  mAlphaImage(nullptr),
78  mOffsetX(0),
79  mOffsetY(0),
80  mStartTime(0),
81  mLastTime(0),
82 #ifndef USE_SDL2
83  mNextRedrawTime(0),
84 #endif // USE_SDL2
85  mNeedsRedraw(false),
86  mEnableAlphaFix(config.getBoolValue("enableAlphaFix")),
87  mDisableAdvBeingCaching(config.getBoolValue("disableAdvBeingCaching")),
88  mDisableBeingCaching(config.getBoolValue("disableBeingCaching"))
89 {
90  mAlpha = 1.0F;
91 }
CompoundItem * mCacheItem
Sprite()
Definition: sprite.h:124
Configuration config
bool getBoolValue(const std::string &key) const
ImagesCache imagesCache
std::vector< Sprite * > mSprites
bool mDisableAdvBeingCaching
float mAlpha
Definition: sprite.h:129

◆ ~CompoundSprite()

CompoundSprite::~CompoundSprite ( )
virtual

Definition at line 93 of file compoundsprite.cpp.

References clear(), mAlphaImage, and mImage.

94 {
95  clear();
96  mImage = nullptr;
97  mAlphaImage = nullptr;
98 }

Member Function Documentation

◆ addSprite()

void CompoundSprite::addSprite ( Sprite *const  sprite)

Definition at line 270 of file compoundsprite.cpp.

References mNeedsRedraw, and mSprites.

Referenced by ActorSprite::setupSpriteDisplay().

271 {
272  mSprites.push_back(sprite);
273  mNeedsRedraw = true;
274 }
std::vector< Sprite * > mSprites

◆ clear()

void CompoundSprite::clear ( )

Definition at line 297 of file compoundsprite.cpp.

References delete2, delete_all(), imagesCache, mCacheItem, mLastTime, mNeedsRedraw, and mSprites.

Referenced by ActorSprite::setupSpriteDisplay(), and ~CompoundSprite().

298 {
299  // Skip if it won't change anything
300  if (!mSprites.empty())
301  {
303  mSprites.clear();
304  }
305  mNeedsRedraw = true;
307  imagesCache.clear();
309  mLastTime = 0;
310 }
CompoundItem * mCacheItem
#define delete2(var)
Definition: delete2.h:24
ImagesCache imagesCache
void delete_all(Container &c)
Definition: dtor.h:55
std::vector< Sprite * > mSprites

◆ drawSimple()

void CompoundSprite::drawSimple ( Graphics *const  graphics,
const int  posX,
const int  posY 
) const

Definition at line 148 of file compoundsprite.cpp.

References Graphics::drawImage(), drawSprites(), FUNC_BLOCK, Sprite::mAlpha, mAlphaImage, mImage, mNeedsRedraw, mOffsetX, mOffsetY, mSprites, Image::setAlpha(), and updateImages().

Referenced by FloorItem::draw(), Being::drawElementalSpriteAt(), Being::drawHomunculusSpriteAt(), Being::drawMercenarySpriteAt(), Being::drawMonsterSpriteAt(), Being::drawOtherSpriteAt(), and Being::drawPortalSpriteAt().

151 {
152  FUNC_BLOCK("CompoundSprite::draw", 1)
153  if (mNeedsRedraw)
154  updateImages();
155 
156  if (mSprites.empty()) // Nothing to draw
157  return;
158 
159  if (mAlpha == 1.0F && (mImage != nullptr))
160  {
161  graphics->drawImage(mImage, posX + mOffsetX, posY + mOffsetY);
162  }
163  else if ((mAlpha != 0.0f) && (mAlphaImage != nullptr))
164  {
166  graphics->drawImage(mAlphaImage,
167  posX + mOffsetX, posY + mOffsetY);
168  }
169  else
170  {
171  CompoundSprite::drawSprites(graphics, posX, posY);
172  }
173 }
if(!vert) return
virtual void setAlpha(const float alpha)
Definition: image.cpp:285
void drawSprites(Graphics *const graphics, const int posX, const int posY) const
std::vector< Sprite * > mSprites
#define FUNC_BLOCK(name, id)
Definition: perfomance.h:80
std::string empty
Definition: podict.cpp:25
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
void updateImages() const
float mAlpha
Definition: sprite.h:129

◆ drawSprites()

void CompoundSprite::drawSprites ( Graphics *const  graphics,
const int  posX,
const int  posY 
) const

Definition at line 175 of file compoundsprite.cpp.

References FOR_EACH, Sprite::mAlpha, and mSprites.

Referenced by drawSimple().

178 {
180  {
181  if (*it != nullptr)
182  {
183  (*it)->setAlpha(mAlpha);
184  (*it)->draw(graphics, posX, posY);
185  }
186  }
187 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::vector< Sprite * >::const_iterator SpriteConstIterator
std::vector< Sprite * > mSprites
float mAlpha
Definition: sprite.h:129

◆ drawSpritesSDL()

void CompoundSprite::drawSpritesSDL ( Graphics *const  graphics,
const int  posX,
const int  posY 
) const
virtual

Reimplemented in Being.

Definition at line 189 of file compoundsprite.cpp.

References FOR_EACH, and mSprites.

Referenced by redraw().

192 {
194  {
195  if (*it != nullptr)
196  (*it)->draw(graphics, posX, posY);
197  }
198 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::vector< Sprite * >::const_iterator SpriteConstIterator
std::vector< Sprite * > mSprites

◆ ensureSize()

void CompoundSprite::ensureSize ( const size_t  layerCount)

Definition at line 312 of file compoundsprite.cpp.

References mSprites.

Referenced by Being::setGender(), Being::setHairColorSpriteID(), Being::setHairTempSprite(), Being::setSpriteCards(), Being::setSpriteColor(), Being::setSpriteColorId(), Being::setSpriteId(), Being::setTempSprite(), and Being::unSetSprite().

313 {
314  // Skip if it won't change anything
315  if (mSprites.size() >= layerCount)
316  return;
317 
318 // resize(layerCount, nullptr);
319  mSprites.resize(layerCount);
320 }
std::vector< Sprite * > mSprites

◆ getCurrentFrame()

unsigned int CompoundSprite::getCurrentFrame ( ) const
virtual

Returns the current frame number for the sprite.

Implements Sprite.

Definition at line 250 of file compoundsprite.cpp.

References FOR_EACH, and mSprites.

251 {
253  {
254  if (*it != nullptr)
255  return (*it)->getCurrentFrame();
256  }
257  return 0;
258 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::vector< Sprite * >::const_iterator SpriteConstIterator
std::vector< Sprite * > mSprites

◆ getFrameCount()

unsigned int CompoundSprite::getFrameCount ( ) const
virtual

Returns the frame count for the sprite.

Implements Sprite.

Definition at line 260 of file compoundsprite.cpp.

References FOR_EACH, and mSprites.

Referenced by Being::logic().

261 {
263  {
264  if (*it != nullptr)
265  return (*it)->getFrameCount();
266  }
267  return 0;
268 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::vector< Sprite * >::const_iterator SpriteConstIterator
std::vector< Sprite * > mSprites

◆ getHeight()

int CompoundSprite::getHeight ( ) const
virtual

Gets the height in pixels of the first sprite in the list.

Implements Sprite.

Definition at line 212 of file compoundsprite.cpp.

References anonymous_namespace{stringutils.cpp}::base, FOR_EACH, Sprite::getHeight(), and mSprites.

Referenced by ActorSprite::getHeight(), and Being::getHeight().

213 {
215  {
216  const Sprite *const base = *it;
217  if (base != nullptr)
218  return base->getHeight();
219  }
220 
221  return 0;
222 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
Definition: sprite.h:32
virtual int getHeight() const =0
std::vector< Sprite * >::const_iterator SpriteConstIterator
std::vector< Sprite * > mSprites

◆ getImage()

const Image * CompoundSprite::getImage ( ) const
virtual

Returns a reference to the current image being drawn.

Implements Sprite.

Definition at line 224 of file compoundsprite.cpp.

References mImage.

225 {
226  return mImage;
227 }

◆ getLastTime()

int CompoundSprite::getLastTime ( ) const
inline

◆ getNumberOfLayers()

int CompoundSprite::getNumberOfLayers ( ) const

Definition at line 243 of file compoundsprite.cpp.

References CAST_S32, mAlphaImage, mImage, and mSprites.

Referenced by Being::drawPlayerSprites(), Being::getNumberOfLayers(), and Being::searchSlotValue().

244 {
245  if ((mImage != nullptr) || (mAlphaImage != nullptr))
246  return 1;
247  return CAST_S32(mSprites.size());
248 }
#define CAST_S32
Definition: cast.h:29
std::vector< Sprite * > mSprites

◆ getStartTime()

int CompoundSprite::getStartTime ( ) const
inline

◆ getWidth()

int CompoundSprite::getWidth ( ) const
virtual

Gets the width in pixels of the first sprite in the list.

Implements Sprite.

Definition at line 200 of file compoundsprite.cpp.

References anonymous_namespace{stringutils.cpp}::base, FOR_EACH, Sprite::getWidth(), and mSprites.

Referenced by ActorSprite::getWidth(), and Being::getWidth().

201 {
203  {
204  const Sprite *const base = *it;
205  if (base != nullptr)
206  return base->getWidth();
207  }
208 
209  return 0;
210 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
Definition: sprite.h:32
std::vector< Sprite * >::const_iterator SpriteConstIterator
std::vector< Sprite * > mSprites
virtual int getWidth() const =0

◆ initCurrentCacheItem()

void CompoundSprite::initCurrentCacheItem ( ) const
protected

Definition at line 524 of file compoundsprite.cpp.

References CompoundItem::alphaImage, CompoundItem::data, FOR_EACH, CompoundItem::image, mAlphaImage, mCacheItem, mImage, and mSprites.

Referenced by updateImages().

525 {
526  delete mCacheItem;
527  mCacheItem = new CompoundItem;
530 // mCacheItem->alpha = mAlpha;
531 
533  {
534  if (*it != nullptr)
535  mCacheItem->data.push_back((*it)->getHash());
536  else
537  mCacheItem->data.push_back(nullptr);
538  }
539 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
CompoundItem * mCacheItem
Image * image
Definition: compounditem.h:42
Image * alphaImage
Definition: compounditem.h:43
std::vector< Sprite * >::const_iterator SpriteConstIterator
std::vector< Sprite * > mSprites
VectorPointers data
Definition: compounditem.h:41

◆ play()

bool CompoundSprite::play ( const std::string &  action)
virtual

Plays an action using the current direction.

Returns
true if the sprite changed, false otherwise

Implements Sprite.

Definition at line 114 of file compoundsprite.cpp.

References FOR_EACH, mLastTime, mNeedsRedraw, and mSprites.

Referenced by Being::setAction().

115 {
116  bool ret = false;
117  bool ret2 = true;
119  {
120  if (*it != nullptr)
121  {
122  const bool tmpVal = (*it)->play(action);
123  ret |= tmpVal;
124  ret2 &= tmpVal;
125  }
126  }
127  mNeedsRedraw |= ret;
128  if (ret2)
129  mLastTime = 0;
130  return ret;
131 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::vector< Sprite * >::iterator SpriteIterator
std::vector< Sprite * > mSprites

◆ redraw()

void CompoundSprite::redraw ( ) const
protected

< Surface is in video memory

< Surface is in video memory

Definition at line 322 of file compoundsprite.cpp.

References SurfaceGraphics::beginDraw(), BlitMode::BLIT_GFX, BUFFER_HEIGHT, delete2, drawSpritesSDL(), Game::getCurrentMap(), Map::getTileWidth(), imageHelper, Game::instance(), ImageHelper::loadSurface(), mAlphaImage, mapTileSize, ImageHelper::mEnableAlpha, mImage, mOffsetX, mOffsetY, MSDL_CreateRGBSurface, MSDL_FreeSurface, SurfaceGraphics::setBlitMode(), and SurfaceGraphics::setTarget().

Referenced by updateImages().

323 {
324 #ifndef USE_SDL2
325 
326 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
327  const int rmask = 0xff000000;
328  const int gmask = 0x00ff0000;
329  const int bmask = 0x0000ff00;
330  const int amask = 0x000000ff;
331 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
332  const int rmask = 0x000000ff;
333  const int gmask = 0x0000ff00;
334  const int bmask = 0x00ff0000;
335  const int amask = 0xff000000;
336 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
337 
338  SDL_Surface *const surface = MSDL_CreateRGBSurface(SDL_HWSURFACE,
339  BUFFER_WIDTH, BUFFER_HEIGHT, 32, rmask, gmask, bmask, amask);
340 
341  if (surface == nullptr)
342  return;
343 
344  SurfaceGraphics *graphics = new SurfaceGraphics;
345  graphics->setBlitMode(BlitMode::BLIT_GFX);
346  graphics->setTarget(surface);
347  graphics->beginDraw();
348 
349  int tileX = mapTileSize / 2;
350  int tileY = mapTileSize;
351 
352  const Game *const game = Game::instance();
353  if (game != nullptr)
354  {
355  const Map *const map = game->getCurrentMap();
356  if (map != nullptr)
357  {
358  tileX = map->getTileWidth() / 2;
359  tileY = map->getTileWidth();
360  }
361  }
362 
363  const int posX = BUFFER_WIDTH / 2 - tileX;
364  const int posY = BUFFER_HEIGHT - tileY;
365 
366  mOffsetX = tileX - BUFFER_WIDTH / 2;
367  mOffsetY = tileY - BUFFER_HEIGHT;
368 
369  drawSpritesSDL(graphics, posX, posY);
370 
371  delete2(graphics);
372 
373  SDL_SetAlpha(surface, 0, SDL_ALPHA_OPAQUE);
374 
375  delete mAlphaImage;
376 
378  {
379  SDL_Surface *const surfaceA = MSDL_CreateRGBSurface(SDL_HWSURFACE,
380  BUFFER_WIDTH, BUFFER_HEIGHT, 32, rmask, gmask, bmask, amask);
381  SDL_BlitSurface(surface, nullptr, surfaceA, nullptr);
382  mAlphaImage = imageHelper->loadSurface(surfaceA);
383  MSDL_FreeSurface(surfaceA);
384  }
385  else
386  {
387  mAlphaImage = nullptr;
388  }
389 
390  delete mImage;
391  mImage = imageHelper->loadSurface(surface);
392  MSDL_FreeSurface(surface);
393 #endif // USE_SDL2
394 }
ImageHelper * imageHelper
Definition: imagehelper.cpp:43
void setBlitMode(const BlitModeT mode)
#define MSDL_FreeSurface(surface)
Definition: debug.h:53
static const int BUFFER_HEIGHT
#define delete2(var)
Definition: delete2.h:24
Map * getCurrentMap() const
Definition: game.h:106
Definition: game.h:62
Definition: map.h:68
void setTarget(SDL_Surface *const target)
virtual void drawSpritesSDL(Graphics *const graphics, const int posX, const int posY) const
virtual Image * loadSurface(SDL_Surface *const)
Definition: imagehelper.h:72
static bool mEnableAlpha
Definition: imagehelper.h:116
int getTileWidth() const
Definition: map.h:174
static const int mapTileSize
Definition: map.h:26
static Game * instance()
Definition: game.h:81
static const int BUFFER_WIDTH
#define MSDL_CreateRGBSurface(flags, w, h, d, r, g, b, a)
Definition: debug.h:54

◆ removeSprite()

void CompoundSprite::removeSprite ( const int  layer)

Definition at line 287 of file compoundsprite.cpp.

References delete2, mNeedsRedraw, and mSprites.

Referenced by Being::setHairTempSprite(), Being::setSpriteCards(), Being::setSpriteColor(), Being::setSpriteColorId(), Being::setSpriteId(), Being::setTempSprite(), and Being::unSetSprite().

288 {
289  // Skip if it won't change anything
290  if (mSprites[layer] == nullptr)
291  return;
292 
293  delete2(mSprites[layer]);
294  mNeedsRedraw = true;
295 }
#define delete2(var)
Definition: delete2.h:24
std::vector< Sprite * > mSprites

◆ reset()

bool CompoundSprite::reset ( )
virtual

Resets the sprite.

Returns
true if the sprite changed, false otherwise

Implements Sprite.

Definition at line 100 of file compoundsprite.cpp.

References FOR_EACH, mLastTime, mNeedsRedraw, and mSprites.

Referenced by Being::handleAttack(), Being::handleSkill(), Ea::PlayerRecv::processPlayerWarp(), and Being::setAction().

101 {
102  bool ret = false;
104  {
105  if (*it != nullptr)
106  ret |= (*it)->reset();
107  }
108  if (ret)
109  mLastTime = 0;
110  mNeedsRedraw |= ret;
111  return ret;
112 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::vector< Sprite * >::iterator SpriteIterator
std::vector< Sprite * > mSprites

◆ setAlpha()

void CompoundSprite::setAlpha ( float  alpha)
virtual

Sets the alpha value of the animated sprite

Reimplemented from Sprite.

Definition at line 396 of file compoundsprite.cpp.

References FOR_EACH, imageHelper, Sprite::mAlpha, mEnableAlphaFix, mSprites, RENDER_SOFTWARE, and ImageHelper::useOpenGL().

Referenced by ActorSprite::setAlpha().

397 {
398  if (alpha != mAlpha)
399  {
400  if (mEnableAlphaFix &&
401 #ifdef USE_OPENGL
403 #endif // USE_OPENGL
404  mSprites.size() > 3U)
405  {
407  {
408  if (*it != nullptr)
409  (*it)->setAlpha(alpha);
410  }
411  }
412  mAlpha = alpha;
413  }
414 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
virtual RenderType useOpenGL() const
Definition: imagehelper.h:106
ImageHelper * imageHelper
Definition: imagehelper.cpp:43
std::vector< Sprite * >::const_iterator SpriteConstIterator
std::vector< Sprite * > mSprites
float mAlpha
Definition: sprite.h:129

◆ setEnableDelay()

static void CompoundSprite::setEnableDelay ( bool  b)
inlinestatic

Definition at line 101 of file compoundsprite.h.

References mEnableDelay.

Referenced by Game::Game().

102  { mEnableDelay = b; }
static bool mEnableDelay

◆ setSprite()

void CompoundSprite::setSprite ( const size_t  layer,
Sprite *const  sprite 
)

Definition at line 276 of file compoundsprite.cpp.

References mNeedsRedraw, and mSprites.

Referenced by Being::setGender(), Being::setHairTempSprite(), Being::setSpriteCards(), Being::setSpriteColor(), Being::setSpriteColorId(), Being::setSpriteId(), and Being::setTempSprite().

277 {
278  // Skip if it won't change anything
279  if (mSprites[layer] == sprite)
280  return;
281 
282  delete mSprites[layer];
283  mSprites[layer] = sprite;
284  mNeedsRedraw = true;
285 }
std::vector< Sprite * > mSprites

◆ setSpriteDirection()

bool CompoundSprite::setSpriteDirection ( const SpriteDirection::Type  direction)
virtual

Sets the direction.

Returns
true if the sprite changed, false otherwise

Implements Sprite.

Definition at line 229 of file compoundsprite.cpp.

References FOR_EACH, mLastTime, mNeedsRedraw, and mSprites.

Referenced by Being::setDirection().

230 {
231  bool ret = false;
233  {
234  if (*it != nullptr)
235  ret |= (*it)->setSpriteDirection(direction);
236  }
237  if (ret)
238  mLastTime = 0;
239  mNeedsRedraw |= ret;
240  return ret;
241 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::vector< Sprite * >::iterator SpriteIterator
std::vector< Sprite * > mSprites

◆ update()

bool CompoundSprite::update ( const int  time)
virtual

Inform the animation of the passed time so that it can output the correct animation frame.

Returns
true if the sprite changed, false otherwise

Implements Sprite.

Definition at line 133 of file compoundsprite.cpp.

References A_UNLIKELY, FOR_EACH, mLastTime, mNeedsRedraw, mSprites, mStartTime, and ServerCommandType::time.

Referenced by ActorSprite::logic().

134 {
135  bool ret = false;
136  if (A_UNLIKELY(mLastTime == 0))
137  mStartTime = time;
138  mLastTime = time;
140  {
141  if (*it != nullptr)
142  ret |= (*it)->update(time);
143  }
144  mNeedsRedraw |= ret;
145  return ret;
146 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
#define A_UNLIKELY(x)
Definition: likely.h:29
std::vector< Sprite * >::iterator SpriteIterator
std::vector< Sprite * > mSprites

◆ updateFromCache()

bool CompoundSprite::updateFromCache ( ) const
protected

Definition at line 455 of file compoundsprite.cpp.

References cache_clean_part, cache_max_size, CompoundItem::data, FOR_EACH, CompoundItem::image, imagesCache, ServerCommandType::item, mAlphaImage, mCacheItem, mImage, and mSprites.

Referenced by updateImages().

456 {
457 #ifndef USE_SDL2
458 // static int hits = 0;
459 // static int miss = 0;
460 
461  if ((mCacheItem != nullptr) && (mCacheItem->image != nullptr))
462  {
463  imagesCache.push_front(mCacheItem);
464  mCacheItem = nullptr;
465  if (imagesCache.size() > cache_max_size)
466  {
467  for (unsigned f = 0; f < cache_clean_part; f ++)
468  {
469  CompoundItem *item = imagesCache.back();
470  imagesCache.pop_back();
471  delete item;
472  }
473  }
474  }
475 
476 // logger->log("cache size: %d, hit %d, miss %d",
477 // (int)imagesCache.size(), hits, miss);
478 
479  const size_t sz = mSprites.size();
480  FOR_EACH (ImagesCache::iterator, it, imagesCache)
481  {
482  CompoundItem *const ic = *it;
483  if ((ic != nullptr) && ic->data.size() == sz)
484  {
485  bool fail(false);
486  VectorPointers::const_iterator it2 = ic->data.begin();
487  const VectorPointers::const_iterator it2_end = ic->data.end();
488 
489  for (SpriteConstIterator it1 = mSprites.begin(),
490  it1_end = mSprites.end();
491  it1 != it1_end && it2 != it2_end;
492  ++ it1, ++ it2)
493  {
494  const void *ptr1 = nullptr;
495  const void *ptr2 = nullptr;
496  if (*it1 != nullptr)
497  ptr1 = (*it1)->getHash();
498  if (*it2 != nullptr)
499  ptr2 = *it2;
500  if (ptr1 != ptr2)
501  {
502  fail = true;
503  break;
504  }
505  }
506  if (!fail)
507  {
508 // hits ++;
509  mImage = (*it)->image;
510  mAlphaImage = (*it)->alphaImage;
511  imagesCache.erase(it);
512  mCacheItem = ic;
513  return true;
514  }
515  }
516  }
517  mImage = nullptr;
518  mAlphaImage = nullptr;
519 // miss++;
520 #endif // USE_SDL2
521  return false;
522 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
CompoundItem * mCacheItem
static const unsigned cache_clean_part
Image * image
Definition: compounditem.h:42
ImagesCache imagesCache
std::vector< Sprite * >::const_iterator SpriteConstIterator
std::vector< Sprite * > mSprites
static const unsigned cache_max_size
VectorPointers data
Definition: compounditem.h:41

◆ updateImages()

void CompoundSprite::updateImages ( ) const
protected

Definition at line 416 of file compoundsprite.cpp.

References get_elapsed_time1(), imageHelper, initCurrentCacheItem(), mDisableAdvBeingCaching, mDisableBeingCaching, mEnableDelay, mImage, mNeedsRedraw, mNextRedrawTime, mSprites, redraw(), RENDER_SOFTWARE, tick_time, updateFromCache(), and ImageHelper::useOpenGL().

Referenced by Being::drawCompound(), and drawSimple().

417 {
418 #ifndef USE_SDL2
419 #ifdef USE_OPENGL
421  return;
422 #endif // USE_OPENGL
423 
424  if (mEnableDelay)
425  {
427  return;
429  }
430  mNeedsRedraw = false;
431 
433  {
434  if (mSprites.size() <= 3U)
435  return;
436 
438  {
439  if (updateFromCache())
440  return;
441 
442  redraw();
443 
444  if (mImage != nullptr)
446  }
447  else
448  {
449  redraw();
450  }
451  }
452 #endif // USE_SDL2
453 }
virtual RenderType useOpenGL() const
Definition: imagehelper.h:106
ImageHelper * imageHelper
Definition: imagehelper.cpp:43
volatile int tick_time
Definition: timer.cpp:52
void initCurrentCacheItem() const
int get_elapsed_time1(const int startTime)
Definition: timer.cpp:104
std::vector< Sprite * > mSprites
static bool mEnableDelay
bool updateFromCache() const
bool mDisableAdvBeingCaching
void redraw() const

◆ updateNumber()

bool CompoundSprite::updateNumber ( const unsigned  num)
virtual

Implements Sprite.

Definition at line 541 of file compoundsprite.cpp.

References FOR_EACH, and mSprites.

Referenced by Being::updatePercentHP().

542 {
543  bool res(false);
545  {
546  if (*it != nullptr)
547  {
548  if ((*it)->updateNumber(num))
549  res = true;
550  }
551  }
552  return res;
553 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::vector< Sprite * >::const_iterator SpriteConstIterator
std::vector< Sprite * > mSprites

Field Documentation

◆ imagesCache

ImagesCache CompoundSprite::imagesCache
mutableprotected

Definition at line 122 of file compoundsprite.h.

Referenced by clear(), and updateFromCache().

◆ mAlphaImage

Image* CompoundSprite::mAlphaImage
mutableprotected

◆ mCacheItem

CompoundItem* CompoundSprite::mCacheItem
mutableprotected

Definition at line 123 of file compoundsprite.h.

Referenced by clear(), initCurrentCacheItem(), and updateFromCache().

◆ mDisableAdvBeingCaching

bool CompoundSprite::mDisableAdvBeingCaching
protected

Definition at line 139 of file compoundsprite.h.

Referenced by updateImages().

◆ mDisableBeingCaching

bool CompoundSprite::mDisableBeingCaching
protected

Definition at line 140 of file compoundsprite.h.

Referenced by updateImages().

◆ mEnableAlphaFix

bool CompoundSprite::mEnableAlphaFix
protected

Definition at line 138 of file compoundsprite.h.

Referenced by setAlpha().

◆ mEnableDelay

bool CompoundSprite::mEnableDelay = true
staticprotected

Definition at line 136 of file compoundsprite.h.

Referenced by setEnableDelay(), and updateImages().

◆ mImage

Image* CompoundSprite::mImage
mutableprotected

◆ mLastTime

int CompoundSprite::mLastTime
protected

Definition at line 131 of file compoundsprite.h.

Referenced by clear(), getLastTime(), play(), reset(), setSpriteDirection(), and update().

◆ mNeedsRedraw

bool CompoundSprite::mNeedsRedraw
mutableprotected

◆ mNextRedrawTime

int CompoundSprite::mNextRedrawTime
mutableprotected

Definition at line 133 of file compoundsprite.h.

Referenced by updateImages().

◆ mOffsetX

int CompoundSprite::mOffsetX
mutableprotected

Definition at line 128 of file compoundsprite.h.

Referenced by Being::drawCompound(), drawSimple(), and redraw().

◆ mOffsetY

int CompoundSprite::mOffsetY
mutableprotected

Definition at line 129 of file compoundsprite.h.

Referenced by Being::drawCompound(), drawSimple(), and redraw().

◆ mSprites

std::vector<Sprite*> CompoundSprite::mSprites

◆ mStartTime

int CompoundSprite::mStartTime
protected

Definition at line 130 of file compoundsprite.h.

Referenced by getStartTime(), and update().


The documentation for this class was generated from the following files: