Author: mordante
Date: Tue Jun 17 21:43:33 2008
New Revision: 27267
URL: http://svn.gna.org/viewcvs/wesnoth?rev=27267&view=rev
Log:
Minor cleanups and added more comment.
Modified:
trunk/src/gui/widgets/canvas.cpp
trunk/src/gui/widgets/canvas.hpp
Modified: trunk/src/gui/widgets/canvas.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/canvas.cpp?rev=27267&r1=27266&r2=27267&view=diff
==============================================================================
--- trunk/src/gui/widgets/canvas.cpp (original)
+++ trunk/src/gui/widgets/canvas.cpp Tue Jun 17 21:43:33 2008
@@ -12,8 +12,10 @@
See the COPYING file for more details.
*/
-//! @file canvas.cpp
-//! Implementation of canvas.hpp.
+/**
+ * @file canvas.cpp
+ * Implementation of canvas.hpp.
+ */
#include "gui/widgets/canvas.hpp"
@@ -24,8 +26,8 @@
#include "gui/widgets/formula.hpp"
#include "gui/widgets/helper.hpp"
#include "log.hpp"
-#include "sdl_utils.hpp"
#include "serialization/parser.hpp"
+#include "variant.hpp"
#include "wml_exception.hpp"
#include <algorithm>
@@ -51,30 +53,47 @@
#define WRN_G_P LOG_STREAM_INDENT(warn, gui_parse)
#define ERR_G_P LOG_STREAM_INDENT(err, gui_parse)
-namespace gui2{
-
-//! Definition of a line shape.
+namespace gui2 {
+
+namespace {
+
+/***** ***** ***** ***** ***** LINE ***** ***** ***** ***** *****/
+
+/** Definition of a line shape. */
class tline : public tcanvas::tshape
{
public:
+
+ /**
+ * Constructor.
+ *
+ * @param cfg The config object to define the line see
+ *
http://www.wesnoth.org/wiki/GUICanvasWML#Line
+ * for more info.
+ */
tline(const config& cfg);
- //! Implement shape::draw().
+ /** Implement shape::draw(). */
void draw(surface& canvas,
const game_logic::map_formula_callable& variables);
private:
tformula<unsigned>
- x1_,
- y1_,
- x2_,
- y2_;
-
+ x1_, /**< The start x coordinate of the line. */
+ y1_, /**< The start y coordinate of the line. */
+ x2_, /**< The end x coordinate of the line. */
+ y2_; /**< The end y coordinate of the line. */
+
+ /** The colour of the line. */
Uint32 colour_;
- //! The thickness of the line:
- //! if the value is odd the x and y are the middle of the line.
- //! if the value is even the x and y are the middle of a line
- //! with width - 1. (0 is special case, does nothing.)
+
+ /**
+ * The thickness of the line.
+ *
+ * if the value is odd the x and y are the middle of the line.
+ * if the value is even the x and y are the middle of a line
+ * with width - 1. (0 is special case, does nothing.)
+ */
unsigned thickness_;
};
@@ -235,9 +254,11 @@
void tline::draw(surface& canvas,
const game_logic::map_formula_callable& variables)
{
- //@todo formulas are now recalculated every draw cycle which is a
- // bit silly unless there has been a resize. So to optimize we should
- // use an extra flag or do the calculation in a separate routine.
+ /**
+ * @todo formulas are now recalculated every draw cycle which is a bit
silly
+ * unless there has been a resize. So to optimize we should use an extra
+ * flag or do the calculation in a separate routine.
+ */
const unsigned x1 = x1_(variables);
const unsigned y1 = y1_(variables);
@@ -254,7 +275,7 @@
static_cast<int>(y2) < canvas->h,
_("Line doesn't fit on canvas."));
- // FIXME respect the thickness.
+ // @todo FIXME respect the thickness.
// now draw the line we use Bresenham's algorithm, which doesn't
// support antialiasing. The advantage is that it's easy for testing.
@@ -269,30 +290,52 @@
}
}
-
-
-//! Definition of a rectangle shape.
+/***** ***** ***** ***** ***** Rectangle ***** ***** ***** ***** *****/
+
+/** Definition of a rectangle shape. */
class trectangle : public tcanvas::tshape
{
public:
+
+ /**
+ * Constructor.
+ *
+ * @param cfg The config object to define the rectangle
see
+ *
http://www.wesnoth.org/wiki/GUICanvasWML#Rectangle
+ * for more info.
+ */
trectangle(const config& cfg);
- //! Implement shape::draw().
+ /** Implement shape::draw(). */
void draw(surface& canvas,
const game_logic::map_formula_callable& variables);
private:
tformula<unsigned>
- x_,
- y_,
- w_,
- h_;
-
- //! Border thickness if 0 the fill colour is used for the entire
- //! widget.
+ x_, /**< The x coordinate of the rectangle. */
+ y_, /**< The y coordinate of the rectangle. */
+ w_, /**< The width of the rectangle. */
+ h_; /**< The height of the rectangle. */
+
+ /**
+ * Border thickness.
+ *
+ * If 0 the fill colour is used for the entire widget.
+ */
unsigned border_thickness_;
+
+ /**
+ * The border colour of the rectangle.
+ *
+ * If the colour is fully transparent the border isn't drawn.
+ */
Uint32 border_colour_;
+ /**
+ * The border colour of the rectangle.
+ *
+ * If the colour is fully transparent the rectangle won't be filled.
+ */
Uint32 fill_colour_;
};
@@ -347,9 +390,11 @@
const game_logic::map_formula_callable& variables)
{
- //@todo formulas are now recalculated every draw cycle which is a
- // bit silly unless there has been a resize. So to optimize we should
- // use an extra flag or do the calculation in a separate routine.
+ /**
+ * @todo formulas are now recalculated every draw cycle which is a bit
+ * silly unless there has been a resize. So to optimize we should use an
+ * extra flag or do the calculation in a separate routine.
+ */
const unsigned x = x_(variables);
const unsigned y = y_(variables);
const unsigned w = w_(variables);
@@ -404,42 +449,39 @@
draw_line(canvas, fill_colour_, left, i, right, i);
}
}
-/*
- const unsigned left = x_ + border_thickness_ + 1;
- const unsigned top = y_ + border_thickness_ + 1;
- const unsigned width = w_ - (2 * border_thickness_) - 2;
- const unsigned height = h_ - (2 * border_thickness_) - 2;
- SDL_Rect rect = create_rect(left, top, width, height);
-
- const Uint32 colour = fill_colour_ & 0xFFFFFF00;
- const Uint8 alpha = fill_colour_ & 0xFF;
-
- // fill
- fill_rect_alpha(rect, colour, alpha, canvas);
- canvas = blend_surface(canvas, 255, 0xAAAA00);
-*/
-}
-
-
-
-//! Definition of an image shape.
+}
+
+/***** ***** ***** ***** ***** IMAGE ***** ***** ***** ***** *****/
+
+/** Definition of an image shape. */
class timage : public tcanvas::tshape
{
public:
+
+ /**
+ * Constructor.
+ *
+ * @param cfg The config object to define the image see
+ *
http://www.wesnoth.org/wiki/GUICanvasWML#Image
+ * for more info.
+ */
timage(const config& cfg);
- //! Implement shape::draw().
+ /** Implement shape::draw(). */
void draw(surface& canvas,
const game_logic::map_formula_callable& variables);
private:
tformula<unsigned>
- x_,
- y_,
- w_,
- h_;
-
+ x_, /**< The x coordinate of the image. */
+ y_, /**< The y coordinate of the image. */
+ w_, /**< The width of the image. */
+ h_; /**< The height of the image. */
+
+ /** Contains the size of the image. */
SDL_Rect src_clip_;
+
+ /** The image is cached in this surface. */
surface image_;
/**
@@ -451,6 +493,11 @@
*/
tformula<std::string> image_name_;
+ /**
+ * When an image needs to be scaled in one direction there are two
options:
+ * - scale, which interpolates the image.
+ * - stretch, which used the first row/column and copies those pixels.
+ */
bool stretch_;
};
@@ -517,9 +564,11 @@
{
DBG_G_D << "Image: draw.\n";
- //@todo formulas are now recalculated every draw cycle which is a
- // bit silly unless there has been a resize. So to optimize we should
- // use an extra flag or do the calculation in a separate routine.
+ /**
+ * @todo formulas are now recalculated every draw cycle which is a bit
+ * silly unless there has been a resize. So to optimize we should use an
+ * extra flag or do the calculation in a separate routine.
+ */
if(image_name_.has_formula()) {
const std::string& name = image_name_(variables);
@@ -596,27 +645,43 @@
blit_surface(surf, &src_clip, canvas, &dst_clip);
}
-//! Definition of a text shape.
+/***** ***** ***** ***** ***** TEXT ***** ***** ***** ***** *****/
+
+/** Definition of a text shape. */
class ttext : public tcanvas::tshape
{
public:
+
+ /**
+ * Constructor.
+ *
+ * @param cfg The config object to define the text see
+ *
http://www.wesnoth.org/wiki/GUICanvasWML#Text
+ * for more info.
+ */
ttext(const config& cfg);
- //! Implement shape::draw().
+ /** Implement shape::draw(). */
void draw(surface& canvas,
const game_logic::map_formula_callable& variables);
private:
tformula<unsigned>
- x_,
- y_,
- w_,
- h_;
-
+ x_, /**< The x coordinate of the text. */
+ y_, /**< The y coordinate of the text. */
+ w_, /**< The width of the text. */
+ h_; /**< The height of the text. */
+
+ /** The font size of the text. */
unsigned font_size_;
+
+ /** The style of the text. */
int font_style_;
+
+ /** The colour of the text. */
Uint32 colour_;
+ /** The text to draw. */
tformula<t_string> text_;
};
@@ -722,6 +787,10 @@
blit_surface(surf, 0, canvas, &dst);
}
+} // namespace
+
+/***** ***** ***** ***** ***** CANVAS ***** ***** ***** ***** *****/
+
tcanvas::tcanvas() :
shapes_(),
w_(0),
@@ -805,14 +874,13 @@
}
}
+/***** ***** ***** ***** ***** SHAPE ***** ***** ***** ***** *****/
+
void tcanvas::tshape::put_pixel(ptrdiff_t start, Uint32 colour, unsigned w,
unsigned x, unsigned y)
{
*reinterpret_cast<Uint32*>(start + (y * w * 4) + x * 4) = colour;
}
-// the surface should be locked
-// the colour should be a const and the value send should already
-// be good for the wanted surface
void tcanvas::tshape::draw_line(surface& canvas, Uint32 colour,
const unsigned x1, unsigned y1, const unsigned x2, unsigned y2)
{
@@ -882,5 +950,5 @@
}
}
-
} // namespace gui2
+
Modified: trunk/src/gui/widgets/canvas.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/canvas.hpp?rev=27267&r1=27266&r2=27267&view=diff
==============================================================================
--- trunk/src/gui/widgets/canvas.hpp (original)
+++ trunk/src/gui/widgets/canvas.hpp Tue Jun 17 21:43:33 2008
@@ -12,9 +12,11 @@
See the COPYING file for more details.
*/
-//! @file canvas.hpp
-//! This file contains the canvas object which is the part where the widgets
-//! draw (tempory) images on.
+/**
+ * @file canvas.hpp
+ * This file contains the canvas object which is the part where the widgets
+ * draw (tempory) images on.
+ */
#ifndef GUI_WIDGETS_CANVAS_HPP_INCLUDED
#define GUI_WIDGETS_CANVAS_HPP_INCLUDED
@@ -22,43 +24,85 @@
#include "formula_callable.hpp"
#include "reference_counted_object.hpp"
#include "sdl_utils.hpp"
-#include "tstring.hpp"
-#include "variant.hpp"
#include <vector>
class config;
class surface;
+class variant;
namespace gui2 {
-//! Base class for the canvas which allows drawing, a later version may
implement
-//! a cache which allows the same scripts with the same input to store their
-//! output surface. But that will be looked into later.
-
-
-//! The copy constructor does a shallow copy of the shapes to draw.
-//! a clone() will be implemented if really needed.
-
-// maybe inherit from surface...
+/**
+ * A simple canvas which can be drawn upon.
+ *
+ * The class has a config which contains what to draw.
+ *
+ * NOTE we might add some caching in a later state, for now every draw cycle
+ * does a full redraw.
+ *
+ * The copy constructor does a shallow copy of the shapes to draw.
+ * a clone() will be implemented if really needed.
+ */
class tcanvas
{
public:
- //! Base class for all other shapes.
- //! The other shapes are declared and defined in canvas.cpp.
+ /**
+ * Abstract base class for all other shapes.
+ *
+ * The other shapes are declared and defined in canvas.cpp, since the
+ * implementation details are not interesting for users of the canvas.
+ */
class tshape : public reference_counted_object
{
public:
+ virtual ~tshape() {}
+
+ /**
+ * Draws the canvas.
+ *
+ * @param canvas The resulting image will be blitted
upon this
+ * canvas.
+ * @param variables The canvas can have formulas in it's
+ * definition, this parameter contains
the values
+ * for these formulas.
+ */
virtual void draw(surface& canvas,
const game_logic::map_formula_callable& variables) = 0;
- virtual ~tshape() {}
protected:
- // draw basic primitives
-
+ /***** ***** ***** ***** drawing primitives ***** ***** *****
*****/
+
+ /**
+ * Draws a single pixel.
+ *
+ * A rather unoptimized single pixel draw routine.
+ * @todo we're unoptimized maybe we should be removed and
replaced by
+ * something more efficient, making us an inlined non-member
function
+ * will probably already help.
+ *
+ * @param start The memory address which is the start
of the
+ * surface buffer to draw in.
+ * @param colour The colour of the pixel to draw.
+ * @param w The width of the surface.
+ * @param x The x coordinate of the pixel to draw.
+ * @param y The y coordinate of the pixel to draw.
+ */
void put_pixel(ptrdiff_t start, Uint32 colour, unsigned w,
unsigned x, unsigned y);
+
+ /**
+ * Draws a line
+ *
+ * @param canvas The canvas to draw upon, the caller
should
+ * lock the surface before calling.
+ * @param colour The colour 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& canvas, Uint32 colour,
const unsigned x1, unsigned y1, const unsigned x2,
unsigned y2);
@@ -68,10 +112,42 @@
typedef boost::intrusive_ptr<const tshape> const_tshape_ptr;
tcanvas();
+
+ /**
+ * Constructor.
+ *
+ * @param cfg The config object with the data to draw,
see
+ * http://www.wesnoth.org/wiki/GUICanvasWML
+ */
tcanvas(const config& cfg);
+ /**
+ * Draws the canvas.
+ *
+ * Sets the config of the canvas to the one send and does a forced
redraw.
+ *
+ * @param cfg The config object with the data to draw,
see
+ * http://www.wesnoth.org/wiki/GUICanvasWML
+ */
void draw(const config& cfg);
+
+ /**
+ * Draws the canvas.
+ *
+ * @param force If the canvas isn't dirty it isn't redrawn
+ * unless force is set to true.
+ */
void draw(const bool force = false);
+
+ /**
+ * Sets the config.
+ *
+ * @param cfg The config object with the data to draw,
see
+ * http://www.wesnoth.org/wiki/GUICanvasWML
+ */
+ void set_cfg(const config& cfg) { parse_cfg(cfg); }
+
+ /***** ***** ***** setters / getters for members ***** ****** *****/
void set_width(const unsigned width) { w_ = width; set_dirty(); }
unsigned get_width() const { return w_; }
@@ -81,30 +157,44 @@
surface& surf() { return canvas_; }
- void set_cfg(const config& cfg) { parse_cfg(cfg); }
-
void set_variable(const std::string& key, const variant& value)
{ variables_.add(key, value); }
private:
+ /** Vector with the shapes to draw. */
+ std::vector<tshape_ptr> shapes_;
+
+ /** Width of the canvas. */
+ unsigned w_;
+
+ /** Height ot the canvas. */
+ unsigned h_;
+
+ /** The surface we draw all items on. */
+ surface canvas_;
+
+ /** The variables of the canvas. */
+ game_logic::map_formula_callable variables_;
+
+ /** The dirty state of the canvas. */
+ bool dirty_;
+
void set_dirty(const bool dirty = true) { dirty_ = dirty; }
+ /**
+ * Parses a config object.
+ *
+ * The config object is parsed and serialized by this function after
which
+ * the config object is no longer required and thus not stored in the
+ * object.
+ *
+ * @param cfg The config object with the data to draw,
see
+ * http://www.wesnoth.org/wiki/GUICanvasWML
+ */
void parse_cfg(const config& cfg);
-
- std::vector<tshape_ptr> shapes_;
-
- unsigned w_;
- unsigned h_;
-
- //! The canvas we draw all items on.
- surface canvas_;
-
- game_logic::map_formula_callable variables_;
-
- bool dirty_;
};
} // namespace gui2
-
#endif
+
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits