GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/image/image.h Lines: 5 12 41.7 %
Date: 2017-11-29 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-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
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
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
58
/**
59
 * Defines a class for loading and storing images.
60
 */
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
95
        A_DELETE_COPY(Image)
96
97
        /**
98
         * Destructor.
99
         */
100
        virtual ~Image();
101
102
        /**
103
         * Frees the resources created by SDL.
104
         */
105
        void unload();
106
107
        /**
108
         * Tells is the image is loaded
109
         */
110
        bool isLoaded() const noexcept2 A_WARN_UNUSED
111
        { return mLoaded; }
112
113
        /**
114
         * Returns the width of the image.
115
         */
116
        inline int getWidth() const noexcept2 A_WARN_UNUSED A_INLINE
117
53560
        { return mBounds.w; }
118
119
        /**
120
         * Returns the height of the image.
121
         */
122
        inline int getHeight() const noexcept2 A_WARN_UNUSED A_INLINE
123
11634
        { return mBounds.h; }
124
125
        /**
126
         * Tells if the image has got an alpha channel
127
         * @return true if it's true, false otherwise.
128
         */
129
        bool hasAlphaChannel() const A_WARN_UNUSED;
130
131
        /**
132
         * Sets the alpha value of this image.
133
         */
134
        virtual void setAlpha(const float alpha);
135
136
        /**
137
         * Creates a new image with the desired clipping rectangle.
138
         *
139
         * @return <code>NULL</code> if creation failed and a valid
140
         *         object otherwise.
141
         */
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
149
        /**
150
         * Gets an scaled instance of an image.
151
         *
152
         * @param width The desired width of the scaled image.
153
         * @param height The desired height of the scaled image.
154
         *
155
         * @return A new Image* object.
156
         */
157
        Image* SDLgetScaledImage(const int width,
158
                                 const int height) const A_WARN_UNUSED;
159
160
        /**
161
         * Get the alpha Channel of a SDL surface.
162
         */
163
        uint8_t *SDLgetAlphaChannel() const noexcept2 A_WARN_UNUSED
164
53010
        { 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
11468
        { return mGLImage; }
179
180
        void decRef() override;
181
182
        GLuint mGLImage;
183
        int mTexWidth;
184
        int mTexHeight;
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
20
        { 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
219
        /** SDL Constructor */
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
235
        /** Alpha Channel pointer used for 32bit based SDL surfaces */
236
        uint8_t *mAlphaChannel;
237
238
        std::map<float, SDL_Surface*> mAlphaCache;
239
240
        bool mLoaded;
241
        bool mHasAlphaChannel;
242
        bool mUseAlphaCache;
243
        bool mIsAlphaVisible;
244
        bool mIsAlphaCalculated;
245
246
        // -----------------------
247
        // OpenGL protected members
248
        // -----------------------
249
#ifdef USE_OPENGL
250
        /**
251
         * OpenGL Constructor.
252
         */
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