Skip to content

Commit

Permalink
Moved SDL_SetRenderDrawColor wrapper to utils file and added a second…
Browse files Browse the repository at this point in the history
… overload

Also renamed the function to a more colloquial version.
  • Loading branch information
Vultraz committed Mar 18, 2018
1 parent 028b4ee commit 5e35f6d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
19 changes: 7 additions & 12 deletions src/gui/core/canvas.cpp
Expand Up @@ -85,11 +85,6 @@ namespace

/***** ***** ***** ***** ***** DRAWING PRIMITIVES ***** ***** ***** ***** *****/

static void set_renderer_color(SDL_Renderer* renderer, color_t color)
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
}

/**
* Draws a line on a surface.
*
Expand Down Expand Up @@ -124,7 +119,7 @@ static void draw_line(const int canvas_w,
assert(static_cast<int>(y1) < canvas_h);
assert(static_cast<int>(y2) < canvas_h);

set_renderer_color(renderer, color);
set_draw_color(renderer, color);

if(x1 == x2 && y1 == y2) {
// Handle single-pixel lines properly
Expand Down Expand Up @@ -168,7 +163,7 @@ static void draw_circle(const int canvas_w,
if(octants & 0x3c) assert((y_center + radius) < canvas_h);
if(octants & 0xc3) assert((y_center - radius) >= 0);

set_renderer_color(renderer, color);
set_draw_color(renderer, color);

// Algorithm based on
// http://de.wikipedia.org/wiki/Rasterung_von_Kreisen#Methode_von_Horn
Expand Down Expand Up @@ -235,7 +230,7 @@ static void fill_circle(const int canvas_w,
if(octants & 0x3c) assert((y_center + radius) < canvas_h);
if(octants & 0xc3) assert((y_center - radius) >= 0);

set_renderer_color(renderer, color);
set_draw_color(renderer, color);

int d = -static_cast<int>(radius);
int x = radius;
Expand Down Expand Up @@ -705,7 +700,7 @@ void rectangle_shape::draw(

// Fill the background, if applicable
if(!fill_color.null() && w && h) {
set_renderer_color(renderer, fill_color);
set_draw_color(renderer, fill_color);

SDL_Rect area {
x + border_thickness_,
Expand All @@ -726,7 +721,7 @@ void rectangle_shape::draw(
h - (i * 2)
};

set_renderer_color(renderer, border_color_(variables));
set_draw_color(renderer, border_color_(variables));

SDL_RenderDrawRect(renderer, &dimensions);
}
Expand Down Expand Up @@ -820,7 +815,7 @@ void round_rectangle_shape::draw(

// Fill the background, if applicable
if(!fill_color.null() && w && h) {
set_renderer_color(renderer, fill_color);
set_draw_color(renderer, fill_color);
static const int count = 3;
SDL_Rect area[count] {
{x + r, y + border_thickness_, w - r * 2, r - border_thickness_ + 1},
Expand All @@ -840,7 +835,7 @@ void round_rectangle_shape::draw(

// Draw the border
for(int i = 0; i < border_thickness_; ++i) {
set_renderer_color(renderer, border_color);
set_draw_color(renderer, border_color);

SDL_RenderDrawLine(renderer, x + r, y + i, x + w - r, y + i);
SDL_RenderDrawLine(renderer, x + r, y + h - i, x + w - r, y + h - i);
Expand Down
16 changes: 16 additions & 0 deletions src/sdl/render_utils.hpp
Expand Up @@ -108,6 +108,22 @@ using render_viewport_setter = render_raii_rect_setter_base<
&SDL_RenderSetViewport>;


/**
* Set renderer drawing color.
*/
inline void set_draw_color(SDL_Renderer* renderer, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
SDL_SetRenderDrawColor(renderer, r, g, b, a);
}

/**
* Set renderer drawing color.
*/
inline void set_draw_color(SDL_Renderer* renderer, color_t color)
{
set_draw_color(renderer, color.r, color.g, color.b, color.a);
}

/*
* TEXTURE SETTERS =========================================================================
* Need to decide if these need their own file.
Expand Down
5 changes: 3 additions & 2 deletions src/sdl/window.cpp
Expand Up @@ -15,8 +15,9 @@
#include "sdl/window.hpp"

#include "ogl/utils.hpp"
#include "sdl/surface.hpp"
#include "sdl/exception.hpp"
#include "sdl/render_utils.hpp"
#include "sdl/surface.hpp"

namespace sdl
{
Expand Down Expand Up @@ -122,7 +123,7 @@ void window::full_screen()

void window::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
SDL_SetRenderDrawColor(*this, r, g, b, a);
set_draw_color(*this, r, g, b, a);
if(SDL_RenderClear(*this) != 0) {
throw exception("Failed to clear the SDL_Renderer object.",
true);
Expand Down

0 comments on commit 5e35f6d

Please sign in to comment.