25 #include <boost/math/constants/constants.hpp>
26 #include <SDL2/SDL_rect.h>
27 #include <SDL2/SDL_render.h>
30 #define DBG_D LOG_STREAM(debug, log_draw)
31 #define WRN_D LOG_STREAM(warn, log_draw)
46 SDL_GetRenderDrawBlendMode(
renderer(), &
b);
47 SDL_SetRenderDrawBlendMode(
renderer(), SDL_BLENDMODE_NONE);
49 SDL_SetRenderDrawBlendMode(
renderer(),
b);
54 uint8_t r, uint8_t
g, uint8_t
b, uint8_t a)
57 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
58 SDL_RenderFillRect(
renderer(), &area);
63 uint8_t r, uint8_t
g, uint8_t
b)
76 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
77 SDL_RenderFillRect(
renderer(),
nullptr);
92 DBG_D <<
"sub-pixel fill";
99 DBG_D <<
"fill " << area;
100 SDL_RenderFillRect(
renderer(), &area);
106 SDL_RenderFillRect(
renderer(),
nullptr);
112 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
118 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, SDL_ALPHA_OPAQUE);
123 DBG_D <<
"set color " <<
c;
129 SDL_SetRenderDrawBlendMode(
renderer(),
b);
169 uint8_t r, uint8_t
g, uint8_t
b, uint8_t a)
172 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
191 DBG_D <<
"line from (" << from_x <<
',' << from_y
192 <<
") to (" << to_x <<
',' << to_y <<
')';
193 SDL_RenderDrawLine(
renderer(), from_x, from_y, to_x, to_y);
198 DBG_D <<
"line from (" << from_x <<
',' << from_y
199 <<
") to (" << to_x <<
',' << to_y
200 <<
") with colour " <<
c;
202 SDL_RenderDrawLine(
renderer(), from_x, from_y, to_x, to_y);
213 DBG_D <<
"point (" << x <<
',' << y <<
')';
214 SDL_RenderDrawPoint(
renderer(), x, y);
225 DBG_D <<
"circle (" << cx <<
',' << cy
226 <<
") -> " << r <<
", oct " << int(octants);
235 std::vector<SDL_Point>
points;
238 if(octants & 0x04)
points.push_back({cx + x, cy + y});
239 if(octants & 0x02)
points.push_back({cx + x, cy - y});
240 if(octants & 0x20)
points.push_back({cx - x, cy + y});
241 if(octants & 0x40)
points.push_back({cx - x, cy - y});
243 if(octants & 0x08)
points.push_back({cx + y, cy + x});
244 if(octants & 0x01)
points.push_back({cx + y, cy - x});
245 if(octants & 0x10)
points.push_back({cx - y, cy + x});
246 if(octants & 0x80)
points.push_back({cx - y, cy - x});
267 DBG_D <<
"disc (" << cx <<
',' << cy
268 <<
") -> " << r <<
", oct " << int(octants);
280 draw::line(cx + x, cy + y + 1, cx + y + 1, cy + y + 1);
283 draw::line(cx + x, cy - y, cx + y + 1, cy - y);
286 draw::line(cx - x - 1, cy + y + 1, cx - y - 2, cy + y + 1);
289 draw::line(cx - x - 1, cy - y, cx - y - 2, cy - y);
293 draw::line(cx + y, cy + x + 1, cx + y, cy + y + 1);
299 draw::line(cx - y - 1, cy + x + 1, cx - y - 1, cy + y + 1);
302 draw::line(cx - y - 1, cy - x, cx - y - 1, cy - y);
326 reinterpret_cast<uint8_t*
>(sdl_surf->pixels), ::
point(sdl_surf->w, sdl_surf->h));
328 cairo_t* ctx = cairo_context.get();
330 cairo_set_antialias(ctx, CAIRO_ANTIALIAS_BEST);
332 cairo_set_source_rgba(ctx, 0.0, 0.0, 0.0, 0.0);
335 cairo_set_line_width(ctx, thickness);
336 cairo_set_source_rgba(ctx,
342 cairo_arc(ctx, r, r, r-thickness, 0, 2*boost::math::constants::pi<double>());
357 reinterpret_cast<uint8_t*
>(sdl_surf->pixels), ::
point(sdl_surf->w, sdl_surf->h));
359 cairo_t* ctx = cairo_context.get();
361 cairo_set_antialias(ctx, CAIRO_ANTIALIAS_BEST);
363 cairo_set_source_rgba(ctx,
369 cairo_arc(ctx, r, r, r, 0, 2*2*boost::math::constants::pi<double>());
386 if (!tex) {
DBG_D <<
"null blit";
return; }
394 if (!tex) {
DBG_D <<
"null blit";
return; }
397 SDL_RenderCopy(
renderer(), tex, tex.
src(),
nullptr);
401 static SDL_RendererFlip
get_flip(
bool flip_h,
bool flip_v)
404 return static_cast<SDL_RendererFlip
>(
405 static_cast<int>(flip_h ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE)
406 |
static_cast<int>(flip_v ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE)
420 if (!tex) {
DBG_D <<
"null flipped";
return; }
421 DBG_D <<
"flipped (" << flip_h <<
'|' << flip_v
424 SDL_RendererFlip flip =
get_flip(flip_h, flip_v);
425 SDL_RenderCopyEx(
renderer(), tex, tex.
src(), &
dst, 0.0,
nullptr, flip);
430 if (!tex) {
DBG_D <<
"null flipped";
return; }
431 DBG_D <<
"flipped (" << flip_h <<
'|' << flip_v <<
')';
433 SDL_RendererFlip flip =
get_flip(flip_h, flip_v);
434 SDL_RenderCopyEx(
renderer(), tex, tex.
src(),
nullptr, 0.0,
nullptr, flip);
442 if (!tex) {
DBG_D <<
"null tiled";
return; }
443 DBG_D <<
"tiled (" << centered <<
'|' << mirrored
449 const int xoff = centered ? (
dst.w - tex.
w()) / 2 : 0;
450 const int yoff = centered ? (
dst.h - tex.
h()) / 2 : 0;
454 SDL_Rect
t{
dst.x - xoff,
dst.y - yoff, tex.
w(), tex.
h()};
455 for (;
t.y <
dst.y +
dst.h;
t.y +=
t.h, vf = !vf) {
457 for (
t.x =
dst.x - xoff;
t.x <
dst.x +
dst.w;
t.x +=
t.w, hf = !hf) {
468 bool centered,
bool mirrored)
470 if (!tex) {
DBG_D <<
"null tiled_highres";
return; }
471 DBG_D <<
"tiled_highres (" << centered <<
'|' << mirrored
480 const float w = float(
size.x) / float(pixel_scale);
481 const float h = float(
size.y) / float(pixel_scale);
482 const float xoff = centered ? (
dst.w -
w) / 2 : 0.0f;
483 const float yoff = centered ? (
dst.h -
h) / 2 : 0.0f;
487 SDL_FRect
t{
dst.x - xoff,
dst.y - yoff,
w,
h};
488 for (;
t.y <
dst.y +
dst.h;
t.y +=
t.h, vf = !vf) {
490 for (
t.x =
dst.x - xoff;
t.x <
dst.x +
dst.w;
t.x +=
t.w, hf = !hf) {
492 SDL_RendererFlip flip =
get_flip(hf, vf);
493 SDL_RenderCopyExF(
renderer(), tex,
nullptr, &
t, 0.0,
nullptr, flip);
495 SDL_RenderCopyF(
renderer(), tex,
nullptr, &
t);
502 const SDL_Color& cTL,
const SDL_Color& cTR,
503 const SDL_Color& cBL,
const SDL_Color& cBR,
504 const SDL_FPoint& uvTL,
const SDL_FPoint& uvTR,
505 const SDL_FPoint& uvBL,
const SDL_FPoint& uvBR)
507 const SDL_FPoint pTL{float(
dst.x), float(
dst.y)};
508 const SDL_FPoint pTR{float(
dst.x +
dst.w), float(
dst.y)};
509 const SDL_FPoint pBL{float(
dst.x), float(
dst.y +
dst.h)};
510 const SDL_FPoint pBR{float(
dst.x +
dst.w), float(
dst.y +
dst.h)};
511 std::array<SDL_Vertex,4> verts {
512 SDL_Vertex{pTL, cTL, uvTL},
513 SDL_Vertex{pTR, cTR, uvTR},
514 SDL_Vertex{pBL, cBL, uvBL},
515 SDL_Vertex{pBR, cBR, uvBR},
521 const SDL_Color& cTL,
const SDL_Color& cTR,
522 const SDL_Color& cBL,
const SDL_Color& cBR)
531 uv[0], uv[1], uv[2], uv[3]);
535 const std::array<SDL_Vertex, 4>& verts)
537 DBG_D <<
"smooth shade, verts:";
538 for (
const SDL_Vertex& v : verts) {
539 DBG_D <<
" {(" << v.position.x <<
',' << v.position.y <<
") "
540 << v.color <<
" (" << v.tex_coord.x <<
',' << v.tex_coord.y
543 int indices[6] = {0, 1, 2, 2, 1, 3};
544 SDL_RenderGeometry(
renderer(), tex, &verts[0], 4, indices, 6);
584 WRN_D <<
"trying to force clip will null renderer";
587 DBG_D <<
"forcing clip to " << clip;
589 SDL_RenderSetClipRect(
renderer(), &clip);
599 if (!SDL_RenderIsClipEnabled(
renderer())) {
604 SDL_RenderGetClipRect(
renderer(), &clip);
613 return SDL_RenderIsClipEnabled(
renderer());
621 SDL_RenderSetClipRect(
renderer(),
nullptr);
622 DBG_D <<
"clip disabled";
630 if (!SDL_RenderIsClipEnabled(
renderer())) {
634 SDL_RenderGetClipRect(
renderer(), &clip);
635 return clip.w <= 0 || clip.h <= 0;
648 c_.x +
v_.x - view.x,
649 c_.y +
v_.y - view.y,
674 WRN_D <<
"trying to force viewport will null renderer";
677 DBG_D <<
"forcing viewport to " << viewport;
679 SDL_RenderSetViewport(
renderer(), &viewport);
685 WRN_D <<
"no renderer available to get viewport";
690 SDL_RenderGetViewport(
renderer(), &viewport);
704 assert(!
t ||
t.get_access() == SDL_TEXTUREACCESS_TARGET);
707 WRN_D <<
"can't set render target with null renderer";
725 WRN_D <<
"can't reset render target with null renderer";
729 SDL_RenderSetViewport(
renderer(), &viewport_);
731 SDL_RenderSetClipRect(
renderer(), &clip_);
737 DBG_D <<
"setting render target to "
738 <<
t.w() <<
'x' <<
t.h() <<
" texture";
740 DBG_D <<
"setting render target to main render buffer";
A class to manage automatic restoration of the clipping region.
clip_setter(const SDL_Rect &clip)
A class to manage automatic restoration of the render target.
render_target_setter(const texture &t)
A class to manage automatic restoration of the viewport region.
viewport_setter(const SDL_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 bool sdl_bad_at_rects()
Some versions of SDL have a bad rectangle drawing implementation.
static SDL_Renderer * renderer()
static void draw_rect_as_lines(const SDL_Rect &rect)
For some SDL versions, draw rectangles as lines.
static SDL_RendererFlip get_flip(bool flip_h, bool flip_v)
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)
viewport_setter set_viewport(const SDL_Rect &viewport)
Set the viewport.
void force_viewport(const SDL_Rect &viewport)
Set the viewport, without any provided way of setting it back.
render_target_setter set_render_target(const texture &t)
Set the given texture as the active render target.
clip_setter override_clip(const SDL_Rect &clip)
Override the clipping area.
void circle(int x, int y, int r, const color_t &c, uint8_t octants=0xff)
Draw a circle of the given colour.
void points(const std::vector< SDL_Point > &points)
Draw a set of points.
void force_clip(const SDL_Rect &clip)
Set the clipping area, without any provided way of setting it back.
SDL_Rect get_viewport()
Get the current viewport.
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.
clip_setter reduce_clip(const SDL_Rect &clip)
Set the clipping area to the intersection of the current clipping area and the given rectangle.
bool clip_enabled()
Whether clipping is enabled.
void cairo_circle(int cx, int cy, int r, const color_t &c, int thickness)
Draw outline of circle using Cairo.
void tiled(const texture &tex, const SDL_Rect &dst, bool centered=false, bool mirrored=false)
Tile a texture to fill a region.
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.
void flipped(const texture &tex, const SDL_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 fill(const SDL_Rect &rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Fill an area with the given colour.
void point(int x, int y)
Draw a single point.
void clear()
Clear the current render target.
void set_blend_mode(SDL_BlendMode b)
Set the blend mode used for drawing operations such as fill() and line().
void blit(const texture &tex, const SDL_Rect &dst)
Draws a texture, or part of a texture, at the given location.
void tiled_highres(const texture &tex, const SDL_Rect &dst, bool centered=false, bool mirrored=false)
Tile a texture to fill a region.
::rect get_clip()
Get the current clipping area, in draw coordinates.
void rect(const SDL_Rect &rect)
Draw a rectangle.
void disable_clip()
Disable clipping.
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
void smooth_shaded(const texture &tex, const SDL_Rect &dst, const SDL_Color &cTL, const SDL_Color &cTR, const SDL_Color &cBL, const SDL_Color &cBR, const SDL_FPoint &uvTL, const SDL_FPoint &uvTR, const SDL_FPoint &uvBL, const SDL_FPoint &uvBR)
Draw a texture with smoothly varying colour and alpha modification, specified at the four corners of ...
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,...
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
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.