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