GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/image/image.h Lines: 5 12 41.7 %
Date: 2019-08-19 Branches: 0 0 0.0 %

Line Branch Exec Source
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
28
#include "enums/resources/imagetype.h"
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
64
/**
65
 * Defines a class for loading and storing images.
66
 */
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
101
        A_DELETE_COPY(Image)
102
103
        /**
104
         * Destructor.
105
         */
106
        ~Image() override;
107
108
        /**
109
         * Frees the resources created by SDL.
110
         */
111
        void unload();
112
113
        /**
114
         * Tells is the image is loaded
115
         */
116
        bool isLoaded() const noexcept2 A_WARN_UNUSED
117
        { return mLoaded; }
118
119
        /**
120
         * Returns the width of the image.
121
         */
122
        inline int getWidth() const noexcept2 A_WARN_UNUSED A_INLINE
123
29546
        { return mBounds.w; }
124
125
        /**
126
         * Returns the height of the image.
127
         */
128
        inline int getHeight() const noexcept2 A_WARN_UNUSED A_INLINE
129
6305
        { return mBounds.h; }
130
131
        /**
132
         * Tells if the image has got an alpha channel
133
         * @return true if it's true, false otherwise.
134
         */
135
        bool hasAlphaChannel() const A_WARN_UNUSED;
136
137
        /**
138
         * Sets the alpha value of this image.
139
         */
140
        virtual void setAlpha(const float alpha);
141
142
        /**
143
         * Creates a new image with the desired clipping rectangle.
144
         *
145
         * @return <code>NULL</code> if creation failed and a valid
146
         *         object otherwise.
147
         */
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
155
        /**
156
         * Gets an scaled instance of an image.
157
         *
158
         * @param width The desired width of the scaled image.
159
         * @param height The desired height of the scaled image.
160
         *
161
         * @return A new Image* object.
162
         */
163
        Image* SDLgetScaledImage(const int width,
164
                                 const int height) const A_WARN_UNUSED;
165
166
        /**
167
         * Get the alpha Channel of a SDL surface.
168
         */
169
        uint8_t *SDLgetAlphaChannel() const noexcept2 A_WARN_UNUSED
170
29006
        { return mAlphaChannel; }
171
172
        void SDLCleanCache();
173
174
        void SDLTerminateAlphaCache();
175
176
#ifdef USE_OPENGL
177
        int getTextureWidth() const noexcept2 A_WARN_UNUSED
178
        { return mTexWidth; }
179
180
        int getTextureHeight() const noexcept2 A_WARN_UNUSED
181
        { return mTexHeight; }
182
183
        GLuint getGLImage() const noexcept2 A_WARN_UNUSED
184
5962
        { return mGLImage; }
185
186
        void decRef() override;
187
188
        GLuint mGLImage;
189
        int mTexWidth;
190
        int mTexHeight;
191
#endif  // USE_OPENGL
192
193
        bool isHasAlphaChannel() const noexcept2 A_WARN_UNUSED
194
        { return mHasAlphaChannel; }
195
196
        bool isAlphaVisible() const noexcept2 A_WARN_UNUSED
197
        { return mIsAlphaVisible; }
198
199
        void setAlphaVisible(const bool b)
200
        { mIsAlphaVisible = b; }
201
202
        bool isAlphaCalculated() const noexcept2 A_WARN_UNUSED
203
        { return mIsAlphaCalculated; }
204
205
        void setAlphaCalculated(const bool b) noexcept2
206
        { mIsAlphaCalculated = b; }
207
208
        SDL_Surface* getSDLSurface() noexcept2 A_WARN_UNUSED
209
10
        { return mSDLSurface; }
210
211
        int calcMemoryLocal() const override;
212
213
        virtual ImageTypeT getType() const noexcept2 A_WARN_UNUSED
214
        { return ImageType::Image; }
215
216
        SDL_Rect mBounds;
217
218
        float mAlpha;
219
220
    protected:
221
        // -----------------------
222
        // SDL protected members
223
        // -----------------------
224
225
        /** SDL Constructor */
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
242
        /** Alpha Channel pointer used for 32bit based SDL surfaces */
243
        uint8_t *mAlphaChannel;
244
245
        std::map<float, SDL_Surface*> mAlphaCache;
246
247
        bool mLoaded;
248
        bool mHasAlphaChannel;
249
        bool mUseAlphaCache;
250
        bool mIsAlphaVisible;
251
        bool mIsAlphaCalculated;
252
253
        // -----------------------
254
        // OpenGL protected members
255
        // -----------------------
256
#ifdef USE_OPENGL
257
        /**
258
         * OpenGL Constructor.
259
         */
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