-
-
Notifications
You must be signed in to change notification settings - Fork 992
/
texture.cpp
306 lines (262 loc) · 5.36 KB
/
texture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
Copyright (C) 2014 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "sdl/texture.hpp"
#include "SDL_image.h"
#include "sdl/exception.hpp"
#include "sdl/rect.hpp"
#include "sdl/utils.hpp"
#include <cassert>
#ifdef SDL_GPU
#include "rect.hpp"
namespace sdl
{
ttexture::ttexture(Uint16 w, Uint16 h)
: image_(NULL)
, rotation_(0)
, hscale_(1)
, vscale_(1)
, smooth_scaling_(false)
, clip_(create_gpu_rect(0, 0, w, h))
, color_mod_(create_color(0, 0, 0, 255))
, hwrap_(GPU_WRAP_NONE)
, vwrap_(GPU_WRAP_NONE)
{
image_ = GPU_CreateImage(w, h, GPU_FORMAT_RGBA);
if (image_ == NULL) {
//TODO: report errorr
} else {
image_->refcount = 1;
}
}
ttexture::ttexture(const std::string &file)
: image_(GPU_LoadImage(file.c_str()))
, rotation_(0)
, hscale_(1)
, vscale_(1)
, smooth_scaling_(false)
, clip_()
, color_mod_(create_color(0, 0, 0, 255))
, hwrap_(GPU_WRAP_NONE)
, vwrap_(GPU_WRAP_NONE)
{
if (image_ == NULL) {
//TODO: report error
} else {
clip_ = create_gpu_rect(0, 0, image_->w, image_->h);
image_->refcount = 1;
}
}
ttexture::ttexture(const surface &source)
: image_(GPU_CopyImageFromSurface(source))
, rotation_(0)
, hscale_(1)
, vscale_(1)
, smooth_scaling_(false)
, clip_()
, color_mod_(create_color(0, 0, 0, 255))
, hwrap_(GPU_WRAP_NONE)
, vwrap_(GPU_WRAP_NONE)
{
if (image_ == NULL) {
//TODO: report error
} else {
clip_ = create_gpu_rect(0, 0, image_->w, image_->h);
image_->refcount = 1;
}
}
ttexture::ttexture(SDL_Surface *source)
: image_(GPU_CopyImageFromSurface(source))
, rotation_(0)
, hscale_(1)
, vscale_(1)
, smooth_scaling_(false)
, clip_()
, color_mod_(create_color(0, 0, 0, 255))
, hwrap_(GPU_WRAP_NONE)
, vwrap_(GPU_WRAP_NONE)
{
if (image_ == NULL) {
//TODO: report error
} else {
clip_ = create_gpu_rect(0, 0, image_->w, image_->h);
image_->refcount = 1;
}
}
ttexture::ttexture()
: image_(NULL)
, rotation_(0)
, hscale_(1)
, vscale_(1)
, smooth_scaling_(false)
, clip_(create_gpu_rect(0, 0, 0, 0))
, color_mod_(create_color(0, 0, 0, 255))
, hwrap_(GPU_WRAP_NONE)
, vwrap_(GPU_WRAP_NONE)
{
}
sdl::ttexture::~ttexture()
{
if (image_ != NULL) {
image_->refcount -= 1;
if (image_->refcount == 0) {
GPU_FreeImage(image_);
}
}
}
ttexture::ttexture(const ttexture &texture)
: image_(texture.image_)
, rotation_(texture.rotation_)
, hscale_(texture.hscale_)
, vscale_(texture.vscale_)
, smooth_scaling_(texture.smooth_scaling_)
, clip_(texture.clip_)
, color_mod_(texture.color_mod_)
, hwrap_(texture.hwrap_)
, vwrap_(texture.vwrap_)
{
if (image_ != NULL) {
image_->refcount += 1;
}
}
ttexture &ttexture::operator =(const ttexture &texture)
{
if (&texture != this) {
this->~ttexture();
new (this) ttexture(texture);
}
return *this;
}
void ttexture::draw(GPU_Target &target, const int x, const int y)
{
GPU_SetImageFilter(image_,
smooth_scaling_
? GPU_FILTER_LINEAR
: GPU_FILTER_NEAREST);
//GPU_SetColor(image_, &color_mod_);
GPU_SetWrapMode(image_, hwrap_, vwrap_);
GPU_BlitTransform(image_, &clip_, &target, x + width()/2,
y + height()/2, rotation_, hscale_, vscale_);
}
void ttexture::set_rotation(float rotation)
{
rotation_ = rotation;
}
float ttexture::rotation() const
{
return rotation_;
}
void ttexture::set_hscale(float factor)
{
hscale_ = factor;
}
void ttexture::set_vscale(float factor)
{
vscale_ = factor;
}
void ttexture::set_scale(float hfactor, float vfactor)
{
hscale_ = hfactor;
vscale_ = vfactor;
}
float ttexture::hscale() const
{
return hscale_;
}
float ttexture::vscale() const
{
return vscale_;
}
void ttexture::set_smooth_scaling(bool use_smooth)
{
smooth_scaling_ = use_smooth;
}
bool ttexture::smooth_scaling() const
{
return smooth_scaling_;
}
Uint16 ttexture::width() const
{
return image_->w * hscale_;
}
Uint16 ttexture::height() const
{
return image_->h * vscale_;
}
void ttexture::set_clip(const SDL_Rect &rect)
{
clip_.x = rect.x;
clip_.y = rect.y;
clip_.w = rect.w;
clip_.h = rect.h;
}
SDL_Rect ttexture::clip() const
{
SDL_Rect result;
result.x = clip_.x;
result.y = clip_.y;
result.w = clip_.w;
result.h = clip_.h;
return result;
}
void ttexture::set_alpha(Uint8 alpha)
{
color_mod_.unused = alpha;
}
Uint8 ttexture::alpha() const
{
return color_mod_.unused;
}
void ttexture::set_color_mod(Uint8 r, Uint8 g, Uint8 b)
{
color_mod_.r = r;
color_mod_.g = g;
color_mod_.b = b;
}
Uint8 ttexture::red_mod() const
{
return color_mod_.r;
}
Uint8 ttexture::green_mod() const
{
return color_mod_.g;
}
Uint8 ttexture::blue_mod() const
{
return color_mod_.b;
}
void ttexture::set_hwrap(GPU_WrapEnum mode)
{
hwrap_ = mode;
}
void ttexture::set_vwrap(GPU_WrapEnum mode)
{
vwrap_ = mode;
}
void ttexture::set_wrap(GPU_WrapEnum hmode, GPU_WrapEnum vmode)
{
hwrap_ = hmode;
vwrap_ = vmode;
}
GPU_WrapEnum ttexture::hwrap() const
{
return hwrap_;
}
GPU_WrapEnum ttexture::vwrap() const
{
return vwrap_;
}
bool ttexture::null() const
{
return image_ == NULL;
}
}
#endif