25 #include <SDL2/SDL_rect.h>
26 #include <SDL2/SDL_render.h>
29 #define DBG_D LOG_STREAM(debug, log_draw)
30 #define WRN_D LOG_STREAM(warn, log_draw)
43 uint8_t r, uint8_t
g, uint8_t
b, uint8_t
a)
47 SDL_RenderFillRect(
renderer(), &area);
52 uint8_t r, uint8_t
g, uint8_t
b)
66 SDL_RenderFillRect(
renderer(),
nullptr);
81 DBG_D <<
"fill " << area;
82 SDL_RenderFillRect(
renderer(), &area);
88 SDL_RenderFillRect(
renderer(),
nullptr);
100 SDL_SetRenderDrawColor(
renderer(), r,
g,
b, SDL_ALPHA_OPAQUE);
105 DBG_D <<
"set color " <<
c;
111 SDL_SetRenderDrawBlendMode(
renderer(),
b);
151 uint8_t r, uint8_t
g, uint8_t
b, uint8_t
a)
173 DBG_D <<
"line from (" << from_x <<
',' << from_y
174 <<
") to (" << to_x <<
',' << to_y <<
')';
175 SDL_RenderDrawLine(
renderer(), from_x, from_y, to_x, to_y);
180 DBG_D <<
"line from (" << from_x <<
',' << from_y
181 <<
") to (" << to_x <<
',' << to_y
182 <<
") with colour " <<
c;
184 SDL_RenderDrawLine(
renderer(), from_x, from_y, to_x, to_y);
195 DBG_D <<
"point (" << x <<
',' << y <<
')';
196 SDL_RenderDrawPoint(
renderer(), x, y);
207 DBG_D <<
"circle (" << cx <<
',' << cy
208 <<
") -> " << r <<
", oct " << int(octants);
217 std::vector<SDL_Point>
points;
220 if(octants & 0x04)
points.push_back({cx + x, cy + y});
221 if(octants & 0x02)
points.push_back({cx + x, cy - y});
222 if(octants & 0x20)
points.push_back({cx - x, cy + y});
223 if(octants & 0x40)
points.push_back({cx - x, cy - y});
225 if(octants & 0x08)
points.push_back({cx + y, cy + x});
226 if(octants & 0x01)
points.push_back({cx + y, cy - x});
227 if(octants & 0x10)
points.push_back({cx - y, cy + x});
228 if(octants & 0x80)
points.push_back({cx - y, cy - x});
249 DBG_D <<
"disc (" << cx <<
',' << cy
250 <<
") -> " << r <<
", oct " << int(octants);
262 draw::line(cx + x, cy + y + 1, cx + y + 1, cy + y + 1);
265 draw::line(cx + x, cy - y, cx + y + 1, cy - y);
268 draw::line(cx - x - 1, cy + y + 1, cx - y - 2, cy + y + 1);
271 draw::line(cx - x - 1, cy - y, cx - y - 2, cy - y);
275 draw::line(cx + y, cy + x + 1, cx + y, cy + y + 1);
281 draw::line(cx - y - 1, cy + x + 1, cx - y - 1, cy + y + 1);
284 draw::line(cx - y - 1, cy - x, cx - y - 1, cy - y);
307 if (!tex) {
DBG_D <<
"null blit";
return; }
308 DBG_D <<
"blit " << dst;
315 if (!tex) {
DBG_D <<
"null blit";
return; }
318 SDL_RenderCopy(
renderer(), tex, tex.
src(),
nullptr);
322 static SDL_RendererFlip
get_flip(
bool flip_h,
bool flip_v)
325 return static_cast<SDL_RendererFlip
>(
326 static_cast<int>(flip_h ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE)
327 |
static_cast<int>(flip_v ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE)
341 if (!tex) {
DBG_D <<
"null flipped";
return; }
342 DBG_D <<
"flipped (" << flip_h <<
'|' << flip_v
345 SDL_RendererFlip flip =
get_flip(flip_h, flip_v);
346 SDL_RenderCopyEx(
renderer(), tex, tex.
src(), &dst, 0.0,
nullptr, flip);
351 if (!tex) {
DBG_D <<
"null flipped";
return; }
352 DBG_D <<
"flipped (" << flip_h <<
'|' << flip_v <<
')';
354 SDL_RendererFlip flip =
get_flip(flip_h, flip_v);
355 SDL_RenderCopyEx(
renderer(), tex, tex.
src(),
nullptr, 0.0,
nullptr, flip);
363 if (!tex) {
DBG_D <<
"null tiled";
return; }
364 DBG_D <<
"tiled (" << centered <<
'|' << mirrored
370 const int xoff = centered ? (dst.w - tex.
w()) / 2 : 0;
371 const int yoff = centered ? (dst.h - tex.
h()) / 2 : 0;
375 SDL_Rect
t{dst.x - xoff, dst.y - yoff, tex.
w(), tex.
h()};
376 for (;
t.y < dst.y + dst.h;
t.y +=
t.h, vf = !vf) {
378 for (
t.x = dst.x - xoff;
t.x < dst.x + dst.w;
t.x +=
t.w, hf = !hf) {
389 bool centered,
bool mirrored)
391 if (!tex) {
DBG_D <<
"null tiled_highres";
return; }
392 DBG_D <<
"tiled_highres (" << centered <<
'|' << mirrored
403 const float xoff = centered ? (dst.w -
w) / 2 : 0.0f;
404 const float yoff = centered ? (dst.h -
h) / 2 : 0.0f;
408 SDL_FRect
t{dst.x - xoff, dst.y - yoff,
w,
h};
409 for (;
t.y < dst.y + dst.h;
t.y +=
t.h, vf = !vf) {
411 for (
t.x = dst.x - xoff;
t.x < dst.x + dst.w;
t.x +=
t.w, hf = !hf) {
413 SDL_RendererFlip flip =
get_flip(hf, vf);
414 SDL_RenderCopyExF(
renderer(), tex,
nullptr, &
t, 0.0,
nullptr, flip);
416 SDL_RenderCopyF(
renderer(), tex,
nullptr, &
t);
460 WRN_D <<
"trying to force clip will null renderer";
463 DBG_D <<
"forcing clip to " << clip;
465 SDL_RenderSetClipRect(
renderer(), &clip);
475 if (!SDL_RenderIsClipEnabled(
renderer())) {
480 SDL_RenderGetClipRect(
renderer(), &clip);
489 return SDL_RenderIsClipEnabled(
renderer());
497 SDL_RenderSetClipRect(
renderer(),
nullptr);
498 DBG_D <<
"clip disabled";
506 if (!SDL_RenderIsClipEnabled(
renderer())) {
510 SDL_RenderGetClipRect(
renderer(), &clip);
511 return clip.w <= 0 || clip.h <= 0;
524 c_.x +
v_.x - view.x,
525 c_.y +
v_.y - view.y,
550 WRN_D <<
"trying to force viewport will null renderer";
553 DBG_D <<
"forcing viewport to " << viewport;
555 SDL_RenderSetViewport(
renderer(), &viewport);
561 WRN_D <<
"no renderer available to get viewport";
566 SDL_RenderGetViewport(
renderer(), &viewport);
580 assert(
t.get_access() == SDL_TEXTUREACCESS_TARGET);
583 WRN_D <<
"can't set render target with null renderer";
596 WRN_D <<
"can't reset render target with null renderer";
600 SDL_RenderSetViewport(
renderer(), &viewport_);
605 DBG_D <<
"setting render target to "
606 <<
t.w() <<
'x' <<
t.h() <<
" texture";
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 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.
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 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.
The basic class for representing 8-bit RGB or RGBA colour values.
An abstract description of a rectangle with integer coordinates.