a8c7e56801cbf740282afa63bc4f6be342e77abd
[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_GUI         = 500
53 };
54
55 class Blend
56 {
57 public:
58   GLenum sfactor;
59   GLenum dfactor;
60   
61   Blend()
62     : sfactor(GL_SRC_ALPHA), dfactor(GL_ONE_MINUS_SRC_ALPHA)
63   {}
64
65   Blend(GLenum s, GLenum d)
66     : sfactor(s), dfactor(d)
67   {}
68 };
69
70 /**
71  * This class provides functions for drawing things on screen. It also
72  * maintains a stack of transforms that are applied to graphics.
73  */
74 class DrawingContext
75 {
76 public:
77   DrawingContext();
78   ~DrawingContext();
79
80   /// Adds a drawing request for a surface into the request list.
81   void draw_surface(const Surface* surface, const Vector& position, 
82                     int layer);
83   /// Adds a drawing request for a surface into the request list.
84   void draw_surface(const Surface* surface, const Vector& position, 
85                     float angle, const Color& color, const Blend& blend,
86                     int layer);
87   /// Adds a drawing request for part of a surface.
88   void draw_surface_part(const Surface* surface, const Vector& source,
89                          const Vector& size, const Vector& dest, int layer);
90   /// Draws a text.
91   void draw_text(const Font* font, const std::string& text,
92                  const Vector& position, FontAlignment alignment, int layer);
93   
94   /// Draws text on screen center (feed Vector.x with a 0).
95   /// This is the same as draw_text() with a SCREEN_WIDTH/2 position and
96   /// alignment set to LEFT_ALLIGN
97   void draw_center_text(const Font* font, const std::string& text,
98                         const Vector& position, int layer);
99   /// Draws a color gradient onto the whole screen */
100   void draw_gradient(const Color& from, const Color& to, int layer);
101   /// Fills a rectangle.
102   void draw_filled_rect(const Vector& topleft, const Vector& size,
103                         const Color& color, int layer);
104   void draw_filled_rect(const Rect& rect, const Color& color, int layer);
105   
106   /// Processes all pending drawing requests and flushes the list.
107   void do_drawing();
108   
109   const Vector& get_translation() const
110   {  return transform.translation;  }
111   
112   void set_translation(const Vector& newtranslation)
113   {  transform.translation = newtranslation;  }
114   
115   void push_transform();
116   void pop_transform();
117   
118   /// Apply that effect in the next draws (effects are listed on surface.h).
119   void set_drawing_effect(DrawingEffect effect);
120   /// return currently applied drawing effect
121   DrawingEffect get_drawing_effect() const;
122   /// apply that alpha in the next draws (1.0 means fully opaque) */
123   void set_alpha(float alpha);
124   /// return currently set alpha
125   float get_alpha() const;
126
127   enum Target {
128     NORMAL, LIGHTMAP
129   };
130   void push_target();
131   void pop_target();
132   void set_target(Target target);
133   
134 private:
135   class Transform
136   {
137   public:
138     Vector translation;
139     DrawingEffect drawing_effect;
140     float alpha;
141     
142     Transform()
143       : drawing_effect(NO_EFFECT), alpha(1.0f)
144     { }
145     
146     Vector apply(const Vector& v) const
147     {
148       return v - translation;
149     }
150   };
151   
152   /// the transform stack
153   std::vector<Transform> transformstack;
154   /// the currently active transform
155   Transform transform;
156
157   std::vector<Blend> blend_stack;
158   Blend blend_mode;
159   
160   enum RequestType
161   {
162     SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT
163   };
164   
165   struct SurfacePartRequest
166   {
167     const Surface* surface;
168     Vector source, size;
169   };
170   
171   struct TextRequest
172   {
173     const Font* font;
174     std::string text;
175     FontAlignment alignment;
176   };
177   
178   struct GradientRequest
179   {
180     Color top, bottom;
181     Vector size;
182   };
183   
184   struct FillRectRequest
185   {
186     Color color;
187     Vector size;
188   };
189   
190   struct DrawingRequest
191   {
192     RequestType type;
193     Vector pos;                
194     
195     int layer;
196     DrawingEffect drawing_effect;
197     float alpha;
198     Blend blend;
199     float angle;
200     Color color;
201
202     void* request_data;
203
204     DrawingRequest()
205       : angle(0.0f),
206         color(1.0f, 1.0f, 1.0f, 1.0f)
207     {}
208     
209     bool operator<(const DrawingRequest& other) const
210     {
211       return layer < other.layer;
212     }
213   };
214
215   typedef std::vector<DrawingRequest> DrawingRequests;
216   
217   void handle_drawing_requests(DrawingRequests& requests);
218   void draw_surface_part(DrawingRequest& request);
219   void draw_text(DrawingRequest& request);
220   void draw_text_center(DrawingRequest& request);
221   void draw_gradient(DrawingRequest& request);
222   void draw_filled_rect(DrawingRequest& request);
223   
224   DrawingRequests drawing_requests;
225   DrawingRequests lightmap_requests;
226
227   DrawingRequests* requests;
228
229   SDL_Surface* screen;
230   Target target;
231   std::vector<Target> target_stack;
232   Texture* lightmap;
233   int lightmap_width, lightmap_height;
234   float lightmap_uv_right, lightmap_uv_bottom;
235 };
236
237 #endif