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