4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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
29 #include <SDL_video.h>
31 #include "obstack/obstack.h"
32 #include "math/vector.hpp"
33 #include "math/rect.hpp"
34 #include "surface.hpp"
40 struct DrawingRequest;
42 // some constants for predefined layer values
44 LAYER_BACKGROUND0 = -300,
45 LAYER_BACKGROUND1 = -200,
46 LAYER_BACKGROUNDTILES = -100,
49 LAYER_FLOATINGOBJECTS = 150,
50 LAYER_FOREGROUNDTILES = 200,
51 LAYER_FOREGROUND0 = 300,
52 LAYER_FOREGROUND1 = 400,
64 : sfactor(GL_SRC_ALPHA), dfactor(GL_ONE_MINUS_SRC_ALPHA)
67 Blend(GLenum s, GLenum d)
68 : sfactor(s), dfactor(d)
73 * This class provides functions for drawing things on screen. It also
74 * maintains a stack of transforms that are applied to graphics.
82 /// Adds a drawing request for a surface into the request list.
83 void draw_surface(const Surface* surface, const Vector& position,
85 /// Adds a drawing request for a surface into the request list.
86 void draw_surface(const Surface* surface, const Vector& position,
87 float angle, const Color& color, const Blend& blend,
89 /// Adds a drawing request for part of a surface.
90 void draw_surface_part(const Surface* surface, const Vector& source,
91 const Vector& size, const Vector& dest, int layer);
93 void draw_text(const Font* font, const std::string& text,
94 const Vector& position, FontAlignment alignment, int layer);
96 /// Draws text on screen center (feed Vector.x with a 0).
97 /// This is the same as draw_text() with a SCREEN_WIDTH/2 position and
98 /// alignment set to LEFT_ALLIGN
99 void draw_center_text(const Font* font, const std::string& text,
100 const Vector& position, int layer);
101 /// Draws a color gradient onto the whole screen */
102 void draw_gradient(const Color& from, const Color& to, int layer);
103 /// Fills a rectangle.
104 void draw_filled_rect(const Vector& topleft, const Vector& size,
105 const Color& color, int layer);
106 void draw_filled_rect(const Rect& rect, const Color& color, int layer);
108 /// Processes all pending drawing requests and flushes the list.
111 const Vector& get_translation() const
112 { return transform.translation; }
114 void set_translation(const Vector& newtranslation)
115 { transform.translation = newtranslation; }
117 void push_transform();
118 void pop_transform();
120 /// Apply that effect in the next draws (effects are listed on surface.h).
121 void set_drawing_effect(DrawingEffect effect);
122 /// return currently applied drawing effect
123 DrawingEffect get_drawing_effect() const;
124 /// apply that alpha in the next draws (1.0 means fully opaque) */
125 void set_alpha(float alpha);
126 /// return currently set alpha
127 float get_alpha() const;
129 /// on next update, set color to lightmap's color at position
130 void get_light(const Vector& position, Color* color );
137 void set_target(Target target);
139 void set_ambient_color( Color new_color );
142 * requests that a screenshot be taken after the next frame has been rendered
144 void take_screenshot();
151 DrawingEffect drawing_effect;
155 : drawing_effect(NO_EFFECT), alpha(1.0f)
158 Vector apply(const Vector& v) const
160 return v - translation;
164 /// the transform stack
165 std::vector<Transform> transformstack;
166 /// the currently active transform
169 std::vector<Blend> blend_stack;
172 typedef std::vector<DrawingRequest*> DrawingRequests;
174 void handle_drawing_requests(DrawingRequests& requests) const;
175 void draw_surface_part(const DrawingRequest& request) const;
176 void draw_text(const DrawingRequest& request) const;
177 void draw_text_center(const DrawingRequest& request) const;
178 void draw_gradient(const DrawingRequest& request) const;
179 void draw_filled_rect(const DrawingRequest& request) const;
180 void draw_lightmap(const DrawingRequest& request) const;
181 void get_light(const DrawingRequest& request) const;
182 void do_take_screenshot();
184 DrawingRequests drawing_requests;
185 DrawingRequests lightmap_requests;
187 DrawingRequests* requests;
192 std::vector<Target> target_stack;
194 int lightmap_width, lightmap_height;
195 float lightmap_uv_right, lightmap_uv_bottom;
197 /* obstack holding the memory of the drawing requests */
200 bool screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */