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