forked from libSDL2pp/libSDL2pp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Renderer.cc
450 lines (368 loc) · 12.4 KB
/
Renderer.cc
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
libSDL2pp - C++ bindings/wrapper for SDL2
Copyright (C) 2013-2016 Dmitry Marakasov <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <vector>
#include <cassert>
#include <SDL.h>
#include <SDL2pp/Renderer.hh>
#include <SDL2pp/Window.hh>
#include <SDL2pp/Exception.hh>
#include <SDL2pp/Texture.hh>
namespace SDL2pp {
Renderer::Renderer(SDL_Renderer* renderer) : renderer_(renderer) {
assert(renderer);
}
Renderer::Renderer(Window& window, int index, Uint32 flags) {
if ((renderer_ = SDL_CreateRenderer(window.Get(), index, flags)) == nullptr)
throw Exception("SDL_CreateRenderer");
}
Renderer::~Renderer() {
if (renderer_ != nullptr)
SDL_DestroyRenderer(renderer_);
}
Renderer::Renderer(Renderer&& other) noexcept : renderer_(other.renderer_) {
other.renderer_ = nullptr;
}
Renderer& Renderer::operator=(Renderer&& other) noexcept {
if (&other == this)
return *this;
if (renderer_ != nullptr)
SDL_DestroyRenderer(renderer_);
renderer_ = other.renderer_;
other.renderer_ = nullptr;
return *this;
}
SDL_Renderer* Renderer::Get() const {
return renderer_;
}
Renderer& Renderer::Present() {
SDL_RenderPresent(renderer_);
return *this;
}
Renderer& Renderer::Clear() {
if (SDL_RenderClear(renderer_) != 0)
throw Exception("SDL_RenderClear");
return *this;
}
void Renderer::GetInfo(SDL_RendererInfo& info) {
if (SDL_GetRendererInfo(renderer_, &info) != 0)
throw Exception("SDL_GetRendererInfo");
}
Renderer& Renderer::Copy(Texture& texture, const Optional<Rect>& srcrect, const Optional<Rect>& dstrect) {
if (SDL_RenderCopy(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr) != 0)
throw Exception("SDL_RenderCopy");
return *this;
}
Renderer& Renderer::Copy(Texture& texture, const Optional<Rect>& srcrect, const Point& dstpoint) {
Rect dstrect(
dstpoint.x,
dstpoint.y,
srcrect ? srcrect->w : texture.GetWidth(),
srcrect ? srcrect->h : texture.GetHeight()
);
return Copy(texture, srcrect, dstrect);
}
Renderer& Renderer::Copy(Texture& texture, const Optional<Rect>& srcrect, const Optional<Rect>& dstrect, double angle, const Optional<Point>& center, int flip) {
if (SDL_RenderCopyEx(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr, angle, center ? &*center : nullptr, static_cast<SDL_RendererFlip>(flip)) != 0)
throw Exception("SDL_RenderCopyEx");
return *this;
}
Renderer& Renderer::Copy(Texture& texture, const Optional<Rect>& srcrect, const Point& dstpoint, double angle, const Optional<Point>& center, int flip) {
Rect dstrect(
dstpoint.x,
dstpoint.y,
srcrect ? srcrect->w : texture.GetWidth(),
srcrect ? srcrect->h : texture.GetHeight()
);
return Copy(texture, srcrect, dstrect, angle, center, flip);
}
Renderer& Renderer::FillCopy(Texture& texture, const Optional<Rect>& srcrect, const Optional<Rect>& dstrect, const Point& offset, int flip) {
// resolve rectangles
Rect src = srcrect ? *srcrect : Rect(0, 0, texture.GetWidth(), texture.GetHeight());
Rect dst = dstrect ? *dstrect : Rect(0, 0, GetOutputWidth(), GetOutputHeight());
// rectangle for single tile
Rect start_tile(
offset.x,
offset.y,
src.w,
src.h
);
// ensure tile is leftmost and topmost
if (start_tile.x + start_tile.w <= 0)
start_tile.x += (-start_tile.x) / start_tile.w * start_tile.w;
if (start_tile.x > 0)
start_tile.x -= (start_tile.x + start_tile.w - 1) / start_tile.w * start_tile.w;
if (start_tile.y + start_tile.h <= 0)
start_tile.y += (-start_tile.y) / start_tile.h * start_tile.h;
if (start_tile.y > 0)
start_tile.y -= (start_tile.y + start_tile.h - 1) / start_tile.h * start_tile.h;
// paint tile array
for (int y = start_tile.y; y < dst.h; y += start_tile.h) {
for (int x = start_tile.x; x < dst.w; x += start_tile.w) {
Rect tile_src = src;
Rect tile_dst(x, y, start_tile.w, start_tile.h);
// clamp with dstrect
int xunderflow = -x;
if (xunderflow > 0) {
tile_src.w -= xunderflow;
tile_src.x += xunderflow;
tile_dst.w -= xunderflow;
tile_dst.x += xunderflow;
}
int yunderflow = -y;
if (yunderflow > 0) {
tile_src.h -= yunderflow;
tile_src.y += yunderflow;
tile_dst.h -= yunderflow;
tile_dst.y += yunderflow;
}
int xoverflow = tile_dst.x + tile_dst.w - dst.w;
if (xoverflow > 0) {
tile_src.w -= xoverflow;
tile_dst.w -= xoverflow;
}
int yoverflow = tile_dst.y + tile_dst.h - dst.h;
if (yoverflow > 0) {
tile_src.h -= yoverflow;
tile_dst.h -= yoverflow;
}
// make tile_dst absolute
tile_dst.x += dst.x;
tile_dst.y += dst.y;
if (flip != 0) {
// mirror tile_src inside src to take flipping into account
if (flip & SDL_FLIP_HORIZONTAL)
tile_src.x = src.w - tile_src.x - tile_src.w;
if (flip & SDL_FLIP_VERTICAL)
tile_src.y = src.h - tile_src.y - tile_src.h;
Copy(texture, tile_src, tile_dst, 0.0, NullOpt, flip);
} else {
Copy(texture, tile_src, tile_dst);
}
}
}
return *this;
}
Renderer& Renderer::SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
if (SDL_SetRenderDrawColor(renderer_, r, g, b, a) != 0)
throw Exception("SDL_SetRenderDrawColor");
return *this;
}
Renderer& Renderer::SetDrawColor(const Color& color) {
return SetDrawColor(color.r, color.g, color.b, color.a);
}
Renderer& Renderer::SetTarget() {
if (SDL_SetRenderTarget(renderer_, nullptr) != 0)
throw Exception("SDL_SetRenderTarget");
return *this;
}
Renderer& Renderer::SetTarget(Texture& texture) {
if (SDL_SetRenderTarget(renderer_, texture.Get()) != 0)
throw Exception("SDL_SetRenderTarget");
return *this;
}
Renderer& Renderer::SetDrawBlendMode(SDL_BlendMode blendMode) {
if (SDL_SetRenderDrawBlendMode(renderer_, blendMode) != 0)
throw Exception("SDL_SetRenderDrawBlendMode");
return *this;
}
Renderer& Renderer::DrawPoint(int x, int y) {
if (SDL_RenderDrawPoint(renderer_, x, y) != 0)
throw Exception("SDL_RenderDrawPoint");
return *this;
}
Renderer& Renderer::DrawPoint(const Point& p) {
DrawPoint(p.x, p.y);
return *this;
}
Renderer& Renderer::DrawPoints(const Point* points, int count) {
std::vector<SDL_Point> sdl_points;
sdl_points.reserve(static_cast<size_t>(count));
for (const Point* p = points; p != points + count; ++p)
sdl_points.emplace_back(*p);
if (SDL_RenderDrawPoints(renderer_, sdl_points.data(), count) != 0)
throw Exception("SDL_RenderDrawPoints");
return *this;
}
Renderer& Renderer::DrawLine(int x1, int y1, int x2, int y2) {
if (SDL_RenderDrawLine(renderer_, x1, y1, x2, y2) != 0)
throw Exception("SDL_RenderDrawLine");
return *this;
}
Renderer& Renderer::DrawLine(const Point& p1, const Point& p2) {
DrawLine(p1.x, p1.y, p2.x, p2.y);
return *this;
}
Renderer& Renderer::DrawLines(const Point* points, int count) {
std::vector<SDL_Point> sdl_points;
sdl_points.reserve(static_cast<size_t>(count));
for (const Point* p = points; p != points + count; ++p)
sdl_points.emplace_back(*p);
if (SDL_RenderDrawLines(renderer_, sdl_points.data(), count) != 0)
throw Exception("SDL_RenderDrawLines");
return *this;
}
Renderer& Renderer::DrawRect(int x1, int y1, int x2, int y2) {
SDL_Rect rect = {x1, y1, x2 - x1 + 1, y2 - y1 + 1};
if (SDL_RenderDrawRect(renderer_, &rect) != 0)
throw Exception("SDL_RenderDrawRect");
return *this;
}
Renderer& Renderer::DrawRect(const Point& p1, const Point& p2) {
DrawRect(p1.x, p1.y, p2.x, p2.y);
return *this;
}
Renderer& Renderer::DrawRect(const Rect& r) {
if (SDL_RenderDrawRect(renderer_, &r) != 0)
throw Exception("SDL_RenderDrawRect");
return *this;
}
Renderer& Renderer::DrawRects(const Rect* rects, int count) {
std::vector<SDL_Rect> sdl_rects;
sdl_rects.reserve(static_cast<size_t>(count));
for (const Rect* r = rects; r != rects + count; ++r)
sdl_rects.emplace_back(*r);
if (SDL_RenderDrawRects(renderer_, sdl_rects.data(), count) != 0)
throw Exception("SDL_RenderDrawRects");
return *this;
}
Renderer& Renderer::FillRect(int x1, int y1, int x2, int y2) {
SDL_Rect rect = {x1, y1, x2 - x1 + 1, y2 - y1 + 1};
if (SDL_RenderFillRect(renderer_, &rect) != 0)
throw Exception("SDL_RenderFillRect");
return *this;
}
Renderer& Renderer::FillRect(const Point& p1, const Point& p2) {
FillRect(p1.x, p1.y, p2.x, p2.y);
return *this;
}
Renderer& Renderer::FillRect(const Rect& r) {
if (SDL_RenderFillRect(renderer_, &r) != 0)
throw Exception("SDL_RenderFillRect");
return *this;
}
Renderer& Renderer::FillRects(const Rect* rects, int count) {
std::vector<SDL_Rect> sdl_rects;
sdl_rects.reserve(static_cast<size_t>(count));
for (const Rect* r = rects; r != rects + count; ++r)
sdl_rects.emplace_back(*r);
if (SDL_RenderFillRects(renderer_, sdl_rects.data(), count) != 0)
throw Exception("SDL_RenderFillRects");
return *this;
}
void Renderer::ReadPixels(const Optional<Rect>& rect, Uint32 format, void* pixels, int pitch) {
if (SDL_RenderReadPixels(renderer_, rect ? &*rect : nullptr, format, pixels, pitch) != 0)
throw Exception("SDL_RenderReadPixels");
}
Renderer& Renderer::SetClipRect(const Optional<Rect>& rect) {
if (SDL_RenderSetClipRect(renderer_, rect ? &*rect : nullptr) != 0)
throw Exception("SDL_RenderSetClipRect");
return *this;
}
Renderer& Renderer::SetLogicalSize(int w, int h) {
if (SDL_RenderSetLogicalSize(renderer_, w, h) != 0)
throw Exception("SDL_RenderSetLogicalSize");
return *this;
}
Renderer& Renderer::SetScale(float scaleX, float scaleY) {
if (SDL_RenderSetScale(renderer_, scaleX, scaleY) != 0)
throw Exception("SDL_RenderSetScale");
return *this;
}
Renderer& Renderer::SetViewport(const Optional<Rect>& rect) {
if (SDL_RenderSetViewport(renderer_, rect ? &*rect : nullptr) != 0)
throw Exception("SDL_RenderSetViewport");
return *this;
}
bool Renderer::TargetSupported() const {
return SDL_RenderTargetSupported(renderer_) == SDL_TRUE;
}
Optional<Rect> Renderer::GetClipRect() const {
SDL_Rect rect;
SDL_RenderGetClipRect(renderer_, &rect);
if (SDL_RectEmpty(&rect))
return NullOpt;
else
return Rect(rect);
}
Point Renderer::GetLogicalSize() const {
int w, h;
SDL_RenderGetLogicalSize(renderer_, &w, &h);
return Point(w, h);
}
int Renderer::GetLogicalWidth() const {
int w;
SDL_RenderGetLogicalSize(renderer_, &w, nullptr);
return w;
}
int Renderer::GetLogicalHeight() const {
int h;
SDL_RenderGetLogicalSize(renderer_, nullptr, &h);
return h;
}
void Renderer::GetScale(float& scalex, float& scaley) const {
SDL_RenderGetScale(renderer_, &scalex, &scaley);
}
float Renderer::GetXScale() const {
float scalex;
SDL_RenderGetScale(renderer_, &scalex, nullptr);
return scalex;
}
float Renderer::GetYScale() const {
float scaley;
SDL_RenderGetScale(renderer_, nullptr, &scaley);
return scaley;
}
Rect Renderer::GetViewport() const {
SDL_Rect rect;
SDL_RenderGetViewport(renderer_, &rect);
return rect;
}
SDL_BlendMode Renderer::GetDrawBlendMode() const {
SDL_BlendMode mode;
if (SDL_GetRenderDrawBlendMode(renderer_, &mode) != 0)
throw Exception("SDL_GetRenderDrawBlendMode");
return mode;
}
Color Renderer::GetDrawColor() const {
Color color;
GetDrawColor(color.r, color.g, color.b, color.a);
return color;
}
void Renderer::GetDrawColor(Uint8& r, Uint8& g, Uint8& b, Uint8& a) const {
if (SDL_GetRenderDrawColor(renderer_, &r, &g, &b, &a) != 0)
throw Exception("SDL_GetRenderDrawColor");
}
Point Renderer::GetOutputSize() const {
int w, h;
if (SDL_GetRendererOutputSize(renderer_, &w, &h) != 0)
throw Exception("SDL_GetRendererOutputSize");
return Point(w, h);
}
int Renderer::GetOutputWidth() const {
int w;
if (SDL_GetRendererOutputSize(renderer_, &w, nullptr) != 0)
throw Exception("SDL_GetRendererOutputSize");
return w;
}
int Renderer::GetOutputHeight() const {
int h;
if (SDL_GetRendererOutputSize(renderer_, nullptr, &h) != 0)
throw Exception("SDL_GetRendererOutputSize");
return h;
}
}