Author: mordante
Date: Thu Apr 10 19:27:43 2008
New Revision: 25739
URL: http://svn.gna.org/viewcvs/wesnoth?rev=25739&view=rev
Log:
Refactoring, move the classes decending from tshape from canvas.hpp to
canvas.cpp. They are an implementation detail, this allows some more
interesting implementation changes later.
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=25739&r1=25738&r2=25739&view=diff
==============================================================================
--- trunk/src/gui/widgets/canvas.cpp (original)
+++ trunk/src/gui/widgets/canvas.cpp Thu Apr 10 19:27:43 2008
@@ -125,177 +125,37 @@
namespace gui2{
-tcanvas::tcanvas() :
- shapes_(),
- w_(0),
- h_(0),
- canvas_(),
- variables_(),
- dirty_(true)
-{
-}
-
-tcanvas::tcanvas(const config& cfg) :
- shapes_(),
- w_(0),
- h_(0),
- canvas_(),
- variables_(),
- dirty_(true)
-{
- parse_cfg(cfg);
-}
-
-void tcanvas::draw(const config& cfg)
-{
- parse_cfg(cfg);
- draw(true);
-}
-
-void tcanvas::draw(const bool force)
-{
- log_scope2(gui_draw, "Canvas: drawing.");
- if(!dirty_ && !force) {
- DBG_G_D << "Canvas: nothing to draw.\n";
- return;
- }
-
- if(dirty_) {
- variables_.add("width",variant(w_));
- variables_.add("height",variant(h_));
- }
-
- // set surface sizes (do nothing now)
-#if 0
- if(fixme test whether -> can be used otherwise we crash w_ !=
canvas_->w || h_ != canvas_->h) {
- // create new
-
- } else {
- // fill current
- }
-#endif
- // instead we overwrite the entire thing for now
- DBG_G_D << "Canvas: create new empty canvas.\n";
- canvas_.assign(SDL_CreateRGBSurface(SDL_SWSURFACE, w_, h_, 32,
0xFF0000, 0xFF00, 0xFF, 0xFF000000));
-
- // draw items
- for(std::vector<tshape_ptr>::iterator itor =
- shapes_.begin(); itor != shapes_.end(); ++itor) {
- log_scope2(gui_draw, "Canvas: draw shape.");
-
- (*itor)->draw(canvas_, variables_);
- }
-
- dirty_ = false;
-}
-
-void tcanvas::parse_cfg(const config& cfg)
-{
- log_scope2(gui_parse, "Canvas: parsing config.");
- shapes_.clear();
-
- for(config::all_children_iterator itor =
- cfg.ordered_begin(); itor != cfg.ordered_end(); ++itor)
{
-
- const std::string& type = *((*itor).first);;
- const config& data = *((*itor).second);
-
- DBG_G_P << "Canvas: found shape of the type " << type << ".\n";
-
- if(type == "line") {
- shapes_.push_back(new tline(data));
- } else if(type == "rectangle") {
- shapes_.push_back(new trectangle(data));
- } else if(type == "image") {
- shapes_.push_back(new timage(data));
- } else if(type == "text") {
- shapes_.push_back(new ttext(data));
- } else {
- ERR_G_P << "Canvas: found a shape of an invalid type "
<< type << ".\n";
- assert(false); // FIXME remove in production code.
- }
- }
-}
-
-void tcanvas::tshape::put_pixel(ptrdiff_t start, Uint32 colour, unsigned w,
unsigned x, unsigned y)
-{
- // fixme the 4 is true due to Uint32..
- *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)
-{
- colour = SDL_MapRGBA(canvas->format,
- ((colour & 0xFF000000) >> 24),
- ((colour & 0x00FF0000) >> 16),
- ((colour & 0x0000FF00) >> 8),
- ((colour & 0x000000FF)));
-
- ptrdiff_t start = reinterpret_cast<ptrdiff_t>(canvas->pixels);
- unsigned w = canvas->w;
-
- DBG_G_D << "Shape: draw line from "
- << x1 << ',' << y1 << " to " << x2 << ',' << y2
- << " canvas width " << w << " canvas height "
- << canvas->h << ".\n";
-
- assert(x1 < canvas->w);
- assert(x2 < canvas->w);
- assert(y1 < canvas->h);
- assert(y2 < canvas->h);
-
- // use a special case for vertical lines
- if(x1 == x2) {
- if(y2 < y1) {
- std::swap(y1, y2);
- }
-
- for(unsigned y = y1; y <= y2; ++y) {
- put_pixel(start, colour, w, x1, y);
- }
- return;
- }
-
- // use a special case for horizontal lines
- if(y1 == y2) {
- for(unsigned x = x1; x <= x2; ++x) {
- put_pixel(start, colour, w, x, y1);
- }
- return;
- }
-
- // draw based on Bresenham on wikipedia
- int dx = x2 - x1;
- int dy = y2 - y1;
- int slope = 1;
- if (dy < 0) {
- slope = -1;
- dy = -dy;
- }
-
- // Bresenham constants
- int incE = 2 * dy;
- int incNE = 2 * dy - 2 * dx;
- int d = 2 * dy - dx;
- int y = y1;
-
- // Blit
- for (unsigned x = x1; x <= x2; ++x) {
- put_pixel(start, colour, w, x, y);
- if (d <= 0) {
- d += incE;
- } else {
- d += incNE;
- y += slope;
- }
- }
-}
-
-tcanvas::tline::tline(const config& cfg) :
+namespace {
+
+//! Definition of a line shape.
+class tline : public tcanvas::tshape
+{
+public:
+ tline(const config& cfg);
+
+ //! Implement shape::draw().
+ void draw(surface& canvas,
+ const game_logic::map_formula_callable& variables);
+
+private:
+ unsigned x1_, y1_;
+ unsigned x2_, y2_;
+
+ std::string
+ x1_formula_,
+ y1_formula_,
+ x2_formula_,
+ y2_formula_;
+
+ 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.)
+ unsigned thickness_;
+};
+
+tline::tline(const config& cfg) :
x1_(0),
y1_(0),
x2_(0),
@@ -397,7 +257,7 @@
}
-void tcanvas::tline::draw(surface& canvas,
+void tline::draw(surface& canvas,
const game_logic::map_formula_callable& variables)
{
//@todo formulas are now recalculated every draw cycle which is a
@@ -446,7 +306,37 @@
}
-tcanvas::trectangle::trectangle(const config& cfg) :
+
+
+//! Definition of a rectangle shape.
+class trectangle : public tcanvas::tshape
+{
+public:
+ trectangle(const config& cfg);
+
+ //! Implement shape::draw().
+ void draw(surface& canvas,
+ const game_logic::map_formula_callable& variables);
+
+private:
+ unsigned x_, y_;
+ unsigned w_, h_;
+
+ std::string
+ x_formula_,
+ y_formula_,
+ w_formula_,
+ h_formula_;
+
+ //! Border thickness if 0 the fill colour is used for the entire
+ //! widget.
+ unsigned border_thickness_;
+ Uint32 border_colour_;
+
+ Uint32 fill_colour_;
+};
+
+trectangle::trectangle(const config& cfg) :
x_(0),
y_(0),
w_(0),
@@ -496,7 +386,7 @@
}
}
-void tcanvas::trectangle::draw(surface& canvas,
+void trectangle::draw(surface& canvas,
const game_logic::map_formula_callable& variables)
{
@@ -585,7 +475,36 @@
*/
}
-tcanvas::timage::timage(const config& cfg) :
+
+
+//! Definition of an image shape.
+class timage : public tcanvas::tshape
+{
+public:
+ timage(const config& cfg);
+
+ //! Implement shape::draw().
+ void draw(surface& canvas,
+ const game_logic::map_formula_callable& variables);
+
+private:
+ unsigned x_, y_;
+ unsigned w_, h_;
+
+ std::string
+ x_formula_,
+ y_formula_,
+ w_formula_,
+ h_formula_;
+
+ SDL_Rect src_clip_;
+ SDL_Rect dst_clip_;
+ surface image_;
+
+ bool stretch_;
+};
+
+timage::timage(const config& cfg) :
x_(0),
y_(0),
w_(0),
@@ -638,7 +557,7 @@
}
}
-void tcanvas::timage::draw(surface& canvas,
+void timage::draw(surface& canvas,
const game_logic::map_formula_callable& variables)
{
DBG_G_D << "Image: draw.\n";
@@ -714,7 +633,35 @@
SDL_BlitSurface(surf, &src_clip, canvas, &dst_clip);
}
-tcanvas::ttext::ttext(const config& cfg) :
+
+
+//! Definition of a text shape.
+class ttext : public tcanvas::tshape
+{
+public:
+ ttext(const config& cfg);
+
+ //! Implement shape::draw().
+ void draw(surface& canvas,
+ const game_logic::map_formula_callable& variables);
+
+private:
+ unsigned x_, y_;
+ unsigned w_, h_;
+
+ std::string
+ x_formula_,
+ y_formula_,
+ w_formula_,
+ h_formula_;
+
+ unsigned font_size_;
+ Uint32 colour_;
+ t_string text_;
+ std::string text_formula_;
+};
+
+ttext::ttext(const config& cfg) :
x_(0),
y_(0),
w_(0),
@@ -766,7 +713,7 @@
}
}
-void tcanvas::ttext::draw(surface& canvas,
+void ttext::draw(surface& canvas,
const game_logic::map_formula_callable& variables)
{
@@ -844,4 +791,181 @@
SDL_BlitSurface(surf, 0, canvas, &dst);
}
+
+
+} // namespace
+
+
+
+tcanvas::tcanvas() :
+ shapes_(),
+ w_(0),
+ h_(0),
+ canvas_(),
+ variables_(),
+ dirty_(true)
+{
+}
+
+tcanvas::tcanvas(const config& cfg) :
+ shapes_(),
+ w_(0),
+ h_(0),
+ canvas_(),
+ variables_(),
+ dirty_(true)
+{
+ parse_cfg(cfg);
+}
+
+void tcanvas::draw(const config& cfg)
+{
+ parse_cfg(cfg);
+ draw(true);
+}
+
+void tcanvas::draw(const bool force)
+{
+ log_scope2(gui_draw, "Canvas: drawing.");
+ if(!dirty_ && !force) {
+ DBG_G_D << "Canvas: nothing to draw.\n";
+ return;
+ }
+
+ if(dirty_) {
+ variables_.add("width",variant(w_));
+ variables_.add("height",variant(h_));
+ }
+
+ // set surface sizes (do nothing now)
+#if 0
+ if(fixme test whether -> can be used otherwise we crash w_ !=
canvas_->w || h_ != canvas_->h) {
+ // create new
+
+ } else {
+ // fill current
+ }
+#endif
+ // instead we overwrite the entire thing for now
+ DBG_G_D << "Canvas: create new empty canvas.\n";
+ canvas_.assign(SDL_CreateRGBSurface(SDL_SWSURFACE, w_, h_, 32,
0xFF0000, 0xFF00, 0xFF, 0xFF000000));
+
+ // draw items
+ for(std::vector<tshape_ptr>::iterator itor =
+ shapes_.begin(); itor != shapes_.end(); ++itor) {
+ log_scope2(gui_draw, "Canvas: draw shape.");
+
+ (*itor)->draw(canvas_, variables_);
+ }
+
+ dirty_ = false;
+}
+
+void tcanvas::parse_cfg(const config& cfg)
+{
+ log_scope2(gui_parse, "Canvas: parsing config.");
+ shapes_.clear();
+
+ for(config::all_children_iterator itor =
+ cfg.ordered_begin(); itor != cfg.ordered_end(); ++itor)
{
+
+ const std::string& type = *((*itor).first);;
+ const config& data = *((*itor).second);
+
+ DBG_G_P << "Canvas: found shape of the type " << type << ".\n";
+
+ if(type == "line") {
+ shapes_.push_back(new tline(data));
+ } else if(type == "rectangle") {
+ shapes_.push_back(new trectangle(data));
+ } else if(type == "image") {
+ shapes_.push_back(new timage(data));
+ } else if(type == "text") {
+ shapes_.push_back(new ttext(data));
+ } else {
+ ERR_G_P << "Canvas: found a shape of an invalid type "
<< type << ".\n";
+ assert(false); // FIXME remove in production code.
+ }
+ }
+}
+
+void tcanvas::tshape::put_pixel(ptrdiff_t start, Uint32 colour, unsigned w,
unsigned x, unsigned y)
+{
+ // fixme the 4 is true due to Uint32..
+ *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)
+{
+ colour = SDL_MapRGBA(canvas->format,
+ ((colour & 0xFF000000) >> 24),
+ ((colour & 0x00FF0000) >> 16),
+ ((colour & 0x0000FF00) >> 8),
+ ((colour & 0x000000FF)));
+
+ ptrdiff_t start = reinterpret_cast<ptrdiff_t>(canvas->pixels);
+ unsigned w = canvas->w;
+
+ DBG_G_D << "Shape: draw line from "
+ << x1 << ',' << y1 << " to " << x2 << ',' << y2
+ << " canvas width " << w << " canvas height "
+ << canvas->h << ".\n";
+
+ assert(x1 < canvas->w);
+ assert(x2 < canvas->w);
+ assert(y1 < canvas->h);
+ assert(y2 < canvas->h);
+
+ // use a special case for vertical lines
+ if(x1 == x2) {
+ if(y2 < y1) {
+ std::swap(y1, y2);
+ }
+
+ for(unsigned y = y1; y <= y2; ++y) {
+ put_pixel(start, colour, w, x1, y);
+ }
+ return;
+ }
+
+ // use a special case for horizontal lines
+ if(y1 == y2) {
+ for(unsigned x = x1; x <= x2; ++x) {
+ put_pixel(start, colour, w, x, y1);
+ }
+ return;
+ }
+
+ // draw based on Bresenham on wikipedia
+ int dx = x2 - x1;
+ int dy = y2 - y1;
+ int slope = 1;
+ if (dy < 0) {
+ slope = -1;
+ dy = -dy;
+ }
+
+ // Bresenham constants
+ int incE = 2 * dy;
+ int incNE = 2 * dy - 2 * dx;
+ int d = 2 * dy - dx;
+ int y = y1;
+
+ // Blit
+ for (unsigned x = x1; x <= x2; ++x) {
+ put_pixel(start, colour, w, x, y);
+ if (d <= 0) {
+ d += incE;
+ } else {
+ d += incNE;
+ y += slope;
+ }
+ }
+}
+
+
} // namespace gui2
Modified: trunk/src/gui/widgets/canvas.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/canvas.hpp?rev=25739&r1=25738&r2=25739&view=diff
==============================================================================
--- trunk/src/gui/widgets/canvas.hpp (original)
+++ trunk/src/gui/widgets/canvas.hpp Thu Apr 10 19:27:43 2008
@@ -48,6 +48,7 @@
public:
//! Base class for all other shapes.
+ //! The other shapes are declared and defined in canvas.cpp.
class tshape : public reference_counted_object
{
public:
@@ -67,115 +68,6 @@
typedef boost::intrusive_ptr<tshape> tshape_ptr;
typedef boost::intrusive_ptr<const tshape> const_tshape_ptr;
-
- //! Definition of a line shape.
- class tline : public tshape
- {
- public:
- tline(const config& cfg);
-
- //! Implement shape::draw().
- void draw(surface& canvas,
- const game_logic::map_formula_callable& variables);
-
- private:
- unsigned x1_, y1_;
- unsigned x2_, y2_;
-
- std::string
- x1_formula_,
- y1_formula_,
- x2_formula_,
- y2_formula_;
-
- 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.)
- unsigned thickness_;
- };
-
- //! Definition of a rectangle shape.
- class trectangle : public tshape
- {
- public:
- trectangle(const config& cfg);
-
- //! Implement shape::draw().
- void draw(surface& canvas,
- const game_logic::map_formula_callable& variables);
-
- private:
- unsigned x_, y_;
- unsigned w_, h_;
-
- std::string
- x_formula_,
- y_formula_,
- w_formula_,
- h_formula_;
-
- //! Border thickness if 0 the fill colour is used for the
entire
- //! widget.
- unsigned border_thickness_;
- Uint32 border_colour_;
-
- Uint32 fill_colour_;
- };
-
- //! Definition of an image shape.
- class timage : public tshape
- {
- public:
- timage(const config& cfg);
-
- //! Implement shape::draw().
- void draw(surface& canvas,
- const game_logic::map_formula_callable& variables);
-
- private:
- unsigned x_, y_;
- unsigned w_, h_;
-
- std::string
- x_formula_,
- y_formula_,
- w_formula_,
- h_formula_;
-
- SDL_Rect src_clip_;
- SDL_Rect dst_clip_;
- surface image_;
-
- bool stretch_;
- };
-
- //! Definition of a text shape.
- class ttext : public tshape
- {
- public:
- ttext(const config& cfg);
-
- //! Implement shape::draw().
- void draw(surface& canvas,
- const game_logic::map_formula_callable& variables);
-
- private:
- unsigned x_, y_;
- unsigned w_, h_;
-
- std::string
- x_formula_,
- y_formula_,
- w_formula_,
- h_formula_;
-
- unsigned font_size_;
- Uint32 colour_;
- t_string text_;
- std::string text_formula_;
- };
tcanvas();
tcanvas(const config& cfg);
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits