canvas.hpp

Go to the documentation of this file.
00001 /* $Id: canvas.hpp 26650 2008-05-16 15:55:56Z mordante $ */
00002 /*
00003    Copyright (C) 2007 - 2008 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 //! @file canvas.hpp
00016 //! This file contains the canvas object which is the part where the widgets
00017 //! draw (tempory) images on.
00018 
00019 #ifndef __GUI_WIDGETS_CANVAS_HPP_INCLUDED__
00020 #define __GUI_WIDGETS_CANVAS_HPP_INCLUDED__
00021 
00022 #include "formula_callable.hpp"
00023 #include "reference_counted_object.hpp"
00024 #include "sdl_utils.hpp"
00025 #include "tstring.hpp"
00026 #include "variant.hpp"
00027 
00028 #include <vector>
00029 
00030 class config;
00031 class surface;
00032 
00033 namespace gui2 {
00034 
00035 //! Base class for the canvas which allows drawing, a later version may implement
00036 //! a cache which allows the same scripts with the same input to store their
00037 //! output surface. But that will be looked into later.
00038 
00039 
00040 //! The copy constructor does a shallow copy of the shapes to draw.
00041 //! a clone() will be implemented if really needed.
00042 
00043 // maybe inherit from surface...
00044 class tcanvas 
00045 {
00046 public:
00047 
00048     //! Base class for all other shapes.
00049     //! The other shapes are declared and defined in canvas.cpp.
00050     class tshape : public reference_counted_object
00051     {
00052     public:
00053         virtual void draw(surface& canvas, 
00054             const game_logic::map_formula_callable& variables) = 0;
00055 
00056         virtual ~tshape() {}
00057     protected:
00058 
00059         // draw basic primitives
00060 
00061         void put_pixel(ptrdiff_t start, Uint32 colour, unsigned w, unsigned x, unsigned y);
00062         void draw_line(surface& canvas, Uint32 colour, 
00063             const unsigned x1, unsigned y1, const unsigned x2, unsigned y2);
00064 
00065     };
00066 
00067     typedef boost::intrusive_ptr<tshape> tshape_ptr;
00068     typedef boost::intrusive_ptr<const tshape> const_tshape_ptr;
00069 
00070     tcanvas();
00071     tcanvas(const config& cfg);
00072 
00073     void draw(const config& cfg);
00074     void draw(const bool force = false);
00075 
00076     void set_width(const unsigned width) { w_ = width; set_dirty(); }
00077     unsigned get_width() const { return w_; }
00078 
00079     void set_height(const unsigned height) { h_ = height; set_dirty(); }
00080     unsigned get_height() const { return h_; }
00081 
00082     surface& surf() { return canvas_; }
00083 
00084     void set_cfg(const config& cfg) { parse_cfg(cfg); }
00085 
00086     void set_variable(const std::string& key, const variant& value)
00087         { variables_.add(key, value); }
00088 
00089 private:
00090     void set_dirty(const bool dirty = true) { dirty_ = dirty; }
00091 
00092     void parse_cfg(const config& cfg);
00093 
00094     std::vector<tshape_ptr> shapes_;
00095 
00096     unsigned w_;
00097     unsigned h_;
00098 
00099     //! The canvas we draw all items on.
00100     surface canvas_;
00101 
00102     game_logic::map_formula_callable variables_;
00103 
00104     bool dirty_;
00105 };
00106 
00107 } // namespace gui2
00108 
00109 
00110 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs