The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
draw.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 - 2025
3 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY.
11
12 See the COPYING file for more details.
13*/
14
15#include "draw.hpp"
16
17#include "color.hpp"
18#include "font/cairo.hpp"
19#include "log.hpp"
20#include "sdl/rect.hpp"
21#include "sdl/texture.hpp"
22#include "sdl/utils.hpp" // sdl::runtime_at_least
23#include "video.hpp"
24
25#include <boost/math/constants/constants.hpp>
26#include <SDL3/SDL_rect.h>
27#include <SDL3/SDL_render.h>
28
29static lg::log_domain log_draw("draw");
30#define DBG_D LOG_STREAM(debug, log_draw)
31#define WRN_D LOG_STREAM(warn, log_draw)
32
33static SDL_Renderer* renderer()
34{
35 return video::get_renderer();
36}
37
38static SDL_FRect rect_to_frect(const SDL_Rect& rect)
39{
40 SDL_FRect frect;
41 frect.h = rect.h;
42 frect.w = rect.w;
43 frect.x = rect.x;
44 frect.y = rect.y;
45 return frect;
46}
47
48namespace
49{
50/**
51 * GPUs limit the size and position of the viewport (for example
52 * GL_MAX_VIEWPORT_DIMS), but SDL calculates its projection from the
53 * viewport it was given, so drawing in a viewport beyond those limits comes
54 * out squashed and misplaced. Widgets can be much larger than the screen,
55 * such as a label with a long scrolled text, so force_viewport() only gives
56 * SDL the part of the requested viewport that lies within the render
57 * target. Drawing co-ordinates are then translated by the difference, so
58 * they remain relative to the requested viewport.
59 */
60struct viewport_state
61{
62 /** The render target the viewport was set on. */
63 SDL_Texture* target = nullptr;
64
65 /** The viewport given to SDL. */
66 rect actual;
67
68 /** The viewport requested by force_viewport(). */
69 rect requested;
70};
71
72viewport_state current_viewport;
73
74/**
75 * The viewport requested by force_viewport(), if it differs from the SDL
76 * viewport and is still in effect.
77 */
78const rect* reduced_viewport()
79{
80 if(!renderer() || current_viewport.actual == current_viewport.requested) {
81 return nullptr;
82 }
83
84 // Ignore it if the render target or viewport was changed by other means.
85 if(SDL_GetRenderTarget(renderer()) != current_viewport.target) {
86 return nullptr;
87 }
88
89 rect viewport;
90 SDL_GetRenderViewport(renderer(), &viewport);
91 if(viewport != current_viewport.actual) {
92 return nullptr;
93 }
94
95 return &current_viewport.requested;
96}
97
98/** The offset from drawing co-ordinates to SDL viewport co-ordinates. */
99point viewport_offset()
100{
101 const rect* requested = reduced_viewport();
102 return requested ? requested->origin() - current_viewport.actual.origin() : point{};
103}
104
105rect to_sdl(const rect& r)
106{
107 return r.shifted_by(viewport_offset());
108}
109
110SDL_FRect to_sdl(const SDL_FRect& r)
111{
112 const point offset = viewport_offset();
113 return {r.x + offset.x, r.y + offset.y, r.w, r.h};
114}
115} // namespace
116
117/**************************************/
118/* basic drawing and pixel primatives */
119/**************************************/
120
122{
123 DBG_D << "clear";
124 SDL_BlendMode b;
125 SDL_GetRenderDrawBlendMode(renderer(), &b);
126 SDL_SetRenderDrawBlendMode(renderer(), SDL_BLENDMODE_NONE);
127 fill(0, 0, 0, 0);
128 SDL_SetRenderDrawBlendMode(renderer(), b);
129}
130
132 const ::rect& area,
133 uint8_t r, uint8_t g, uint8_t b, uint8_t a)
134{
135 DBG_D << "fill " << area << ' ' << color_t{r,g,b,a};
136 SDL_SetRenderDrawColor(renderer(), r, g, b, a);
137 SDL_FRect frect = rect_to_frect(to_sdl(area));
138 SDL_RenderFillRect(renderer(), &frect);
139}
140
142 const ::rect& area,
143 uint8_t r, uint8_t g, uint8_t b)
144{
145 draw::fill(area, r, g, b, SDL_ALPHA_OPAQUE);
146}
147
148void draw::fill(const ::rect& area, const color_t& c)
149{
150 draw::fill(area, c.r, c.g, c.b, c.a);
151}
152
153void draw::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
154{
155 DBG_D << "fill " << color_t{r,g,b,a};
156 SDL_SetRenderDrawColor(renderer(), r, g, b, a);
157 SDL_RenderFillRect(renderer(), nullptr);
158}
159
160void draw::fill(uint8_t r, uint8_t g, uint8_t b)
161{
162 draw::fill(r, g, b, SDL_ALPHA_OPAQUE);
163}
164
165void draw::fill(const color_t& c)
166{
167 draw::fill(c.r, c.g, c.b, c.a);
168}
169
170void draw::fill(const SDL_FRect& rect, const color_t& c)
171{
172 DBG_D << "sub-pixel fill";
173 SDL_SetRenderDrawColor(renderer(), c.r, c.g, c.b, c.a);
174 SDL_FRect frect = to_sdl(rect);
175 SDL_RenderFillRect(renderer(), &frect);
176}
177
178void draw::fill(const ::rect& area)
179{
180 DBG_D << "fill " << area;
181 SDL_FRect frect = rect_to_frect(to_sdl(area));
182 SDL_RenderFillRect(renderer(), &frect);
183}
184
186{
187 DBG_D << "fill";
188 SDL_RenderFillRect(renderer(), nullptr);
189}
190
191void draw::set_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
192{
193 DBG_D << "set color " << color_t{r,g,b,a};
194 SDL_SetRenderDrawColor(renderer(), r, g, b, a);
195}
196
197void draw::set_color(uint8_t r, uint8_t g, uint8_t b)
198{
199 DBG_D << "set color " << color_t{r,g,b};
200 SDL_SetRenderDrawColor(renderer(), r, g, b, SDL_ALPHA_OPAQUE);
201}
202
204{
205 DBG_D << "set color " << c;
206 SDL_SetRenderDrawColor(renderer(), c.r, c.g, c.b, c.a);
207}
208
209void draw::set_blend_mode(SDL_BlendMode b)
210{
211 SDL_SetRenderDrawBlendMode(renderer(), b);
212}
213
214/** Some versions of SDL have a bad rectangle drawing implementation. */
215static bool sdl_bad_at_rects()
216{
217 // This could be done once at program start and cached,
218 // but it isn't all that heavy.
219 if (sdl::runtime_at_least(2,0,15) && !sdl::runtime_at_least(2,0,18)) {
220 return true;
221 }
222 return false;
223}
224
225/** For some SDL versions, draw rectangles as lines. */
226static void draw_rect_as_lines(const rect& rect)
227{
228 // w and h indicate the final pixel width/height of the box.
229 // This is 1 greater than the difference in corner coordinates.
230 if (rect.w <= 0 || rect.h <= 0) {
231 return;
232 }
233 int x2 = rect.x + rect.w - 1;
234 int y2 = rect.y + rect.h - 1;
235 draw::line(rect.x, rect.y, x2, rect.y);
236 draw::line(rect.x, rect.y, rect.x, y2);
237 draw::line(x2, rect.y, x2, y2);
238 draw::line(rect.x, y2, x2, y2);
239}
240
241void draw::rect(const ::rect& rect)
242{
243 DBG_D << "rect " << rect;
244 if (sdl_bad_at_rects()) {
245 return draw_rect_as_lines(rect);
246 }
247 SDL_FRect frect = rect_to_frect(to_sdl(rect));
248 SDL_RenderRect(renderer(), &frect);
249}
250
251void draw::rect(const ::rect& rect,
252 uint8_t r, uint8_t g, uint8_t b, uint8_t a)
253{
254 DBG_D << "rect " << rect << ' ' << color_t{r,g,b,a};
255 SDL_SetRenderDrawColor(renderer(), r, g, b, a);
256 if (sdl_bad_at_rects()) {
257 return draw_rect_as_lines(rect);
258 }
259 SDL_FRect frect = rect_to_frect(to_sdl(rect));
260 SDL_RenderRect(renderer(), &frect);
261}
262
263void draw::rect(const ::rect& rect, uint8_t r, uint8_t g, uint8_t b)
264{
265 draw::rect(rect, r, g, b, SDL_ALPHA_OPAQUE);
266}
267
268void draw::rect(const ::rect& rect, const color_t& c)
269{
270 draw::rect(rect, c.r, c.g, c.b, c.a);
271}
272
273void draw::line(int from_x, int from_y, int to_x, int to_y)
274{
275 DBG_D << "line from (" << from_x << ',' << from_y
276 << ") to (" << to_x << ',' << to_y << ')';
277 const ::point offset = viewport_offset();
278 SDL_RenderLine(renderer(), from_x + offset.x, from_y + offset.y, to_x + offset.x, to_y + offset.y);
279}
280
281void draw::line(int from_x, int from_y, int to_x, int to_y, const color_t& c)
282{
283 DBG_D << "line from (" << from_x << ',' << from_y
284 << ") to (" << to_x << ',' << to_y
285 << ") with colour " << c;
286 SDL_SetRenderDrawColor(renderer(), c.r, c.g, c.b, c.a);
287 const ::point offset = viewport_offset();
288 SDL_RenderLine(renderer(), from_x + offset.x, from_y + offset.y, to_x + offset.x, to_y + offset.y);
289}
290
291void draw::points(const std::vector<SDL_FPoint>& points)
292{
293 // We cannot decay vector<point> to SDL_Point* unless the two types are the same size.
294 static_assert(sizeof(::point) == sizeof(SDL_Point));
295
296 DBG_D << points.size() << " points";
297 const ::point offset = viewport_offset();
298 if(offset == ::point{}) {
299 SDL_RenderPoints(renderer(), points.data(), points.size());
300 return;
301 }
302
303 std::vector<SDL_FPoint> translated = points;
304 for(SDL_FPoint& p : translated) {
305 p.x += offset.x;
306 p.y += offset.y;
307 }
308 SDL_RenderPoints(renderer(), translated.data(), translated.size());
309}
310
311void draw::point(int x, int y)
312{
313 DBG_D << "point (" << x << ',' << y << ')';
314 const ::point offset = viewport_offset();
315 SDL_RenderPoint(renderer(), x + offset.x, y + offset.y);
316}
317
318void draw::circle(int cx, int cy, int r, const color_t& c, uint8_t octants)
319{
321 draw::circle(cx, cy, r, octants);
322}
323
324void draw::circle(int cx, int cy, int r, uint8_t octants)
325{
326 DBG_D << "circle (" << cx << ',' << cy
327 << ") -> " << r << ", oct " << int(octants);
328
329 // Algorithm based on
330 // http://de.wikipedia.org/wiki/Rasterung_von_Kreisen#Methode_von_Horn
331 // version of 2011.02.07.
332 int d = -r;
333 int x = r;
334 int y = 0;
335
336 std::vector<SDL_FPoint> points;
337
338 if(octants & 0x08) {
339 points.push_back({static_cast<float>(cx + y), static_cast<float>(cy + x)});
340 }
341 if(octants & 0x01) {
342 points.push_back({static_cast<float>(cx + y), static_cast<float>(cy - x)});
343 }
344 if(octants & 0x10) {
345 points.push_back({static_cast<float>(cx - y), static_cast<float>(cy + x)});
346 }
347 if(octants & 0x80) {
348 points.push_back({static_cast<float>(cx - y), static_cast<float>(cy - x)});
349 }
350
351 d += 2 * y + 1;
352 ++y;
353 if(d > 0) {
354 d += -2 * x + 2;
355 --x;
356 }
357
359}
360
361void draw::disc(int cx, int cy, int r, const color_t& c, uint8_t octants)
362{
364 draw::disc(cx, cy, r, octants);
365}
366
367void draw::disc(int cx, int cy, int r, uint8_t octants)
368{
369 DBG_D << "disc (" << cx << ',' << cy
370 << ") -> " << r << ", oct " << int(octants);
371
372 int d = -r;
373 int x = r;
374 int y = 0;
375
376 while(!(y > x)) {
377 // I use the formula of Bresenham's line algorithm
378 // to determine the boundaries of a segment.
379 // The slope of the line is always 1 or -1 in this case.
380 if(octants & 0x04)
381 // x2 - 1 = y2 - (cy + 1) + cx
382 draw::line(cx + x, cy + y + 1, cx + y + 1, cy + y + 1);
383 if(octants & 0x02)
384 // x2 - 1 = cy - y2 + cx
385 draw::line(cx + x, cy - y, cx + y + 1, cy - y);
386 if(octants & 0x20)
387 // x2 + 1 = (cy + 1) - y2 + (cx - 1)
388 draw::line(cx - x - 1, cy + y + 1, cx - y - 2, cy + y + 1);
389 if(octants & 0x40)
390 // x2 + 1 = y2 - cy + (cx - 1)
391 draw::line(cx - x - 1, cy - y, cx - y - 2, cy - y);
392
393 if(octants & 0x08)
394 // y2 = x2 - cx + (cy + 1)
395 draw::line(cx + y, cy + x + 1, cx + y, cy + y + 1);
396 if(octants & 0x01)
397 // y2 = cx - x2 + cy
398 draw::line(cx + y, cy - x, cx + y, cy - y);
399 if(octants & 0x10)
400 // y2 = (cx - 1) - x2 + (cy + 1)
401 draw::line(cx - y - 1, cy + x + 1, cx - y - 1, cy + y + 1);
402 if(octants & 0x80)
403 // y2 = x2 - (cx - 1) + cy
404 draw::line(cx - y - 1, cy - x, cx - y - 1, cy - y);
405
406 d += 2 * y + 1;
407 ++y;
408 if(d > 0) {
409 d += -2 * x + 2;
410 --x;
411 }
412 }
413}
414
415/********************/
416/* Cairo primitives */
417/********************/
418
419void draw::cairo_circle(int cx, int cy, int r, const color_t& c, int thickness)
420{
421 if (r <= 0) {
422 return;
423 }
424
425 int draw_size = 2 * r;
426 int pixel_scale = video::get_pixel_scale();
427 int render_size = draw_size * pixel_scale;
428
429 surface sdl_surf(render_size, render_size);
430 auto cairo_surface = cairo::create_surface(
431 reinterpret_cast<uint8_t*>(sdl_surf->pixels), ::point(sdl_surf->w, sdl_surf->h));
432
433 auto cairo_context = cairo::create_context(cairo_surface);
434 cairo_t* ctx = cairo_context.get();
435 cairo_scale(ctx, pixel_scale, pixel_scale);
436
437 cairo_set_antialias(ctx, CAIRO_ANTIALIAS_BEST);
438 cairo_set_source_rgba(ctx, 0.0, 0.0, 0.0, 0.0);
439 cairo_paint(ctx);
440
441 cairo_set_line_width(ctx, thickness);
442 cairo_set_source_rgba(ctx,
443 c.r / 255.0,
444 c.g / 255.0,
445 c.b / 255.0,
446 c.a / 255.0
447 );
448
449 // Reduce the arc radius by half the stroke width so the border stays within the circle
450 double half_thickness = 1.0 * thickness / 2;
451 cairo_arc(ctx, r, r, r - half_thickness, 0, 2 * boost::math::constants::pi<double>());
452 cairo_stroke(ctx);
453
454 draw::blit(texture(sdl_surf), ::rect(cx - r, cy - r, draw_size, draw_size));
455}
456
457void draw::cairo_disc(int cx, int cy, int r, const color_t& c)
458{
459 if (r <= 0) {
460 return;
461 }
462
463 int draw_size = 2 * r;
464 int pixel_scale = video::get_pixel_scale();
465 int render_size = draw_size * pixel_scale;
466
467 surface sdl_surf(render_size, render_size);
468 auto cairo_surface = cairo::create_surface(
469 reinterpret_cast<uint8_t*>(sdl_surf->pixels), ::point(sdl_surf->w, sdl_surf->h));
470
471 auto cairo_context = cairo::create_context(cairo_surface);
472 cairo_t* ctx = cairo_context.get();
473 cairo_scale(ctx, pixel_scale, pixel_scale);
474
475 cairo_set_antialias(ctx, CAIRO_ANTIALIAS_BEST);
476 cairo_set_source_rgba(ctx,
477 c.r / 255.0,
478 c.g / 255.0,
479 c.b / 255.0,
480 c.a / 255.0
481 );
482 cairo_arc(ctx, r, r, r, 0, 2 * boost::math::constants::pi<double>());
483 cairo_fill(ctx);
484
485 draw::blit(texture(sdl_surf), ::rect(cx - r, cy - r, draw_size, draw_size));
486}
487
488/*******************/
489/* texture drawing */
490/*******************/
491
492
493void draw::blit(const texture& tex, const ::rect& dst)
494{
495 if (dst == sdl::empty_rect) {
496 return draw::blit(tex);
497 }
498
499 if (!tex) { DBG_D << "null blit"; return; }
500 DBG_D << "blit " << dst;
501
502 const SDL_Rect* rect = tex.src();
503 SDL_FRect fdst = rect_to_frect(to_sdl(dst));
504 if(rect) {
505 SDL_FRect frect = rect_to_frect(*rect);
506 SDL_RenderTexture(renderer(), tex, &frect, &fdst);
507 } else {
508 SDL_RenderTexture(renderer(), tex, nullptr, &fdst);
509 }
510}
511
512void draw::blit(const texture& tex)
513{
514 if (!tex) { DBG_D << "null blit"; return; }
515
516 if(const ::rect* requested = reduced_viewport()) {
517 return draw::blit(tex, ::rect{::point{}, requested->size()});
518 }
519 DBG_D << "blit";
520
521 const SDL_Rect* rect = tex.src();
522 if(rect) {
523 SDL_FRect frect = rect_to_frect(*rect);
524 SDL_RenderTexture(renderer(), tex, &frect, nullptr);
525 } else {
526 SDL_RenderTexture(renderer(), tex, nullptr, nullptr);
527 }
528}
529
530
531static SDL_FlipMode get_flip(bool flip_h, bool flip_v)
532{
533 // This should be easier than it is.
534 return static_cast<SDL_FlipMode>(
535 static_cast<int>(flip_h ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE)
536 | static_cast<int>(flip_v ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE)
537 );
538}
539
541 const texture& tex,
542 const ::rect& dst,
543 bool flip_h,
544 bool flip_v)
545{
546 if (dst == sdl::empty_rect) {
547 return draw::flipped(tex, flip_h, flip_v);
548 }
549
550 if (!tex) { DBG_D << "null flipped"; return; }
551 DBG_D << "flipped (" << flip_h << '|' << flip_v
552 << ") to " << dst;
553
554 SDL_FlipMode flip = get_flip(flip_h, flip_v);
555 const SDL_Rect* rect = tex.src();
556 SDL_FRect fdst = rect_to_frect(to_sdl(dst));
557 if(rect) {
558 SDL_FRect frect = rect_to_frect(*rect);
559 SDL_RenderTextureRotated(renderer(), tex, &frect, &fdst, 0.0, nullptr, flip);
560 } else {
561 SDL_RenderTextureRotated(renderer(), tex, nullptr, &fdst, 0.0, nullptr, flip);
562 }
563}
564
565void draw::flipped(const texture& tex, bool flip_h, bool flip_v)
566{
567 if (!tex) { DBG_D << "null flipped"; return; }
568
569 if(const ::rect* requested = reduced_viewport()) {
570 return draw::flipped(tex, ::rect{::point{}, requested->size()}, flip_h, flip_v);
571 }
572 DBG_D << "flipped (" << flip_h << '|' << flip_v << ')';
573
574 SDL_FlipMode flip = get_flip(flip_h, flip_v);
575 const SDL_Rect* rect = tex.src();
576 if(rect) {
577 SDL_FRect frect = rect_to_frect(*rect);
578 SDL_RenderTextureRotated(renderer(), tex, &frect, nullptr, 0.0, nullptr, flip);
579 } else {
580 SDL_RenderTextureRotated(renderer(), tex, nullptr, nullptr, 0.0, nullptr, flip);
581 }
582}
583
584
585// TODO: highdpi - maybe expose this mirrored mode to WML somehow
586void draw::tiled(const texture& tex, const ::rect& dst, bool centered,
587 bool mirrored)
588{
589 if (!tex) { DBG_D << "null tiled"; return; }
590 DBG_D << "tiled (" << centered << '|' << mirrored
591 << ") " << dst;
592
593 // Reduce clip to dst.
594 auto clipper = draw::reduce_clip(dst);
595
596 const int xoff = centered ? (dst.w - tex.w()) / 2 : 0;
597 const int yoff = centered ? (dst.h - tex.h()) / 2 : 0;
598
599 // Just blit the image however many times is necessary.
600 bool vf = false;
601 ::rect t{dst.x - xoff, dst.y - yoff, tex.w(), tex.h()};
602 for (; t.y < dst.y + dst.h; t.y += t.h, vf = !vf) {
603 bool hf = false;
604 for (t.x = dst.x - xoff; t.x < dst.x + dst.w; t.x += t.w, hf = !hf) {
605 if (mirrored) {
606 draw::flipped(tex, t, hf, vf);
607 } else {
608 draw::blit(tex, t);
609 }
610 }
611 }
612}
613
614void draw::tiled_highres(const texture& tex, const ::rect& dst,
615 bool centered, bool mirrored)
616{
617 if (!tex) { DBG_D << "null tiled_highres"; return; }
618 DBG_D << "tiled_highres (" << centered << '|' << mirrored
619 << ") " << dst;
620
621 const int pixel_scale = video::get_pixel_scale();
622
623 // Reduce clip to dst.
624 auto clipper = draw::reduce_clip(dst);
625
626 const ::point size = tex.get_raw_size();
627 const float w = float(size.x) / float(pixel_scale);
628 const float h = float(size.y) / float(pixel_scale);
629 const float xoff = centered ? (dst.w - w) / 2 : 0.0f;
630 const float yoff = centered ? (dst.h - h) / 2 : 0.0f;
631
632 // Just blit the image however many times is necessary.
633 bool vf = false;
634 SDL_FRect t{dst.x - xoff, dst.y - yoff, w, h};
635 for (; t.y < dst.y + dst.h; t.y += t.h, vf = !vf) {
636 bool hf = false;
637 for (t.x = dst.x - xoff; t.x < dst.x + dst.w; t.x += t.w, hf = !hf) {
638 const SDL_FRect sdl_t = to_sdl(t);
639 if (mirrored) {
640 SDL_FlipMode flip = get_flip(hf, vf);
641 SDL_RenderTextureRotated(renderer(), tex, nullptr, &sdl_t, 0.0, nullptr, flip);
642 } else {
643 SDL_RenderTexture(renderer(), tex, nullptr, &sdl_t);
644 }
645 }
646 }
647}
648
650 const std::array<SDL_Vertex, 4>& verts)
651{
652 DBG_D << "smooth shade, verts:";
653 for (const SDL_Vertex& v : verts) {
654 DBG_D << " {(" << v.position.x << ',' << v.position.y << ") "
655 << v.color.r << "," << v.color.g << "," << v.color.b << "," << v.color.a
656 << " (" << v.tex_coord.x << ',' << v.tex_coord.y
657 << ")}";
658 }
659 std::array<SDL_Vertex, 4> translated = verts;
660 const ::point offset = viewport_offset();
661 for(SDL_Vertex& v : translated) {
662 v.position.x += offset.x;
663 v.position.y += offset.y;
664 }
665
666 int indices[6] = {0, 1, 2, 2, 1, 3};
667 SDL_RenderGeometry(renderer(), tex, &translated[0], 4, indices, 6);
668}
669
670/***************************/
671/* RAII state manipulation */
672/***************************/
673
674
676 : c_(draw::get_clip()), clip_enabled_(draw::clip_enabled())
677{
678 draw::force_clip(clip);
679}
680
682{
683 if (clip_enabled_) {
685 } else {
687 }
688}
689
691{
692 return draw::clip_setter(clip);
693}
694
696{
697 if (!draw::clip_enabled()) {
698 return draw::clip_setter(clip);
699 }
700 return draw::clip_setter(draw::get_clip().intersect(clip));
701}
702
703void draw::force_clip(const ::rect& clip)
704{
705 // TODO: highdpi - fix whatever reason there is for this guard (CI fail)
706 if (!renderer()) {
707 WRN_D << "trying to force clip will null renderer";
708 return;
709 }
710 DBG_D << "forcing clip to " << clip;
711
712 const ::rect sdl_clip = to_sdl(clip);
713 SDL_SetRenderClipRect(renderer(), &sdl_clip);
714}
715
717{
718 // TODO: highdpi - fix whatever reason there is for this guard (CI fail)
719 if (!renderer()) {
720 return {};
721 }
722
723 if (!SDL_RenderClipEnabled(renderer())) {
724 return draw::get_viewport();
725 }
726
727 ::rect clip;
728 SDL_GetRenderClipRect(renderer(), &clip);
729 clip.shift(-viewport_offset());
730 return clip;
731}
732
734{
735 if (!renderer()) {
736 return false;
737 }
738 return SDL_RenderClipEnabled(renderer());
739}
740
742{
743 if (!renderer()) {
744 return;
745 }
746 SDL_SetRenderClipRect(renderer(), nullptr);
747 DBG_D << "clip disabled";
748}
749
751{
752 if (!renderer()) {
753 return true;
754 }
755 if (!SDL_RenderClipEnabled(renderer())) {
756 return false;
757 }
758 ::rect clip;
759 SDL_GetRenderClipRect(renderer(), &clip);
760 return clip.w <= 0 || clip.h <= 0;
761}
762
763
765 : v_(), c_(), clip_enabled_(draw::clip_enabled())
766{
767 // Get the clip first, as it's interpreted relative to the current viewport.
769 if (clip_enabled_) {
770 c_ = draw::get_clip();
771 }
773 if (clip_enabled_) {
774 // adjust clip for difference in viewport position
775 ::rect c_view = {
776 c_.x + v_.x - view.x,
777 c_.y + v_.y - view.y,
778 c_.w, c_.h
779 };
780 draw::force_clip(c_view);
781 }
782}
783
785{
787 if (clip_enabled_) {
789 } else {
791 }
792}
793
795{
796 return draw::viewport_setter(viewport);
797}
798
799void draw::force_viewport(const ::rect& viewport)
800{
801 if (!renderer()) {
802 WRN_D << "trying to force viewport will null renderer";
803 return;
804 }
805 DBG_D << "forcing viewport to " << viewport;
806
807 // Only give SDL the part within the render target, see viewport_state.
808 ::rect actual = viewport;
809 const ::rect bounds = video::draw_area();
810 if(!bounds.empty()) {
811 actual.clip(bounds);
812 if(actual.empty()) {
813 // Nothing can be seen, but SDL still needs a valid viewport.
814 actual = {bounds.origin(), {1, 1}};
815 }
816 }
817
818 SDL_SetRenderViewport(renderer(), &actual);
819
820 // Store what SDL reports, so reduced_viewport() can compare it exactly.
821 SDL_GetRenderViewport(renderer(), &actual);
822 current_viewport = {SDL_GetRenderTarget(renderer()), actual, viewport};
823}
824
826{
827 if (!renderer()) {
828 WRN_D << "no renderer available to get viewport";
829 return {};
830 }
831
832 if(const ::rect* requested = reduced_viewport()) {
833 return *requested;
834 }
835
836 ::rect viewport;
837 SDL_GetRenderViewport(renderer(), &viewport);
838
839 if (viewport == sdl::empty_rect) {
840 return video::draw_area();
841 }
842 return viewport;
843}
844
846 : target_()
847 , viewport_()
848 , requested_viewport_()
849 , clip_()
850{
851 // Validate we can render to this texture.
852 assert(!t || t.get_access() == SDL_TEXTUREACCESS_TARGET);
853
854 if (!renderer()) {
855 WRN_D << "can't set render target with null renderer";
856 return;
857 }
858
860 SDL_GetRenderViewport(renderer(), &viewport_);
861 SDL_GetRenderClipRect(renderer(), &clip_);
862
863 const ::rect* requested = reduced_viewport();
864 requested_viewport_ = requested ? *requested : viewport_;
865
866 if (t) {
868 } else {
870 }
871}
872
874{
875 if (!renderer()) {
876 WRN_D << "can't reset render target with null renderer";
877 return;
878 }
880 SDL_SetRenderViewport(renderer(), &viewport_);
881
882 ::rect actual;
883 SDL_GetRenderViewport(renderer(), &actual);
884 current_viewport = {SDL_GetRenderTarget(renderer()), actual, requested_viewport_};
885
886 if(clip_ == sdl::empty_rect) return;
887 SDL_SetRenderClipRect(renderer(), &clip_);
888}
889
891{
892 if (t) {
893 DBG_D << "setting render target to "
894 << t.w() << 'x' << t.h() << " texture";
895 } else {
896 DBG_D << "setting render target to main render buffer";
897 }
899}
double t
A class to manage automatic restoration of the clipping region.
Definition draw.hpp:328
clip_setter(const ::rect &clip)
Definition draw.cpp:675
A class to manage automatic restoration of the render target.
Definition draw.hpp:460
render_target_setter(const texture &t)
Definition draw.cpp:845
A class to manage automatic restoration of the viewport region.
Definition draw.hpp:400
viewport_setter(const ::rect &viewport)
Definition draw.cpp:764
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition texture.hpp:33
int w() const
The draw-space width of the texture, in pixels.
Definition texture.hpp:103
point get_raw_size() const
The raw internal texture size.
Definition texture.cpp:109
const rect * src() const
A pointer to a rect indicating the source region of the underlying SDL_Texture to be used when drawin...
Definition texture.hpp:146
int h() const
The draw-space height of the texture, in pixels.
Definition texture.hpp:112
static lg::log_domain log_draw("draw")
static SDL_Renderer * renderer()
Definition draw.cpp:33
static bool sdl_bad_at_rects()
Some versions of SDL have a bad rectangle drawing implementation.
Definition draw.cpp:215
static void draw_rect_as_lines(const rect &rect)
For some SDL versions, draw rectangles as lines.
Definition draw.cpp:226
static SDL_FlipMode get_flip(bool flip_h, bool flip_v)
Definition draw.cpp:531
#define WRN_D
Definition draw.cpp:31
static SDL_FRect rect_to_frect(const SDL_Rect &rect)
Definition draw.cpp:38
#define DBG_D
Definition draw.cpp:30
Drawing functions, for drawing things on the screen.
const gui2::tracked_drawable & target_
The drawable whose render calls we are tracking.
Standard logging facilities (interface).
context_ptr create_context(const surface_ptr &surf)
Definition cairo.hpp:41
surface_ptr create_surface(uint8_t *buffer, const point &size)
Definition cairo.hpp:30
Definition draw.hpp:43
void force_viewport(const ::rect &viewport)
Set the viewport, without any provided way of setting it back.
Definition draw.cpp:799
void tiled_highres(const texture &tex, const ::rect &dst, bool centered=false, bool mirrored=false)
Tile a texture to fill a region.
Definition draw.cpp:614
render_target_setter set_render_target(const texture &t)
Set the given texture as the active render target.
Definition draw.cpp:890
void circle(int x, int y, int r, const color_t &c, uint8_t octants=0xff)
Draw a circle of the given colour.
Definition draw.cpp:318
void tiled(const texture &tex, const ::rect &dst, bool centered=false, bool mirrored=false)
Tile a texture to fill a region.
Definition draw.cpp:586
void points(const std::vector< SDL_FPoint > &points)
Draw a set of points.
Definition draw.cpp:291
void rect(const ::rect &rect)
Draw a rectangle.
Definition draw.cpp:241
void cairo_disc(int cx, int cy, int r, const color_t &c)
Draw filled circle using Cairo.
Definition draw.cpp:457
bool null_clip()
Whether the current clipping region will disallow drawing.
Definition draw.cpp:750
bool clip_enabled()
Whether clipping is enabled.
Definition draw.cpp:733
clip_setter reduce_clip(const ::rect &clip)
Set the clipping area to the intersection of the current clipping area and the given rectangle.
Definition draw.cpp:695
void flipped(const texture &tex, const ::rect &dst, bool flip_h=true, bool flip_v=false)
Draws a texture, or part of a texture, at the given location, also mirroring/flipping the texture hor...
Definition draw.cpp:540
void cairo_circle(int cx, int cy, int r, const color_t &c, int thickness)
Draw outline of circle using Cairo.
Definition draw.cpp:419
void disc(int x, int y, int r, const color_t &c, uint8_t octants=0xff)
Draw a solid disc of the given colour.
Definition draw.cpp:361
void set_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Set the drawing colour.
Definition draw.cpp:191
clip_setter override_clip(const ::rect &clip)
Override the clipping area.
Definition draw.cpp:690
void point(int x, int y)
Draw a single point.
Definition draw.cpp:311
void clear()
Clear the current render target.
Definition draw.cpp:121
void smooth_shaded(const texture &tex, const std::array< SDL_Vertex, 4 > &verts)
Draw a texture with smoothly varying colour and alpha modification, specified at the four corners of ...
Definition draw.cpp:649
void fill()
Definition draw.cpp:185
void set_blend_mode(SDL_BlendMode b)
Set the blend mode used for drawing operations such as fill() and line().
Definition draw.cpp:209
void force_clip(const ::rect &clip)
Set the clipping area, without any provided way of setting it back.
Definition draw.cpp:703
viewport_setter set_viewport(const ::rect &viewport)
Set the viewport.
Definition draw.cpp:794
void fill(const ::rect &rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Fill an area with the given colour.
Definition draw.cpp:131
::rect get_viewport()
Get the current viewport.
Definition draw.cpp:825
::rect get_clip()
Get the current clipping area, in draw coordinates.
Definition draw.cpp:716
void disable_clip()
Disable clipping.
Definition draw.cpp:741
void blit(const texture &tex, const ::rect &dst)
Draws a texture, or part of a texture, at the given location.
Definition draw.cpp:493
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
Definition draw.cpp:273
constexpr const SDL_Rect empty_rect
Definition rect.hpp:32
bool runtime_at_least(uint8_t major, uint8_t minor=0, uint8_t patch=0)
Returns true if the runtime SDL version is at or greater than the specified version,...
Definition utils.cpp:48
rect draw_area()
The current drawable area.
Definition video.cpp:442
void reset_render_target()
Reset the render target to the primary render buffer.
Definition video.cpp:558
void force_render_target(const texture &t)
Set the render target, without any provided way of setting it back.
Definition video.cpp:522
int get_pixel_scale()
Get the current active pixel scale multiplier.
Definition video.cpp:480
SDL_Renderer * get_renderer()
Definition video.cpp:703
texture get_render_target()
Get the current render target.
Definition video.cpp:563
int w
Definition pathfind.cpp:188
Contains the SDL_Rect helper code.
rect dst
Location on the final composed sheet.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition color.hpp:51
Holds a 2D point.
Definition point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition rect.hpp:49
void clip(const rect &r)
Clip this rectangle by the given rectangle.
Definition rect.cpp:101
bool empty() const
False if both w and h are > 0, true otherwise.
Definition rect.cpp:49
rect shifted_by(int x, int y) const
Returns a new rectangle shifted by the given relative position.
Definition rect.cpp:112
constexpr point origin() const
Definition rect.hpp:66
constexpr point size() const
Definition rect.hpp:67
void shift(const point &p)
Shift the rectangle by the given relative position.
Definition rect.cpp:106
mock_party p
#define g
#define d
#define h
#define c
#define b