GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/sdl.cc Lines: 209 209 100.0 %
Date: 2021-03-17 Branches: 346 870 39.8 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "unittests/unittests.h"
23
24
#include "configmanager.h"
25
#include "configuration.h"
26
#include "client.h"
27
#include "dirs.h"
28
#include "graphicsmanager.h"
29
30
#include "being/actorsprite.h"
31
32
#include "fs/files.h"
33
34
#include "fs/virtfs/fs.h"
35
36
#include "gui/userpalette.h"
37
#include "gui/theme.h"
38
39
#include "resources/sdlimagehelper.h"
40
#ifdef USE_SDL2
41
#include "resources/surfaceimagehelper.h"
42
#endif  // USE_SDL2
43
44
#include "resources/resourcemanager/resourcemanager.h"
45
46
#include "utils/env.h"
47
#include "utils/delete2.h"
48
49
#ifndef USE_SDL2
50
PRAGMA48(GCC diagnostic push)
51
PRAGMA48(GCC diagnostic ignored "-Wshadow")
52
#include <SDL_image.h>
53
PRAGMA48(GCC diagnostic pop)
54
#endif  // USE_SDL2
55
56
#include "debug.h"
57
58
7
TEST_CASE("sdl tests", "sdl")
59
{
60
5
    setEnv("SDL_VIDEODRIVER", "dummy");
61
62
5
    client = new Client;
63
5
    XML::initXML();
64
5
    SDL_Init(SDL_INIT_VIDEO);
65
20
    std::string name("data/test/test.zip");
66
10
    std::string prefix;
67

5
    if (Files::existsLocal(name) == false)
68
10
        prefix = "../" + prefix;
69
70

15
    VirtFs::mountDirSilent("data", Append_false);
71

15
    VirtFs::mountDirSilent("../data", Append_false);
72
73
5
    Dirs::initRootDir();
74
5
    Dirs::initHomeDir();
75
76
5
    setBrandingDefaults(branding);
77
5
    ConfigManager::initConfiguration();
78
79
#ifdef USE_SDL2
80
    imageHelper = new SurfaceImageHelper;
81
82
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
83
        GraphicsManager::createWindow(640, 480, 0,
84
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
85
#else  // USE_SDL2
86
87
10
    imageHelper = new SDLImageHelper();
88
89
5
    GraphicsManager::createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
90
#endif  // USE_SDL2
91
92

5
    userPalette = new UserPalette;
93

15
    config.setValue("fontSize", 16);
94
95

5
    theme = new Theme;
96
5
    Theme::selectSkin();
97
98
5
    ActorSprite::load();
99
100



35
    SECTION("sdl SDL DuplicateSurface1")
101
    {
102
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
103
        const int rmask = 0xff000000;
104
        const int gmask = 0x00ff0000;
105
        const int bmask = 0x0000ff00;
106
        const int amask = 0x000000ff;
107
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
108
109
1
        const int rmask = 0x000000ff;
110
1
        const int gmask = 0x0000ff00;
111
1
        const int bmask = 0x00ff0000;
112
1
        const int amask = 0xff000000;
113
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
114
115
        SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_HWSURFACE,
116
1
            10, 10, 32, rmask, gmask, bmask, amask);
117
1
        uint32_t *ptr = reinterpret_cast<uint32_t*>(surface->pixels);
118
101
        for (int f = 0; f < 100; f ++)
119
        {
120
100
            ptr[f] = 300 * f;
121
        }
122
#ifdef USE_SDL2
123
        SDL_Surface *surface2 = SurfaceImageHelper::SDLDuplicateSurface(
124
            surface);
125
#else  // USE_SDL2
126
127
        SDL_Surface *surface2 = SDLImageHelper::SDLDuplicateSurface(
128
1
            surface);
129
#endif  // USE_SDL2
130
131
1
        uint32_t *ptr2 = reinterpret_cast<uint32_t*>(surface2->pixels);
132
101
        for (int f = 0; f < 100; f ++)
133
        {
134



400
            REQUIRE(ptr[f] == ptr2[f]);
135
        }
136
137
1
        SDL_FreeSurface(surface);
138
1
        SDL_FreeSurface(surface2);
139
    }
140
141



35
    SECTION("sdl SDL DuplicateSurface2")
142
    {
143
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
144
        const int rmask = 0x000000ff;
145
        const int gmask = 0x0000ff00;
146
        const int bmask = 0x00ff0000;
147
        const int amask = 0xff000000;
148
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
149
150
1
        const int rmask = 0xff000000;
151
1
        const int gmask = 0x00ff0000;
152
1
        const int bmask = 0x0000ff00;
153
1
        const int amask = 0x000000ff;
154
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
155
156
        SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_HWSURFACE,
157
1
            10, 10, 32, rmask, gmask, bmask, amask);
158
1
        uint32_t *ptr = reinterpret_cast<uint32_t*>(surface->pixels);
159
101
        for (int f = 0; f < 100; f ++)
160
        {
161
100
            ptr[f] = 300 * f;
162
        }
163
#ifdef USE_SDL2
164
        SDL_Surface *surface2 = SurfaceImageHelper::SDLDuplicateSurface(
165
            surface);
166
#else  // USE_SDL2
167
168
        SDL_Surface *surface2 = SDLImageHelper::SDLDuplicateSurface(
169
1
            surface);
170
#endif  // USE_SDL2
171
172
1
        uint32_t *ptr2 = reinterpret_cast<uint32_t*>(surface2->pixels);
173
101
        for (int f = 0; f < 100; f ++)
174
        {
175



400
            REQUIRE(ptr[f] == ptr2[f]);
176
        }
177
178
1
        SDL_FreeSurface(surface);
179
1
        SDL_FreeSurface(surface2);
180
    }
181
182



35
    SECTION("sdl SDL_ConvertSurface1")
183
    {
184
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
185
        const int rmask = 0xff000000;
186
        const int gmask = 0x00ff0000;
187
        const int bmask = 0x0000ff00;
188
        const int amask = 0x000000ff;
189
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
190
191
1
        const int rmask = 0x000000ff;
192
1
        const int gmask = 0x0000ff00;
193
1
        const int bmask = 0x00ff0000;
194
1
        const int amask = 0xff000000;
195
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
196
197
        SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_HWSURFACE,
198
1
            4, 4, 32, rmask, gmask, bmask, amask);
199
1
        uint32_t *ptr = reinterpret_cast<uint32_t*>(surface->pixels);
200
1
        ptr[0] = 0x11223344;
201
1
        ptr[1] = 0x22334455;
202
1
        ptr[2] = 0x33445566;
203
1
        ptr[3] = 0x44556677;
204
1
        ptr[4] = 0x55667788;
205
1
        ptr[5] = 0x66778899;
206
1
        ptr[6] = 0x778899aa;
207
1
        ptr[7] = 0x8899aabb;
208
1
        ptr[8] = 0x99aabbcc;
209
1
        ptr[9] = 0xaabbccdd;
210
1
        ptr[10] = 0xbbccddee;
211
1
        ptr[11] = 0xccddeeff;
212
1
        ptr[12] = 0xff000000;
213
1
        ptr[13] = 0x00ff0000;
214
1
        ptr[14] = 0x0000ff00;
215
1
        ptr[15] = 0x000000ff;
216
217
        SDL_PixelFormat rgba;
218
1
        rgba.palette = nullptr;
219
1
        rgba.BitsPerPixel = 32;
220
1
        rgba.BytesPerPixel = 4;
221
222
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
223
        rgba.Rmask = 0x000000FF;
224
        rgba.Gmask = 0x0000FF00;
225
        rgba.Bmask = 0x00FF0000;
226
        rgba.Amask = 0xFF000000;
227
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
228
229
1
        rgba.Rmask = 0xFF000000;
230
1
        rgba.Gmask = 0x00FF0000;
231
1
        rgba.Bmask = 0x0000FF00;
232
1
        rgba.Amask = 0x000000FF;
233
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
234
235
1
        SDL_Surface *const surface2 = MSDL_ConvertSurface(
236
            surface, &rgba, SDL_SWSURFACE);
237
238
1
        uint32_t *ptr2 = reinterpret_cast<uint32_t*>(surface2->pixels);
239



4
        REQUIRE(ptr2[0] == 0x44332211);
240



4
        REQUIRE(ptr2[1] == 0x55443322);
241



4
        REQUIRE(ptr2[2] == 0x66554433);
242



4
        REQUIRE(ptr2[3] == 0x77665544);
243



4
        REQUIRE(ptr2[4] == 0x88776655);
244



4
        REQUIRE(ptr2[5] == 0x99887766);
245



4
        REQUIRE(ptr2[6] == 0xaa998877);
246



4
        REQUIRE(ptr2[7] == 0xbbaa9988);
247



4
        REQUIRE(ptr2[8] == 0xccbbaa99);
248



4
        REQUIRE(ptr2[9] == 0xddccbbaa);
249



4
        REQUIRE(ptr2[10] == 0xeeddccbb);
250



4
        REQUIRE(ptr2[11] == 0xffeeddcc);
251



4
        REQUIRE(ptr2[12] == 0x000000ff);
252



4
        REQUIRE(ptr2[13] == 0x0000ff00);
253



4
        REQUIRE(ptr2[14] == 0x00ff0000);
254



4
        REQUIRE(ptr2[15] == 0xff000000);
255
256
1
        SDL_FreeSurface(surface);
257
1
        SDL_FreeSurface(surface2);
258
    }
259
260



35
    SECTION("sdl SDL_ConvertSurface2")
261
    {
262
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
263
        const int rmask = 0x000000ff;
264
        const int gmask = 0x0000ff00;
265
        const int bmask = 0x00ff0000;
266
        const int amask = 0xff000000;
267
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
268
269
1
        const int rmask = 0xff000000;
270
1
        const int gmask = 0x00ff0000;
271
1
        const int bmask = 0x0000ff00;
272
1
        const int amask = 0x000000ff;
273
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
274
275
        SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_HWSURFACE,
276
1
            4, 4, 32, rmask, gmask, bmask, amask);
277
1
        uint32_t *ptr = reinterpret_cast<uint32_t*>(surface->pixels);
278
1
        ptr[0] = 0x11223344;
279
1
        ptr[1] = 0x22334455;
280
1
        ptr[2] = 0x33445566;
281
1
        ptr[3] = 0x44556677;
282
1
        ptr[4] = 0x55667788;
283
1
        ptr[5] = 0x66778899;
284
1
        ptr[6] = 0x778899aa;
285
1
        ptr[7] = 0x8899aabb;
286
1
        ptr[8] = 0x99aabbcc;
287
1
        ptr[9] = 0xaabbccdd;
288
1
        ptr[10] = 0xbbccddee;
289
1
        ptr[11] = 0xccddeeff;
290
1
        ptr[12] = 0xff000000;
291
1
        ptr[13] = 0x00ff0000;
292
1
        ptr[14] = 0x0000ff00;
293
1
        ptr[15] = 0x000000ff;
294
295
        SDL_PixelFormat rgba;
296
1
        rgba.palette = nullptr;
297
1
        rgba.BitsPerPixel = 32;
298
1
        rgba.BytesPerPixel = 4;
299
300
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
301
        rgba.Rmask = 0xFF000000;
302
        rgba.Gmask = 0x00FF0000;
303
        rgba.Bmask = 0x0000FF00;
304
        rgba.Amask = 0x000000FF;
305
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
306
307
1
        rgba.Rmask = 0x000000FF;
308
1
        rgba.Gmask = 0x0000FF00;
309
1
        rgba.Bmask = 0x00FF0000;
310
1
        rgba.Amask = 0xFF000000;
311
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
312
313
1
        SDL_Surface *const surface2 = MSDL_ConvertSurface(
314
            surface, &rgba, SDL_SWSURFACE);
315
316
1
        uint32_t *ptr2 = reinterpret_cast<uint32_t*>(surface2->pixels);
317



4
        REQUIRE(ptr2[0] == 0x44332211);
318



4
        REQUIRE(ptr2[1] == 0x55443322);
319



4
        REQUIRE(ptr2[2] == 0x66554433);
320



4
        REQUIRE(ptr2[3] == 0x77665544);
321



4
        REQUIRE(ptr2[4] == 0x88776655);
322



4
        REQUIRE(ptr2[5] == 0x99887766);
323



4
        REQUIRE(ptr2[6] == 0xaa998877);
324



4
        REQUIRE(ptr2[7] == 0xbbaa9988);
325



4
        REQUIRE(ptr2[8] == 0xccbbaa99);
326



4
        REQUIRE(ptr2[9] == 0xddccbbaa);
327



4
        REQUIRE(ptr2[10] == 0xeeddccbb);
328



4
        REQUIRE(ptr2[11] == 0xffeeddcc);
329



4
        REQUIRE(ptr2[12] == 0x000000ff);
330



4
        REQUIRE(ptr2[13] == 0x0000ff00);
331



4
        REQUIRE(ptr2[14] == 0x00ff0000);
332



4
        REQUIRE(ptr2[15] == 0xff000000);
333
334
1
        SDL_FreeSurface(surface);
335
1
        SDL_FreeSurface(surface2);
336
    }
337
338



35
    SECTION("sdl SDL_ConvertSurface3")
339
    {
340
1
        const int rmask = 0x000000ff;
341
1
        const int gmask = 0x0000ff00;
342
1
        const int bmask = 0x00ff0000;
343
1
        const int amask = 0xff000000;
344
345
        SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_HWSURFACE,
346
1
            4, 4, 32, rmask, gmask, bmask, amask);
347
1
        uint32_t *ptr = reinterpret_cast<uint32_t*>(surface->pixels);
348
1
        ptr[0] = 0x11223344;
349
1
        ptr[1] = 0x22334455;
350
1
        ptr[2] = 0x33445566;
351
1
        ptr[3] = 0x44556677;
352
1
        ptr[4] = 0x55667788;
353
1
        ptr[5] = 0x66778899;
354
1
        ptr[6] = 0x778899aa;
355
1
        ptr[7] = 0x8899aabb;
356
1
        ptr[8] = 0x99aabbcc;
357
1
        ptr[9] = 0xaabbccdd;
358
1
        ptr[10] = 0xbbccddee;
359
1
        ptr[11] = 0xccddeeff;
360
1
        ptr[12] = 0xff000000;
361
1
        ptr[13] = 0x00ff0000;
362
1
        ptr[14] = 0x0000ff00;
363
1
        ptr[15] = 0x000000ff;
364
365
        SDL_PixelFormat rgba;
366
1
        rgba.palette = nullptr;
367
1
        rgba.BitsPerPixel = 32;
368
1
        rgba.BytesPerPixel = 4;
369
1
        rgba.Rmask = 0x000000FF;
370
1
        rgba.Gmask = 0x0000FF00;
371
1
        rgba.Bmask = 0x00FF0000;
372
1
        rgba.Amask = 0xFF000000;
373
374
1
        SDL_Surface *const surface2 = MSDL_ConvertSurface(
375
            surface, &rgba, SDL_SWSURFACE);
376
377
1
        uint32_t *ptr2 = reinterpret_cast<uint32_t*>(surface2->pixels);
378



4
        REQUIRE(ptr2[0] == 0x11223344);
379



4
        REQUIRE(ptr2[1] == 0x22334455);
380



4
        REQUIRE(ptr2[2] == 0x33445566);
381



4
        REQUIRE(ptr2[3] == 0x44556677);
382



4
        REQUIRE(ptr2[4] == 0x55667788);
383



4
        REQUIRE(ptr2[5] == 0x66778899);
384


3
        REQUIRE(ptr2[6] == 0x778899aa);
385



4
        REQUIRE(ptr2[7] == 0x8899aabb);
386



4
        REQUIRE(ptr2[8] == 0x99aabbcc);
387



4
        REQUIRE(ptr2[9] == 0xaabbccdd);
388



4
        REQUIRE(ptr2[10] == 0xbbccddee);
389



4
        REQUIRE(ptr2[11] == 0xccddeeff);
390



4
        REQUIRE(ptr2[12] == 0xff000000);
391



4
        REQUIRE(ptr2[13] == 0x00ff0000);
392



4
        REQUIRE(ptr2[14] == 0x0000ff00);
393



4
        REQUIRE(ptr2[15] == 0x000000ff);
394
395
1
        SDL_FreeSurface(surface);
396
1
        SDL_FreeSurface(surface2);
397
    }
398
399
5
    ResourceManager::cleanOrphans(true);
400
5
    delete2(userPalette)
401
5
    delete2(theme)
402
5
    delete2(client)
403
404

15
    VirtFs::unmountDirSilent("data");
405

20
    VirtFs::unmountDirSilent("../data");
406

8
}