cc2bc0104bf4b87e0eb8945c95c79f6633c1a616
[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 Lightmap;
35 class Renderer;
36 class Surface;
37 class Texture;
38
39 // some constants for predefined layer values
40 enum {
41   // Image/gradient backgrounds (should cover entire screen)
42   LAYER_BACKGROUND0 = -300,
43   // Particle backgrounds
44   LAYER_BACKGROUND1 = -200,
45   // Tilemap backgrounds
46   LAYER_BACKGROUNDTILES = -100,
47   // Solid tilemaps
48   LAYER_TILES = 0,
49   // Ordinary objects
50   LAYER_OBJECTS = 50,
51   // Objects that pass through walls
52   LAYER_FLOATINGOBJECTS = 150,
53   //
54   LAYER_FOREGROUNDTILES = 200,
55   //
56   LAYER_FOREGROUND0 = 300,
57   //
58   LAYER_FOREGROUND1 = 400,
59   // Hitpoints, time, coins, etc.
60   LAYER_HUD = 500,
61   // Menus, mouse, console etc.
62   LAYER_GUI         = 600
63 };
64
65 class Blend
66 {
67 public:
68   GLenum sfactor;
69   GLenum dfactor;
70
71   Blend()
72     : sfactor(GL_SRC_ALPHA), dfactor(GL_ONE_MINUS_SRC_ALPHA)
73   {}
74
75   Blend(GLenum s, GLenum d)
76     : sfactor(s), dfactor(d)
77   {}
78 };
79
80 enum Target {
81   NORMAL, LIGHTMAP
82 };
83
84 /**
85  * This class provides functions for drawing things on screen. It also
86  * maintains a stack of transforms that are applied to graphics.
87  */
88 class DrawingContext
89 {
90 public:
91   DrawingContext(Renderer& renderer, Lightmap& lightmap);
92   ~DrawingContext();
93
94   /// Adds a drawing request for a surface into the request list.
95   void draw_surface(SurfacePtr surface, const Vector& position,
96                     int layer);
97   /// Adds a drawing request for a surface into the request list.
98   void draw_surface(SurfacePtr surface, const Vector& position,
99                     float angle, const Color& color, const Blend& blend,
100                     int layer);
101   /// Adds a drawing request for part of a surface.
102   void draw_surface_part(SurfacePtr surface,
103                          const Rectf& srcrect, const Rectf& dstrect,
104                          int layer);
105   /// Draws a text.
106   void draw_text(FontPtr font, const std::string& text,
107                  const Vector& position, FontAlignment alignment, int layer, Color color = Color(1.0,1.0,1.0));
108
109   /// Draws text on screen center (feed Vector.x with a 0).
110   /// This is the same as draw_text() with a SCREEN_WIDTH/2 position and
111   /// alignment set to LEFT_ALIGN
112   void draw_center_text(FontPtr font, const std::string& text,
113                         const Vector& position, int layer, Color color = Color(1.0,1.0,1.0));
114   /// Draws a color gradient onto the whole screen */
115   void draw_gradient(const Color& from, const Color& to, int layer);
116   /// Fills a rectangle.
117   void draw_filled_rect(const Vector& topleft, const Vector& size,
118                         const Color& color, int layer);
119   void draw_filled_rect(const Rectf& rect, const Color& color, int layer);
120   void draw_filled_rect(const Rectf& rect, const Color& color, float radius, int layer);
121
122   void draw_inverse_ellipse(const Vector& pos, const Vector& size, const Color& color, int layer);
123
124   /// Returns the visible area in world coordinates
125   Rectf get_cliprect() const;
126
127   /// Processes all pending drawing requests and flushes the list.
128   void do_drawing();
129
130   const Vector& get_translation() const
131   {  return transform.translation;  }
132
133   void set_translation(const Vector& newtranslation)
134   {  transform.translation = newtranslation;  }
135
136   void push_transform();
137   void pop_transform();
138
139   /// Apply that effect in the next draws (effects are listed on surface.h).
140   void set_drawing_effect(DrawingEffect effect);
141   /// return currently applied drawing effect
142   DrawingEffect get_drawing_effect() const;
143   /// apply that alpha in the next draws (1.0 means fully opaque) */
144   void set_alpha(float alpha);
145   /// return currently set alpha
146   float get_alpha() const;
147
148   /// on next update, set color to lightmap's color at position
149   void get_light(const Vector& position, Color* color );
150
151   typedef ::Target Target;
152   static const Target NORMAL = ::NORMAL;
153   static const Target LIGHTMAP = ::LIGHTMAP;
154   void push_target();
155   void pop_target();
156   void set_target(Target target);
157
158   void set_ambient_color( Color new_color );
159
160   /**
161    * requests that a screenshot be taken after the next frame has been rendered
162    */
163   void take_screenshot();
164
165 private:
166   typedef std::vector<DrawingRequest*> DrawingRequests;
167
168 private:
169   void handle_drawing_requests(DrawingRequests& requests);
170
171 private:
172   class Transform
173   {
174   public:
175     Vector translation;
176     DrawingEffect drawing_effect;
177     float alpha;
178
179     Transform() :
180       translation(),
181       drawing_effect(NO_EFFECT),
182       alpha(1.0f)
183     { }
184
185     Vector apply(const Vector& v) const
186     {
187       return v - translation;
188     }
189   };
190
191   void clear_drawing_requests(DrawingRequests& requests);
192
193 private:
194   Renderer& renderer;
195   Lightmap& lightmap;
196
197   /// the transform stack
198   std::vector<Transform> transformstack;
199   /// the currently active transform
200   Transform transform;
201
202   std::vector<Blend> blend_stack;
203   Blend blend_mode;
204
205   DrawingRequests drawing_requests;
206   DrawingRequests lightmap_requests;
207
208   DrawingRequests* requests;
209   Color ambient_color;
210
211   Target target;
212   std::vector<Target> target_stack;
213
214   /* obstack holding the memory of the drawing requests */
215   struct obstack obst;
216
217   bool screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */
218
219 private:
220   DrawingContext(const DrawingContext&);
221   DrawingContext& operator=(const DrawingContext&);
222 };
223
224 #endif
225
226 /* EOF */