GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/sdl2imagehelper.cpp Lines: 14 29 48.3 %
Date: 2021-03-17 Branches: 5 18 27.8 %

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
#ifdef USE_SDL2
25
26
#include "resources/sdl2imagehelper.h"
27
28
#include "resources/image/image.h"
29
30
#include "utils/checkutils.h"
31
#include "utils/sdlcheckutils.h"
32
33
#include "debug.h"
34
35
bool SDLImageHelper::mEnableAlphaCache = false;
36
SDL_Renderer *SDLImageHelper::mRenderer = nullptr;
37
38
1413
Image *SDLImageHelper::loadSurface(SDL_Surface *const tmpImage)
39
{
40
1413
    return _SDLload(tmpImage);
41
}
42
43
244
Image *SDLImageHelper::createTextSurface(SDL_Surface *const tmpImage,
44
                                         const int width A_UNUSED,
45
                                         const int height A_UNUSED,
46
                                         const float alpha)
47
{
48
244
    if (!tmpImage)
49
        return nullptr;
50
51
244
    Image *const img = _SDLload(tmpImage);
52
244
    if (img)
53
244
        img->setAlpha(alpha);
54
    return img;
55
}
56
57
SDL_Surface* SDLImageHelper::SDLDuplicateSurface(SDL_Surface *const tmpImage)
58
{
59
    if (!tmpImage || !tmpImage->format)
60
        return nullptr;
61
62
    return MSDL_ConvertSurface(tmpImage, tmpImage->format, SDL_SWSURFACE);
63
}
64
65
1657
Image *SDLImageHelper::_SDLload(SDL_Surface *tmpImage)
66
{
67
1657
    if (!tmpImage)
68
        return nullptr;
69
70
1657
    SDL_Texture *const texture = SDL_CreateTextureFromSurface(
71
1657
        mRenderer, tmpImage);
72
1657
    if (!texture)
73
    {
74
        reportAlways("Texture from surface creation failed: %s",
75
            SDL_GetError())
76
        return nullptr;
77
    }
78
1657
    SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
79
1657
    return new Image(texture, tmpImage->w, tmpImage->h);
80
}
81
82
int SDLImageHelper::combineSurface(SDL_Surface *restrict const src,
83
                                   SDL_Rect *restrict const srcrect,
84
                                   SDL_Surface *restrict const dst,
85
                                   SDL_Rect *restrict const dstrect)
86
{
87
    SDL_SetSurfaceBlendMode(src, SDL_BLENDMODE_BLEND);
88
    SDL_BlitSurface(src, srcrect, dst, dstrect);
89
    return 1;
90
}
91
92
void SDLImageHelper::copySurfaceToImage(const Image *const image,
93
                                        const int x, const int y,
94
                                        SDL_Surface *const surface) const
95
{
96
    if (!image || !surface)
97
        return;
98
99
    SDL_SetSurfaceAlphaMod(surface, SDL_ALPHA_OPAQUE);
100
    SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
101
102
    SDL_Rect rect =
103
    {
104
        x, y,
105
        surface->w, surface->h
106
    };
107
108
    SDL_BlitSurface(surface, nullptr, image->mSDLSurface, &rect);
109
}
110
#endif  // USE_SDL2