Fixed trailing whitespaces in all(?) source files of supertux, also fixed some svn...
[supertux.git] / src / video / drawing_context.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 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
20 #ifndef SUPERTUX_DRAWINGCONTEXT_H
21 #define SUPERTUX_DRAWINGCONTEXT_H
22
23 #include <vector>
24 #include <string>
25 #include <stdint.h>
26
27 #include <GL/gl.h>
28 #include <SDL.h>
29 #include <stdint.h>
30 #include <memory>
31
32 #include "math/vector.hpp"
33 #include "math/rect.hpp"
34 #include "surface.hpp"
35 #include "font.hpp"
36 #include "color.hpp"
37
38 class Surface;
39 class Texture;
40
41 // some constants for predefined layer values
42 enum {
43   LAYER_BACKGROUND0 = -300,
44   LAYER_BACKGROUND1 = -200,
45   LAYER_BACKGROUNDTILES = -100,
46   LAYER_TILES = 0,
47   LAYER_OBJECTS = 50,
48   LAYER_FLOATINGOBJECTS = 150,
49   LAYER_FOREGROUNDTILES = 200,
50   LAYER_FOREGROUND0 = 300,
51   LAYER_FOREGROUND1 = 400,
52   LAYER_HUD = 500,
53   LAYER_GUI         = 600
54 };
55
56 class Blend
57 {
58 public:
59   GLenum sfactor;
60   GLenum dfactor;
61
62   Blend()
63     : sfactor(GL_SRC_ALPHA), dfactor(GL_ONE_MINUS_SRC_ALPHA)
64   {}
65
66   Blend(GLenum s, GLenum d)
67     : sfactor(s), dfactor(d)
68   {}
69 };
70
71 /**
72  * This class provides functions for drawing things on screen. It also
73  * maintains a stack of transforms that are applied to graphics.
74  */
75 class DrawingContext
76 {
77 public:
78   DrawingContext();
79   ~DrawingContext();
80
81   /// Adds a drawing request for a surface into the request list.
82   void draw_surface(const Surface* surface, const Vector& position,
83                     int layer);
84   /// Adds a drawing request for a surface into the request list.
85   void draw_surface(const Surface* surface, const Vector& position,
86                     float angle, const Color& color, const Blend& blend,
87                     int layer);
88   /// Adds a drawing request for part of a surface.
89   void draw_surface_part(const Surface* surface, const Vector& source,
90                          const Vector& size, const Vector& dest, int layer);
91   /// Draws a text.
92   void draw_text(const Font* font, const std::string& text,
93                  const Vector& position, FontAlignment alignment, int layer);
94
95   /// Draws text on screen center (feed Vector.x with a 0).
96   /// This is the same as draw_text() with a SCREEN_WIDTH/2 position and
97   /// alignment set to LEFT_ALLIGN
98   void draw_center_text(const Font* font, const std::string& text,
99                         const Vector& position, int layer);
100   /// Draws a color gradient onto the whole screen */
101   void draw_gradient(const Color& from, const Color& to, int layer);
102   /// Fills a rectangle.
103   void draw_filled_rect(const Vector& topleft, const Vector& size,
104                         const Color& color, int layer);
105   void draw_filled_rect(const Rect& rect, const Color& color, int layer);
106
107   /// Processes all pending drawing requests and flushes the list.
108   void do_drawing();
109
110   const Vector& get_translation() const
111   {  return transform.translation;  }
112
113   void set_translation(const Vector& newtranslation)
114   {  transform.translation = newtranslation;  }
115
116   void push_transform();
117   void pop_transform();
118
119   /// Apply that effect in the next draws (effects are listed on surface.h).
120   void set_drawing_effect(DrawingEffect effect);
121   /// return currently applied drawing effect
122   DrawingEffect get_drawing_effect() const;
123   /// apply that alpha in the next draws (1.0 means fully opaque) */
124   void set_alpha(float alpha);
125   /// return currently set alpha
126   float get_alpha() const;
127
128   /// on next update, set color to lightmap's color at position
129   void get_light(const Vector& position, Color* color );
130
131   enum Target {
132     NORMAL, LIGHTMAP
133   };
134   void push_target();
135   void pop_target();
136   void set_target(Target target);
137
138   void set_ambient_color( Color new_color );
139
140 private:
141   class Transform
142   {
143   public:
144     Vector translation;
145     DrawingEffect drawing_effect;
146     float alpha;
147
148     Transform()
149       : drawing_effect(NO_EFFECT), alpha(1.0f)
150     { }
151
152     Vector apply(const Vector& v) const
153     {
154       return v - translation;
155     }
156   };
157
158   /// the transform stack
159   std::vector<Transform> transformstack;
160   /// the currently active transform
161   Transform transform;
162
163   std::vector<Blend> blend_stack;
164   Blend blend_mode;
165
166   enum RequestType
167   {
168     SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT, LIGHTMAPREQUEST, GETLIGHT
169   };
170
171   struct SurfacePartRequest
172   {
173     const Surface* surface;
174     Vector source, size;
175   };
176
177   struct TextRequest
178   {
179     const Font* font;
180     std::string text;
181     FontAlignment alignment;
182   };
183
184   struct GradientRequest
185   {
186     Color top, bottom;
187     Vector size;
188   };
189
190   struct FillRectRequest
191   {
192     Color color;
193     Vector size;
194   };
195
196   struct DrawingRequest
197   {
198     RequestType type;
199     Vector pos;
200
201     int layer;
202     DrawingEffect drawing_effect;
203     float alpha;
204     Blend blend;
205     float angle;
206     Color color;
207
208     void* request_data;
209
210     DrawingRequest()
211       : angle(0.0f),
212         color(1.0f, 1.0f, 1.0f, 1.0f)
213     {}
214
215     bool operator<(const DrawingRequest& other) const
216     {
217       return layer < other.layer;
218     }
219   };
220
221   struct GetLightRequest
222   {
223     Color* color_ptr;
224   };
225
226   typedef std::vector<DrawingRequest> DrawingRequests;
227
228   void handle_drawing_requests(DrawingRequests& requests);
229   void draw_surface_part(DrawingRequest& request);
230   void draw_text(DrawingRequest& request);
231   void draw_text_center(DrawingRequest& request);
232   void draw_gradient(DrawingRequest& request);
233   void draw_filled_rect(DrawingRequest& request);
234   void draw_lightmap(DrawingRequest& request);
235   void get_light(DrawingRequest& request);
236
237   DrawingRequests drawing_requests;
238   DrawingRequests lightmap_requests;
239
240   DrawingRequests* requests;
241   Color ambient_color;
242
243   SDL_Surface* screen;
244   Target target;
245   std::vector<Target> target_stack;
246   Texture* lightmap;
247   int lightmap_width, lightmap_height;
248   float lightmap_uv_right, lightmap_uv_bottom;
249 };
250
251 #endif