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