The SuperTux library features a SuperTux namespace now.
[supertux.git] / lib / video / drawing_context.h
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #ifndef SUPERTUX_DRAWINGCONTEXT_H
20 #define SUPERTUX_DRAWINGCONTEXT_H
21
22 #include <vector>
23 #include <string>
24
25 #include "SDL.h"
26
27 #include "math/vector.h"
28 #include "video/screen.h"
29 #include "video/surface.h"
30
31 namespace SuperTux
32   {
33
34   class Surface;
35   class Font;
36
37   // some constants for predefined layer values
38   enum {
39     LAYER_BACKGROUND0 = -300,
40     LAYER_BACKGROUND1 = -200,
41     LAYER_BACKGROUNDTILES = -100,
42     LAYER_TILES = 0,
43     LAYER_OBJECTS = 100,
44     LAYER_FOREGROUNDTILES = 200,
45     LAYER_FOREGROUND0 = 300,
46     LAYER_FOREGROUND1 = 400,
47     LAYER_GUI         = 500
48   };
49
50   /**
51    * This class provides functions for drawing things on screen. It also
52    * maintains a stack of transforms that are applied to graphics.
53    */
54   class DrawingContext
55     {
56     public:
57       DrawingContext();
58       ~DrawingContext();
59
60       /** Adds a drawing request for a surface into the request list */
61       void draw_surface(const Surface* surface, const Vector& position, int layer,
62                         Uint32 drawing_effect = NONE_EFFECT);
63       /** Adds a drawing request for part of a surface */
64       void draw_surface_part(const Surface* surface, const Vector& source,
65                              const Vector& size, const Vector& dest, int layer,
66                              Uint32 drawing_effect = NONE_EFFECT);
67       /** draws a text */
68       void draw_text(Font* font, const std::string& text, const Vector& position,
69                      int layer, Uint32 drawing_effect = NONE_EFFECT);
70       /** draws aligned text */
71       void draw_text_center(Font* font, const std::string& text,
72                             const Vector& position, int layer, Uint32 drawing_effect = NONE_EFFECT);
73       /** draws a color gradient onto the whole screen */
74       void draw_gradient(Color from, Color to, int layer);
75       /** fills a rectangle */
76       void draw_filled_rect(const Vector& topleft, const Vector& size,
77                             Color color, int layer);
78
79       /** Processes all pending drawing requests and flushes the list */
80       void do_drawing();
81
82       const Vector& get_translation() const
83         {
84           return transform.translation;
85         }
86       void set_translation(const Vector& newtranslation)
87       {
88         transform.translation = newtranslation;
89       }
90
91       void push_transform();
92       void pop_transform();
93
94       /** apply that effect in the next draws (effects are listed on surface.h) */
95       void set_drawing_effect(int effect);
96
97     private:
98       class Transform
99         {
100         public:
101           Vector translation; // only translation for now...
102
103           Vector apply(const Vector& v) const
104             {
105               return v - translation;
106             }
107
108           int draw_effect;
109         };
110
111       /// the transform stack
112       std::vector<Transform> transformstack;
113       /// the currently active transform
114       Transform transform;
115
116       enum RequestType
117       {
118         SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT
119       };
120
121       struct SurfacePartRequest
122         {
123           const Surface* surface;
124           Vector source, size;
125         };
126
127       struct TextRequest
128         {
129           Font* font;
130           std::string text;
131         };
132
133       struct GradientRequest
134         {
135           Color top, bottom;
136           Vector size;
137         };
138
139       struct FillRectRequest
140         {
141           Color color;
142           Vector size;
143         };
144
145       struct DrawingRequest
146         {
147           int layer;
148           Uint32 drawing_effect;
149
150           RequestType type;
151           Vector pos;
152
153           void* request_data;
154
155           bool operator<(const DrawingRequest& other) const
156             {
157               return layer < other.layer;
158             }
159         };
160
161       void draw_surface_part(DrawingRequest& request);
162       void draw_text(DrawingRequest& request);
163       void draw_gradient(DrawingRequest& request);
164       void draw_filled_rect(DrawingRequest& request);
165
166       typedef std::vector<DrawingRequest> DrawingRequests;
167       DrawingRequests drawingrequests;
168     };
169
170 } //namespace SuperTux
171
172 #endif /*SUPERTUX_DRAWINGCONTEXT_H*/
173