The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
canvas.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * This file contains the canvas object which is the part where the widgets
18  * draw (temporally) images on.
19  */
20 
21 #ifndef GUI_AUXILIARY_CANVAS_HPP_INCLUDED
22 #define GUI_AUXILIARY_CANVAS_HPP_INCLUDED
23 
24 #include "formula/callable.hpp"
25 #include "sdl/utils.hpp"
26 
27 class config;
28 class variant;
29 
30 namespace gui2
31 {
32 
33 /**
34  * A simple canvas which can be drawn upon.
35  *
36  * The class has a config which contains what to draw.
37  *
38  * NOTE we might add some caching in a later state, for now every draw cycle
39  * does a full redraw.
40  *
41  * The copy constructor does a shallow copy of the shapes to draw.
42  * a clone() will be implemented if really needed.
43  */
44 class tcanvas
45 {
46 public:
47  /**
48  * Abstract base class for all other shapes.
49  *
50  * The other shapes are declared and defined in canvas.cpp, since the
51  * implementation details are not interesting for users of the canvas.
52  */
54  {
55  public:
56  virtual ~tshape()
57  {
58  }
59 
60  /**
61  * Draws the canvas.
62  *
63  * @param canvas The resulting image will be blitted upon this
64  * canvas.
65  * @param variables The canvas can have formulas in it's
66  * definition, this parameter contains the values
67  * for these formulas.
68  */
69  virtual void draw(surface& canvas,
70  const game_logic::map_formula_callable& variables)
71  = 0;
72  };
73 
76 
77  tcanvas();
78 
79  /**
80  * Draws the canvas.
81  *
82  * @param force If the canvas isn't dirty it isn't redrawn
83  * unless force is set to true.
84  */
85  void draw(const bool force = false);
86 
87  /**
88  * Blits the canvas unto another surface.
89  *
90  * It makes sure the image on the canvas is up to date. Also executes the
91  * pre-blitting functions.
92  *
93  * @param surf The surface to blit upon.
94  * @param rect The place to blit to.
95  */
96  void blit(surface& surf, SDL_Rect rect);
97 
98  /**
99  * Sets the config.
100  *
101  * @param cfg The config object with the data to draw, see
102  * http://www.wesnoth.org/wiki/GUICanvasWML for
103  * more information.
104  */
105  void set_cfg(const config& cfg)
106  {
107  parse_cfg(cfg);
108  }
109 
110  /***** ***** ***** setters / getters for members ***** ****** *****/
111 
112  void set_width(const unsigned width)
113  {
114  w_ = width;
115  set_is_dirty(true);
116  }
117  unsigned get_width() const
118  {
119  return w_;
120  }
121 
122  void set_height(const unsigned height)
123  {
124  h_ = height;
125  set_is_dirty(true);
126  }
127  unsigned get_height() const
128  {
129  return h_;
130  }
131 
133  {
134  return canvas_;
135  }
136 
137  void set_variable(const std::string& key, const variant& value)
138  {
139  variables_.add(key, value);
140  set_is_dirty(true);
141  }
142 
143 private:
144  /** Vector with the shapes to draw. */
145  std::vector<tshape_ptr> shapes_;
146 
147  /**
148  * The depth of the blur to use in the pre committing.
149  *
150  * @note at the moment there's one pre commit function, namely the
151  * blurring so use a variable here, might get more functions in the
152  * future. When that happens need to evaluate whether variables are the
153  * best thing to use.
154  */
155  unsigned blur_depth_;
156 
157  /** Width of the canvas. */
158  unsigned w_;
159 
160  /** Height of the canvas. */
161  unsigned h_;
162 
163  /** The surface we draw all items on. */
165 
166  /** The variables of the canvas. */
168 
169  /** The dirty state of the canvas. */
170  bool is_dirty_;
171 
172  void set_is_dirty(const bool is_dirty)
173  {
174  is_dirty_ = is_dirty;
175  }
176 
177  /**
178  * Parses a config object.
179  *
180  * The config object is parsed and serialized by this function after which
181  * the config object is no longer required and thus not stored in the
182  * object.
183  *
184  * @param cfg The config object with the data to draw, see
185  * http://www.wesnoth.org/wiki/GUICanvasWML
186  */
187  void parse_cfg(const config& cfg);
188 };
189 
190 } // namespace gui2
191 
192 #endif
unsigned blur_depth_
The depth of the blur to use in the pre committing.
Definition: canvas.hpp:155
unsigned w_
Width of the canvas.
Definition: canvas.hpp:158
virtual void draw(surface &canvas, const game_logic::map_formula_callable &variables)=0
Draws the canvas.
void set_variable(const std::string &key, const variant &value)
Definition: canvas.hpp:137
surface & surf()
Definition: canvas.hpp:132
void set_width(const unsigned width)
Definition: canvas.hpp:112
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void set_cfg(const config &cfg)
Sets the config.
Definition: canvas.hpp:105
unsigned h_
Height of the canvas.
Definition: canvas.hpp:161
Abstract base class for all other shapes.
Definition: canvas.hpp:53
surface canvas_
The surface we draw all items on.
Definition: canvas.hpp:164
GLsizei const GLfloat * value
Definition: glew.h:1817
bool is_dirty_
The dirty state of the canvas.
Definition: canvas.hpp:170
void draw(const bool force=false)
Draws the canvas.
Definition: canvas.cpp:1467
boost::intrusive_ptr< const tshape > const_tshape_ptr
Definition: canvas.hpp:75
void set_height(const unsigned height)
Definition: canvas.hpp:122
A simple canvas which can be drawn upon.
Definition: canvas.hpp:44
void parse_cfg(const config &cfg)
Parses a config object.
Definition: canvas.cpp:1521
void blit(surface &surf, SDL_Rect rect)
Blits the canvas unto another surface.
Definition: canvas.cpp:1497
std::vector< tshape_ptr > shapes_
Vector with the shapes to draw.
Definition: canvas.hpp:145
virtual ~tshape()
Definition: canvas.hpp:56
map_formula_callable & add(const std::string &key, const variant &value)
Definition: formula.cpp:41
unsigned get_width() const
Definition: canvas.hpp:117
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1220
boost::intrusive_ptr< tshape > tshape_ptr
Definition: canvas.hpp:74
void set_is_dirty(const bool is_dirty)
Definition: canvas.hpp:172
unsigned get_height() const
Definition: canvas.hpp:127
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
game_logic::map_formula_callable variables_
The variables of the canvas.
Definition: canvas.hpp:167