0051301b8006ac77f16e65f058a3caab27fc8ade
[supertux.git] / src / screen / drawing_context.h
1 //  $Id$
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
25 #include "SDL.h"
26
27 #include "vector.h"
28 #include "screen.h"
29
30 class Surface;
31 class Font;
32
33 // some constants for predefined layer values
34 enum {
35   LAYER_BACKGROUND0 = -300,
36   LAYER_BACKGROUND1 = -200,
37   LAYER_BACKGROUNDTILES = -100,
38   LAYER_TILES = 0,
39   LAYER_OBJECTS = 100,
40   LAYER_FOREGROUNDTILES = 200,
41   LAYER_FOREGROUND0 = 300,
42   LAYER_FOREGROUND1 = 400,
43   LAYER_GUI         = 500
44 };
45
46 class Color
47 {
48 public:
49   Color() 
50     : red(0), green(0), blue(0), alpha(0)
51   {}
52   
53   Color(Uint8 red_, Uint8 green_, Uint8 blue_, Uint8 alpha_ = 0)
54     : red(red_), green(green_), blue(blue_), alpha(alpha_)
55   {}
56
57   Color(const Color& o)
58     : red(o.red), green(o.green), blue(o.blue), alpha(o.alpha)
59   { }
60
61   Uint8 red, green, blue, alpha;
62 };
63
64 /**
65  * This class provides functions for drawing things on screen. It also
66  * maintains a stack of transforms that are applied to graphics.
67  */
68 class DrawingContext
69 {
70 public:
71   DrawingContext();
72   ~DrawingContext();
73
74   /** Adds a drawing request for a surface into the request list */
75   void draw_surface(const Surface* surface, const Vector& position, int layer,
76       Uint32 drawing_effect = NONE_EFFECT);
77   /** Adds a drawing request for part of a surface */
78   void draw_surface_part(const Surface* surface, const Vector& source,
79       const Vector& size, const Vector& dest, int layer,
80       Uint32 drawing_effect = NONE_EFFECT);
81   /** draws a text */
82   void draw_text(Font* font, const std::string& text, const Vector& position,
83       int layer);
84   /** draws aligned text */
85   void draw_text_center(Font* font, const std::string& text,
86       const Vector& position, int layer);
87   /** draws a color gradient onto the whole screen */  
88   void draw_gradient(Color from, Color to, int layer);
89   /** fills a rectangle */
90   void draw_filled_rect(const Vector& topleft, const Vector& downright,
91           Color color, int layer);
92   
93   /** Processes all pending drawing requests and flushes the list */
94   void do_drawing();
95
96   const Vector& get_translation() const
97   { return transform.translation; }
98   void set_translation(const Vector& newtranslation)
99   { transform.translation = newtranslation; }
100
101   void push_transform();
102   void pop_transform();
103
104 private:
105   class Transform
106   {
107   public:
108     Vector translation; // only translation for now...
109
110     Vector apply(const Vector& v) const
111     {
112       return v - translation;
113     }
114   };
115
116   /// the transform stack
117   std::vector<Transform> transformstack;
118   /// the currently active transform
119   Transform transform;
120
121   enum RequestType
122   {
123     SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT
124   };
125
126   struct SurfacePartRequest
127   {
128     const Surface* surface;
129     Vector source, size;
130   };
131
132   struct TextRequest
133   {
134     Font* font;
135     std::string text;
136   };
137
138   struct GradientRequest
139   {
140     Color top, bottom;
141     Vector size;
142   };
143
144   struct FillRectRequest
145   {
146     Color color;
147     Vector size;
148   };
149
150   struct DrawingRequest
151   {
152     int layer;
153     Uint32 drawing_effect;
154
155     RequestType type;
156     Vector pos;
157
158     void* request_data;
159
160     bool operator<(const DrawingRequest& other) const
161     {
162       return layer < other.layer;
163     }
164   };
165
166   void draw_surface_part(DrawingRequest& request);
167   void draw_text(DrawingRequest& request);
168   void draw_gradient(DrawingRequest& request);
169   void draw_filled_rect(DrawingRequest& request);
170   
171   typedef std::vector<DrawingRequest> DrawingRequests;
172   DrawingRequests drawingrequests;
173 };
174
175 #endif /*SUPERTUX_DRAWINGCONTEXT_H*/
176