e396659fa61522f71f5e08d8efab31feaf51a5a6
[supertux.git] / src / video / drawing_context.h
1 //  $Id: drawing_context.h 2334 2005-04-04 16:26:14Z grumbel $
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 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 <stdint.h>
25
26 #include <SDL.h>
27
28 #include "math/vector.h"
29 #include "video/screen.h"
30 #include "video/surface.h"
31 #include "video/font.h"
32
33 class Surface;
34
35 // some constants for predefined layer values
36 enum {
37   LAYER_BACKGROUND0 = -300,
38   LAYER_BACKGROUND1 = -200,
39   LAYER_BACKGROUNDTILES = -100,
40   LAYER_TILES = 0,
41   LAYER_OBJECTS = 100,
42   LAYER_FOREGROUNDTILES = 200,
43   LAYER_FOREGROUND0 = 300,
44   LAYER_FOREGROUND1 = 400,
45   LAYER_GUI         = 500
46 };
47
48 /// Handles drawing of things.
49 /**
50  * This class provides functions for drawing things on screen. It also
51  * maintains a stack of transforms that are applied to graphics.
52  */
53 class DrawingContext
54 {
55 public:
56   DrawingContext(SDL_Surface* targetsurface = 0);
57   ~DrawingContext();
58   
59   /// Adds a drawing request for a surface into the request list.
60   void draw_surface(const Surface* surface, const Vector& position,
61                     int layer, uint32_t drawing_effect = NONE_EFFECT);
62   /// Adds a drawing request for part of a surface.
63   void draw_surface_part(const Surface* surface, const Vector& source,
64                          const Vector& size, const Vector& dest, int layer,
65                          uint32_t drawing_effect = NONE_EFFECT);
66   /// Draws a text.
67   void draw_text(const Font* font, const std::string& text,
68                  const Vector& position, FontAlignment alignment, int layer,
69                  uint32_t drawing_effect = NONE_EFFECT);
70   
71   /// Draws text on screen center (feed Vector.x with a 0).
72   /// This is the same as draw_text() with a SCREEN_WIDTH/2 position and
73   /// alignment set to LEFT_ALLIGN
74   void draw_center_text(const Font* font, const std::string& text,
75                         const Vector& position, int layer,
76                         uint32_t drawing_effect = NONE_EFFECT);
77   /// Draws a color gradient onto the whole screen */
78   void draw_gradient(Color from, Color to, int layer);
79   /// Fills a rectangle.
80   void draw_filled_rect(const Vector& topleft, const Vector& size,
81                         Color color, int layer);
82   
83   /// Processes all pending drawing requests and flushes the list.
84   void do_drawing();
85   
86   const Vector& get_translation() const
87   {  return transform.translation;  }
88   uint32_t get_drawing_effect() const
89   {  return transform.drawing_effect;  }
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(int effect);
99   /// apply that zoom in the next draws */
100   void set_zooming(float zoom);
101   /// apply that alpha in the next draws */
102   void set_alpha(int alpha);
103   
104 private:
105   class Transform
106   {
107   public:
108     Vector translation;
109     uint32_t drawing_effect;
110     float zoom;
111     int alpha;
112     
113     Transform()
114       : drawing_effect(NONE_EFFECT), zoom(1), alpha(255)
115     { }
116     
117     Vector apply(const Vector& v) const
118     {
119       return v - translation;
120     }
121   };
122   
123   /// the transform stack
124   std::vector<Transform> transformstack;
125   /// the currently active transform
126   Transform transform;
127   
128   enum RequestType
129   {
130     SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT
131   };
132   
133   struct SurfacePartRequest
134   {
135     const Surface* surface;
136     Vector source, size;
137   };
138   
139   struct TextRequest
140   {
141     const Font* font;
142     std::string text;
143     FontAlignment alignment;
144   };
145   
146   struct GradientRequest
147   {
148     Color top, bottom;
149     Vector size;
150   };
151   
152   struct FillRectRequest
153   {
154     Color color;
155     Vector size;
156   };
157   
158   struct DrawingRequest
159   {
160     RequestType type;
161     Vector pos;                
162     
163     int layer;
164     uint32_t drawing_effect;
165     float zoom;
166     int alpha;
167     
168     void* request_data;
169     
170     bool operator<(const DrawingRequest& other) const
171     {
172       return layer < other.layer;
173     }
174   };
175   
176   void draw_surface_part(DrawingRequest& request);
177   void draw_text(DrawingRequest& request);
178   void draw_text_center(DrawingRequest& request);
179   void draw_gradient(DrawingRequest& request);
180   void draw_filled_rect(DrawingRequest& request);
181   
182   typedef std::vector<DrawingRequest> DrawingRequests;
183   DrawingRequests drawingrequests;
184
185   SDL_Surface* screen;
186 };
187
188 #endif