GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/sdl.cc Lines: 209 209 100.0 %
Date: 2017-11-29 Branches: 347 874 39.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2017  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
14
TEST_CASE("sdl tests", "sdl")
58
{
59
10
    setEnv("SDL_VIDEODRIVER", "dummy");
60
61
10
    client = new Client;
62
10
    XML::initXML();
63
10
    SDL_Init(SDL_INIT_VIDEO);
64
40
    std::string name("data/test/test.zip");
65
20
    std::string prefix;
66

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

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

40
    VirtFs::mountDirSilent("../data", Append_false);
71
72
10
    Dirs::initRootDir();
73
10
    Dirs::initHomeDir();
74
75
10
    setBrandingDefaults(branding);
76
10
    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
20
    imageHelper = new SDLImageHelper();
87
88
10
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
89
#endif  // USE_SDL2
90
91

10
    userPalette = new UserPalette;
92

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

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



70
    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
2
        const int rmask = 0x000000ff;
109
2
        const int gmask = 0x0000ff00;
110
2
        const int bmask = 0x00ff0000;
111
2
        const int amask = 0xff000000;
112
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
113
114
        SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_HWSURFACE,
115
2
            10, 10, 32, rmask, gmask, bmask, amask);
116
2
        uint32_t *ptr = reinterpret_cast<uint32_t*>(surface->pixels);
117
202
        for (int f = 0; f < 100; f ++)
118
        {
119
200
            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
2
            surface);
128
#endif  // USE_SDL2
129
130
2
        uint32_t *ptr2 = reinterpret_cast<uint32_t*>(surface2->pixels);
131
202
        for (int f = 0; f < 100; f ++)
132
        {
133



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



70
    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
2
        const int rmask = 0xff000000;
150
2
        const int gmask = 0x00ff0000;
151
2
        const int bmask = 0x0000ff00;
152
2
        const int amask = 0x000000ff;
153
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
154
155
        SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_HWSURFACE,
156
2
            10, 10, 32, rmask, gmask, bmask, amask);
157
2
        uint32_t *ptr = reinterpret_cast<uint32_t*>(surface->pixels);
158
202
        for (int f = 0; f < 100; f ++)
159
        {
160
200
            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
2
            surface);
169
#endif  // USE_SDL2
170
171
2
        uint32_t *ptr2 = reinterpret_cast<uint32_t*>(surface2->pixels);
172
202
        for (int f = 0; f < 100; f ++)
173
        {
174



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



70
    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
2
        const int rmask = 0x000000ff;
191
2
        const int gmask = 0x0000ff00;
192
2
        const int bmask = 0x00ff0000;
193
2
        const int amask = 0xff000000;
194
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
195
196
        SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_HWSURFACE,
197
2
            4, 4, 32, rmask, gmask, bmask, amask);
198
2
        uint32_t *ptr = reinterpret_cast<uint32_t*>(surface->pixels);
199
2
        ptr[0] = 0x11223344;
200
2
        ptr[1] = 0x22334455;
201
2
        ptr[2] = 0x33445566;
202
2
        ptr[3] = 0x44556677;
203
2
        ptr[4] = 0x55667788;
204
2
        ptr[5] = 0x66778899;
205
2
        ptr[6] = 0x778899aa;
206
2
        ptr[7] = 0x8899aabb;
207
2
        ptr[8] = 0x99aabbcc;
208
2
        ptr[9] = 0xaabbccdd;
209
2
        ptr[10] = 0xbbccddee;
210
2
        ptr[11] = 0xccddeeff;
211
2
        ptr[12] = 0xff000000;
212
2
        ptr[13] = 0x00ff0000;
213
2
        ptr[14] = 0x0000ff00;
214
2
        ptr[15] = 0x000000ff;
215
216
        SDL_PixelFormat rgba;
217
2
        rgba.palette = nullptr;
218
2
        rgba.BitsPerPixel = 32;
219
2
        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
2
        rgba.Rmask = 0xFF000000;
229
2
        rgba.Gmask = 0x00FF0000;
230
2
        rgba.Bmask = 0x0000FF00;
231
2
        rgba.Amask = 0x000000FF;
232
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
233
234
2
        SDL_Surface *const surface2 = MSDL_ConvertSurface(
235
            surface, &rgba, SDL_SWSURFACE);
236
237
2
        uint32_t *ptr2 = reinterpret_cast<uint32_t*>(surface2->pixels);
238



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



70
    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
2
        const int rmask = 0xff000000;
269
2
        const int gmask = 0x00ff0000;
270
2
        const int bmask = 0x0000ff00;
271
2
        const int amask = 0x000000ff;
272
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
273
274
        SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_HWSURFACE,
275
2
            4, 4, 32, rmask, gmask, bmask, amask);
276
2
        uint32_t *ptr = reinterpret_cast<uint32_t*>(surface->pixels);
277
2
        ptr[0] = 0x11223344;
278
2
        ptr[1] = 0x22334455;
279
2
        ptr[2] = 0x33445566;
280
2
        ptr[3] = 0x44556677;
281
2
        ptr[4] = 0x55667788;
282
2
        ptr[5] = 0x66778899;
283
2
        ptr[6] = 0x778899aa;
284
2
        ptr[7] = 0x8899aabb;
285
2
        ptr[8] = 0x99aabbcc;
286
2
        ptr[9] = 0xaabbccdd;
287
2
        ptr[10] = 0xbbccddee;
288
2
        ptr[11] = 0xccddeeff;
289
2
        ptr[12] = 0xff000000;
290
2
        ptr[13] = 0x00ff0000;
291
2
        ptr[14] = 0x0000ff00;
292
2
        ptr[15] = 0x000000ff;
293
294
        SDL_PixelFormat rgba;
295
2
        rgba.palette = nullptr;
296
2
        rgba.BitsPerPixel = 32;
297
2
        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
2
        rgba.Rmask = 0x000000FF;
307
2
        rgba.Gmask = 0x0000FF00;
308
2
        rgba.Bmask = 0x00FF0000;
309
2
        rgba.Amask = 0xFF000000;
310
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
311
312
2
        SDL_Surface *const surface2 = MSDL_ConvertSurface(
313
            surface, &rgba, SDL_SWSURFACE);
314
315
2
        uint32_t *ptr2 = reinterpret_cast<uint32_t*>(surface2->pixels);
316



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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

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

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

16
}