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-2017 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", this, mSource.c_str());
52 #endif // DEBUG_IMAGES
53 #ifdef DEBUG_BIND_TEXTURE
54  mIdPath = parent->mIdPath;
55 #endif // DEBUG_BIND_TEXTURE
56  }
57  else
58  {
59  mHasAlphaChannel = false;
60  mIsAlphaVisible = false;
61  mAlphaChannel = nullptr;
62  }
63 
64  // Set up the rectangle.
65  mBounds.x = CAST_S16(x);
66  mBounds.y = CAST_S16(y);
67  mBounds.w = CAST_U16(width);
68  mBounds.h = CAST_U16(height);
69  if (mParent)
70  {
71  mInternalBounds.x = mParent->mBounds.x;
72  mInternalBounds.y = mParent->mBounds.y;
73  mInternalBounds.w = mParent->mBounds.w;
74  mInternalBounds.h = mParent->mBounds.h;
75  }
76  else
77  {
78  mInternalBounds.x = 0;
79  mInternalBounds.y = 0;
80  mInternalBounds.w = 1;
81  mInternalBounds.h = 1;
82  }
83  mUseAlphaCache = false;
84 }
85 #endif // USE_SDL2
86 
87 SubImage::SubImage(Image *const parent,
88  SDL_Surface *const image,
89  const int x, const int y,
90  const int width, const int height) :
91  Image(image, false),
92  mInternalBounds(),
93  mParent(parent)
94 {
95  if (mParent != nullptr)
96  {
97  mParent->incRef();
102  mSource = parent->mIdPath;
103 #ifdef DEBUG_IMAGES
104  logger->log("set name2 %p, %s", static_cast<void*>(this),
105  mSource.c_str());
106 #endif // DEBUG_IMAGES
107 #ifdef DEBUG_BIND_TEXTURE
108  mIdPath = parent->mIdPath;
109 #endif // DEBUG_BIND_TEXTURE
110  }
111  else
112  {
113  mHasAlphaChannel = false;
114  mIsAlphaVisible = false;
115  mAlphaChannel = nullptr;
116  }
117 
118  // Set up the rectangle.
119  mBounds.x = CAST_S16(x);
120  mBounds.y = CAST_S16(y);
121  mBounds.w = CAST_U16(width);
122  mBounds.h = CAST_U16(height);
123  if (mParent != nullptr)
124  {
129  }
130  else
131  {
132  mInternalBounds.x = 0;
133  mInternalBounds.y = 0;
134  mInternalBounds.w = 1;
135  mInternalBounds.h = 1;
136  }
137  mUseAlphaCache = false;
138 }
139 
140 #ifdef USE_OPENGL
141 SubImage::SubImage(Image *const parent,
142  const GLuint image,
143  const int x, const int y,
144  const int width, const int height,
145  const int texWidth, const int texHeight) :
146  Image(image, width, height, texWidth, texHeight),
147  mInternalBounds(),
148  mParent(parent)
149 {
150  if (mParent != nullptr)
151  mParent->incRef();
152 
153  // Set up the rectangle.
154  mBounds.x = CAST_S16(x);
155  mBounds.y = CAST_S16(y);
156  mBounds.w = CAST_U16(width);
157  mBounds.h = CAST_U16(height);
158  if (mParent != nullptr)
159  {
164  mSource = parent->mIdPath;
165 #ifdef DEBUG_IMAGES
166  logger->log("set name2 %p, %s", static_cast<void*>(this),
167  mSource.c_str());
168 #endif // DEBUG_IMAGES
169 #ifdef DEBUG_BIND_TEXTURE
170  mIdPath = parent->mIdPath;
171 #endif // DEBUG_BIND_TEXTURE
172  }
173  else
174  {
175  mInternalBounds.x = 0;
176  mInternalBounds.y = 0;
177  mInternalBounds.w = 1;
178  mInternalBounds.h = 1;
179  }
181 }
182 #endif // USE_OPENGL
183 
185 {
186 #ifdef DEBUG_IMAGES
187  logger->log("delete subimage: %p", static_cast<void*>(this));
188  logger->log(" %s, %s", mIdPath.c_str(), mSource.c_str());
189 #endif // DEBUG_IMAGES
190  // Avoid destruction of the image
191  mSDLSurface = nullptr;
192  // Avoid possible destruction of its alpha channel
193  mAlphaChannel = nullptr;
194 #ifdef USE_SDL2
195  // Avoid destruction of texture
196  mTexture = nullptr;
197 #endif // USE_SDL2
198 #ifdef USE_OPENGL
199  mGLImage = 0;
200 #endif // USE_OPENGL
201  if (mParent != nullptr)
202  {
203 #ifdef DEBUG_IMAGES
204  logger->log("decref from subminage: %p, parent: %p",
205  static_cast<void*>(this), static_cast<void*>(mParent));
206 #endif // DEBUG_IMAGES
207 
208  mParent->decRef();
209  mParent = nullptr;
210  }
211 }
212 
213 Image *SubImage::getSubImage(const int x, const int y,
214  const int w, const int h)
215 {
216  if (mParent != nullptr)
217  return mParent->getSubImage(mBounds.x + x, mBounds.y + y, w, h);
218  return nullptr;
219 }
220 
221 #ifdef USE_OPENGL
223 {
225 }
226 #endif // USE_OPENGL
227 
229 {
230  return static_cast<int>(sizeof(SubImage) +
231  sizeof(std::map<float, SDL_Surface*>)) +
233 }
std::string mSource
Definition: resource.h:84
bool hasAlphaChannel() const
Definition: image.cpp:263
void decRef()
Definition: subimage.cpp:222
virtual void decRef()
Definition: resource.cpp:49
void SDLTerminateAlphaCache()
Definition: image.cpp:500
int calcMemoryLocal() const
Definition: resource.cpp:75
SDL_Rect mInternalBounds
Definition: subimage.h:78
uint8_t * SDLgetAlphaChannel() const
Definition: image.h:163
int calcMemoryLocal() const
Definition: subimage.cpp:228
Logger * logger
Definition: logger.cpp:95
#define CAST_U16
Definition: cast.h:28
SDL_Surface * mSDLSurface
Definition: image.h:230
Image * mParent
Definition: subimage.h:81
bool mIsAlphaVisible
Definition: image.h:243
virtual Image * getSubImage(const int x, const int y, const int width, const int height)
Definition: image.cpp:465
uint8_t * mAlphaChannel
Definition: image.h:236
bool mUseAlphaCache
Definition: image.h:242
bool mHasAlphaChannel
Definition: image.h:241
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:213
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:243
std::string mIdPath
Definition: resource.h:83
void decRef()
Definition: image.cpp:521
SubImage(Image *const parent, SDL_Surface *const image, const int x, const int y, const int width, const int height)
Definition: subimage.cpp:87