Skip to content

Commit

Permalink
Split drawing primitives helpers out of the GUI2 canvas and into thei…
Browse files Browse the repository at this point in the history
…r own file

This doesn't move the draw() functions in each tshape class, just three static helpers.
  • Loading branch information
Vultraz committed Sep 15, 2016
1 parent 75eda82 commit bdf5721
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 126 deletions.
4 changes: 2 additions & 2 deletions projectfiles/CodeBlocks/wesnoth.cbp
Expand Up @@ -956,13 +956,13 @@
<Unit filename="../../src/scripting/plugins/context.hpp" />
<Unit filename="../../src/scripting/plugins/manager.cpp" />
<Unit filename="../../src/scripting/plugins/manager.hpp" />
<Unit filename="../../src/sdl/alpha.cpp" />
<Unit filename="../../src/sdl/alpha.hpp" />
<Unit filename="../../src/sdl/exception.cpp" />
<Unit filename="../../src/sdl/exception.hpp" />
<Unit filename="../../src/sdl/image.cpp" />
<Unit filename="../../src/sdl/image.hpp" />
<Unit filename="../../src/sdl/rect.cpp" />
<Unit filename="../../src/sdl/renderer.cpp" />
<Unit filename="../../src/sdl/renderer.hpp" />
<Unit filename="../../src/sdl/utils.cpp" />
<Unit filename="../../src/sdl/utils.hpp" />
<Unit filename="../../src/sdl/window.cpp" />
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -394,6 +394,7 @@ set_target_properties(wesnoth-lua
set(wesnoth-sdl_SRC
sdl/exception.cpp
sdl/rect.cpp
sdl/renderer.cpp
sdl/image.cpp
sdl/window.cpp
sdl/utils.cpp
Expand Down
1 change: 1 addition & 0 deletions src/SConscript
Expand Up @@ -165,6 +165,7 @@ libwesnoth_sdl_sources = Split("""
sdl/exception.cpp
sdl/image.cpp
sdl/rect.cpp
sdl/renderer.cpp
sdl/utils.cpp
sdl/window.cpp
tracer.cpp
Expand Down
129 changes: 5 additions & 124 deletions src/gui/core/canvas.cpp
Expand Up @@ -29,6 +29,7 @@
#include "gui/core/log.hpp"
#include "gui/widgets/helper.hpp"
#include "sdl/rect.hpp"
#include "sdl/renderer.hpp"
#include "text.hpp"
#include "video.hpp"
#include "wml_exception.hpp"
Expand Down Expand Up @@ -79,126 +80,6 @@ namespace
* @end{tag}{name="pre_commit"}
*/

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

static void set_renderer_color(SDL_Renderer* renderer, Uint32 color)
{
SDL_SetRenderDrawColor(renderer,
(color & 0xFF000000) >> 24,
(color & 0x00FF0000) >> 16,
(color & 0x0000FF00) >> 8,
(color & 0x000000FF));
}

/**
* Draws a line on a surface.
*
* @pre The caller needs to make sure the entire line fits on
* the @p surface.
* @pre @p x2 >= @p x1
* @pre The @p surface is locked.
*
* @param canvas The canvas to draw upon, the caller should lock the
* surface before calling.
* @param color The color of the line to draw.
* @param x1 The start x coordinate of the line to draw.
* @param y1 The start y coordinate of the line to draw.
* @param x2 The end x coordinate of the line to draw.
* @param y2 The end y coordinate of the line to draw.
*/
static void draw_line(surface& canvas,
SDL_Renderer* renderer,
Uint32 color,
unsigned x1,
unsigned y1,
const unsigned x2,
unsigned y2)
{
unsigned w = canvas->w;

DBG_GUI_D << "Shape: draw line from " << x1 << ',' << y1 << " to " << x2
<< ',' << y2 << " canvas width " << w << " canvas height "
<< canvas->h << ".\n";

assert(static_cast<int>(x1) < canvas->w);
assert(static_cast<int>(x2) < canvas->w);
assert(static_cast<int>(y1) < canvas->h);
assert(static_cast<int>(y2) < canvas->h);

set_renderer_color(renderer, color);

if(x1 == x2 && y1 == y2) {
// Handle single-pixel lines properly
SDL_RenderDrawPoint(renderer, x1, y1);
} else {
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
}
}

/**
* Draws a circle on a surface.
*
* @pre The circle must fit on the canvas.
* @pre The @p surface is locked.
*
* @param canvas The canvas to draw upon, the caller should lock the
* surface before calling.
* @param color The color of the circle to draw.
* @param x_center The x coordinate of the center of the circle to draw.
* @param y_center The y coordinate of the center of the circle to draw.
* @param radius The radius of the circle to draw.
*/
static void draw_circle(surface& canvas,
SDL_Renderer* renderer,
Uint32 color,
const int x_center,
const int y_center,
const int radius)
{
unsigned w = canvas->w;

DBG_GUI_D << "Shape: draw circle at " << x_center << ',' << y_center
<< " with radius " << radius << " canvas width " << w
<< " canvas height " << canvas->h << ".\n";

assert(static_cast<int>(x_center + radius) < canvas->w);
assert(static_cast<int>(x_center - radius) >= 0);
assert(static_cast<int>(y_center + radius) < canvas->h);
assert(static_cast<int>(y_center - radius) >= 0);

set_renderer_color(renderer, color);

// Algorithm based on
// http://de.wikipedia.org/wiki/Rasterung_von_Kreisen#Methode_von_Horn
// version of 2011.02.07.
int d = -static_cast<int>(radius);
int x = radius;
int y = 0;

std::vector<SDL_Point> points;

while(!(y > x)) {
points.push_back({x_center + x, y_center + y});
points.push_back({x_center + x, y_center - y});
points.push_back({x_center - x, y_center + y});
points.push_back({x_center - x, y_center - y});

points.push_back({x_center + y, y_center + x});
points.push_back({x_center + y, y_center - x});
points.push_back({x_center - y, y_center + x});
points.push_back({x_center - y, y_center - x});

d += 2 * y + 1;
++y;
if(d > 0) {
d += -2 * x + 2;
--x;
}
}

SDL_RenderDrawPoints(renderer, points.data(), points.size());
}

/***** ***** ***** ***** ***** LINE ***** ***** ***** ***** *****/

/** Definition of a line shape. */
Expand Down Expand Up @@ -595,7 +476,7 @@ void tline::draw(surface& canvas,
// lock the surface
surface_lock locker(canvas);

draw_line(canvas, renderer, color_, x1, y1, x2, y2);
sdl::draw_line(canvas, renderer, color_, x1, y1, x2, y2);
}

/***** ***** ***** ***** ***** Rectangle ***** ***** ***** ***** *****/
Expand Down Expand Up @@ -732,14 +613,14 @@ void trectangle::draw(surface& canvas,
h - (i * 2)
};

set_renderer_color(renderer, border_color_);
sdl::set_renderer_color(renderer, border_color_);

SDL_RenderDrawRect(renderer, &dimensions);
}

// Fill the background, if applicable
if(fill_color_) {
set_renderer_color(renderer, fill_color_);
sdl::set_renderer_color(renderer, fill_color_);

SDL_Rect area {
x + border_thickness_,
Expand Down Expand Up @@ -862,7 +743,7 @@ void tcircle::draw(surface& canvas,
// lock the surface
surface_lock locker(canvas);

draw_circle(canvas, renderer, color_, x, y, radius);
sdl::draw_circle(canvas, renderer, color_, x, y, radius);
}

/***** ***** ***** ***** ***** IMAGE ***** ***** ***** ***** *****/
Expand Down
117 changes: 117 additions & 0 deletions src/sdl/renderer.cpp
@@ -0,0 +1,117 @@
/*
Copyright (C) 2007 - 2016 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#include "sdl/renderer.hpp"

#include "image.hpp"
#include "log.hpp"

static lg::log_domain log_sdl("sdl_renderer");
#define LOG_SDL LOG_STREAM(info, log_sdl)

namespace sdl {

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

void set_renderer_color(SDL_Renderer* renderer, Uint32 color)
{
SDL_SetRenderDrawColor(renderer,
(color & 0xFF000000) >> 24,
(color & 0x00FF0000) >> 16,
(color & 0x0000FF00) >> 8,
(color & 0x000000FF));
}

void draw_line(
surface& surface,
SDL_Renderer* renderer,
Uint32 color,
const unsigned x1,
const unsigned y1,
const unsigned x2,
const unsigned y2)
{
unsigned w = surface->w;

LOG_SDL << "Shape: draw line from " << x1 << ',' << y1 << " to " << x2
<< ',' << y2 << " surface width " << w << " surface height "
<< surface->h << ".\n";

assert(static_cast<int>(x1) < surface->w);
assert(static_cast<int>(x2) < surface->w);
assert(static_cast<int>(y1) < surface->h);
assert(static_cast<int>(y2) < surface->h);

set_renderer_color(renderer, color);

if(x1 == x2 && y1 == y2) {
// Handle single-pixel lines properly
SDL_RenderDrawPoint(renderer, x1, y1);
} else {
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
}
}

void draw_circle(
surface& surface,
SDL_Renderer* renderer,
Uint32 color,
const int x_center,
const int y_center,
const int radius)
{
unsigned w = surface->w;

LOG_SDL << "Shape: draw circle at " << x_center << ',' << y_center
<< " with radius " << radius << " surface width " << w
<< " surface height " << surface->h << ".\n";

assert(static_cast<int>(x_center + radius) < surface->w);
assert(static_cast<int>(x_center - radius) >= 0);
assert(static_cast<int>(y_center + radius) < surface->h);
assert(static_cast<int>(y_center - radius) >= 0);

set_renderer_color(renderer, color);

// Algorithm based on
// http://de.wikipedia.org/wiki/Rasterung_von_Kreisen#Methode_von_Horn
// version of 2011.02.07.
int d = -static_cast<int>(radius);
int x = radius;
int y = 0;

std::vector<SDL_Point> points;

while(!(y > x)) {
points.push_back({x_center + x, y_center + y});
points.push_back({x_center + x, y_center - y});
points.push_back({x_center - x, y_center + y});
points.push_back({x_center - x, y_center - y});

points.push_back({x_center + y, y_center + x});
points.push_back({x_center + y, y_center - x});
points.push_back({x_center - y, y_center + x});
points.push_back({x_center - y, y_center - x});

d += 2 * y + 1;
++y;
if(d > 0) {
d += -2 * x + 2;
--x;
}
}

SDL_RenderDrawPoints(renderer, points.data(), points.size());
}

}
73 changes: 73 additions & 0 deletions src/sdl/renderer.hpp
@@ -0,0 +1,73 @@
/*
Copyright (C) 2007 - 2016 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#ifndef SDL_RENDERER_INCLUDED
#define SDL_RENDERER_INCLUDED

#include "sdl/utils.hpp"

#include <SDL.h>

namespace sdl {

void set_renderer_color(SDL_Renderer* renderer, Uint32 color);

/**
* Draws a line on a surface.
*
* @pre The caller needs to make sure the entire line fits on
* the @p surface.
* @pre @p x2 >= @p x1
* @pre The @p surface is locked.
*
* @param surface The surface to draw upon, the caller should lock the
* surface before calling.
* @param color The color of the line to draw.
* @param x1 The start x coordinate of the line to draw.
* @param y1 The start y coordinate of the line to draw.
* @param x2 The end x coordinate of the line to draw.
* @param y2 The end y coordinate of the line to draw.
*/
void draw_line(
surface& surface,
SDL_Renderer* renderer,
Uint32 color,
const unsigned x1,
const unsigned y1,
const unsigned x2,
const unsigned y2);

/**
* Draws a circle on a surface.
*
* @pre The circle must fit on the surface.
* @pre The @p surface is locked.
*
* @param surface The surface to draw upon, the caller should lock the
* surface before calling.
* @param color The color of the circle to draw.
* @param x_center The x coordinate of the center of the circle to draw.
* @param y_center The y coordinate of the center of the circle to draw.
* @param radius The radius of the circle to draw.
*/
void draw_circle(
surface& surface,
SDL_Renderer* renderer,
Uint32 color,
const int x_center,
const int y_center,
const int radius);

} // namespace sdl

#endif

0 comments on commit bdf5721

Please sign in to comment.