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-2019 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 #ifdef HAVE_GLEXT
41 #define NO_SDL_GLEXT
42 #endif // HAVE_GLEXT
43 PRAGMA48(GCC diagnostic push)
44 PRAGMA48(GCC diagnostic ignored "-Wshadow")
45 #include <SDL_opengl.h>
46 PRAGMA48(GCC diagnostic pop)
47 #ifdef HAVE_GLEXT
48 #include <GL/glext.h>
49 #endif // HAVE_GLEXT
50 #endif // ANDROID
51 #endif // USE_OPENGL
52 
53 PRAGMA48(GCC diagnostic push)
54 PRAGMA48(GCC diagnostic ignored "-Wshadow")
55 #ifdef USE_SDL2
56 #include <SDL_render.h>
57 #else // USE_SDL2
58 #include <SDL_video.h>
59 #endif // USE_SDL2
60 PRAGMA48(GCC diagnostic pop)
61 
62 #include <map>
63 
67 class Image notfinal : public Resource
68 {
69  friend class CompoundSprite;
70  friend class Graphics;
71  friend class ImageHelper;
72  friend class SDLGraphics;
73  friend class SDLImageHelper;
74  friend class SurfaceGraphics;
75 #ifdef USE_SDL2
76  friend class SDL2SoftwareGraphics;
77  friend class SDL2SoftwareImageHelper;
78  friend class SurfaceImageHelper;
79 #endif // USE_SDL2
80  friend class TestLauncher;
81 #ifdef USE_OPENGL
82  friend class AtlasManager;
83  friend class MobileOpenGL2Graphics;
84  friend class MobileOpenGLGraphics;
85  friend class ModernOpenGLGraphics;
86  friend class NormalOpenGLGraphics;
87  friend class NullOpenGLGraphics;
88  friend class SafeOpenGLGraphics;
89  friend class OpenGLImageHelper;
90 #ifndef ANDROID
91  friend class SafeOpenGLImageHelper;
92 #endif // ANDROID
93 #endif // USE_OPENGL
94 
95  public:
96 #ifdef UNITTESTS
97  Image(const int width,
98  const int height);
99 #endif // UNITTESTS
100 
102 
103 
106  ~Image() override;
107 
111  void unload();
112 
117  { return mLoaded; }
118 
122  inline int getWidth() const noexcept2 A_WARN_UNUSED A_INLINE
123  { return mBounds.w; }
124 
128  inline int getHeight() const noexcept2 A_WARN_UNUSED A_INLINE
129  { return mBounds.h; }
130 
135  bool hasAlphaChannel() const A_WARN_UNUSED;
136 
140  virtual void setAlpha(const float alpha);
141 
148  virtual Image *getSubImage(const int x, const int y,
149  const int width,
150  const int height) A_WARN_UNUSED;
151 
152 
153  // SDL only public functions
154 
163  Image* SDLgetScaledImage(const int width,
164  const int height) const A_WARN_UNUSED;
165 
170  { return mAlphaChannel; }
171 
172  void SDLCleanCache();
173 
174  void SDLTerminateAlphaCache();
175 
176 #ifdef USE_OPENGL
178  { return mTexWidth; }
179 
181  { return mTexHeight; }
182 
184  { return mGLImage; }
185 
186  void decRef() override;
187 
188  GLuint mGLImage;
191 #endif // USE_OPENGL
192 
194  { return mHasAlphaChannel; }
195 
197  { return mIsAlphaVisible; }
198 
199  void setAlphaVisible(const bool b)
200  { mIsAlphaVisible = b; }
201 
203  { return mIsAlphaCalculated; }
204 
205  void setAlphaCalculated(const bool b) noexcept2
206  { mIsAlphaCalculated = b; }
207 
209  { return mSDLSurface; }
210 
211  int calcMemoryLocal() const override;
212 
214  { return ImageType::Image; }
215 
216  SDL_Rect mBounds;
217 
218  float mAlpha;
219 
220  protected:
221  // -----------------------
222  // SDL protected members
223  // -----------------------
224 
226  Image(SDL_Surface *restrict const image,
227  const bool hasAlphaChannel,
228  uint8_t *restrict const alphaChannel);
229 
230 #ifdef USE_SDL2
231  Image(SDL_Texture *restrict const image,
232  const int width, const int height);
233 #endif // USE_SDL2
234 
235  SDL_Surface *getByAlpha(const float alpha) A_WARN_UNUSED;
236 
237  SDL_Surface *mSDLSurface;
238 #ifdef USE_SDL2
239  SDL_Texture *mTexture;
240 #endif // USE_SDL2
241 
243  uint8_t *mAlphaChannel;
244 
245  std::map<float, SDL_Surface*> mAlphaCache;
246 
247  bool mLoaded;
252 
253  // -----------------------
254  // OpenGL protected members
255  // -----------------------
256 #ifdef USE_OPENGL
257 
260  Image(const GLuint glimage, const int width, const int height,
261  const int texWidth, const int texHeight);
262 #endif // USE_OPENGL
263 };
264 
265 #endif // RESOURCES_IMAGE_H
Image::hasAlphaChannel
bool hasAlphaChannel() const
Definition: image.cpp:264
SDLImageHelper
Definition: sdlimagehelper.h:42
Image::getTextureHeight
int getTextureHeight() const
Definition: image.h:180
imagetype.h
SafeOpenGLGraphics
Definition: safeopenglgraphics.h:50
CompoundSprite
Definition: compoundsprite.h:36
Image::mIsAlphaCalculated
bool mIsAlphaCalculated
Definition: image.h:251
Image::setAlphaVisible
void setAlphaVisible(const bool b)
Definition: image.h:199
Image::calcMemoryLocal
int calcMemoryLocal() const
Definition: image.cpp:507
Image::SDLCleanCache
void SDLCleanCache()
Definition: image.cpp:216
Image::mLoaded
bool mLoaded
Definition: image.h:247
Image::getHeight
int getHeight() const A_INLINE
Definition: image.h:128
ModernOpenGLGraphics
Definition: modernopenglgraphics.h:57
MobileOpenGLGraphics
Definition: mobileopenglgraphics.h:55
Resource
Definition: resource.h:33
Image::SDLgetAlphaChannel
uint8_t * SDLgetAlphaChannel() const
Definition: image.h:169
Image::SDLTerminateAlphaCache
void SDLTerminateAlphaCache()
Definition: image.cpp:501
A_DELETE_COPY
#define A_DELETE_COPY(func)
Definition: localconsts.h:52
Image::isHasAlphaChannel
bool isHasAlphaChannel() const
Definition: image.h:193
Image::setAlpha
virtual void setAlpha(const float alpha)
Definition: image.cpp:286
Image::~Image
~Image()
Definition: image.cpp:206
NullOpenGLGraphics
Definition: nullopenglgraphics.h:55
Image::isAlphaCalculated
bool isAlphaCalculated() const
Definition: image.h:202
ImageType::Image
@ Image
Definition: imagetype.h:28
ImageHelper
Definition: imagehelper.h:41
Image
Definition: image.h:67
notfinal
#define notfinal
Definition: localconsts.h:260
Graphics
Definition: graphics.h:107
localconsts.h
Image::Image
Image(SDL_Surface *const image, const bool hasAlphaChannel, uint8_t *const alphaChannel)
Definition: image.cpp:126
SafeOpenGLImageHelper
Definition: safeopenglimagehelper.h:62
Image::isAlphaVisible
bool isAlphaVisible() const
Definition: image.h:196
noexcept2
#define noexcept2
Definition: localconsts.h:49
Image::unload
void unload()
Definition: image.cpp:229
Image::getTextureWidth
int getTextureWidth() const
Definition: image.h:177
SurfaceGraphics
Definition: surfacegraphics.h:39
Image::mHasAlphaChannel
bool mHasAlphaChannel
Definition: image.h:248
Image::mIsAlphaVisible
bool mIsAlphaVisible
Definition: image.h:250
OpenGLImageHelper
Definition: openglimagehelper.h:62
Image::mAlphaChannel
uint8_t * mAlphaChannel
Definition: image.h:243
Image::mSDLSurface
SDL_Surface * mSDLSurface
Definition: image.h:237
Image::getSDLSurface
SDL_Surface * getSDLSurface()
Definition: image.h:208
TestLauncher
Definition: testlauncher.h:38
resource.h
Image::isLoaded
bool isLoaded() const
Definition: image.h:116
Image::mAlpha
float mAlpha
Definition: image.h:218
Image::mTexHeight
int mTexHeight
Definition: image.h:190
Image::getSubImage
virtual Image * getSubImage(const int x, const int y, const int width, const int height)
Definition: image.cpp:466
PRAGMA48
#define PRAGMA48(str)
Definition: localconsts.h:198
Image::mUseAlphaCache
bool mUseAlphaCache
Definition: image.h:249
x
x
Definition: graphics_calcImageRect.hpp:72
Image::getType
virtual ImageTypeT getType() const
Definition: image.h:213
SurfaceImageHelper
#define SurfaceImageHelper
Definition: sdlshared.h:61
NormalOpenGLGraphics
Definition: normalopenglgraphics.h:56
MobileOpenGL2Graphics
Definition: mobileopengl2graphics.h:56
Image::getByAlpha
SDL_Surface * getByAlpha(const float alpha)
Definition: image.cpp:277
Image::SDLgetScaledImage
Image * SDLgetScaledImage(const int width, const int height) const
Definition: image.cpp:436
Image::mBounds
SDL_Rect mBounds
Definition: image.h:216
Image::mGLImage
GLuint mGLImage
Definition: image.h:188
SDLGraphics
Definition: sdlgraphics.h:39
ImageTypeT
ImageType ::T ImageTypeT
Definition: imagetype.h:31
restrict
#define restrict
Definition: localconsts.h:164
Image::mAlphaCache
std::map< float, SDL_Surface * > mAlphaCache
Definition: image.h:245
Image::setAlphaCalculated
void setAlphaCalculated(const bool b)
Definition: image.h:205
A_WARN_UNUSED
#define A_WARN_UNUSED
Definition: localconsts.h:160
AtlasManager
Definition: atlasmanager.h:35
Image::getGLImage
GLuint getGLImage() const
Definition: image.h:183
y
y
Definition: graphics_calcImageRect.hpp:72
Image::mTexWidth
int mTexWidth
Definition: image.h:189
Image::getWidth
int getWidth() const A_INLINE
Definition: image.h:122
Image::decRef
void decRef()
Definition: image.cpp:522