24 #include <SDL2/SDL_rect.h>
25 #include <SDL2/SDL_render.h>
28 #define DBG_D LOG_STREAM(debug, log_draw)
29 #define WRN_D LOG_STREAM(warn, log_draw)
44 SDL_GetRenderDrawBlendMode(
renderer(), &
b);
45 SDL_SetRenderDrawBlendMode(
renderer(), SDL_BLENDMODE_NONE);
47 SDL_SetRenderDrawBlendMode(
renderer(),
b);
52 uint8_t r, uint8_t
g, uint8_t
b, uint8_t a)
55 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
56 SDL_RenderFillRect(
renderer(), &area);
61 uint8_t r, uint8_t
g, uint8_t
b)
74 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
75 SDL_RenderFillRect(
renderer(),
nullptr);
90 DBG_D <<
"sub-pixel fill";
97 DBG_D <<
"fill " << area;
98 SDL_RenderFillRect(
renderer(), &area);
104 SDL_RenderFillRect(
renderer(),
nullptr);
110 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
116 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, SDL_ALPHA_OPAQUE);
121 DBG_D <<
"set color " <<
c;
127 SDL_SetRenderDrawBlendMode(
renderer(),
b);
167 uint8_t r, uint8_t
g, uint8_t
b, uint8_t a)
170 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
189 DBG_D <<
"line from (" << from_x <<
',' << from_y
190 <<
") to (" << to_x <<
',' << to_y <<
')';
191 SDL_RenderDrawLine(
renderer(), from_x, from_y, to_x, to_y);
196 DBG_D <<
"line from (" << from_x <<
',' << from_y
197 <<
") to (" << to_x <<
',' << to_y
198 <<
") with colour " <<
c;
200 SDL_RenderDrawLine(
renderer(), from_x, from_y, to_x, to_y);
211 DBG_D <<
"point (" << x <<
',' << y <<
')';
212 SDL_RenderDrawPoint(
renderer(), x, y);
223 DBG_D <<
"circle (" << cx <<
',' << cy
224 <<
") -> " << r <<
", oct " << int(octants);
233 std::vector<SDL_Point>
points;
236 if(octants & 0x04)
points.push_back({cx + x, cy + y});
237 if(octants & 0x02)
points.push_back({cx + x, cy - y});
238 if(octants & 0x20)
points.push_back({cx - x, cy + y});
239 if(octants & 0x40)
points.push_back({cx - x, cy - y});
241 if(octants & 0x08)
points.push_back({cx + y, cy + x});
242 if(octants & 0x01)
points.push_back({cx + y, cy - x});
243 if(octants & 0x10)
points.push_back({cx - y, cy + x});
244 if(octants & 0x80)
points.push_back({cx - y, cy - x});
265 DBG_D <<
"disc (" << cx <<
',' << cy
266 <<
") -> " << r <<
", oct " << int(octants);
278 draw::line(cx + x, cy + y + 1, cx + y + 1, cy + y + 1);
281 draw::line(cx + x, cy - y, cx + y + 1, cy - y);
284 draw::line(cx - x - 1, cy + y + 1, cx - y - 2, cy + y + 1);
287 draw::line(cx - x - 1, cy - y, cx - y - 2, cy - y);
291 draw::line(cx + y, cy + x + 1, cx + y, cy + y + 1);
297 draw::line(cx - y - 1, cy + x + 1, cx - y - 1, cy + y + 1);
300 draw::line(cx - y - 1, cy - x, cx - y - 1, cy - y);
323 if (!tex) {
DBG_D <<
"null blit";
return; }
331 if (!tex) {
DBG_D <<
"null blit";
return; }
334 SDL_RenderCopy(
renderer(), tex, tex.
src(),
nullptr);
338 static SDL_RendererFlip
get_flip(
bool flip_h,
bool flip_v)
341 return static_cast<SDL_RendererFlip
>(
342 static_cast<int>(flip_h ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE)
343 |
static_cast<int>(flip_v ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE)
357 if (!tex) {
DBG_D <<
"null flipped";
return; }
358 DBG_D <<
"flipped (" << flip_h <<
'|' << flip_v
361 SDL_RendererFlip flip =
get_flip(flip_h, flip_v);
362 SDL_RenderCopyEx(
renderer(), tex, tex.
src(), &
dst, 0.0,
nullptr, flip);
367 if (!tex) {
DBG_D <<
"null flipped";
return; }
368 DBG_D <<
"flipped (" << flip_h <<
'|' << flip_v <<
')';
370 SDL_RendererFlip flip =
get_flip(flip_h, flip_v);
371 SDL_RenderCopyEx(
renderer(), tex, tex.
src(),
nullptr, 0.0,
nullptr, flip);
379 if (!tex) {
DBG_D <<
"null tiled";
return; }
380 DBG_D <<
"tiled (" << centered <<
'|' << mirrored
386 const int xoff = centered ? (
dst.w - tex.
w()) / 2 : 0;
387 const int yoff = centered ? (
dst.h - tex.
h()) / 2 : 0;
391 SDL_Rect
t{
dst.x - xoff,
dst.y - yoff, tex.
w(), tex.
h()};
392 for (;
t.y <
dst.y +
dst.h;
t.y +=
t.h, vf = !vf) {
394 for (
t.x =
dst.x - xoff;
t.x <
dst.x +
dst.w;
t.x +=
t.w, hf = !hf) {
405 bool centered,
bool mirrored)
407 if (!tex) {
DBG_D <<
"null tiled_highres";
return; }
408 DBG_D <<
"tiled_highres (" << centered <<
'|' << mirrored
417 const float w = float(
size.x) / float(pixel_scale);
418 const float h = float(
size.y) / float(pixel_scale);
419 const float xoff = centered ? (
dst.w -
w) / 2 : 0.0f;
420 const float yoff = centered ? (
dst.h -
h) / 2 : 0.0f;
424 SDL_FRect
t{
dst.x - xoff,
dst.y - yoff,
w,
h};
425 for (;
t.y <
dst.y +
dst.h;
t.y +=
t.h, vf = !vf) {
427 for (
t.x =
dst.x - xoff;
t.x <
dst.x +
dst.w;
t.x +=
t.w, hf = !hf) {
429 SDL_RendererFlip flip =
get_flip(hf, vf);
430 SDL_RenderCopyExF(
renderer(), tex,
nullptr, &
t, 0.0,
nullptr, flip);
432 SDL_RenderCopyF(
renderer(), tex,
nullptr, &
t);
439 const SDL_Color& cTL,
const SDL_Color& cTR,
440 const SDL_Color& cBL,
const SDL_Color& cBR,
441 const SDL_FPoint& uvTL,
const SDL_FPoint& uvTR,
442 const SDL_FPoint& uvBL,
const SDL_FPoint& uvBR)
444 const SDL_FPoint pTL{float(
dst.x), float(
dst.y)};
445 const SDL_FPoint pTR{float(
dst.x +
dst.w), float(
dst.y)};
446 const SDL_FPoint pBL{float(
dst.x), float(
dst.y +
dst.h)};
447 const SDL_FPoint pBR{float(
dst.x +
dst.w), float(
dst.y +
dst.h)};
448 std::array<SDL_Vertex,4> verts {
449 SDL_Vertex{pTL, cTL, uvTL},
450 SDL_Vertex{pTR, cTR, uvTR},
451 SDL_Vertex{pBL, cBL, uvBL},
452 SDL_Vertex{pBR, cBR, uvBR},
458 const SDL_Color& cTL,
const SDL_Color& cTR,
459 const SDL_Color& cBL,
const SDL_Color& cBR)
468 uv[0], uv[1], uv[2], uv[3]);
472 const std::array<SDL_Vertex, 4>& verts)
474 DBG_D <<
"smooth shade, verts:";
475 for (
const SDL_Vertex& v : verts) {
476 DBG_D <<
" {(" << v.position.x <<
',' << v.position.y <<
") "
477 << v.color <<
" (" << v.tex_coord.x <<
',' << v.tex_coord.y
480 int indices[6] = {0, 1, 2, 2, 1, 3};
481 SDL_RenderGeometry(
renderer(), tex, &verts[0], 4, indices, 6);
521 WRN_D <<
"trying to force clip will null renderer";
524 DBG_D <<
"forcing clip to " << clip;
526 SDL_RenderSetClipRect(
renderer(), &clip);
536 if (!SDL_RenderIsClipEnabled(
renderer())) {
541 SDL_RenderGetClipRect(
renderer(), &clip);
550 return SDL_RenderIsClipEnabled(
renderer());
558 SDL_RenderSetClipRect(
renderer(),
nullptr);
559 DBG_D <<
"clip disabled";
567 if (!SDL_RenderIsClipEnabled(
renderer())) {
571 SDL_RenderGetClipRect(
renderer(), &clip);
572 return clip.w <= 0 || clip.h <= 0;
585 c_.x +
v_.x - view.x,
586 c_.y +
v_.y - view.y,
611 WRN_D <<
"trying to force viewport will null renderer";
614 DBG_D <<
"forcing viewport to " << viewport;
616 SDL_RenderSetViewport(
renderer(), &viewport);
622 WRN_D <<
"no renderer available to get viewport";
627 SDL_RenderGetViewport(
renderer(), &viewport);
641 assert(!
t ||
t.get_access() == SDL_TEXTUREACCESS_TARGET);
644 WRN_D <<
"can't set render target with null renderer";
662 WRN_D <<
"can't reset render target with null renderer";
666 SDL_RenderSetViewport(
renderer(), &viewport_);
668 SDL_RenderSetClipRect(
renderer(), &clip_);
674 DBG_D <<
"setting render target to "
675 <<
t.w() <<
'x' <<
t.h() <<
" texture";
677 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.
Standard logging facilities (interface).
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.
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 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.