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