GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/image/image.h Lines: 5 7 71.4 %
Date: 2021-03-17 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
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#ifndef RESOURCES_IMAGE_H
25
#define RESOURCES_IMAGE_H
26
27
#include "localconsts.h"
28
29
#include "enums/resources/imagetype.h"
30
31
#include "resources/resource.h"
32
33
#ifdef USE_OPENGL
34
35
#ifdef ANDROID
36
#include <GLES/gl.h>
37
#else  // ANDROID
38
#ifndef USE_SDL2
39
#define GL_GLEXT_PROTOTYPES 1
40
#endif  // USE_SDL2
41
#ifdef HAVE_GLEXT
42
#define NO_SDL_GLEXT
43
#endif  // HAVE_GLEXT
44
PRAGMA48(GCC diagnostic push)
45
PRAGMA48(GCC diagnostic ignored "-Wshadow")
46
// ignore -Wredundant-decls for SDL 1.2
47
PRAGMA45(GCC diagnostic push)
48
PRAGMA45(GCC diagnostic ignored "-Wredundant-decls")
49
#include <SDL_opengl.h>
50
PRAGMA45(GCC diagnostic pop)
51
PRAGMA48(GCC diagnostic pop)
52
#ifdef HAVE_GLEXT
53
PRAGMA45(GCC diagnostic push)
54
PRAGMA45(GCC diagnostic ignored "-Wredundant-decls")
55
#include <GL/glext.h>
56
PRAGMA45(GCC diagnostic pop)
57
#endif  // HAVE_GLEXT
58
#endif  // ANDROID
59
#endif  // USE_OPENGL
60
61
PRAGMA48(GCC diagnostic push)
62
PRAGMA48(GCC diagnostic ignored "-Wshadow")
63
#ifdef USE_SDL2
64
#include <SDL_render.h>
65
#else  // USE_SDL2
66
#include <SDL_video.h>
67
#endif  // USE_SDL2
68
PRAGMA48(GCC diagnostic pop)
69
70
#include <map>
71
72
/**
73
 * Defines a class for loading and storing images.
74
 */
75
class Image notfinal : public Resource
76
{
77
    friend class CompoundSprite;
78
    friend class Graphics;
79
    friend class ImageHelper;
80
    friend class SDLGraphics;
81
    friend class SDLImageHelper;
82
    friend class SurfaceGraphics;
83
#ifdef USE_SDL2
84
    friend class SDL2SoftwareGraphics;
85
    friend class SDL2SoftwareImageHelper;
86
    friend class SurfaceImageHelper;
87
#endif  // USE_SDL2
88
    friend class TestLauncher;
89
#ifdef USE_OPENGL
90
    friend class AtlasManager;
91
    friend class MobileOpenGL2Graphics;
92
    friend class MobileOpenGLGraphics;
93
    friend class ModernOpenGLGraphics;
94
    friend class NormalOpenGLGraphics;
95
    friend class NullOpenGLGraphics;
96
    friend class SafeOpenGLGraphics;
97
    friend class OpenGLImageHelper;
98
#ifndef ANDROID
99
    friend class SafeOpenGLImageHelper;
100
#endif  // ANDROID
101
#endif  // USE_OPENGL
102
103
    public:
104
#ifdef UNITTESTS
105
        Image(const int width,
106
              const int height);
107
#endif  // UNITTESTS
108
109
        A_DELETE_COPY(Image)
110
111
        /**
112
         * Destructor.
113
         */
114
        ~Image() override;
115
116
        /**
117
         * Frees the resources created by SDL.
118
         */
119
        void unload();
120
121
        /**
122
         * Tells is the image is loaded
123
         */
124
        bool isLoaded() const noexcept2 A_WARN_UNUSED
125
        { return mLoaded; }
126
127
        /**
128
         * Returns the width of the image.
129
         */
130
        inline int getWidth() const noexcept2 A_WARN_UNUSED A_INLINE
131
28292
        { return mBounds.w; }
132
133
        /**
134
         * Returns the height of the image.
135
         */
136
        inline int getHeight() const noexcept2 A_WARN_UNUSED A_INLINE
137
6085
        { return mBounds.h; }
138
139
        /**
140
         * Tells if the image has got an alpha channel
141
         * @return true if it's true, false otherwise.
142
         */
143
        bool hasAlphaChannel() const A_WARN_UNUSED;
144
145
        /**
146
         * Sets the alpha value of this image.
147
         */
148
        virtual void setAlpha(const float alpha);
149
150
        /**
151
         * Creates a new image with the desired clipping rectangle.
152
         *
153
         * @return <code>NULL</code> if creation failed and a valid
154
         *         object otherwise.
155
         */
156
        virtual Image *getSubImage(const int x, const int y,
157
                                   const int width,
158
                                   const int height) A_WARN_UNUSED;
159
160
161
        // SDL only public functions
162
163
        /**
164
         * Gets an scaled instance of an image.
165
         *
166
         * @param width The desired width of the scaled image.
167
         * @param height The desired height of the scaled image.
168
         *
169
         * @return A new Image* object.
170
         */
171
        Image* SDLgetScaledImage(const int width,
172
                                 const int height) const A_WARN_UNUSED;
173
174
        /**
175
         * Get the alpha Channel of a SDL surface.
176
         */
177
        uint8_t *SDLgetAlphaChannel() const noexcept2 A_WARN_UNUSED
178
27906
        { return mAlphaChannel; }
179
180
        void SDLCleanCache();
181
182
        void SDLTerminateAlphaCache();
183
184
#ifdef USE_OPENGL
185
        int getTextureWidth() const noexcept2 A_WARN_UNUSED
186
        { return mTexWidth; }
187
188
        int getTextureHeight() const noexcept2 A_WARN_UNUSED
189
        { return mTexHeight; }
190
191
        GLuint getGLImage() const noexcept2 A_WARN_UNUSED
192
5896
        { return mGLImage; }
193
194
        void decRef() override;
195
196
        GLuint mGLImage;
197
        int mTexWidth;
198
        int mTexHeight;
199
#endif  // USE_OPENGL
200
201
        bool isHasAlphaChannel() const noexcept2 A_WARN_UNUSED
202
        { return mHasAlphaChannel; }
203
204
        bool isAlphaVisible() const noexcept2 A_WARN_UNUSED
205
        { return mIsAlphaVisible; }
206
207
        void setAlphaVisible(const bool b)
208
        { mIsAlphaVisible = b; }
209
210
        bool isAlphaCalculated() const noexcept2 A_WARN_UNUSED
211
        { return mIsAlphaCalculated; }
212
213
        void setAlphaCalculated(const bool b) noexcept2
214
        { mIsAlphaCalculated = b; }
215
216
        SDL_Surface* getSDLSurface() noexcept2 A_WARN_UNUSED
217
10
        { return mSDLSurface; }
218
219
        int calcMemoryLocal() const override;
220
221
        virtual ImageTypeT getType() const noexcept2 A_WARN_UNUSED
222
        { return ImageType::Image; }
223
224
        SDL_Rect mBounds;
225
226
        float mAlpha;
227
228
    protected:
229
        // -----------------------
230
        // SDL protected members
231
        // -----------------------
232
233
        /** SDL Constructor */
234
        Image(SDL_Surface *restrict const image,
235
              const bool hasAlphaChannel,
236
              uint8_t *restrict const alphaChannel);
237
238
#ifdef USE_SDL2
239
        Image(SDL_Texture *restrict const image,
240
              const int width, const int height);
241
#endif  // USE_SDL2
242
243
        SDL_Surface *getByAlpha(const float alpha) A_WARN_UNUSED;
244
245
        SDL_Surface *mSDLSurface;
246
#ifdef USE_SDL2
247
        SDL_Texture *mTexture;
248
#endif  // USE_SDL2
249
250
        /** Alpha Channel pointer used for 32bit based SDL surfaces */
251
        uint8_t *mAlphaChannel;
252
253
        std::map<float, SDL_Surface*> mAlphaCache;
254
255
        bool mLoaded;
256
        bool mHasAlphaChannel;
257
        bool mUseAlphaCache;
258
        bool mIsAlphaVisible;
259
        bool mIsAlphaCalculated;
260
261
        // -----------------------
262
        // OpenGL protected members
263
        // -----------------------
264
#ifdef USE_OPENGL
265
        /**
266
         * OpenGL Constructor.
267
         */
268
        Image(const GLuint glimage, const int width, const int height,
269
              const int texWidth, const int texHeight);
270
#endif  // USE_OPENGL
271
};
272
273
#endif  // RESOURCES_IMAGE_H