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

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

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

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

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

5
    userPalette = new UserPalette;
92

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

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



4
        REQUIRE(ptr2[6] == 0x778899aa);
384


3
        REQUIRE(ptr2[7] == 0x8899aabb);
385



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



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



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



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



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



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



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



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

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

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

8
}