fixed type :) yeah, the really important commit today! :)
[supertux.git] / lib / video / 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 "math/vector.h"
28 #include "video/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 /**
47  * This class provides functions for drawing things on screen. It also
48  * maintains a stack of transforms that are applied to graphics.
49  */
50 class DrawingContext
51 {
52 public:
53   DrawingContext();
54   ~DrawingContext();
55
56   /** Adds a drawing request for a surface into the request list */
57   void draw_surface(const Surface* surface, const Vector& position, int layer,
58       Uint32 drawing_effect = NONE_EFFECT);
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       Uint32 drawing_effect = NONE_EFFECT);
63   /** draws a text */
64   void draw_text(Font* font, const std::string& text, const Vector& position,
65       int layer, Uint32 drawing_effect = NONE_EFFECT);
66   /** draws aligned text */
67   void draw_text_center(Font* font, const std::string& text,
68       const Vector& position, int layer, Uint32 drawing_effect = NONE_EFFECT);
69   /** draws a color gradient onto the whole screen */  
70   void draw_gradient(Color from, Color to, int layer);
71   /** fills a rectangle */
72   void draw_filled_rect(const Vector& topleft, const Vector& size,
73           Color color, int layer);
74   
75   /** Processes all pending drawing requests and flushes the list */
76   void do_drawing();
77
78   const Vector& get_translation() const
79   { return transform.translation; }
80   void set_translation(const Vector& newtranslation)
81   { transform.translation = newtranslation; }
82
83   void push_transform();
84   void pop_transform();
85
86   /** apply that effect in the next draws (effects are listed on surface.h) */
87   void set_drawing_effect(int effect);
88
89 private:
90   class Transform
91   {
92   public:
93     Vector translation; // only translation for now...
94
95     Vector apply(const Vector& v) const
96     {
97       return v - translation;
98     }
99
100     int draw_effect;
101   };
102
103   /// the transform stack
104   std::vector<Transform> transformstack;
105   /// the currently active transform
106   Transform transform;
107
108   enum RequestType
109   {
110     SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT
111   };
112
113   struct SurfacePartRequest
114   {
115     const Surface* surface;
116     Vector source, size;
117   };
118
119   struct TextRequest
120   {
121     Font* font;
122     std::string text;
123   };
124
125   struct GradientRequest
126   {
127     Color top, bottom;
128     Vector size;
129   };
130
131   struct FillRectRequest
132   {
133     Color color;
134     Vector size;
135   };
136
137   struct DrawingRequest
138   {
139     int layer;
140     Uint32 drawing_effect;
141
142     RequestType type;
143     Vector pos;
144
145     void* request_data;
146
147     bool operator<(const DrawingRequest& other) const
148     {
149       return layer < other.layer;
150     }
151   };
152
153   void draw_surface_part(DrawingRequest& request);
154   void draw_text(DrawingRequest& request);
155   void draw_gradient(DrawingRequest& request);
156   void draw_filled_rect(DrawingRequest& request);
157   
158   typedef std::vector<DrawingRequest> DrawingRequests;
159   DrawingRequests drawingrequests;
160 };
161
162 #endif /*SUPERTUX_DRAWINGCONTEXT_H*/
163