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