Removed pointless check for screen mode change, as we are creating the window for...
[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/drawing_request.hpp"
30 #include "video/font.hpp"
31 #include "video/font_ptr.hpp"
32 #include "video/texture.hpp"
33
34 class Surface;
35 class Texture;
36 class Renderer;
37 class Lightmap;
38
39 inline int next_po2(int val)
40 {
41   int result = 1;
42   while(result < val)
43     result *= 2;
44
45   return result;
46 }
47
48 /**
49  * This class provides functions for drawing things on screen. It also
50  * maintains a stack of transforms that are applied to graphics.
51  */
52 class DrawingContext
53 {
54 public:
55   DrawingContext(Renderer& renderer, Lightmap& lightmap);
56   ~DrawingContext();
57
58   /// Adds a drawing request for a surface into the request list.
59   void draw_surface(SurfacePtr surface, const Vector& position,
60                     int layer);
61   /// Adds a drawing request for a surface into the request list.
62   void draw_surface(SurfacePtr surface, const Vector& position,
63                     float angle, const Color& color, const Blend& blend,
64                     int layer);
65   /// Adds a drawing request for part of a surface.
66   void draw_surface_part(SurfacePtr surface, const Vector& source,
67                          const Vector& size, const Vector& dest, int layer);
68   /// Draws a text.
69   void draw_text(FontPtr font, const std::string& text,
70                  const Vector& position, FontAlignment alignment, int layer, Color color = Color(1.0,1.0,1.0));
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_ALIGN
75   void draw_center_text(FontPtr font, const std::string& text,
76                         const Vector& position, int layer, Color color = Color(1.0,1.0,1.0));
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   void draw_filled_rect(const Rectf& rect, const Color& color, int layer);
83   void draw_filled_rect(const Rectf& rect, const Color& color, float radius, int layer);
84
85   void draw_inverse_ellipse(const Vector& pos, const Vector& size, const Color& color, int layer);
86
87   /// Returns the visible area in world coordinates
88   Rectf get_cliprect() const;
89
90   /// Processes all pending drawing requests and flushes the list.
91   void do_drawing();
92
93   const Vector& get_translation() const
94   {  return transform.translation;  }
95
96   void set_translation(const Vector& newtranslation)
97   {  transform.translation = newtranslation;  }
98
99   void push_transform();
100   void pop_transform();
101
102   /// Apply that effect in the next draws (effects are listed on surface.h).
103   void set_drawing_effect(DrawingEffect effect);
104   /// return currently applied drawing effect
105   DrawingEffect get_drawing_effect() const;
106   /// apply that alpha in the next draws (1.0 means fully opaque) */
107   void set_alpha(float alpha);
108   /// return currently set alpha
109   float get_alpha() const;
110
111   /// on next update, set color to lightmap's color at position
112   void get_light(const Vector& position, Color* color );
113
114   typedef ::Target Target;
115   static const Target NORMAL = ::NORMAL;
116   static const Target LIGHTMAP = ::LIGHTMAP;
117   void push_target();
118   void pop_target();
119   void set_target(Target target);
120
121   void set_ambient_color( Color new_color );
122
123   /**
124    * requests that a screenshot be taken after the next frame has been rendered
125    */
126   void take_screenshot();
127
128 private:
129   typedef std::vector<DrawingRequest*> DrawingRequests;
130
131 private:
132   void handle_drawing_requests(DrawingRequests& requests);
133
134 private:
135   class Transform
136   {
137   public:
138     Vector translation;
139     DrawingEffect drawing_effect;
140     float alpha;
141
142     Transform() :
143       translation(),
144       drawing_effect(NO_EFFECT), 
145       alpha(1.0f)
146     { }
147
148     Vector apply(const Vector& v) const
149     {
150       return v - translation;
151     }
152   };
153
154 private:
155   Renderer& renderer;
156   Lightmap& lightmap;
157
158   /// the transform stack
159   std::vector<Transform> transformstack;
160   /// the currently active transform
161   Transform transform;
162
163   std::vector<Blend> blend_stack;
164   Blend blend_mode;
165
166   DrawingRequests drawing_requests;
167   DrawingRequests lightmap_requests;
168
169   DrawingRequests* requests;
170   Color ambient_color;
171
172   Target target;
173   std::vector<Target> target_stack;
174
175   /* obstack holding the memory of the drawing requests */
176   struct obstack obst;
177
178   bool screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */
179
180 private:
181   DrawingContext(const DrawingContext&);
182   DrawingContext& operator=(const DrawingContext&);
183 };
184
185 #endif
186
187 /* EOF */