ManaPlus
subimage.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2018 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "utils/cast.h"
26 
27 #ifdef DEBUG_IMAGES
28 #include "logger.h"
29 #endif // DEBUG_IMAGES
30 
31 #include "debug.h"
32 
33 #ifdef USE_SDL2
34 SubImage::SubImage(Image *const parent,
35  SDL_Texture *const image,
36  const int x, const int y,
37  const int width, const int height) :
38  Image(image, width, height),
39  mInternalBounds(),
40  mParent(parent)
41 {
42  if (mParent)
43  {
44  mParent->incRef();
45  mParent->SDLTerminateAlphaCache();
46  mHasAlphaChannel = mParent->hasAlphaChannel();
48  mAlphaChannel = mParent->SDLgetAlphaChannel();
49  mSource = parent->mIdPath;
50 #ifdef DEBUG_IMAGES
51  logger->log("set name2 %p, %s",
52  static_cast<void*>(this),
53  mSource.c_str());
54 #endif // DEBUG_IMAGES
55 #ifdef DEBUG_BIND_TEXTURE
56  mIdPath = parent->mIdPath;
57 #endif // DEBUG_BIND_TEXTURE
58  }
59  else
60  {
61  mHasAlphaChannel = false;
62  mIsAlphaVisible = false;
63  mAlphaChannel = nullptr;
64  }
65 
66  // Set up the rectangle.
67  mBounds.x = CAST_S16(x);
68  mBounds.y = CAST_S16(y);
69  mBounds.w = CAST_U16(width);
70  mBounds.h = CAST_U16(height);
71  if (mParent)
72  {
73  mInternalBounds.x = mParent->mBounds.x;
74  mInternalBounds.y = mParent->mBounds.y;
75  mInternalBounds.w = mParent->mBounds.w;
76  mInternalBounds.h = mParent->mBounds.h;
77  }
78  else
79  {
80  mInternalBounds.x = 0;
81  mInternalBounds.y = 0;
82  mInternalBounds.w = 1;
83  mInternalBounds.h = 1;
84  }
85  mUseAlphaCache = false;
86 }
87 #endif // USE_SDL2
88 
89 SubImage::SubImage(Image *const parent,
90  SDL_Surface *const image,
91  const int x, const int y,
92  const int width, const int height) :
93  Image(image, false, nullptr),
94  mInternalBounds(),
95  mParent(parent)
96 {
97  if (mParent != nullptr)
98  {
99  mParent->incRef();
104  mSource = parent->mIdPath;
105 #ifdef DEBUG_IMAGES
106  logger->log("set name2 %p, %s", static_cast<void*>(this),
107  mSource.c_str());
108 #endif // DEBUG_IMAGES
109 #ifdef DEBUG_BIND_TEXTURE
110  mIdPath = parent->mIdPath;
111 #endif // DEBUG_BIND_TEXTURE
112  }
113  else
114  {
115  mHasAlphaChannel = false;
116  mIsAlphaVisible = false;
117  mAlphaChannel = nullptr;
118  }
119 
120  // Set up the rectangle.
121  mBounds.x = CAST_S16(x);
122  mBounds.y = CAST_S16(y);
123  mBounds.w = CAST_U16(width);
124  mBounds.h = CAST_U16(height);
125  if (mParent != nullptr)
126  {
131  }
132  else
133  {
134  mInternalBounds.x = 0;
135  mInternalBounds.y = 0;
136  mInternalBounds.w = 1;
137  mInternalBounds.h = 1;
138  }
139  mUseAlphaCache = false;
140 }
141 
142 #ifdef USE_OPENGL
143 SubImage::SubImage(Image *const parent,
144  const GLuint image,
145  const int x, const int y,
146  const int width, const int height,
147  const int texWidth, const int texHeight) :
148  Image(image, width, height, texWidth, texHeight),
149  mInternalBounds(),
150  mParent(parent)
151 {
152  if (mParent != nullptr)
153  mParent->incRef();
154 
155  // Set up the rectangle.
156  mBounds.x = CAST_S16(x);
157  mBounds.y = CAST_S16(y);
158  mBounds.w = CAST_U16(width);
159  mBounds.h = CAST_U16(height);
160  if (mParent != nullptr)
161  {
166  mSource = parent->mIdPath;
167 #ifdef DEBUG_IMAGES
168  logger->log("set name2 %p, %s", static_cast<void*>(this),
169  mSource.c_str());
170 #endif // DEBUG_IMAGES
171 #ifdef DEBUG_BIND_TEXTURE
172  mIdPath = parent->mIdPath;
173 #endif // DEBUG_BIND_TEXTURE
174  }
175  else
176  {
177  mInternalBounds.x = 0;
178  mInternalBounds.y = 0;
179  mInternalBounds.w = 1;
180  mInternalBounds.h = 1;
181  }
183 }
184 #endif // USE_OPENGL
185 
187 {
188 #ifdef DEBUG_IMAGES
189  logger->log("delete subimage: %p", static_cast<void*>(this));
190  logger->log(" %s, %s", mIdPath.c_str(), mSource.c_str());
191 #endif // DEBUG_IMAGES
192  // Avoid destruction of the image
193  mSDLSurface = nullptr;
194  // Avoid possible destruction of its alpha channel
195  mAlphaChannel = nullptr;
196 #ifdef USE_SDL2
197  // Avoid destruction of texture
198  mTexture = nullptr;
199 #endif // USE_SDL2
200 #ifdef USE_OPENGL
201  mGLImage = 0;
202 #endif // USE_OPENGL
203  if (mParent != nullptr)
204  {
205 #ifdef DEBUG_IMAGES
206  logger->log("decref from subminage: %p, parent: %p",
207  static_cast<void*>(this), static_cast<void*>(mParent));
208 #endif // DEBUG_IMAGES
209 
210  mParent->decRef();
211  mParent = nullptr;
212  }
213 }
214 
215 Image *SubImage::getSubImage(const int x, const int y,
216  const int w, const int h)
217 {
218  if (mParent != nullptr)
219  return mParent->getSubImage(mBounds.x + x, mBounds.y + y, w, h);
220  return nullptr;
221 }
222 
223 #ifdef USE_OPENGL
225 {
227 }
228 #endif // USE_OPENGL
229 
231 {
232  return static_cast<int>(sizeof(SubImage) +
233  sizeof(std::map<float, SDL_Surface*>)) +
235 }
std::string mSource
Definition: resource.h:84
bool hasAlphaChannel() const
Definition: image.cpp:264
void decRef()
Definition: subimage.cpp:224
int calcMemoryLocal() const
Definition: image.cpp:507
virtual void decRef()
Definition: resource.cpp:49
void SDLTerminateAlphaCache()
Definition: image.cpp:501
SDL_Rect mInternalBounds
Definition: subimage.h:78
uint8_t * SDLgetAlphaChannel() const
Definition: image.h:163
int calcMemoryLocal() const
Definition: subimage.cpp:230
Logger * logger
Definition: logger.cpp:88
#define CAST_U16
Definition: cast.h:28
SDL_Surface * mSDLSurface
Definition: image.h:231
Image * mParent
Definition: subimage.h:81
bool mIsAlphaVisible
Definition: image.h:244
virtual Image * getSubImage(const int x, const int y, const int width, const int height)
Definition: image.cpp:466
uint8_t * mAlphaChannel
Definition: image.h:237
bool mUseAlphaCache
Definition: image.h:243
#define nullptr
Definition: localconsts.h:44
bool mHasAlphaChannel
Definition: image.h:242
GLuint mGLImage
Definition: image.h:182
virtual void incRef()
Definition: resource.cpp:37
Image * getSubImage(const int x, const int y, const int width, const int height)
Definition: subimage.cpp:215
Definition: image.h:61
SDL_Rect mBounds
Definition: image.h:210
#define CAST_S16
Definition: cast.h:27
void log(const char *const log_text,...)
Definition: logger.cpp:264
std::string mIdPath
Definition: resource.h:83
void decRef()
Definition: image.cpp:522
SubImage(Image *const parent, SDL_Surface *const image, const int x, const int y, const int width, const int height)
Definition: subimage.cpp:89