ManaPlus
image.h
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 
23 #ifndef RESOURCES_IMAGE_H
24 #define RESOURCES_IMAGE_H
25 
26 #include "localconsts.h"
27 
29 
30 #include "resources/resource.h"
31 
32 #ifdef USE_OPENGL
33 
34 #ifdef ANDROID
35 #include <GLES/gl.h>
36 #else // ANDROID
37 #ifndef USE_SDL2
38 #define GL_GLEXT_PROTOTYPES 1
39 #endif // USE_SDL2
40 PRAGMA48(GCC diagnostic push)
41 PRAGMA48(GCC diagnostic ignored "-Wshadow")
42 #include <SDL_opengl.h>
43 PRAGMA48(GCC diagnostic pop)
44 #endif // ANDROID
45 #endif // USE_OPENGL
46 
47 PRAGMA48(GCC diagnostic push)
48 PRAGMA48(GCC diagnostic ignored "-Wshadow")
49 #ifdef USE_SDL2
50 #include <SDL_render.h>
51 #else // USE_SDL2
52 #include <SDL_video.h>
53 #endif // USE_SDL2
54 PRAGMA48(GCC diagnostic pop)
55 
56 #include <map>
57 
61 class Image notfinal : public Resource
62 {
63  friend class CompoundSprite;
64  friend class Graphics;
65  friend class ImageHelper;
66  friend class SDLGraphics;
67  friend class SDLImageHelper;
68  friend class SurfaceGraphics;
69 #ifdef USE_SDL2
70  friend class SDL2SoftwareGraphics;
71  friend class SDL2SoftwareImageHelper;
72  friend class SurfaceImageHelper;
73 #endif // USE_SDL2
74  friend class TestLauncher;
75 #ifdef USE_OPENGL
76  friend class AtlasManager;
77  friend class MobileOpenGL2Graphics;
78  friend class MobileOpenGLGraphics;
79  friend class ModernOpenGLGraphics;
80  friend class NormalOpenGLGraphics;
81  friend class NullOpenGLGraphics;
82  friend class SafeOpenGLGraphics;
83  friend class OpenGLImageHelper;
84 #ifndef ANDROID
85  friend class SafeOpenGLImageHelper;
86 #endif // ANDROID
87 #endif // USE_OPENGL
88 
89  public:
90 #ifdef UNITTESTS
91  Image(const int width,
92  const int height);
93 #endif // UNITTESTS
94 
96 
97 
100  virtual ~Image();
101 
105  void unload();
106 
111  { return mLoaded; }
112 
116  inline int getWidth() const noexcept2 A_WARN_UNUSED A_INLINE
117  { return mBounds.w; }
118 
122  inline int getHeight() const noexcept2 A_WARN_UNUSED A_INLINE
123  { return mBounds.h; }
124 
129  bool hasAlphaChannel() const A_WARN_UNUSED;
130 
134  virtual void setAlpha(const float alpha);
135 
142  virtual Image *getSubImage(const int x, const int y,
143  const int width,
144  const int height) A_WARN_UNUSED;
145 
146 
147  // SDL only public functions
148 
157  Image* SDLgetScaledImage(const int width,
158  const int height) const A_WARN_UNUSED;
159 
163  uint8_t *SDLgetAlphaChannel() const noexcept2 A_WARN_UNUSED
164  { return mAlphaChannel; }
165 
166  void SDLCleanCache();
167 
168  void SDLTerminateAlphaCache();
169 
170 #ifdef USE_OPENGL
171  int getTextureWidth() const noexcept2 A_WARN_UNUSED
172  { return mTexWidth; }
173 
174  int getTextureHeight() const noexcept2 A_WARN_UNUSED
175  { return mTexHeight; }
176 
177  GLuint getGLImage() const noexcept2 A_WARN_UNUSED
178  { return mGLImage; }
179 
180  void decRef() override;
181 
182  GLuint mGLImage;
185 #endif // USE_OPENGL
186 
187  bool isHasAlphaChannel() const noexcept2 A_WARN_UNUSED
188  { return mHasAlphaChannel; }
189 
190  bool isAlphaVisible() const noexcept2 A_WARN_UNUSED
191  { return mIsAlphaVisible; }
192 
193  void setAlphaVisible(const bool b)
194  { mIsAlphaVisible = b; }
195 
196  bool isAlphaCalculated() const noexcept2 A_WARN_UNUSED
197  { return mIsAlphaCalculated; }
198 
199  void setAlphaCalculated(const bool b) noexcept2
200  { mIsAlphaCalculated = b; }
201 
202  SDL_Surface* getSDLSurface() noexcept2 A_WARN_UNUSED
203  { return mSDLSurface; }
204 
205  int calcMemoryLocal() const override;
206 
207  virtual ImageTypeT getType() const noexcept2 A_WARN_UNUSED
208  { return ImageType::Image; }
209 
210  SDL_Rect mBounds;
211 
212  float mAlpha;
213 
214  protected:
215  // -----------------------
216  // SDL protected members
217  // -----------------------
218 
220  Image(SDL_Surface *restrict const image, const bool hasAlphaChannel,
221  uint8_t *restrict const alphaChannel = nullptr);
222 
223 #ifdef USE_SDL2
224  Image(SDL_Texture *restrict const image,
225  const int width, const int height);
226 #endif // USE_SDL2
227 
228  SDL_Surface *getByAlpha(const float alpha) A_WARN_UNUSED;
229 
230  SDL_Surface *mSDLSurface;
231 #ifdef USE_SDL2
232  SDL_Texture *mTexture;
233 #endif // USE_SDL2
234 
236  uint8_t *mAlphaChannel;
237 
238  std::map<float, SDL_Surface*> mAlphaCache;
239 
240  bool mLoaded;
245 
246  // -----------------------
247  // OpenGL protected members
248  // -----------------------
249 #ifdef USE_OPENGL
250 
253  Image(const GLuint glimage, const int width, const int height,
254  const int texWidth, const int texHeight);
255 #endif // USE_OPENGL
256 };
257 
258 #endif // RESOURCES_IMAGE_H
#define A_DELETE_COPY(func)
Definition: localconsts.h:52
ImageType ::T ImageTypeT
Definition: imagetype.h:31
bool hasAlphaChannel() const
Definition: image.cpp:263
void SDLCleanCache()
Definition: image.cpp:215
virtual ~Image()
Definition: image.cpp:205
bool isAlphaCalculated() const
Definition: image.h:196
int calcMemoryLocal() const
Definition: image.cpp:506
void SDLTerminateAlphaCache()
Definition: image.cpp:500
int getTextureHeight() const
Definition: image.h:174
#define A_WARN_UNUSED
Definition: localconsts.h:172
bool mIsAlphaCalculated
Definition: image.h:244
uint8_t * SDLgetAlphaChannel() const
Definition: image.h:163
bool mLoaded
Definition: image.h:240
bool isAlphaVisible() const
Definition: image.h:190
#define SurfaceImageHelper
Definition: sdlshared.h:61
bool isLoaded() const
Definition: image.h:110
bool isHasAlphaChannel() const
Definition: image.h:187
virtual void setAlpha(const float alpha)
Definition: image.cpp:285
SDL_Surface * getSDLSurface()
Definition: image.h:202
virtual ImageTypeT getType() const
Definition: image.h:207
SDL_Surface * mSDLSurface
Definition: image.h:230
int getTextureWidth() const
Definition: image.h:171
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
int mTexHeight
Definition: image.h:184
uint8_t * mAlphaChannel
Definition: image.h:236
bool mUseAlphaCache
Definition: image.h:242
#define notfinal
Definition: localconsts.h:289
void unload()
Definition: image.cpp:228
float mAlpha
Definition: image.h:212
bool mHasAlphaChannel
Definition: image.h:241
#define PRAGMA48(str)
Definition: localconsts.h:214
Image(SDL_Surface *const image, const bool hasAlphaChannel, uint8_t *const alphaChannel=0)
Definition: image.cpp:125
Image * SDLgetScaledImage(const int width, const int height) const
Definition: image.cpp:435
GLuint mGLImage
Definition: image.h:182
SDL_Surface * getByAlpha(const float alpha)
Definition: image.cpp:276
GLuint getGLImage() const
Definition: image.h:177
void setAlphaCalculated(const bool b)
Definition: image.h:199
Definition: image.h:61
#define restrict
Definition: localconsts.h:176
int getWidth() const A_INLINE
Definition: image.h:116
SDL_Rect mBounds
Definition: image.h:210
int mTexWidth
Definition: image.h:183
void setAlphaVisible(const bool b)
Definition: image.h:193
void decRef()
Definition: image.cpp:521
#define noexcept2
Definition: localconsts.h:49
std::map< float, SDL_Surface * > mAlphaCache
Definition: image.h:238
int getHeight() const A_INLINE
Definition: image.h:122