Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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
24 #include <stdint.h>
25
26 #include "math/rect.hpp"
27 #include "math/vector.hpp"
28 #include "obstack/obstack.h"
29 #include "video/color.hpp"
30 #include "video/drawing_request.hpp"
31 #include "video/font.hpp"
32 #include "video/texture.hpp"
33
34 class Surface;
35 class Texture;
36 class Renderer;
37 class Lightmap;
38
39 /**
40  * This class provides functions for drawing things on screen. It also
41  * maintains a stack of transforms that are applied to graphics.
42  */
43 class DrawingContext
44 {
45 public:
46   DrawingContext();
47   ~DrawingContext();
48
49   void init_renderer();
50
51   /// Adds a drawing request for a surface into the request list.
52   void draw_surface(const Surface* surface, const Vector& position,
53                     int layer);
54   /// Adds a drawing request for a surface into the request list.
55   void draw_surface(const Surface* surface, const Vector& position,
56                     float angle, const Color& color, const Blend& blend,
57                     int layer);
58   /// Adds a drawing request for part of a surface.
59   void draw_surface_part(const Surface* surface, const Vector& source,
60                          const Vector& size, const Vector& dest, int layer);
61   /// Draws a text.
62   void draw_text(const Font* font, const std::string& text,
63                  const Vector& position, FontAlignment alignment, int layer, Color color = Color(1.0,1.0,1.0));
64
65   /// Draws text on screen center (feed Vector.x with a 0).
66   /// This is the same as draw_text() with a SCREEN_WIDTH/2 position and
67   /// alignment set to LEFT_ALIGN
68   void draw_center_text(const Font* font, const std::string& text,
69                         const Vector& position, int layer, Color color = Color(1.0,1.0,1.0));
70   /// Draws a color gradient onto the whole screen */
71   void draw_gradient(const Color& from, const Color& to, int layer);
72   /// Fills a rectangle.
73   void draw_filled_rect(const Vector& topleft, const Vector& size,
74                         const Color& color, int layer);
75   void draw_filled_rect(const Rect& rect, const Color& color, int layer);
76   void draw_filled_rect(const Rect& rect, const Color& color, float radius, int layer);
77
78   void draw_inverse_ellipse(const Vector& pos, const Vector& size, const Color& color, int layer);
79
80   /// Processes all pending drawing requests and flushes the list.
81   void do_drawing();
82
83   const Vector& get_translation() const
84   {  return transform.translation;  }
85
86   void set_translation(const Vector& newtranslation)
87   {  transform.translation = newtranslation;  }
88
89   void push_transform();
90   void pop_transform();
91
92   /// Apply that effect in the next draws (effects are listed on surface.h).
93   void set_drawing_effect(DrawingEffect effect);
94   /// return currently applied drawing effect
95   DrawingEffect get_drawing_effect() const;
96   /// apply that alpha in the next draws (1.0 means fully opaque) */
97   void set_alpha(float alpha);
98   /// return currently set alpha
99   float get_alpha() const;
100
101   /// on next update, set color to lightmap's color at position
102   void get_light(const Vector& position, Color* color );
103
104   typedef ::Target Target;
105   static const Target NORMAL = ::NORMAL;
106   static const Target LIGHTMAP = ::LIGHTMAP;
107   void push_target();
108   void pop_target();
109   void set_target(Target target);
110
111   void set_ambient_color( Color new_color );
112
113   /**
114    * requests that a screenshot be taken after the next frame has been rendered
115    */
116   void take_screenshot();
117
118 private:
119   class Transform
120   {
121   public:
122     Vector translation;
123     DrawingEffect drawing_effect;
124     float alpha;
125
126     Transform() :
127       translation(),
128       drawing_effect(NO_EFFECT), 
129       alpha(1.0f)
130     { }
131
132     Vector apply(const Vector& v) const
133     {
134       return v - translation;
135     }
136   };
137
138   Renderer *renderer;
139   Lightmap *lightmap;
140
141   /// the transform stack
142   std::vector<Transform> transformstack;
143   /// the currently active transform
144   Transform transform;
145
146   std::vector<Blend> blend_stack;
147   Blend blend_mode;
148
149   typedef std::vector<DrawingRequest*> DrawingRequests;
150
151   void handle_drawing_requests(DrawingRequests& requests);
152
153   DrawingRequests drawing_requests;
154   DrawingRequests lightmap_requests;
155
156   DrawingRequests* requests;
157   Color ambient_color;
158
159   Target target;
160   std::vector<Target> target_stack;
161
162   /* obstack holding the memory of the drawing requests */
163   struct obstack obst;
164
165   bool screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */
166
167 private:
168   DrawingContext(const DrawingContext&);
169   DrawingContext& operator=(const DrawingContext&);
170 };
171
172 #endif
173
174 /* EOF */