ManaPlus
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes | Friends
Image Class Reference

#include <image.h>

Inheritance diagram for Image:
Resource MemoryCounter SubImage

Public Member Functions

virtual ~Image ()
 
void unload ()
 
bool isLoaded () const
 
int getWidth () const A_INLINE
 
int getHeight () const A_INLINE
 
bool hasAlphaChannel () const
 
virtual void setAlpha (const float alpha)
 
virtual ImagegetSubImage (const int x, const int y, const int width, const int height)
 
ImageSDLgetScaledImage (const int width, const int height) const
 
uint8_t * SDLgetAlphaChannel () const
 
void SDLCleanCache ()
 
void SDLTerminateAlphaCache ()
 
int getTextureWidth () const
 
int getTextureHeight () const
 
GLuint getGLImage () const
 
void decRef ()
 
bool isHasAlphaChannel () const
 
bool isAlphaVisible () const
 
void setAlphaVisible (const bool b)
 
bool isAlphaCalculated () const
 
void setAlphaCalculated (const bool b)
 
SDL_Surface * getSDLSurface ()
 
int calcMemoryLocal () const
 
virtual ImageTypeT getType () const
 
- Public Member Functions inherited from Resource
 Resource ()
 
virtual ~Resource ()
 
virtual void incRef ()
 
int calcMemoryLocal () const
 
std::string getCounterName () const
 
- Public Member Functions inherited from MemoryCounter
 MemoryCounter ()
 
virtual ~MemoryCounter ()
 
int calcMemory (const int level) const
 
virtual int calcMemoryChilds (const int level) const
 

Data Fields

GLuint mGLImage
 
int mTexWidth
 
int mTexHeight
 
SDL_Rect mBounds
 
float mAlpha
 
- Data Fields inherited from Resource
time_t mTimeStamp
 
std::string mIdPath
 
std::string mSource
 
unsigned int mRefCount
 
bool mProtected
 
bool mNotCount
 

Protected Member Functions

 Image (SDL_Surface *const image, const bool hasAlphaChannel, uint8_t *const alphaChannel=0)
 
SDL_Surface * getByAlpha (const float alpha)
 
 Image (const GLuint glimage, const int width, const int height, const int texWidth, const int texHeight)
 

Protected Attributes

SDL_Surface * mSDLSurface
 
uint8_t * mAlphaChannel
 
std::map< float, SDL_Surface * > mAlphaCache
 
bool mLoaded
 
bool mHasAlphaChannel
 
bool mUseAlphaCache
 
bool mIsAlphaVisible
 
bool mIsAlphaCalculated
 

Friends

class CompoundSprite
 
class Graphics
 
class ImageHelper
 
class SDLGraphics
 
class SDLImageHelper
 
class SurfaceGraphics
 
class TestLauncher
 
class AtlasManager
 
class MobileOpenGL2Graphics
 
class MobileOpenGLGraphics
 
class ModernOpenGLGraphics
 
class NormalOpenGLGraphics
 
class NullOpenGLGraphics
 
class SafeOpenGLGraphics
 
class OpenGLImageHelper
 
class SafeOpenGLImageHelper
 

Detailed Description

Defines a class for loading and storing images.

Definition at line 61 of file image.h.

Constructor & Destructor Documentation

◆ ~Image()

Image::~Image ( )
virtual

Destructor.

Definition at line 205 of file image.cpp.

References Logger::log(), logger, Resource::mIdPath, Resource::mSource, and unload().

206 {
207 #ifdef DEBUG_IMAGES
208  logger->log("delete image: %p", static_cast<void*>(this));
209  logger->log(" %s, %s", mIdPath.c_str(), mSource.c_str());
210 #endif // DEBUG_IMAGES
211 
212  unload();
213 }
std::string mSource
Definition: resource.h:84
Logger * logger
Definition: logger.cpp:95
void unload()
Definition: image.cpp:228
void log(const char *const log_text,...)
Definition: logger.cpp:243
std::string mIdPath
Definition: resource.h:83

◆ Image() [1/2]

Image::Image ( SDL_Surface *const  image,
const bool  hasAlphaChannel,
uint8_t *const  alphaChannel = 0 
)
protected

SDL Constructor

Definition at line 125 of file image.cpp.

References CAST_U16, Logger::log(), logger, mBounds, mLoaded, and mSDLSurface.

Referenced by calcMemoryLocal().

126  :
127  Resource(),
128 #ifdef USE_OPENGL
129  mGLImage(0),
130  mTexWidth(0),
131  mTexHeight(0),
132 #endif // USE_OPENGL
133  mBounds(),
134  mAlpha(1.0F),
135  mSDLSurface(image),
136 #ifdef USE_SDL2
137  mTexture(nullptr),
138 #endif // USE_SDL2
139  mAlphaChannel(alphaChannel),
140  mAlphaCache(),
141  mLoaded(false),
142  mHasAlphaChannel(hasAlphaChannel0),
144  mIsAlphaVisible(hasAlphaChannel0),
145  mIsAlphaCalculated(false)
146 {
147 #ifdef DEBUG_IMAGES
148  logger->log("created image: %p", static_cast<void*>(this));
149 #endif // DEBUG_IMAGES
150 
151  mBounds.x = 0;
152  mBounds.y = 0;
153 
154  if (mSDLSurface != nullptr)
155  {
156  mBounds.w = CAST_U16(mSDLSurface->w);
157  mBounds.h = CAST_U16(mSDLSurface->h);
158 
159  mLoaded = true;
160  }
161  else
162  {
163  mBounds.w = 0;
164  mBounds.h = 0;
165  }
166 }
Resource()
Definition: resource.h:39
bool mIsAlphaCalculated
Definition: image.h:244
bool mLoaded
Definition: image.h:240
Logger * logger
Definition: logger.cpp:95
#define CAST_U16
Definition: cast.h:28
SDL_Surface * mSDLSurface
Definition: image.h:230
bool mIsAlphaVisible
Definition: image.h:243
int mTexHeight
Definition: image.h:184
uint8_t * mAlphaChannel
Definition: image.h:236
bool mUseAlphaCache
Definition: image.h:242
float mAlpha
Definition: image.h:212
bool mHasAlphaChannel
Definition: image.h:241
GLuint mGLImage
Definition: image.h:182
SDL_Rect mBounds
Definition: image.h:210
void log(const char *const log_text,...)
Definition: logger.cpp:243
static bool mEnableAlphaCache
int mTexWidth
Definition: image.h:183
std::map< float, SDL_Surface * > mAlphaCache
Definition: image.h:238

◆ Image() [2/2]

Image::Image ( const GLuint  glimage,
const int  width,
const int  height,
const int  texWidth,
const int  texHeight 
)
protected

OpenGL Constructor.

Definition at line 169 of file image.cpp.

References CAST_U16, Logger::log(), logger, mBounds, mGLImage, and mLoaded.

170  :
171  Resource(),
172  mGLImage(glimage),
173  mTexWidth(texWidth),
174  mTexHeight(texHeight),
175  mBounds(),
176  mAlpha(1.0F),
177  mSDLSurface(nullptr),
178 #ifdef USE_SDL2
179  mTexture(nullptr),
180 #endif // USE_SDL2
181  mAlphaChannel(nullptr),
182  mAlphaCache(),
183  mLoaded(false),
184  mHasAlphaChannel(true),
185  mUseAlphaCache(false),
186  mIsAlphaVisible(true),
187  mIsAlphaCalculated(false)
188 {
189 #ifdef DEBUG_IMAGES
190  logger->log("created image: %p", static_cast<void*>(this));
191 #endif // DEBUG_IMAGES
192 
193  mBounds.x = 0;
194  mBounds.y = 0;
195  mBounds.w = CAST_U16(width);
196  mBounds.h = CAST_U16(height);
197 
198  if (mGLImage != 0u)
199  {
200  mLoaded = true;
201  }
202 }
Resource()
Definition: resource.h:39
bool mIsAlphaCalculated
Definition: image.h:244
bool mLoaded
Definition: image.h:240
Logger * logger
Definition: logger.cpp:95
#define CAST_U16
Definition: cast.h:28
SDL_Surface * mSDLSurface
Definition: image.h:230
bool mIsAlphaVisible
Definition: image.h:243
int mTexHeight
Definition: image.h:184
uint8_t * mAlphaChannel
Definition: image.h:236
bool mUseAlphaCache
Definition: image.h:242
float mAlpha
Definition: image.h:212
bool mHasAlphaChannel
Definition: image.h:241
GLuint mGLImage
Definition: image.h:182
SDL_Rect mBounds
Definition: image.h:210
void log(const char *const log_text,...)
Definition: logger.cpp:243
int mTexWidth
Definition: image.h:183
std::map< float, SDL_Surface * > mAlphaCache
Definition: image.h:238

Member Function Documentation

◆ calcMemoryLocal()

int Image::calcMemoryLocal ( ) const
virtual

Reimplemented from MemoryCounter.

Reimplemented in SubImage.

Definition at line 506 of file image.cpp.

References Resource::calcMemoryLocal(), CAST_S32, MemoryManager::getSurfaceSize(), Image(), mAlphaCache, memoryManager, and mSDLSurface.

Referenced by getSDLSurface().

507 {
508  // +++ this calculation can be wrong for SDL2
509  int sz = static_cast<int>(sizeof(Image) +
510  sizeof(std::map<float, SDL_Surface*>)) +
512  if (mSDLSurface != nullptr)
513  {
514  sz += CAST_S32(mAlphaCache.size()) *
516  }
517  return sz;
518 }
int calcMemoryLocal() const
Definition: resource.cpp:75
MemoryManager memoryManager
SDL_Surface * mSDLSurface
Definition: image.h:230
#define CAST_S32
Definition: cast.h:29
Image(SDL_Surface *const image, const bool hasAlphaChannel, uint8_t *const alphaChannel=0)
Definition: image.cpp:125
int getSurfaceSize(const SDL_Surface *const surface)
std::map< float, SDL_Surface * > mAlphaCache
Definition: image.h:238

◆ decRef()

void Image::decRef ( )
virtual

Decrements the reference count and deletes the object if no references are left.

Returns
true if the object was deleted false otherwise.

Reimplemented from Resource.

Reimplemented in SubImage.

Definition at line 521 of file image.cpp.

References Resource::decRef(), OpenGLImageHelper::invalidate(), mGLImage, and Resource::mRefCount.

Referenced by DragDrop::clear(), AtlasManager::convertAtlas(), AtlasManager::convertEmptyAtlas(), Minimap::deleteMapImage(), DragDrop::dragCommand(), DragDrop::dragItem(), DragDrop::dragSkill(), OutfitWindow::draw(), getGLImage(), ParticleEmitter::getImageSet(), ImageSetLoader::load(), TextCommand::loadImage(), ParticleEmitter::ParticleEmitter(), Theme::readSkin(), MapReader::readTileset(), OutfitWindow::safeDraw(), ItemContainer::setCellBackgroundImage(), Item::setId(), Tab::setImage(), ItemPopup::setItem(), Window::setResizable(), MapItem::setType(), ActorSprite::setupSpriteDisplay(), Theme::unloadRect(), AmbientLayer::~AmbientLayer(), AtlasResource::~AtlasResource(), AvatarListBox::~AvatarListBox(), Desktop::~Desktop(), DragDrop::~DragDrop(), DropDown::~DropDown(), EquipmentWindow::~EquipmentWindow(), Icon::~Icon(), ImageSprite::~ImageSprite(), Item::~Item(), ItemContainer::~ItemContainer(), ItemPopup::~ItemPopup(), LangListModel::~LangListModel(), LinePart::~LinePart(), MapItem::~MapItem(), Particle::~Particle(), ParticleEmitter::~ParticleEmitter(), QuestsWindow::~QuestsWindow(), ScrollArea::~ScrollArea(), ShortcutContainer::~ShortcutContainer(), SkillData::~SkillData(), Skin::~Skin(), SubImage::~SubImage(), Tab::~Tab(), Text::~Text(), TextCommand::~TextCommand(), and Window::~Window().

522 {
523  if ((mGLImage != 0u) && mRefCount <= 1)
526 }
virtual void decRef()
Definition: resource.cpp:49
unsigned int mRefCount
Definition: resource.h:86
static void invalidate(const GLuint textureId)
GLuint mGLImage
Definition: image.h:182

◆ getByAlpha()

SDL_Surface * Image::getByAlpha ( const float  alpha)
protected

Definition at line 276 of file image.cpp.

References mAlphaCache.

Referenced by setAlpha().

277 {
278  const std::map<float, SDL_Surface*>::const_iterator
279  it = mAlphaCache.find(alpha);
280  if (it != mAlphaCache.end())
281  return (*it).second;
282  return nullptr;
283 }
std::map< float, SDL_Surface * > mAlphaCache
Definition: image.h:238

◆ getGLImage()

GLuint Image::getGLImage ( ) const
inline

Definition at line 177 of file image.h.

References decRef(), and mGLImage.

Referenced by Actions::doSit(), and ResourceManager::logResource().

178  { return mGLImage; }
GLuint mGLImage
Definition: image.h:182

◆ getHeight()

int Image::getHeight ( ) const
inline

◆ getSDLSurface()

SDL_Surface* Image::getSDLSurface ( )
inline

Definition at line 202 of file image.h.

References calcMemoryLocal(), and mSDLSurface.

Referenced by main().

203  { return mSDLSurface; }
SDL_Surface * mSDLSurface
Definition: image.h:230

◆ getSubImage()

Image * Image::getSubImage ( const int  x,
const int  y,
const int  width,
const int  height 
)
virtual

Creates a new image with the desired clipping rectangle.

Returns
NULL if creation failed and a valid object otherwise.

Reimplemented in SubImage.

Definition at line 465 of file image.cpp.

References mGLImage, mSDLSurface, mTexHeight, mTexWidth, ImageHelper::mUseOpenGL, RENDER_GLES2_OPENGL, RENDER_GLES_OPENGL, RENDER_MODERN_OPENGL, RENDER_NORMAL_OPENGL, RENDER_SAFE_OPENGL, RENDER_SOFTWARE, and ImageType::SubImage.

Referenced by AtlasManager::convertAtlas(), AtlasManager::convertEmptyAtlas(), getHeight(), SubImage::getSubImage(), ImageSet::ImageSet(), anonymous_namespace{subimageloader.cpp}::SubImageLoader::load(), and TestLauncher::testTextures().

467 {
468  // Create a new clipped sub-image
469 #ifdef USE_OPENGL
471  if (mode == RENDER_NORMAL_OPENGL ||
472  mode == RENDER_SAFE_OPENGL ||
473  mode == RENDER_GLES_OPENGL ||
474  mode == RENDER_GLES2_OPENGL ||
475  mode == RENDER_MODERN_OPENGL)
476  {
477  return new SubImage(this,
478  mGLImage,
479  x, y,
480  width, height,
482  }
483 #endif // USE_OPENGL
484 
485 #ifdef USE_SDL2
486 #ifndef USE_OPENGL
487  const RenderType mode = ImageHelper::mUseOpenGL;
488 #endif // USE_OPENGL
489 
490  if (mode == RENDER_SOFTWARE)
491  return new SubImage(this, mSDLSurface, x, y, width, height);
492  else
493  return new SubImage(this, mTexture, x, y, width, height);
494 #else // USE_SDL2
495 
496  return new SubImage(this, mSDLSurface, x, y, width, height);
497 #endif // USE_SDL2
498 }
static RenderType mUseOpenGL
Definition: imagehelper.h:117
SDL_Surface * mSDLSurface
Definition: image.h:230
int mTexHeight
Definition: image.h:184
GLuint mGLImage
Definition: image.h:182
int mTexWidth
Definition: image.h:183
RenderType
Definition: rendertype.h:24

◆ getTextureHeight()

int Image::getTextureHeight ( ) const
inline

Definition at line 174 of file image.h.

References mTexHeight.

175  { return mTexHeight; }
int mTexHeight
Definition: image.h:184

◆ getTextureWidth()

int Image::getTextureWidth ( ) const
inline

Definition at line 171 of file image.h.

References mTexWidth.

172  { return mTexWidth; }
int mTexWidth
Definition: image.h:183

◆ getType()

virtual ImageTypeT Image::getType ( ) const
inlinevirtual

Reimplemented in SubImage.

Definition at line 207 of file image.h.

References ImageType::Image.

Referenced by MapReader::readTileset().

208  { return ImageType::Image; }

◆ getWidth()

int Image::getWidth ( ) const
inline

◆ hasAlphaChannel()

bool Image::hasAlphaChannel ( ) const

Tells if the image has got an alpha channel

Returns
true if it's true, false otherwise.

Definition at line 263 of file image.cpp.

References mHasAlphaChannel, mLoaded, ImageHelper::mUseOpenGL, and RENDER_SOFTWARE.

Referenced by getHeight(), and SubImage::SubImage().

264 {
265  if (mLoaded)
266  return mHasAlphaChannel;
267 
268 #ifdef USE_OPENGL
270  return true;
271 #endif // USE_OPENGL
272 
273  return false;
274 }
bool mLoaded
Definition: image.h:240
static RenderType mUseOpenGL
Definition: imagehelper.h:117
bool mHasAlphaChannel
Definition: image.h:241

◆ isAlphaCalculated()

bool Image::isAlphaCalculated ( ) const
inline

Definition at line 196 of file image.h.

References mIsAlphaCalculated.

197  { return mIsAlphaCalculated; }
bool mIsAlphaCalculated
Definition: image.h:244

◆ isAlphaVisible()

bool Image::isAlphaVisible ( ) const
inline

Definition at line 190 of file image.h.

References mIsAlphaVisible.

191  { return mIsAlphaVisible; }
bool mIsAlphaVisible
Definition: image.h:243

◆ isHasAlphaChannel()

bool Image::isHasAlphaChannel ( ) const
inline

Definition at line 187 of file image.h.

References mHasAlphaChannel.

188  { return mHasAlphaChannel; }
bool mHasAlphaChannel
Definition: image.h:241

◆ isLoaded()

bool Image::isLoaded ( ) const
inline

Tells is the image is loaded

Definition at line 110 of file image.h.

References mLoaded.

111  { return mLoaded; }
bool mLoaded
Definition: image.h:240

◆ SDLCleanCache()

void Image::SDLCleanCache ( )

Definition at line 215 of file image.cpp.

References mAlphaCache, mSDLSurface, and ResourceManager::scheduleDelete().

Referenced by SDLgetAlphaChannel(), SDLTerminateAlphaCache(), setAlpha(), and unload().

216 {
217  for (std::map<float, SDL_Surface*>::iterator
218  i = mAlphaCache.begin(), i_end = mAlphaCache.end();
219  i != i_end; ++i)
220  {
221  if (mSDLSurface != i->second)
223  i->second = nullptr;
224  }
225  mAlphaCache.clear();
226 }
SDL_Surface * mSDLSurface
Definition: image.h:230
void scheduleDelete(SDL_Surface *const surface)
std::map< float, SDL_Surface * > mAlphaCache
Definition: image.h:238

◆ SDLgetAlphaChannel()

uint8_t* Image::SDLgetAlphaChannel ( ) const
inline

Get the alpha Channel of a SDL surface.

Definition at line 163 of file image.h.

References mAlphaChannel, SDLCleanCache(), and SDLTerminateAlphaCache().

Referenced by SubImage::SubImage().

164  { return mAlphaChannel; }
uint8_t * mAlphaChannel
Definition: image.h:236

◆ SDLgetScaledImage()

Image * Image::SDLgetScaledImage ( const int  width,
const int  height 
) const

Gets an scaled instance of an image.

Parameters
widthThe desired width of the scaled image.
heightThe desired height of the scaled image.
Returns
A new Image* object.

Definition at line 435 of file image.cpp.

References imageHelper, ImageHelper::loadSurface(), mBounds, MSDL_FreeSurface, mSDLSurface, and zoomSurface().

Referenced by SDLGraphics::drawRescaledImage(), SDLGraphics::drawRescaledPattern(), getHeight(), and anonymous_namespace{rescaledloader.cpp}::RescaledLoader::load().

436 {
437  // No scaling on incorrect new values.
438  if (width == 0 || height == 0)
439  return nullptr;
440 
441  // No scaling when there is ... no different given size ...
442  if (width == mBounds.w && height == mBounds.h)
443  return nullptr;
444 
445  Image* scaledImage = nullptr;
446 
447  if (mSDLSurface != nullptr)
448  {
449  SDL_Surface *const scaledSurface = zoomSurface(mSDLSurface,
450  static_cast<double>(width) / mBounds.w,
451  static_cast<double>(height) / mBounds.h,
452  1);
453 
454  // The load function takes care of the SDL<->OpenGL implementation
455  // and about freeing the given SDL_surface*.
456  if (scaledSurface != nullptr)
457  {
458  scaledImage = imageHelper->loadSurface(scaledSurface);
459  MSDL_FreeSurface(scaledSurface);
460  }
461  }
462  return scaledImage;
463 }
ImageHelper * imageHelper
Definition: imagehelper.cpp:43
#define MSDL_FreeSurface(surface)
Definition: debug.h:53
SDL_Surface * mSDLSurface
Definition: image.h:230
virtual Image * loadSurface(SDL_Surface *const)
Definition: imagehelper.h:72
Definition: image.h:61
SDL_Rect mBounds
Definition: image.h:210
SDL_Surface * zoomSurface(SDL_Surface *src, double zoomx, double zoomy, int smooth)
Zoom a surface by independent horizontal and vertical factors with optional smoothing.

◆ SDLTerminateAlphaCache()

void Image::SDLTerminateAlphaCache ( )

Definition at line 500 of file image.cpp.

References mUseAlphaCache, and SDLCleanCache().

Referenced by SDLgetAlphaChannel(), and SubImage::SubImage().

501 {
502  SDLCleanCache();
503  mUseAlphaCache = false;
504 }
void SDLCleanCache()
Definition: image.cpp:215
bool mUseAlphaCache
Definition: image.h:242

◆ setAlpha()

void Image::setAlpha ( const float  alpha)
virtual

Sets the alpha value of this image.

< Blit uses source alpha blending

< Surface is in video memory

< Use asynchronous blits if possible

< Surface is RLE encoded

< Surface is in video memory

< Use asynchronous blits if possible

< Surface is RLE encoded

Definition at line 285 of file image.cpp.

References CAST_U8, getByAlpha(), Logger::log(), logger, mAlpha, mAlphaCache, mAlphaChannel, mBounds, ImageHelper::mEnableAlpha, mHasAlphaChannel, mSDLSurface, mUseAlphaCache, SDLCleanCache(), SDLImageHelper::SDLDuplicateSurface(), Catch::toString(), and y.

Referenced by OpenGLImageHelper::createTextSurface(), SafeOpenGLImageHelper::createTextSurface(), ImageSprite::draw(), ImageParticle::draw(), EmoteShortcutContainer::draw(), SpellShortcutContainer::draw(), ItemShortcutContainer::draw(), VirtShortcutContainer::draw(), ShopListBox::draw(), AnimatedSprite::draw(), EquipmentWindow::draw(), ItemContainer::draw(), PlayerBox::draw(), Gui::draw(), Being::drawCompound(), AnimatedSprite::drawRaw(), AnimatedSprite::drawRescaled(), CompoundSprite::drawSimple(), Font::drawString(), DropDown::DropDown(), EmoteShortcutContainer::EmoteShortcutContainer(), getHeight(), ItemShortcutContainer::safeDraw(), SpellShortcutContainer::safeDraw(), EmoteShortcutContainer::safeDraw(), VirtShortcutContainer::safeDraw(), EquipmentWindow::safeDraw(), ItemContainer::safeDraw(), AnimatedSprite::setAlpha(), Minimap::setMap(), ShortcutContainer::ShortcutContainer(), TestLauncher::testDraw(), Text::Text(), DropDown::updateAlpha(), Skin::updateAlpha(), Tab::updateAlpha(), Slider::updateAlpha(), CheckBox::updateAlpha(), TextField::updateAlpha(), RadioButton::updateAlpha(), ScrollArea::updateAlpha(), and Button::updateAlpha().

286 {
287  if (mAlpha == alpha || !ImageHelper::mEnableAlpha)
288  return;
289 
290  if (alpha < 0.0F || alpha > 1.0F)
291  return;
292 
293  if (mSDLSurface != nullptr)
294  {
295  if (mUseAlphaCache)
296  {
297  SDL_Surface *surface = getByAlpha(mAlpha);
298  if (surface == nullptr)
299  {
300  if (mAlphaCache.size() > 100)
301  {
302 #ifdef DEBUG_ALPHA_CACHE
303  logger->log("cleanCache");
304  for (std::map<float, SDL_Surface*>::const_iterator
305  i = mAlphaCache.begin(), i_end = mAlphaCache.end();
306  i != i_end; ++i)
307  {
308  logger->log("alpha: " + toString(i->first));
309  }
310 #endif // DEBUG_ALPHA_CACHE
311 
312  SDLCleanCache();
313  }
314  surface = mSDLSurface;
315  if (surface != nullptr)
316  mAlphaCache[mAlpha] = surface;
317  }
318  else
319  {
320  logger->log("cache bug");
321  }
322 
323  surface = getByAlpha(alpha);
324  if (surface != nullptr)
325  {
326  if (mSDLSurface == surface)
327  logger->log("bug");
328  mAlphaCache.erase(alpha);
329  mSDLSurface = surface;
330  mAlpha = alpha;
331  return;
332  }
334  if (mSDLSurface == nullptr)
335  return;
336  }
337 
338  mAlpha = alpha;
339 
340  if (!mHasAlphaChannel)
341  {
342 #ifdef USE_SDL2
343  SDL_SetSurfaceAlphaMod(mSDLSurface,
344  CAST_U8(255 * mAlpha));
345 #else // USE_SDL2
346 
347  // Set the alpha value this image is drawn at
348  SDL_SetAlpha(mSDLSurface, SDL_SRCALPHA,
349  CAST_U8(255 * mAlpha));
350 #endif // USE_SDL2
351  }
352  else
353  {
354  if (SDL_MUSTLOCK(mSDLSurface))
355  SDL_LockSurface(mSDLSurface);
356 
357  const int bx = mBounds.x;
358  const int by = mBounds.y;
359  const int bw = mBounds.w;
360  const int bh = mBounds.h;
361  const int bxw = bx + bw;
362  const int sw = mSDLSurface->w;
363  const int maxHeight = std::min(by + bh, mSDLSurface->h);
364  const int maxWidth = std::min(bxw, sw);
365  const int i1 = by * sw + bx;
366  const SDL_PixelFormat * const fmt = mSDLSurface->format;
367  const uint32_t amask = fmt->Amask;
368  const uint32_t invMask = ~fmt->Amask;
369  const uint8_t aloss = fmt->Aloss;
370  const uint8_t ashift = fmt->Ashift;
371 
372  if ((bx == 0) && bxw == sw)
373  {
374  const int i2 = (maxHeight - 1) * sw + maxWidth - 1;
375  for (int i = i1; i <= i2; i++)
376  {
377  const uint8_t sourceAlpha = mAlphaChannel[i];
378  if (sourceAlpha > 0)
379  {
380  const uint8_t a = CAST_U8(
381  static_cast<float>(sourceAlpha) * mAlpha);
382 
383  uint32_t c = (static_cast<uint32_t*>(
384  mSDLSurface->pixels))[i];
385  c &= invMask;
386  c |= ((a >> aloss) << ashift & amask);
387  (static_cast<uint32_t*>(mSDLSurface->pixels))[i] = c;
388  }
389  }
390  }
391  else
392  {
393  for (int y = by; y < maxHeight; y ++)
394  {
395  const int idx = y * sw;
396  const int x1 = idx + bx;
397  const int x2 = idx + maxWidth;
398  for (int i = x1; i < x2; i ++)
399  {
400  const uint8_t sourceAlpha = mAlphaChannel[i];
401  if (sourceAlpha > 0)
402  {
403  const uint8_t a = CAST_U8(
404  static_cast<float>(sourceAlpha) * mAlpha);
405 
406  uint32_t c = (static_cast<uint32_t*>(
407  mSDLSurface->pixels))[i];
408  c &= invMask;
409  c |= ((a >> aloss) << ashift & amask);
410  (static_cast<uint32_t*>(
411  mSDLSurface->pixels))[i] = c;
412  }
413  }
414  }
415  }
416 
417  if (SDL_MUSTLOCK(mSDLSurface))
418  SDL_UnlockSurface(mSDLSurface);
419  }
420  }
421 #ifdef USE_SDL2
422  else if (mTexture)
423  {
424  mAlpha = alpha;
425  SDL_SetTextureAlphaMod(mTexture,
426  CAST_U8(255 * mAlpha));
427  }
428 #endif // USE_SDL2
429  else
430  {
431  mAlpha = alpha;
432  }
433 }
void SDLCleanCache()
Definition: image.cpp:215
#define CAST_U8
Definition: cast.h:26
Logger * logger
Definition: logger.cpp:95
SDL_Surface * mSDLSurface
Definition: image.h:230
uint8_t * mAlphaChannel
Definition: image.h:236
bool mUseAlphaCache
Definition: image.h:242
float mAlpha
Definition: image.h:212
bool mHasAlphaChannel
Definition: image.h:241
SDL_Surface * getByAlpha(const float alpha)
Definition: image.cpp:276
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
static bool mEnableAlpha
Definition: imagehelper.h:116
static SDL_Surface * SDLDuplicateSurface(SDL_Surface *const tmpImage)
SDL_Rect mBounds
Definition: image.h:210
void log(const char *const log_text,...)
Definition: logger.cpp:243
std::map< float, SDL_Surface * > mAlphaCache
Definition: image.h:238

◆ setAlphaCalculated()

void Image::setAlphaCalculated ( const bool  b)
inline

Definition at line 199 of file image.h.

References mIsAlphaCalculated.

200  { mIsAlphaCalculated = b; }
bool mIsAlphaCalculated
Definition: image.h:244

◆ setAlphaVisible()

void Image::setAlphaVisible ( const bool  b)
inline

Definition at line 193 of file image.h.

References mIsAlphaVisible.

194  { mIsAlphaVisible = b; }
bool mIsAlphaVisible
Definition: image.h:243

◆ unload()

void Image::unload ( )

Frees the resources created by SDL.

Definition at line 228 of file image.cpp.

References mAlphaChannel, mGLImage, mLoaded, MSDL_FreeSurface, mSDLSurface, SDLCleanCache(), and textures_count.

Referenced by ~Image().

229 {
230  mLoaded = false;
231 
232  if (mSDLSurface != nullptr)
233  {
234  SDLCleanCache();
235  // Free the image surface.
237  mSDLSurface = nullptr;
238 
239  delete [] mAlphaChannel;
240  mAlphaChannel = nullptr;
241  }
242 #ifdef USE_SDL2
243  if (mTexture)
244  {
245  SDL_DestroyTexture(mTexture);
246  mTexture = nullptr;
247  }
248 #endif // USE_SDL2
249 
250 #ifdef USE_OPENGL
251  if (mGLImage != 0u)
252  {
253  glDeleteTextures(1, &mGLImage);
254  mGLImage = 0;
255 #ifdef DEBUG_OPENGL_LEAKS
256  if (textures_count > 0)
257  textures_count --;
258 #endif // DEBUG_OPENGL_LEAKS
259  }
260 #endif // USE_OPENGL
261 }
void SDLCleanCache()
Definition: image.cpp:215
int textures_count
Definition: client.cpp:125
#define MSDL_FreeSurface(surface)
Definition: debug.h:53
bool mLoaded
Definition: image.h:240
SDL_Surface * mSDLSurface
Definition: image.h:230
uint8_t * mAlphaChannel
Definition: image.h:236
GLuint mGLImage
Definition: image.h:182

Friends And Related Function Documentation

◆ AtlasManager

friend class AtlasManager
friend

Definition at line 76 of file image.h.

◆ CompoundSprite

friend class CompoundSprite
friend

Definition at line 63 of file image.h.

◆ Graphics

friend class Graphics
friend

Definition at line 64 of file image.h.

◆ ImageHelper

friend class ImageHelper
friend

Definition at line 65 of file image.h.

◆ MobileOpenGL2Graphics

friend class MobileOpenGL2Graphics
friend

Definition at line 77 of file image.h.

◆ MobileOpenGLGraphics

friend class MobileOpenGLGraphics
friend

Definition at line 78 of file image.h.

◆ ModernOpenGLGraphics

friend class ModernOpenGLGraphics
friend

Definition at line 79 of file image.h.

◆ NormalOpenGLGraphics

friend class NormalOpenGLGraphics
friend

Definition at line 80 of file image.h.

◆ NullOpenGLGraphics

friend class NullOpenGLGraphics
friend

Definition at line 81 of file image.h.

◆ OpenGLImageHelper

friend class OpenGLImageHelper
friend

Definition at line 83 of file image.h.

◆ SafeOpenGLGraphics

friend class SafeOpenGLGraphics
friend

Definition at line 82 of file image.h.

◆ SafeOpenGLImageHelper

friend class SafeOpenGLImageHelper
friend

Definition at line 85 of file image.h.

◆ SDLGraphics

friend class SDLGraphics
friend

Definition at line 66 of file image.h.

◆ SDLImageHelper

friend class SDLImageHelper
friend

Definition at line 67 of file image.h.

◆ SurfaceGraphics

friend class SurfaceGraphics
friend

Definition at line 68 of file image.h.

◆ TestLauncher

friend class TestLauncher
friend

Definition at line 74 of file image.h.

Field Documentation

◆ mAlpha

float Image::mAlpha

◆ mAlphaCache

std::map<float, SDL_Surface*> Image::mAlphaCache
protected

Definition at line 238 of file image.h.

Referenced by calcMemoryLocal(), getByAlpha(), SDLCleanCache(), and setAlpha().

◆ mAlphaChannel

uint8_t* Image::mAlphaChannel
protected

Alpha Channel pointer used for 32bit based SDL surfaces

Definition at line 236 of file image.h.

Referenced by SDLgetAlphaChannel(), setAlpha(), SubImage::SubImage(), unload(), and SubImage::~SubImage().

◆ mBounds

SDL_Rect Image::mBounds

◆ mGLImage

GLuint Image::mGLImage

◆ mHasAlphaChannel

bool Image::mHasAlphaChannel
protected

Definition at line 241 of file image.h.

Referenced by hasAlphaChannel(), isHasAlphaChannel(), setAlpha(), and SubImage::SubImage().

◆ mIsAlphaCalculated

bool Image::mIsAlphaCalculated
protected

Definition at line 244 of file image.h.

Referenced by isAlphaCalculated(), and setAlphaCalculated().

◆ mIsAlphaVisible

bool Image::mIsAlphaVisible
protected

Definition at line 243 of file image.h.

Referenced by isAlphaVisible(), setAlphaVisible(), and SubImage::SubImage().

◆ mLoaded

bool Image::mLoaded
protected

Definition at line 240 of file image.h.

Referenced by hasAlphaChannel(), Image(), isLoaded(), and unload().

◆ mSDLSurface

SDL_Surface* Image::mSDLSurface
protected

◆ mTexHeight

int Image::mTexHeight

Definition at line 184 of file image.h.

Referenced by getSubImage(), and getTextureHeight().

◆ mTexWidth

int Image::mTexWidth

Definition at line 183 of file image.h.

Referenced by getSubImage(), and getTextureWidth().

◆ mUseAlphaCache

bool Image::mUseAlphaCache
protected

Definition at line 242 of file image.h.

Referenced by SDLTerminateAlphaCache(), setAlpha(), and SubImage::SubImage().


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