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