25#include <boost/math/constants/constants.hpp>
26#include <SDL3/SDL_rect.h>
27#include <SDL3/SDL_render.h>
30#define DBG_D LOG_STREAM(debug, log_draw)
31#define WRN_D LOG_STREAM(warn, log_draw)
63 SDL_Texture* target =
nullptr;
72viewport_state current_viewport;
78const rect* reduced_viewport()
80 if(!
renderer() || current_viewport.actual == current_viewport.requested) {
85 if(SDL_GetRenderTarget(
renderer()) != current_viewport.target) {
90 SDL_GetRenderViewport(
renderer(), &viewport);
91 if(viewport != current_viewport.actual) {
95 return ¤t_viewport.requested;
99point viewport_offset()
101 const rect* requested = reduced_viewport();
102 return requested ? requested->
origin() - current_viewport.actual.origin() :
point{};
110SDL_FRect to_sdl(
const SDL_FRect& r)
112 const point offset = viewport_offset();
113 return {r.x + offset.x, r.y + offset.y, r.w, r.h};
125 SDL_GetRenderDrawBlendMode(
renderer(), &
b);
126 SDL_SetRenderDrawBlendMode(
renderer(), SDL_BLENDMODE_NONE);
128 SDL_SetRenderDrawBlendMode(
renderer(),
b);
133 uint8_t r, uint8_t
g, uint8_t
b, uint8_t a)
136 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
138 SDL_RenderFillRect(
renderer(), &frect);
143 uint8_t r, uint8_t
g, uint8_t
b)
156 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
157 SDL_RenderFillRect(
renderer(),
nullptr);
172 DBG_D <<
"sub-pixel fill";
174 SDL_FRect frect = to_sdl(
rect);
175 SDL_RenderFillRect(
renderer(), &frect);
180 DBG_D <<
"fill " << area;
182 SDL_RenderFillRect(
renderer(), &frect);
188 SDL_RenderFillRect(
renderer(),
nullptr);
194 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
200 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, SDL_ALPHA_OPAQUE);
205 DBG_D <<
"set color " <<
c;
211 SDL_SetRenderDrawBlendMode(
renderer(),
b);
252 uint8_t r, uint8_t
g, uint8_t
b, uint8_t a)
255 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
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);
283 DBG_D <<
"line from (" << from_x <<
',' << from_y
284 <<
") to (" << to_x <<
',' << to_y
285 <<
") with colour " <<
c;
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);
294 static_assert(
sizeof(
::point) ==
sizeof(SDL_Point));
297 const ::point offset = viewport_offset();
303 std::vector<SDL_FPoint> translated =
points;
304 for(SDL_FPoint&
p : translated) {
308 SDL_RenderPoints(
renderer(), translated.data(), translated.size());
313 DBG_D <<
"point (" << x <<
',' << y <<
')';
314 const ::point offset = viewport_offset();
315 SDL_RenderPoint(
renderer(), x + offset.x, y + offset.y);
326 DBG_D <<
"circle (" << cx <<
',' << cy
327 <<
") -> " << r <<
", oct " << int(octants);
336 std::vector<SDL_FPoint>
points;
339 points.push_back({
static_cast<float>(cx + y),
static_cast<float>(cy + x)});
342 points.push_back({
static_cast<float>(cx + y),
static_cast<float>(cy - x)});
345 points.push_back({
static_cast<float>(cx - y),
static_cast<float>(cy + x)});
348 points.push_back({
static_cast<float>(cx - y),
static_cast<float>(cy - x)});
369 DBG_D <<
"disc (" << cx <<
',' << cy
370 <<
") -> " << r <<
", oct " << int(octants);
382 draw::line(cx + x, cy + y + 1, cx + y + 1, cy + y + 1);
385 draw::line(cx + x, cy - y, cx + y + 1, cy - y);
388 draw::line(cx - x - 1, cy + y + 1, cx - y - 2, cy + y + 1);
391 draw::line(cx - x - 1, cy - y, cx - y - 2, cy - y);
395 draw::line(cx + y, cy + x + 1, cx + y, cy + y + 1);
401 draw::line(cx - y - 1, cy + x + 1, cx - y - 1, cy + y + 1);
404 draw::line(cx - y - 1, cy - x, cx - y - 1, cy - y);
425 int draw_size = 2 * r;
427 int render_size = draw_size * pixel_scale;
429 surface sdl_surf(render_size, render_size);
431 reinterpret_cast<uint8_t*
>(sdl_surf->pixels),
::point(sdl_surf->w, sdl_surf->h));
434 cairo_t* ctx = cairo_context.get();
435 cairo_scale(ctx, pixel_scale, pixel_scale);
437 cairo_set_antialias(ctx, CAIRO_ANTIALIAS_BEST);
438 cairo_set_source_rgba(ctx, 0.0, 0.0, 0.0, 0.0);
441 cairo_set_line_width(ctx, thickness);
442 cairo_set_source_rgba(ctx,
450 double half_thickness = 1.0 * thickness / 2;
451 cairo_arc(ctx, r, r, r - half_thickness, 0, 2 * boost::math::constants::pi<double>());
463 int draw_size = 2 * r;
465 int render_size = draw_size * pixel_scale;
467 surface sdl_surf(render_size, render_size);
469 reinterpret_cast<uint8_t*
>(sdl_surf->pixels),
::point(sdl_surf->w, sdl_surf->h));
472 cairo_t* ctx = cairo_context.get();
473 cairo_scale(ctx, pixel_scale, pixel_scale);
475 cairo_set_antialias(ctx, CAIRO_ANTIALIAS_BEST);
476 cairo_set_source_rgba(ctx,
482 cairo_arc(ctx, r, r, r, 0, 2 * boost::math::constants::pi<double>());
499 if (!tex) {
DBG_D <<
"null blit";
return; }
502 const SDL_Rect*
rect = tex.
src();
506 SDL_RenderTexture(
renderer(), tex, &frect, &fdst);
508 SDL_RenderTexture(
renderer(), tex,
nullptr, &fdst);
514 if (!tex) {
DBG_D <<
"null blit";
return; }
516 if(const ::rect* requested = reduced_viewport()) {
521 const SDL_Rect*
rect = tex.
src();
524 SDL_RenderTexture(
renderer(), tex, &frect,
nullptr);
526 SDL_RenderTexture(
renderer(), tex,
nullptr,
nullptr);
531static SDL_FlipMode
get_flip(
bool flip_h,
bool flip_v)
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)
550 if (!tex) {
DBG_D <<
"null flipped";
return; }
551 DBG_D <<
"flipped (" << flip_h <<
'|' << flip_v
554 SDL_FlipMode flip =
get_flip(flip_h, flip_v);
555 const SDL_Rect*
rect = tex.
src();
559 SDL_RenderTextureRotated(
renderer(), tex, &frect, &fdst, 0.0,
nullptr, flip);
561 SDL_RenderTextureRotated(
renderer(), tex,
nullptr, &fdst, 0.0,
nullptr, flip);
567 if (!tex) {
DBG_D <<
"null flipped";
return; }
569 if(const ::rect* requested = reduced_viewport()) {
572 DBG_D <<
"flipped (" << flip_h <<
'|' << flip_v <<
')';
574 SDL_FlipMode flip =
get_flip(flip_h, flip_v);
575 const SDL_Rect*
rect = tex.
src();
578 SDL_RenderTextureRotated(
renderer(), tex, &frect,
nullptr, 0.0,
nullptr, flip);
580 SDL_RenderTextureRotated(
renderer(), tex,
nullptr,
nullptr, 0.0,
nullptr, flip);
589 if (!tex) {
DBG_D <<
"null tiled";
return; }
590 DBG_D <<
"tiled (" << centered <<
'|' << mirrored
596 const int xoff = centered ? (
dst.w - tex.
w()) / 2 : 0;
597 const int yoff = centered ? (
dst.h - tex.
h()) / 2 : 0;
602 for (;
t.y <
dst.y +
dst.h;
t.y +=
t.h, vf = !vf) {
604 for (
t.x =
dst.x - xoff;
t.x <
dst.x +
dst.w;
t.x +=
t.w, hf = !hf) {
615 bool centered,
bool mirrored)
617 if (!tex) {
DBG_D <<
"null tiled_highres";
return; }
618 DBG_D <<
"tiled_highres (" << centered <<
'|' << mirrored
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;
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) {
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);
640 SDL_FlipMode flip =
get_flip(hf, vf);
641 SDL_RenderTextureRotated(
renderer(), tex,
nullptr, &sdl_t, 0.0,
nullptr, flip);
643 SDL_RenderTexture(
renderer(), tex,
nullptr, &sdl_t);
650 const std::array<SDL_Vertex, 4>& verts)
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
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;
666 int indices[6] = {0, 1, 2, 2, 1, 3};
667 SDL_RenderGeometry(
renderer(), tex, &translated[0], 4, indices, 6);
707 WRN_D <<
"trying to force clip will null renderer";
710 DBG_D <<
"forcing clip to " << clip;
712 const ::rect sdl_clip = to_sdl(clip);
713 SDL_SetRenderClipRect(
renderer(), &sdl_clip);
723 if (!SDL_RenderClipEnabled(
renderer())) {
728 SDL_GetRenderClipRect(
renderer(), &clip);
729 clip.
shift(-viewport_offset());
738 return SDL_RenderClipEnabled(
renderer());
746 SDL_SetRenderClipRect(
renderer(),
nullptr);
747 DBG_D <<
"clip disabled";
755 if (!SDL_RenderClipEnabled(
renderer())) {
759 SDL_GetRenderClipRect(
renderer(), &clip);
760 return clip.w <= 0 || clip.h <= 0;
776 c_.x +
v_.x - view.x,
777 c_.y +
v_.y - view.y,
802 WRN_D <<
"trying to force viewport will null renderer";
805 DBG_D <<
"forcing viewport to " << viewport;
810 if(!bounds.empty()) {
814 actual = {bounds.
origin(), {1, 1}};
818 SDL_SetRenderViewport(
renderer(), &actual);
821 SDL_GetRenderViewport(
renderer(), &actual);
822 current_viewport = {SDL_GetRenderTarget(
renderer()), actual, viewport};
828 WRN_D <<
"no renderer available to get viewport";
832 if(const ::rect* requested = reduced_viewport()) {
837 SDL_GetRenderViewport(
renderer(), &viewport);
848 , requested_viewport_()
852 assert(!
t ||
t.get_access() == SDL_TEXTUREACCESS_TARGET);
855 WRN_D <<
"can't set render target with null renderer";
863 const ::rect* requested = reduced_viewport();
876 WRN_D <<
"can't reset render target with null renderer";
880 SDL_SetRenderViewport(
renderer(), &viewport_);
883 SDL_GetRenderViewport(
renderer(), &actual);
884 current_viewport = {SDL_GetRenderTarget(
renderer()), actual, requested_viewport_};
887 SDL_SetRenderClipRect(
renderer(), &clip_);
893 DBG_D <<
"setting render target to "
894 <<
t.w() <<
'x' <<
t.h() <<
" texture";
896 DBG_D <<
"setting render target to main render buffer";
A class to manage automatic restoration of the clipping region.
clip_setter(const ::rect &clip)
A class to manage automatic restoration of the render target.
render_target_setter(const texture &t)
::rect requested_viewport_
A class to manage automatic restoration of the viewport region.
viewport_setter(const ::rect &viewport)
Wrapper class to encapsulate creation and management of an SDL_Texture.
int w() const
The draw-space width of the texture, in pixels.
point get_raw_size() const
The raw internal texture size.
const rect * src() const
A pointer to a rect indicating the source region of the underlying SDL_Texture to be used when drawin...
int h() const
The draw-space height of the texture, in pixels.
static lg::log_domain log_draw("draw")
static SDL_Renderer * renderer()
static bool sdl_bad_at_rects()
Some versions of SDL have a bad rectangle drawing implementation.
static void draw_rect_as_lines(const rect &rect)
For some SDL versions, draw rectangles as lines.
static SDL_FlipMode get_flip(bool flip_h, bool flip_v)
static SDL_FRect rect_to_frect(const SDL_Rect &rect)
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)
surface_ptr create_surface(uint8_t *buffer, const point &size)
void force_viewport(const ::rect &viewport)
Set the viewport, without any provided way of setting it back.
void tiled_highres(const texture &tex, const ::rect &dst, bool centered=false, bool mirrored=false)
Tile a texture to fill a region.
render_target_setter set_render_target(const texture &t)
Set the given texture as the active render target.
void circle(int x, int y, int r, const color_t &c, uint8_t octants=0xff)
Draw a circle of the given colour.
void tiled(const texture &tex, const ::rect &dst, bool centered=false, bool mirrored=false)
Tile a texture to fill a region.
void points(const std::vector< SDL_FPoint > &points)
Draw a set of points.
void rect(const ::rect &rect)
Draw a rectangle.
void cairo_disc(int cx, int cy, int r, const color_t &c)
Draw filled circle using Cairo.
bool null_clip()
Whether the current clipping region will disallow drawing.
bool clip_enabled()
Whether clipping is enabled.
clip_setter reduce_clip(const ::rect &clip)
Set the clipping area to the intersection of the current clipping area and the given rectangle.
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...
void cairo_circle(int cx, int cy, int r, const color_t &c, int thickness)
Draw outline of circle using Cairo.
void disc(int x, int y, int r, const color_t &c, uint8_t octants=0xff)
Draw a solid disc of the given colour.
void set_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Set the drawing colour.
clip_setter override_clip(const ::rect &clip)
Override the clipping area.
void point(int x, int y)
Draw a single point.
void clear()
Clear the current render target.
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 ...
void set_blend_mode(SDL_BlendMode b)
Set the blend mode used for drawing operations such as fill() and line().
void force_clip(const ::rect &clip)
Set the clipping area, without any provided way of setting it back.
viewport_setter set_viewport(const ::rect &viewport)
Set the viewport.
void fill(const ::rect &rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Fill an area with the given colour.
::rect get_viewport()
Get the current viewport.
::rect get_clip()
Get the current clipping area, in draw coordinates.
void disable_clip()
Disable clipping.
void blit(const texture &tex, const ::rect &dst)
Draws a texture, or part of a texture, at the given location.
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
constexpr const SDL_Rect empty_rect
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,...
rect draw_area()
The current drawable area.
void reset_render_target()
Reset the render target to the primary render buffer.
void force_render_target(const texture &t)
Set the render target, without any provided way of setting it back.
int get_pixel_scale()
Get the current active pixel scale multiplier.
SDL_Renderer * get_renderer()
texture get_render_target()
Get the current render target.
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.
An abstract description of a rectangle with integer coordinates.
void clip(const rect &r)
Clip this rectangle by the given rectangle.
bool empty() const
False if both w and h are > 0, true otherwise.
rect shifted_by(int x, int y) const
Returns a new rectangle shifted by the given relative position.
constexpr point origin() const
constexpr point size() const
void shift(const point &p)
Shift the rectangle by the given relative position.