ManaPlus
surfacegraphics.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 #include "render/surfacegraphics.h"
25 
26 #ifndef USE_SDL2
28 #endif // USE_SDL2
29 
30 #include "resources/image/image.h"
31 
32 #include "debug.h"
33 
35  Graphics(),
36  mBlitMode(BlitMode::BLIT_NORMAL),
37  mTarget(nullptr)
38 {
40  mName = "Surface";
41 }
42 
44 {
45 }
46 
47 void SurfaceGraphics::drawImage(const Image *restrict const image,
48  int dstX, int dstY) restrict2
49 {
50  FUNC_BLOCK("Graphics::drawImage", 1)
51  // Check that preconditions for blitting are met.
52  if (mTarget == nullptr ||
53  image == nullptr ||
54  image->mSDLSurface == nullptr)
55  {
56  return;
57  }
58 
59  const SDL_Rect &imageRect = image->mBounds;
60  SDL_Rect dstRect;
61  SDL_Rect srcRect;
62  dstRect.x = CAST_S16(dstX);
63  dstRect.y = CAST_S16(dstY);
64  srcRect.x = CAST_S16(imageRect.x);
65  srcRect.y = CAST_S16(imageRect.y);
66  srcRect.w = CAST_U16(imageRect.w);
67  srcRect.h = CAST_U16(imageRect.h);
68 
69 #ifdef USE_SDL2
70  SDL_BlitSurface(image->mSDLSurface, &srcRect, mTarget, &dstRect);
71 #else // USE_SDL2
72 
73  if (mBlitMode == BlitMode::BLIT_NORMAL)
74  {
75  SDL_BlitSurface(image->mSDLSurface, &srcRect, mTarget, &dstRect);
76  }
77  else
78  {
79  SurfaceImageHelper::combineSurface(image->mSDLSurface,
80  &srcRect, mTarget, &dstRect);
81  }
82 #endif // USE_SDL2
83 }
84 
85 void SurfaceGraphics::copyImage(const Image *restrict const image,
86  int dstX, int dstY) restrict2
87 {
88  FUNC_BLOCK("Graphics::drawImage", 1)
89  // Check that preconditions for blitting are met.
90  if (mTarget == nullptr ||
91  image == nullptr ||
92  image->mSDLSurface == nullptr)
93  {
94  return;
95  }
96 
97  const SDL_Rect &imageRect = image->mBounds;
98  SDL_Rect dstRect;
99  SDL_Rect srcRect;
100  dstRect.x = CAST_S16(dstX);
101  dstRect.y = CAST_S16(dstY);
102  srcRect.x = CAST_S16(imageRect.x);
103  srcRect.y = CAST_S16(imageRect.y);
104  srcRect.w = CAST_U16(imageRect.w);
105  srcRect.h = CAST_U16(imageRect.h);
106 
107 #ifdef USE_SDL2
108  // probably need change some flags
109  SDL_BlitSurface(image->mSDLSurface, &srcRect, mTarget, &dstRect);
110 #else // USE_SDL2
111 
112  SDL_BlitSurface(image->mSDLSurface, &srcRect, mTarget, &dstRect);
113 #endif // USE_SDL2
114 }
115 
117  int x, int y) restrict2
118 {
119  FUNC_BLOCK("Graphics::drawImageCached", 1)
120  // Check that preconditions for blitting are met.
121  if (mTarget == nullptr ||
122  image == nullptr ||
123  image->mSDLSurface == nullptr)
124  {
125  return;
126  }
127 
128  const SDL_Rect &rect = image->mBounds;
129 
130  SDL_Rect dstRect;
131  SDL_Rect srcRect;
132  dstRect.x = CAST_S16(x);
133  dstRect.y = CAST_S16(y);
134  srcRect.x = CAST_S16(rect.x);
135  srcRect.y = CAST_S16(rect.y);
136  srcRect.w = CAST_U16(rect.w);
137  srcRect.h = CAST_U16(rect.h);
138 
139 #ifdef USE_SDL2
140  SDL_BlitSurface(image->mSDLSurface, &srcRect, mTarget, &dstRect);
141 #else // USE_SDL2
142 
143  if (mBlitMode == BlitMode::BLIT_NORMAL)
144  {
145  SDL_BlitSurface(image->mSDLSurface, &srcRect, mTarget, &dstRect);
146  }
147  else
148  {
149  SurfaceImageHelper::combineSurface(image->mSDLSurface, &srcRect,
150  mTarget, &dstRect);
151  }
152 #endif // USE_SDL2
153 }
154 
156 {
157 }
#define CAST_U16
Definition: cast.h:29
#define CAST_S16
Definition: cast.h:28
RenderType mOpenGL
Definition: graphics.h:545
std::string mName
Definition: graphics.h:549
void copyImage(const Image *const image, int dstX, int dstY)
void drawImageCached(const Image *const image, int x, int y)
void drawImage(const Image *const image, int dstX, int dstY)
#define restrict
Definition: localconsts.h:165
#define restrict2
Definition: localconsts.h:166
#define nullptr
Definition: localconsts.h:45
@ BLIT_NORMAL
Definition: blitmode.h:31
#define FUNC_BLOCK(name, id)
Definition: perfomance.h:81
@ RENDER_SOFTWARE
Definition: rendertype.h:27