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