5f392ef0e7bfda86fbb5212d2d0dcb0cdcf29c13
[supertux.git] / src / video / drawing_context.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #ifndef SUPERTUX_DRAWINGCONTEXT_H
20 #define SUPERTUX_DRAWINGCONTEXT_H
21
22 #include <vector>
23 #include <string>
24 #include <memory>
25
26 #include <stdint.h>
27
28 #include "obstack/obstack.h"
29 #include "math/vector.hpp"
30 #include "math/rect.hpp"
31 #include "color.hpp"
32 #include "font.hpp"
33 #include "drawing_request.hpp"
34
35 class Surface;
36 class Texture;
37 class Renderer;
38 class Lightmap;
39
40 /**
41  * This class provides functions for drawing things on screen. It also
42  * maintains a stack of transforms that are applied to graphics.
43  */
44 class DrawingContext
45 {
46 public:
47   DrawingContext();
48   ~DrawingContext();
49
50   void init_renderer();
51
52   /// Adds a drawing request for a surface into the request list.
53   void draw_surface(const Surface* surface, const Vector& position,
54                     int layer);
55   /// Adds a drawing request for a surface into the request list.
56   void draw_surface(const Surface* surface, const Vector& position,
57                     float angle, const Color& color, const Blend& blend,
58                     int layer);
59   /// Adds a drawing request for part of a surface.
60   void draw_surface_part(const Surface* surface, const Vector& source,
61                          const Vector& size, const Vector& dest, int layer);
62   /// Draws a text.
63   void draw_text(const Font* font, const std::string& text,
64                  const Vector& position, FontAlignment alignment, int layer, Color color = Color(1.0,1.0,1.0));
65
66   /// Draws text on screen center (feed Vector.x with a 0).
67   /// This is the same as draw_text() with a SCREEN_WIDTH/2 position and
68   /// alignment set to LEFT_ALIGN
69   void draw_center_text(const Font* font, const std::string& text,
70                         const Vector& position, int layer, Color color = Color(1.0,1.0,1.0));
71   /// Draws a color gradient onto the whole screen */
72   void draw_gradient(const Color& from, const Color& to, int layer);
73   /// Fills a rectangle.
74   void draw_filled_rect(const Vector& topleft, const Vector& size,
75                         const Color& color, int layer);
76   void draw_filled_rect(const Rect& rect, const Color& color, int layer);
77   void draw_filled_rect(const Rect& rect, const Color& color, float radius, int layer);
78
79   void draw_inverse_ellipse(const Vector& pos, const Vector& size, const Color& color, int layer);
80
81   /// Processes all pending drawing requests and flushes the list.
82   void do_drawing();
83
84   const Vector& get_translation() const
85   {  return transform.translation;  }
86
87   void set_translation(const Vector& newtranslation)
88   {  transform.translation = newtranslation;  }
89
90   void push_transform();
91   void pop_transform();
92
93   /// Apply that effect in the next draws (effects are listed on surface.h).
94   void set_drawing_effect(DrawingEffect effect);
95   /// return currently applied drawing effect
96   DrawingEffect get_drawing_effect() const;
97   /// apply that alpha in the next draws (1.0 means fully opaque) */
98   void set_alpha(float alpha);
99   /// return currently set alpha
100   float get_alpha() const;
101
102   /// on next update, set color to lightmap's color at position
103   void get_light(const Vector& position, Color* color );
104
105   typedef ::Target Target;
106   static const Target NORMAL = ::NORMAL;
107   static const Target LIGHTMAP = ::LIGHTMAP;
108   void push_target();
109   void pop_target();
110   void set_target(Target target);
111
112   void set_ambient_color( Color new_color );
113
114   /**
115    * requests that a screenshot be taken after the next frame has been rendered
116    */
117   void take_screenshot();
118
119 private:
120   class Transform
121   {
122   public:
123     Vector translation;
124     DrawingEffect drawing_effect;
125     float alpha;
126
127     Transform()
128       : drawing_effect(NO_EFFECT), alpha(1.0f)
129     { }
130
131     Vector apply(const Vector& v) const
132     {
133       return v - translation;
134     }
135   };
136
137   Renderer *renderer;
138   Lightmap *lightmap;
139
140   /// the transform stack
141   std::vector<Transform> transformstack;
142   /// the currently active transform
143   Transform transform;
144
145   std::vector<Blend> blend_stack;
146   Blend blend_mode;
147
148   typedef std::vector<DrawingRequest*> DrawingRequests;
149
150   void handle_drawing_requests(DrawingRequests& requests);
151
152   DrawingRequests drawing_requests;
153   DrawingRequests lightmap_requests;
154
155   DrawingRequests* requests;
156   Color ambient_color;
157
158   Target target;
159   std::vector<Target> target_stack;
160
161   /* obstack holding the memory of the drawing requests */
162   struct obstack obst;
163
164   bool screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */
165 };
166
167 #endif
168