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 <<
"fill " << area;
91 SDL_RenderFillRect(
renderer(), &area);
97 SDL_RenderFillRect(
renderer(),
nullptr);
103 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
109 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, SDL_ALPHA_OPAQUE);
114 DBG_D <<
"set color " <<
c;
120 SDL_SetRenderDrawBlendMode(
renderer(),
b);
160 uint8_t r, uint8_t
g, uint8_t
b, uint8_t a)
163 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, a);
182 DBG_D <<
"line from (" << from_x <<
',' << from_y
183 <<
") to (" << to_x <<
',' << to_y <<
')';
184 SDL_RenderDrawLine(
renderer(), from_x, from_y, to_x, to_y);
189 DBG_D <<
"line from (" << from_x <<
',' << from_y
190 <<
") to (" << to_x <<
',' << to_y
191 <<
") with colour " <<
c;
193 SDL_RenderDrawLine(
renderer(), from_x, from_y, to_x, to_y);
204 DBG_D <<
"point (" << x <<
',' << y <<
')';
205 SDL_RenderDrawPoint(
renderer(), x, y);
216 DBG_D <<
"circle (" << cx <<
',' << cy
217 <<
") -> " << r <<
", oct " << int(octants);
226 std::vector<SDL_Point>
points;
229 if(octants & 0x04)
points.push_back({cx + x, cy + y});
230 if(octants & 0x02)
points.push_back({cx + x, cy - y});
231 if(octants & 0x20)
points.push_back({cx - x, cy + y});
232 if(octants & 0x40)
points.push_back({cx - x, cy - y});
234 if(octants & 0x08)
points.push_back({cx + y, cy + x});
235 if(octants & 0x01)
points.push_back({cx + y, cy - x});
236 if(octants & 0x10)
points.push_back({cx - y, cy + x});
237 if(octants & 0x80)
points.push_back({cx - y, cy - x});
258 DBG_D <<
"disc (" << cx <<
',' << cy
259 <<
") -> " << r <<
", oct " << int(octants);
271 draw::line(cx + x, cy + y + 1, cx + y + 1, cy + y + 1);
274 draw::line(cx + x, cy - y, cx + y + 1, cy - y);
277 draw::line(cx - x - 1, cy + y + 1, cx - y - 2, cy + y + 1);
280 draw::line(cx - x - 1, cy - y, cx - y - 2, cy - y);
284 draw::line(cx + y, cy + x + 1, cx + y, cy + y + 1);
290 draw::line(cx - y - 1, cy + x + 1, cx - y - 1, cy + y + 1);
293 draw::line(cx - y - 1, cy - x, cx - y - 1, cy - y);
316 if (!tex) {
DBG_D <<
"null blit";
return; }
324 if (!tex) {
DBG_D <<
"null blit";
return; }
327 SDL_RenderCopy(
renderer(), tex, tex.
src(),
nullptr);
331 static SDL_RendererFlip
get_flip(
bool flip_h,
bool flip_v)
334 return static_cast<SDL_RendererFlip
>(
335 static_cast<int>(flip_h ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE)
336 |
static_cast<int>(flip_v ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE)
350 if (!tex) {
DBG_D <<
"null flipped";
return; }
351 DBG_D <<
"flipped (" << flip_h <<
'|' << flip_v
354 SDL_RendererFlip flip =
get_flip(flip_h, flip_v);
355 SDL_RenderCopyEx(
renderer(), tex, tex.
src(), &
dst, 0.0,
nullptr, flip);
360 if (!tex) {
DBG_D <<
"null flipped";
return; }
361 DBG_D <<
"flipped (" << flip_h <<
'|' << flip_v <<
')';
363 SDL_RendererFlip flip =
get_flip(flip_h, flip_v);
364 SDL_RenderCopyEx(
renderer(), tex, tex.
src(),
nullptr, 0.0,
nullptr, flip);
372 if (!tex) {
DBG_D <<
"null tiled";
return; }
373 DBG_D <<
"tiled (" << centered <<
'|' << mirrored
379 const int xoff = centered ? (
dst.w - tex.
w()) / 2 : 0;
380 const int yoff = centered ? (
dst.h - tex.
h()) / 2 : 0;
384 SDL_Rect
t{
dst.x - xoff,
dst.y - yoff, tex.
w(), tex.
h()};
385 for (;
t.y <
dst.y +
dst.h;
t.y +=
t.h, vf = !vf) {
387 for (
t.x =
dst.x - xoff;
t.x <
dst.x +
dst.w;
t.x +=
t.w, hf = !hf) {
398 bool centered,
bool mirrored)
400 if (!tex) {
DBG_D <<
"null tiled_highres";
return; }
401 DBG_D <<
"tiled_highres (" << centered <<
'|' << mirrored
410 const float w = float(
size.x) / float(pixel_scale);
411 const float h = float(
size.y) / float(pixel_scale);
412 const float xoff = centered ? (
dst.w -
w) / 2 : 0.0f;
413 const float yoff = centered ? (
dst.h -
h) / 2 : 0.0f;
417 SDL_FRect
t{
dst.x - xoff,
dst.y - yoff,
w,
h};
418 for (;
t.y <
dst.y +
dst.h;
t.y +=
t.h, vf = !vf) {
420 for (
t.x =
dst.x - xoff;
t.x <
dst.x +
dst.w;
t.x +=
t.w, hf = !hf) {
422 SDL_RendererFlip flip =
get_flip(hf, vf);
423 SDL_RenderCopyExF(
renderer(), tex,
nullptr, &
t, 0.0,
nullptr, flip);
425 SDL_RenderCopyF(
renderer(), tex,
nullptr, &
t);
432 const SDL_Color& cTL,
const SDL_Color& cTR,
433 const SDL_Color& cBL,
const SDL_Color& cBR,
434 const SDL_FPoint& uvTL,
const SDL_FPoint& uvTR,
435 const SDL_FPoint& uvBL,
const SDL_FPoint& uvBR)
437 const SDL_FPoint pTL{float(
dst.x), float(
dst.y)};
438 const SDL_FPoint pTR{float(
dst.x +
dst.w), float(
dst.y)};
439 const SDL_FPoint pBL{float(
dst.x), float(
dst.y +
dst.h)};
440 const SDL_FPoint pBR{float(
dst.x +
dst.w), float(
dst.y +
dst.h)};
441 std::array<SDL_Vertex,4> verts {
442 SDL_Vertex{pTL, cTL, uvTL},
443 SDL_Vertex{pTR, cTR, uvTR},
444 SDL_Vertex{pBL, cBL, uvBL},
445 SDL_Vertex{pBR, cBR, uvBR},
451 const SDL_Color& cTL,
const SDL_Color& cTR,
452 const SDL_Color& cBL,
const SDL_Color& cBR)
461 uv[0], uv[1], uv[2], uv[3]);
465 const std::array<SDL_Vertex, 4>& verts)
467 DBG_D <<
"smooth shade, verts:";
468 for (
const SDL_Vertex& v : verts) {
469 DBG_D <<
" {(" << v.position.x <<
',' << v.position.y <<
") "
470 << v.color <<
" (" << v.tex_coord.x <<
',' << v.tex_coord.y
473 int indices[6] = {0, 1, 2, 2, 1, 3};
474 SDL_RenderGeometry(
renderer(), tex, &verts[0], 4, indices, 6);
514 WRN_D <<
"trying to force clip will null renderer";
517 DBG_D <<
"forcing clip to " << clip;
519 SDL_RenderSetClipRect(
renderer(), &clip);
529 if (!SDL_RenderIsClipEnabled(
renderer())) {
534 SDL_RenderGetClipRect(
renderer(), &clip);
543 return SDL_RenderIsClipEnabled(
renderer());
551 SDL_RenderSetClipRect(
renderer(),
nullptr);
552 DBG_D <<
"clip disabled";
560 if (!SDL_RenderIsClipEnabled(
renderer())) {
564 SDL_RenderGetClipRect(
renderer(), &clip);
565 return clip.w <= 0 || clip.h <= 0;
578 c_.x +
v_.x - view.x,
579 c_.y +
v_.y - view.y,
604 WRN_D <<
"trying to force viewport will null renderer";
607 DBG_D <<
"forcing viewport to " << viewport;
609 SDL_RenderSetViewport(
renderer(), &viewport);
615 WRN_D <<
"no renderer available to get viewport";
620 SDL_RenderGetViewport(
renderer(), &viewport);
634 assert(!
t ||
t.get_access() == SDL_TEXTUREACCESS_TARGET);
637 WRN_D <<
"can't set render target with null renderer";
655 WRN_D <<
"can't reset render target with null renderer";
659 SDL_RenderSetViewport(
renderer(), &viewport_);
661 SDL_RenderSetClipRect(
renderer(), &clip_);
667 DBG_D <<
"setting render target to "
668 <<
t.w() <<
'x' <<
t.h() <<
" texture";
670 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(const std::string &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.