ManaPlus
sdl2imagehelper.cpp
Go to the documentation of this file.
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 
27 
28 #include "resources/image/image.h"
29 
30 #include "utils/checkutils.h"
31 #include "utils/sdlcheckutils.h"
32 
33 #include "debug.h"
34 
36 SDL_Renderer *SDLImageHelper::mRenderer = nullptr;
37 
38 Image *SDLImageHelper::loadSurface(SDL_Surface *const tmpImage)
39 {
40  return _SDLload(tmpImage);
41 }
42 
43 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  if (!tmpImage)
49  return nullptr;
50 
51  Image *const img = _SDLload(tmpImage);
52  if (img)
53  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 Image *SDLImageHelper::_SDLload(SDL_Surface *tmpImage)
66 {
67  if (!tmpImage)
68  return nullptr;
69 
70  SDL_Texture *const texture = SDL_CreateTextureFromSurface(
71  mRenderer, tmpImage);
72  if (!texture)
73  {
74  reportAlways("Texture from surface creation failed: %s",
75  SDL_GetError())
76  return nullptr;
77  }
78  SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
79  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
#define reportAlways(...)
Definition: checkutils.h:253
static Image * _SDLload(SDL_Surface *tmpImage)
static int combineSurface(SDL_Surface *const src, SDL_Rect *const srcrect, SDL_Surface *const dst, SDL_Rect *const dstrect)
static SDL_Surface * SDLDuplicateSurface(SDL_Surface *const tmpImage)
void copySurfaceToImage(const Image *const image, const int x, const int y, SDL_Surface *const surface) const
Image * createTextSurface(SDL_Surface *const tmpImage, const int width, const int height, const float alpha)
static bool mEnableAlphaCache
Image * loadSurface(SDL_Surface *const tmpImage)
#define MSDL_ConvertSurface(src, fmt, flags)
Definition: debug.h:57
#define new
Definition: debug_new.h:147
#define restrict
Definition: localconsts.h:165
#define A_UNUSED
Definition: localconsts.h:160