ManaPlus
Public Member Functions | Data Fields | Private Attributes
SubImage Class Reference

#include <subimage.h>

Inheritance diagram for SubImage:
Image

Public Member Functions

 SubImage (Image *const parent, SDL_Surface *const image, const int x, const int y, const int width, const int height)
 
 SubImage (Image *const parent, const GLuint image, const int x, const int y, const int width, const int height, const int texWidth, const int textHeight)
 
 ~SubImage ()
 
ImagegetSubImage (const int x, const int y, const int width, const int height)
 
ImageTypeT getType () const
 
int calcMemoryLocal () const
 
void decRef ()
 

Data Fields

SDL_Rect mInternalBounds
 

Private Attributes

ImagemParent
 

Detailed Description

A clipped version of a larger image.

Definition at line 34 of file subimage.h.

Constructor & Destructor Documentation

◆ SubImage() [1/2]

SubImage::SubImage ( Image *const  parent,
SDL_Surface *const  image,
const int  x,
const int  y,
const int  width,
const int  height 
)

Constructor.

Definition at line 90 of file subimage.cpp.

93  :
94  Image(image, false, nullptr),
96  mParent(parent)
97 {
98  if (mParent != nullptr)
99  {
100  mParent->incRef();
101  mParent->SDLTerminateAlphaCache();
102  mHasAlphaChannel = mParent->hasAlphaChannel();
103  mIsAlphaVisible = mHasAlphaChannel;
104  mAlphaChannel = mParent->SDLgetAlphaChannel();
105  mSource = parent->mIdPath;
106 #ifdef DEBUG_IMAGES
107  logger->log("set name2 %p, %s", static_cast<void*>(this),
108  mSource.c_str());
109 #endif // DEBUG_IMAGES
110 #ifdef DEBUG_BIND_TEXTURE
111  mIdPath = parent->mIdPath;
112 #endif // DEBUG_BIND_TEXTURE
113  }
114  else
115  {
116  mHasAlphaChannel = false;
117  mIsAlphaVisible = false;
118  mAlphaChannel = nullptr;
119  }
120 
121  // Set up the rectangle.
122  mBounds.x = CAST_S16(x);
123  mBounds.y = CAST_S16(y);
124  mBounds.w = CAST_U16(width);
125  mBounds.h = CAST_U16(height);
126  if (mParent != nullptr)
127  {
128  mInternalBounds.x = mParent->mBounds.x;
129  mInternalBounds.y = mParent->mBounds.y;
130  mInternalBounds.w = mParent->mBounds.w;
131  mInternalBounds.h = mParent->mBounds.h;
132  }
133  else
134  {
135  mInternalBounds.x = 0;
136  mInternalBounds.y = 0;
137  mInternalBounds.w = 1;
138  mInternalBounds.h = 1;
139  }
140  mUseAlphaCache = false;
141 }
#define CAST_U16
Definition: cast.h:29
#define CAST_S16
Definition: cast.h:28
void log(const char *const log_text,...)
Definition: logger.cpp:269
Image * mParent
Definition: subimage.h:82
SDL_Rect mInternalBounds
Definition: subimage.h:79
Logger * logger
Definition: logger.cpp:89

References CAST_S16, CAST_U16, Logger::log(), logger, mInternalBounds, mParent, x, and y.

Referenced by calcMemoryLocal().

◆ SubImage() [2/2]

SubImage::SubImage ( Image *const  parent,
const GLuint  image,
const int  x,
const int  y,
const int  width,
const int  height,
const int  texWidth,
const int  textHeight 
)

Definition at line 144 of file subimage.cpp.

148  :
149  Image(image, width, height, texWidth, texHeight),
150  mInternalBounds(),
151  mParent(parent)
152 {
153  if (mParent != nullptr)
154  mParent->incRef();
155 
156  // Set up the rectangle.
157  mBounds.x = CAST_S16(x);
158  mBounds.y = CAST_S16(y);
159  mBounds.w = CAST_U16(width);
160  mBounds.h = CAST_U16(height);
161  if (mParent != nullptr)
162  {
163  mInternalBounds.x = mParent->mBounds.x;
164  mInternalBounds.y = mParent->mBounds.y;
165  mInternalBounds.w = mParent->mBounds.w;
166  mInternalBounds.h = mParent->mBounds.h;
167  mSource = parent->mIdPath;
168 #ifdef DEBUG_IMAGES
169  logger->log("set name2 %p, %s", static_cast<void*>(this),
170  mSource.c_str());
171 #endif // DEBUG_IMAGES
172 #ifdef DEBUG_BIND_TEXTURE
173  mIdPath = parent->mIdPath;
174 #endif // DEBUG_BIND_TEXTURE
175  }
176  else
177  {
178  mInternalBounds.x = 0;
179  mInternalBounds.y = 0;
180  mInternalBounds.w = 1;
181  mInternalBounds.h = 1;
182  }
183  mIsAlphaVisible = mHasAlphaChannel;
184 }

References CAST_S16, CAST_U16, Logger::log(), logger, mInternalBounds, mParent, x, and y.

◆ ~SubImage()

SubImage::~SubImage ( )

Destructor.

Definition at line 187 of file subimage.cpp.

188 {
189 #ifdef DEBUG_IMAGES
190  logger->log("delete subimage: %p", static_cast<void*>(this));
191  logger->log(" %s, %s", mIdPath.c_str(), mSource.c_str());
192 #endif // DEBUG_IMAGES
193  // Avoid destruction of the image
194  mSDLSurface = nullptr;
195  // Avoid possible destruction of its alpha channel
196  mAlphaChannel = nullptr;
197 #ifdef USE_SDL2
198  // Avoid destruction of texture
199  mTexture = nullptr;
200 #endif // USE_SDL2
201 #ifdef USE_OPENGL
202  mGLImage = 0;
203 #endif // USE_OPENGL
204  if (mParent != nullptr)
205  {
206 #ifdef DEBUG_IMAGES
207  logger->log("decref from subminage: %p, parent: %p",
208  static_cast<void*>(this), static_cast<void*>(mParent));
209 #endif // DEBUG_IMAGES
210 
211  mParent->decRef();
212  mParent = nullptr;
213  }
214 }

References Logger::log(), logger, and mParent.

Member Function Documentation

◆ calcMemoryLocal()

int SubImage::calcMemoryLocal ( ) const

Definition at line 231 of file subimage.cpp.

232 {
233  return static_cast<int>(sizeof(SubImage) +
234  sizeof(std::map<float, SDL_Surface*>)) +
236 }
SubImage(Image *const parent, SDL_Surface *const image, const int x, const int y, const int width, const int height)
Definition: subimage.cpp:90

References ResourceManager::calcMemoryLocal(), and SubImage().

◆ decRef()

void SubImage::decRef ( )

Definition at line 225 of file subimage.cpp.

226 {
228 }
virtual void decRef()
Definition: resource.cpp:50

References Resource::decRef().

◆ getSubImage()

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

Creates a new image with the desired clipping rectangle.

Returns
NULL if creation failed and a valid image otherwise.

Definition at line 216 of file subimage.cpp.

218 {
219  if (mParent != nullptr)
220  return mParent->getSubImage(mBounds.x + x, mBounds.y + y, w, h);
221  return nullptr;
222 }

References mParent, x, and y.

◆ getType()

ImageTypeT SubImage::getType ( ) const
inline

Definition at line 70 of file subimage.h.

71  { return ImageType::SubImage; }
@ SubImage
Definition: imagetype.h:30

References ImageType::SubImage.

Field Documentation

◆ mInternalBounds

SDL_Rect SubImage::mInternalBounds

Definition at line 79 of file subimage.h.

Referenced by Map::reduce(), and SubImage().

◆ mParent

Image* SubImage::mParent
private

Definition at line 82 of file subimage.h.

Referenced by getSubImage(), SubImage(), and ~SubImage().


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