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