The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
image.cpp
Go to the documentation of this file.
1 
2 /*
3  Copyright (C) 2014 - 2016 by Mark de Wever <[email protected]>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "sdl/image.hpp"
17 
18 #include <SDL_image.h>
19 #include "sdl/exception.hpp"
20 #include "sdl/rect.hpp"
21 #include "sdl/utils.hpp"
22 #include "video.hpp"
23 
24 #include <cassert>
25 
26 #ifdef SDL_GPU
27 #include "rect.hpp"
28 namespace sdl
29 {
30 timage::timage(Uint16 w, Uint16 h)
31  : image_(nullptr)
32  , rotation_(0)
33  , hscale_(1)
34  , vscale_(1)
35  , clip_()
36  , red_mod_(0)
37  , green_mod_(0)
38  , blue_mod_(0)
39  , alpha_mod_(0)
40  , hwrap_(GPU_WRAP_NONE)
41  , vwrap_(GPU_WRAP_NONE)
42  , effects_(0)
43 {
44  clip_ = create_gpu_rect(0, 0, w, h);
46  if (image_ == nullptr) {
47  throw tgpu_exception("Failed to construct timage object.", true);
48  } else {
49  image_->refcount = 1;
50  static SDL_Color black = {0, 0, 0, 0};
51  GPU_SetColor(image_, &black);
52  }
53 }
54 
55 timage::timage(const std::string &file)
56  : image_(GPU_LoadImage(file.c_str()))
57  , rotation_(0)
58  , hscale_(1)
59  , vscale_(1)
60  , clip_()
61  , red_mod_(0)
62  , green_mod_(0)
63  , blue_mod_(0)
64  , alpha_mod_(0)
65  , hwrap_(GPU_WRAP_NONE)
66  , vwrap_(GPU_WRAP_NONE)
67  , effects_(0)
68 {
69  if (image_ == nullptr) {
70  throw tgpu_exception("Failed to construct timage object.", true);
71  } else {
72  clip_ = create_gpu_rect(0, 0, image_->w, image_->h);
73  image_->refcount = 1;
74  static SDL_Color black = {0, 0, 0, 0};
75  GPU_SetColor(image_, &black);
76  }
77 }
78 
79 timage::timage(const surface &source)
81  , rotation_(0)
82  , hscale_(1)
83  , vscale_(1)
84  , clip_()
85  , red_mod_(0)
86  , green_mod_(0)
87  , blue_mod_(0)
88  , alpha_mod_(0)
89  , hwrap_(GPU_WRAP_NONE)
90  , vwrap_(GPU_WRAP_NONE)
91  , smooth_(false)
92  , submerge_(0)
93  , effects_(0)
94 {
95  if (image_ == nullptr) {
96  if (!source.nullptr()) {
97  throw tgpu_exception("Failed to construct timage object.", true);
98  }
99  } else {
100  clip_ = create_gpu_rect(0, 0, image_->w, image_->h);
101  image_->refcount = 1;
102  static SDL_Color black = {0, 0, 0, 0};
103  GPU_SetColor(image_, &black);
104  }
105 }
106 
107 timage::timage(SDL_Surface *source)
109  , rotation_(0)
110  , hscale_(1)
111  , vscale_(1)
112  , clip_()
113  , red_mod_(0)
114  , green_mod_(0)
115  , blue_mod_(0)
116  , alpha_mod_(0)
117  , hwrap_(GPU_WRAP_NONE)
118  , vwrap_(GPU_WRAP_NONE)
119  , smooth_(false)
120  , submerge_(0)
121  , effects_(0)
122 {
123  if (image_ == nullptr) {
124  if (source != nullptr) {
125  throw tgpu_exception("Failed to construct timage object.", true);
126  }
127  } else {
128  clip_ = create_gpu_rect(0, 0, image_->w, image_->h);
129  image_->refcount = 1;
130  static SDL_Color black = {0, 0, 0, 0};
131  GPU_SetColor(image_, &black);
132  }
133 }
134 
135 timage::timage()
136  : image_(nullptr)
137  , rotation_(0)
138  , hscale_(1)
139  , vscale_(1)
140  , clip_()
141  , red_mod_(0)
142  , green_mod_(0)
143  , blue_mod_(0)
144  , alpha_mod_(0)
145  , hwrap_(GPU_WRAP_NONE)
146  , vwrap_(GPU_WRAP_NONE)
147  , smooth_(false)
148  , submerge_(0)
149  , effects_(0)
150 {
151 }
152 
153 sdl::timage::~timage()
154 {
155  if (image_ != nullptr) {
156  image_->refcount -= 1;
157  if (image_->refcount == 0) {
159  }
160  }
161 }
162 
163 timage::timage(const timage &texture)
164  : image_(texture.image_)
165  , rotation_(texture.rotation_)
166  , hscale_(texture.hscale_)
167  , vscale_(texture.vscale_)
168  , clip_(texture.clip_)
169  , red_mod_(texture.red_mod_)
170  , green_mod_(texture.green_mod_)
171  , blue_mod_(texture.blue_mod_)
172  , alpha_mod_(texture.alpha_mod_)
173  , hwrap_(texture.hwrap_)
174  , vwrap_(texture.vwrap_)
175  , smooth_(texture.smooth_)
176  , submerge_(texture.submerge_)
177  , effects_(texture.effects_)
178 {
179  if (image_ != nullptr) {
180  image_->refcount += 1;
181  }
182 }
183 
184 timage &timage::operator =(const timage &texture)
185 {
186  if (&texture != this) {
187  this->~timage();
188  new (this) timage(texture);
189  }
190 
191  return *this;
192 }
193 
194 void timage::draw(CVideo &video, const int x, const int y)
195 {
197  GPU_SetWrapMode(image_, hwrap_, vwrap_);
198  video.set_texture_color_modulation(red_mod_, green_mod_, blue_mod_, alpha_mod_);
199  video.set_texture_submerge(float(submerge_));
200  video.set_texture_effects(effects_);
201  GPU_BlitTransform(image_, &clip_, video.render_target(), x + width()/2, y + height()/2,
202  rotation_, hscale_, vscale_);
203 }
204 
205 void timage::set_rotation(float rotation)
206 {
207  rotation_ = rotation;
208 }
209 
210 float timage::rotation() const
211 {
212  return rotation_;
213 }
214 
215 void timage::set_hscale(float factor)
216 {
217  hscale_ = factor;
218 }
219 
220 void timage::set_vscale(float factor)
221 {
222  vscale_ = factor;
223 }
224 
225 void timage::set_scale(float hfactor, float vfactor)
226 {
227  hscale_ = hfactor;
228  vscale_ = vfactor;
229 }
230 
231 float timage::hscale() const
232 {
233  return hscale_;
234 }
235 
236 float timage::vscale() const
237 {
238  return vscale_;
239 }
240 
241 void timage::set_smooth_scaling(bool use_smooth)
242 {
243  smooth_ = use_smooth;
244 }
245 
246 bool timage::smooth_scaling() const
247 {
248  return smooth_;
249 }
250 
251 int timage::width() const
252 {
253  return clip_.w * hscale_;
254 }
255 
256 int timage::height() const
257 {
258  return clip_.h * vscale_;
259 }
260 
261 Uint16 timage::base_width() const
262 {
263  return image_->w;
264 }
265 
266 Uint16 timage::base_height() const
267 {
268  return image_->h;
269 }
270 
271 void timage::set_clip(const SDL_Rect &rect)
272 {
273  clip_.x = rect.x;
274  clip_.y = rect.y;
275  clip_.w = rect.w;
276  clip_.h = rect.h;
277 }
278 
279 SDL_Rect timage::clip() const
280 {
281  SDL_Rect result;
282  result.x = clip_.x;
283  result.y = clip_.y;
284  result.w = clip_.w;
285  result.h = clip_.h;
286 
287  return result;
288 }
289 
290 void timage::set_alpha(int alpha)
291 {
292  alpha_mod_ = alpha;
293 }
294 
295 int timage::alpha() const
296 {
297  return alpha_mod_;
298 }
299 
300 void timage::set_color_mod(int r, int g, int b)
301 {
302  red_mod_ = r;
303  green_mod_ = g;
304  blue_mod_ = b;
305 }
306 
307 int timage::red_mod() const
308 {
309  return red_mod_;
310 }
311 
312 int timage::green_mod() const
313 {
314  return green_mod_;
315 }
316 
317 int timage::blue_mod() const
318 {
319  return blue_mod_;
320 }
321 
322 void timage::set_hwrap(GPU_WrapEnum mode)
323 {
324  hwrap_ = mode;
325 }
326 
327 void timage::set_vwrap(GPU_WrapEnum mode)
328 {
329  vwrap_ = mode;
330 }
331 
332 void timage::set_wrap(GPU_WrapEnum hmode, GPU_WrapEnum vmode)
333 {
334  hwrap_ = hmode;
335  vwrap_ = vmode;
336 }
337 
338 GPU_WrapEnum timage::hwrap() const
339 {
340  return hwrap_;
341 }
342 
343 GPU_WrapEnum timage::vwrap() const
344 {
345  return vwrap_;
346 }
347 
348 void timage::set_submerge(double val)
349 {
350  submerge_ = val;
351 }
352 
353 double timage::submerge() const
354 {
355  return submerge_;
356 }
357 
358 void timage::set_effects(int effects)
359 {
360  effects_ = effects;
361 }
362 
363 int timage::effects() const
364 {
365  return effects_;
366 }
367 
368 bool timage::nullptr() const
369 {
370  return image_ == nullptr;
371 }
372 
373 timage timage::clone() const
374 {
375  timage res;
376  res.image_ = GPU_CopyImage(image_);
377  res.set_alpha(alpha());
378  res.set_clip(clip());
379  res.set_color_mod(red_mod(), green_mod(), blue_mod());
380  res.set_wrap(hwrap(), vwrap());
381  res.set_rotation(rotation());
382  res.set_scale(hscale(), vscale());
383  res.set_smooth_scaling(smooth_scaling());
384  res.set_submerge(submerge());
385  res.set_effects(effects());
386 
387  return res;
388 }
389 
390 GPU_Image *timage::raw() const
391 {
392  return image_;
393 }
394 
395 }
396 
397 #endif
GPU_Image * GPU_CopyImageFromSurface(SDL_Surface *surface)
Definition: SDL_gpu.c:841
Definition: video.hpp:58
void GPU_SetColor(GPU_Image *image, SDL_Color *color)
Definition: SDL_gpu.c:1844
surface image_
The image is cached in this surface.
Definition: canvas.cpp:961
GLuint const GLfloat * val
Definition: glew.h:2614
GLenum GLenum GLuint texture
Definition: glew.h:3453
void GPU_BlitTransform(GPU_Image *image, GPU_Rect *src_rect, GPU_Target *target, float x, float y, float angle, float scaleX, float scaleY)
Definition: SDL_gpu.c:963
GLboolean GLboolean g
Definition: glew.h:7319
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
GLenum mode
Definition: glew.h:2390
-file util.hpp
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
GLuint64EXT * result
Definition: glew.h:10727
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
GPU_Image * GPU_LoadImage(const char *filename)
Definition: SDL_gpu.c:660
GLclampf GLclampf GLclampf alpha
Definition: glew.h:1488
void draw(surface screen)
GPU_Image * GPU_CopyImage(GPU_Image *image)
Definition: SDL_gpu.c:684
GLuint res
Definition: glew.h:9258
void GPU_SetImageFilter(GPU_Image *image, GPU_FilterEnum filter)
Definition: SDL_gpu.c:2094
Contains a wrapper class for the GPU_Image class.
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
GPU_Image * GPU_CreateImage(Uint16 w, Uint16 h, GPU_FormatEnum format)
Definition: SDL_gpu.c:652
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
void GPU_SetWrapMode(GPU_Image *image, GPU_WrapEnum wrap_mode_x, GPU_WrapEnum wrap_mode_y)
Definition: SDL_gpu.c:2120
Contains a basic exception class for SDL operations.
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1220
Contains the SDL_Rect helper code.
void GPU_FreeImage(GPU_Image *image)
Definition: SDL_gpu.c:873
#define g
Definition: glew.h:12730
GPU_WrapEnum
Definition: SDL_gpu.h:143
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
GLsizei const GLcharARB ** string
Definition: glew.h:4503
GLsizei GLsizei GLchar * source
Definition: glew.h:1800