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