-Some cleanups in text scrolling code
[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 #include <stdint.h>
25
26 #include "SDL.h"
27
28 #include "math/vector.h"
29 #include "video/screen.h"
30 #include "video/surface.h"
31 #include "video/font.h"
32
33 namespace SuperTux
34   {
35
36   class Surface;
37
38   // some constants for predefined layer values
39   enum {
40     LAYER_BACKGROUND0 = -300,
41     LAYER_BACKGROUND1 = -200,
42     LAYER_BACKGROUNDTILES = -100,
43     LAYER_TILES = 0,
44     LAYER_OBJECTS = 100,
45     LAYER_FOREGROUNDTILES = 200,
46     LAYER_FOREGROUND0 = 300,
47     LAYER_FOREGROUND1 = 400,
48     LAYER_GUI         = 500
49   };
50
51   /// Handles drawing of things.
52   /**
53    * This class provides functions for drawing things on screen. It also
54    * maintains a stack of transforms that are applied to graphics.
55    */
56   class DrawingContext
57     {
58     public:
59       DrawingContext();
60       ~DrawingContext();
61
62       /// Adds a drawing request for a surface into the request list.
63       void draw_surface(const Surface* surface, const Vector& position,
64           int layer, uint32_t drawing_effect = NONE_EFFECT);
65       /// Adds a drawing request for part of a surface.
66       void draw_surface_part(const Surface* surface, const Vector& source,
67                              const Vector& size, const Vector& dest, int layer,
68                              uint32_t drawing_effect = NONE_EFFECT);
69       /// Draws a text.
70       void draw_text(const Font* font, const std::string& text,
71           const Vector& position, FontAlignment alignment, int layer,
72           uint32_t drawing_effect = NONE_EFFECT);
73
74       /// Draws text on screen center (feed Vector.x with a 0).
75       /// This is the same as draw_text() with a screen->w/2 position and
76       /// alignment set to LEFT_ALLIGN
77       void draw_center_text(const Font* font, const std::string& text,
78                            const Vector& position, int layer,
79                            uint32_t drawing_effect = NONE_EFFECT);
80       /// Draws a color gradient onto the whole screen */
81       void draw_gradient(Color from, Color to, int layer);
82       /// Fills a rectangle.
83       void draw_filled_rect(const Vector& topleft, const Vector& size,
84                             Color color, int layer);
85
86       /// Processes all pending drawing requests and flushes the list.
87       void do_drawing();
88
89       const Vector& get_translation() const
90       {  return transform.translation;  }
91       uint32_t get_drawing_effect() const
92       {  return transform.drawing_effect;  }
93
94       void set_translation(const Vector& newtranslation)
95         {  transform.translation = newtranslation;  }
96
97       void push_transform();
98       void pop_transform();
99
100       /// Apply that effect in the next draws (effects are listed on surface.h).
101       void set_drawing_effect(int effect);
102       /// apply that zoom in the next draws */
103       void set_zooming(float zoom);
104       /// apply that alpha in the next draws */
105       void set_alpha(int alpha);
106
107     private:
108       class Transform
109       {
110       public:
111         Vector translation;
112         uint32_t drawing_effect;
113         float zoom;
114         int alpha;
115
116         Transform()
117           : drawing_effect(NONE_EFFECT), zoom(1), alpha(255)
118         { }
119
120         Vector apply(const Vector& v) const
121         {
122           return v - translation;
123         }
124       };
125
126       /// the transform stack
127       std::vector<Transform> transformstack;
128       /// the currently active transform
129       Transform transform;
130
131       enum RequestType
132       {
133         SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT
134       };
135
136       struct SurfacePartRequest
137         {
138           const Surface* surface;
139           Vector source, size;
140         };
141
142       struct TextRequest
143         {
144           const Font* font;
145           std::string text;
146           FontAlignment alignment;
147         };
148
149       struct GradientRequest
150         {
151           Color top, bottom;
152           Vector size;
153         };
154
155       struct FillRectRequest
156         {
157           Color color;
158           Vector size;
159         };
160
161       struct DrawingRequest
162         {
163           RequestType type;
164           Vector pos;                
165           
166           int layer;
167           uint32_t drawing_effect;
168           float zoom;
169           int alpha;
170
171           void* request_data;
172
173           bool operator<(const DrawingRequest& other) const
174             {
175               return layer < other.layer;
176             }
177         };
178
179       void draw_surface_part(DrawingRequest& request);
180       void draw_text(DrawingRequest& request);
181       void draw_text_center(DrawingRequest& request);
182       void draw_gradient(DrawingRequest& request);
183       void draw_filled_rect(DrawingRequest& request);
184
185       typedef std::vector<DrawingRequest> DrawingRequests;
186       DrawingRequests drawingrequests;
187     };
188
189 } //namespace SuperTux
190
191 #endif /*SUPERTUX_DRAWINGCONTEXT_H*/
192