Fixed resize handling of lightmaps, they are now recreated on resize()
[supertux.git] / src / video / drawing_context.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef HEADER_SUPERTUX_VIDEO_DRAWING_CONTEXT_HPP
18 #define HEADER_SUPERTUX_VIDEO_DRAWING_CONTEXT_HPP
19
20 #include <memory>
21 #include <string>
22 #include <vector>
23 #include <stdint.h>
24 #include <obstack.h>
25
26 #include "math/rectf.hpp"
27 #include "math/vector.hpp"
28 #include "video/color.hpp"
29 #include "video/font.hpp"
30 #include "video/font_ptr.hpp"
31 #include "video/texture.hpp"
32
33 class DrawingRequest;
34 class Surface;
35 class Texture;
36 class VideoSystem;
37
38 // some constants for predefined layer values
39 enum {
40   // Image/gradient backgrounds (should cover entire screen)
41   LAYER_BACKGROUND0 = -300,
42   // Particle backgrounds
43   LAYER_BACKGROUND1 = -200,
44   // Tilemap backgrounds
45   LAYER_BACKGROUNDTILES = -100,
46   // Solid tilemaps
47   LAYER_TILES = 0,
48   // Ordinary objects
49   LAYER_OBJECTS = 50,
50   // Objects that pass through walls
51   LAYER_FLOATINGOBJECTS = 150,
52   //
53   LAYER_FOREGROUNDTILES = 200,
54   //
55   LAYER_FOREGROUND0 = 300,
56   //
57   LAYER_FOREGROUND1 = 400,
58   // Hitpoints, time, coins, etc.
59   LAYER_HUD = 500,
60   // Menus, mouse, console etc.
61   LAYER_GUI         = 600
62 };
63
64 class Blend
65 {
66 public:
67   GLenum sfactor;
68   GLenum dfactor;
69
70   Blend()
71     : sfactor(GL_SRC_ALPHA), dfactor(GL_ONE_MINUS_SRC_ALPHA)
72   {}
73
74   Blend(GLenum s, GLenum d)
75     : sfactor(s), dfactor(d)
76   {}
77 };
78
79 enum Target {
80   NORMAL, LIGHTMAP
81 };
82
83 /**
84  * This class provides functions for drawing things on screen. It also
85  * maintains a stack of transforms that are applied to graphics.
86  */
87 class DrawingContext
88 {
89 public:
90   DrawingContext(VideoSystem& video_system);
91   ~DrawingContext();
92
93   /// Adds a drawing request for a surface into the request list.
94   void draw_surface(SurfacePtr surface, const Vector& position,
95                     int layer);
96   /// Adds a drawing request for a surface into the request list.
97   void draw_surface(SurfacePtr surface, const Vector& position,
98                     float angle, const Color& color, const Blend& blend,
99                     int layer);
100   /// Adds a drawing request for part of a surface.
101   void draw_surface_part(SurfacePtr surface,
102                          const Rectf& srcrect, const Rectf& dstrect,
103                          int layer);
104   /// Draws a text.
105   void draw_text(FontPtr font, const std::string& text,
106                  const Vector& position, FontAlignment alignment, int layer, Color color = Color(1.0,1.0,1.0));
107
108   /// Draws text on screen center (feed Vector.x with a 0).
109   /// This is the same as draw_text() with a SCREEN_WIDTH/2 position and
110   /// alignment set to LEFT_ALIGN
111   void draw_center_text(FontPtr font, const std::string& text,
112                         const Vector& position, int layer, Color color = Color(1.0,1.0,1.0));
113   /// Draws a color gradient onto the whole screen */
114   void draw_gradient(const Color& from, const Color& to, int layer);
115   /// Fills a rectangle.
116   void draw_filled_rect(const Vector& topleft, const Vector& size,
117                         const Color& color, int layer);
118   void draw_filled_rect(const Rectf& rect, const Color& color, int layer);
119   void draw_filled_rect(const Rectf& rect, const Color& color, float radius, int layer);
120
121   void draw_inverse_ellipse(const Vector& pos, const Vector& size, const Color& color, int layer);
122
123   /// Returns the visible area in world coordinates
124   Rectf get_cliprect() const;
125
126   /// Processes all pending drawing requests and flushes the list.
127   void do_drawing();
128
129   const Vector& get_translation() const
130   {  return transform.translation;  }
131
132   void set_translation(const Vector& newtranslation)
133   {  transform.translation = newtranslation;  }
134
135   void push_transform();
136   void pop_transform();
137
138   /// Apply that effect in the next draws (effects are listed on surface.h).
139   void set_drawing_effect(DrawingEffect effect);
140   /// return currently applied drawing effect
141   DrawingEffect get_drawing_effect() const;
142   /// apply that alpha in the next draws (1.0 means fully opaque) */
143   void set_alpha(float alpha);
144   /// return currently set alpha
145   float get_alpha() const;
146
147   /// on next update, set color to lightmap's color at position
148   void get_light(const Vector& position, Color* color );
149
150   typedef ::Target Target;
151   static const Target NORMAL = ::NORMAL;
152   static const Target LIGHTMAP = ::LIGHTMAP;
153   void push_target();
154   void pop_target();
155   void set_target(Target target);
156
157   void set_ambient_color( Color new_color );
158
159   /**
160    * requests that a screenshot be taken after the next frame has been rendered
161    */
162   void take_screenshot();
163
164 private:
165   typedef std::vector<DrawingRequest*> DrawingRequests;
166
167 private:
168   void handle_drawing_requests(DrawingRequests& requests);
169
170 private:
171   class Transform
172   {
173   public:
174     Vector translation;
175     DrawingEffect drawing_effect;
176     float alpha;
177
178     Transform() :
179       translation(),
180       drawing_effect(NO_EFFECT),
181       alpha(1.0f)
182     { }
183
184     Vector apply(const Vector& v) const
185     {
186       return v - translation;
187     }
188   };
189
190   void clear_drawing_requests(DrawingRequests& requests);
191
192 private:
193   VideoSystem& video_system;
194
195   /// the transform stack
196   std::vector<Transform> transformstack;
197   /// the currently active transform
198   Transform transform;
199
200   std::vector<Blend> blend_stack;
201   Blend blend_mode;
202
203   DrawingRequests drawing_requests;
204   DrawingRequests lightmap_requests;
205
206   DrawingRequests* requests;
207   Color ambient_color;
208
209   Target target;
210   std::vector<Target> target_stack;
211
212   /* obstack holding the memory of the drawing requests */
213   struct obstack obst;
214
215   bool screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */
216
217 private:
218   DrawingContext(const DrawingContext&);
219   DrawingContext& operator=(const DrawingContext&);
220 };
221
222 #endif
223
224 /* EOF */