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