1 // $Id: drawing_context.cpp 2334 2005-04-04 16:26:14Z grumbel $
3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 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.
24 #include <SDL_image.h>
28 #include "drawing_context.hpp"
29 #include "surface.hpp"
32 #include "gameconfig.hpp"
34 #include "texture.hpp"
35 #include "texture_manager.hpp"
37 #define LIGHTMAP_DIV 4
39 static inline int next_po2(int val)
48 DrawingContext::DrawingContext()
50 screen = SDL_GetVideoSurface();
52 lightmap_width = screen->w / LIGHTMAP_DIV;
53 lightmap_height = screen->h / LIGHTMAP_DIV;
54 unsigned int width = next_po2(lightmap_width);
55 unsigned int height = next_po2(lightmap_height);
57 lightmap = new Texture(width, height, GL_RGB);
59 lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
60 lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
61 texture_manager->register_texture(lightmap);
63 requests = &drawing_requests;
66 DrawingContext::~DrawingContext()
68 texture_manager->remove_texture(lightmap);
73 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
78 DrawingRequest request;
80 request.type = SURFACE;
81 request.pos = transform.apply(position);
83 if(request.pos.x >= SCREEN_WIDTH || request.pos.y >= SCREEN_HEIGHT
84 || request.pos.x + surface->get_width() < 0
85 || request.pos.y + surface->get_height() < 0)
88 request.layer = layer;
89 request.drawing_effect = transform.drawing_effect;
90 request.alpha = transform.alpha;
91 request.request_data = const_cast<Surface*> (surface);
93 requests->push_back(request);
97 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
98 const Vector& size, const Vector& dest, int layer)
100 assert(surface != 0);
102 DrawingRequest request;
104 request.type = SURFACE_PART;
105 request.pos = transform.apply(dest);
106 request.layer = layer;
107 request.drawing_effect = transform.drawing_effect;
108 request.alpha = transform.alpha;
110 SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
111 surfacepartrequest->size = size;
112 surfacepartrequest->source = source;
113 surfacepartrequest->surface = surface;
115 // clip on screen borders
116 if(request.pos.x < 0) {
117 surfacepartrequest->size.x += request.pos.x;
118 if(surfacepartrequest->size.x <= 0)
120 surfacepartrequest->source.x -= request.pos.x;
123 if(request.pos.y < 0) {
124 surfacepartrequest->size.y += request.pos.y;
125 if(surfacepartrequest->size.y <= 0)
127 surfacepartrequest->source.y -= request.pos.y;
130 request.request_data = surfacepartrequest;
132 requests->push_back(request);
136 DrawingContext::draw_text(const Font* font, const std::string& text,
137 const Vector& position, FontAlignment alignment, int layer)
139 DrawingRequest request;
142 request.pos = transform.apply(position);
143 request.layer = layer;
144 request.drawing_effect = transform.drawing_effect;
145 request.alpha = transform.alpha;
147 TextRequest* textrequest = new TextRequest;
148 textrequest->font = font;
149 textrequest->text = text;
150 textrequest->alignment = alignment;
151 request.request_data = textrequest;
153 requests->push_back(request);
157 DrawingContext::draw_center_text(const Font* font, const std::string& text,
158 const Vector& position, int layer)
160 draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
161 CENTER_ALLIGN, layer);
165 DrawingContext::draw_gradient(const Color& top, const Color& bottom, int layer)
167 DrawingRequest request;
169 request.type = GRADIENT;
170 request.pos = Vector(0,0);
171 request.layer = layer;
173 request.drawing_effect = transform.drawing_effect;
174 request.alpha = transform.alpha;
176 GradientRequest* gradientrequest = new GradientRequest;
177 gradientrequest->top = top;
178 gradientrequest->bottom = bottom;
179 request.request_data = gradientrequest;
181 requests->push_back(request);
185 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
186 const Color& color, int layer)
188 DrawingRequest request;
190 request.type = FILLRECT;
191 request.pos = transform.apply(topleft);
192 request.layer = layer;
194 request.drawing_effect = transform.drawing_effect;
195 request.alpha = transform.alpha;
197 FillRectRequest* fillrectrequest = new FillRectRequest;
198 fillrectrequest->size = size;
199 fillrectrequest->color = color;
200 fillrectrequest->color.alpha = color.alpha * transform.alpha;
201 request.request_data = fillrectrequest;
203 requests->push_back(request);
207 DrawingContext::draw_surface_part(DrawingRequest& request)
209 SurfacePartRequest* surfacepartrequest
210 = (SurfacePartRequest*) request.request_data;
212 surfacepartrequest->surface->draw_part(
213 surfacepartrequest->source.x, surfacepartrequest->source.y,
214 request.pos.x, request.pos.y,
215 surfacepartrequest->size.x, surfacepartrequest->size.y,
216 request.alpha, request.drawing_effect);
218 delete surfacepartrequest;
222 DrawingContext::draw_gradient(DrawingRequest& request)
224 GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
225 const Color& top = gradientrequest->top;
226 const Color& bottom = gradientrequest->bottom;
228 glDisable(GL_TEXTURE_2D);
230 glColor4f(top.red, top.green, top.blue, top.alpha);
232 glVertex2f(SCREEN_WIDTH, 0);
233 glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
234 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
235 glVertex2f(0, SCREEN_HEIGHT);
237 glEnable(GL_TEXTURE_2D);
239 delete gradientrequest;
243 DrawingContext::draw_text(DrawingRequest& request)
245 TextRequest* textrequest = (TextRequest*) request.request_data;
247 textrequest->font->draw(textrequest->text, request.pos,
248 textrequest->alignment, request.drawing_effect, request.alpha);
254 DrawingContext::draw_filled_rect(DrawingRequest& request)
256 FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
258 float x = request.pos.x;
259 float y = request.pos.y;
260 float w = fillrectrequest->size.x;
261 float h = fillrectrequest->size.y;
263 glDisable(GL_TEXTURE_2D);
264 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
265 fillrectrequest->color.blue, fillrectrequest->color.alpha);
270 glVertex2f(x+w, y+h);
273 glEnable(GL_TEXTURE_2D);
275 delete fillrectrequest;
279 DrawingContext::do_drawing()
282 assert(transformstack.empty());
283 assert(target_stack.empty());
285 transformstack.clear();
286 target_stack.clear();
288 bool use_lightmap = lightmap_requests.size() != 0;
290 // PART1: create lightmap
292 glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
293 glMatrixMode(GL_PROJECTION);
295 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
296 glMatrixMode(GL_MODELVIEW);
299 //glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
300 glClearColor(0, 0, 0, 1);
301 glClear(GL_COLOR_BUFFER_BIT);
302 handle_drawing_requests(lightmap_requests);
303 lightmap_requests.clear();
306 glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
307 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
309 glViewport(0, 0, screen->w, screen->h);
310 glMatrixMode(GL_PROJECTION);
312 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
313 glMatrixMode(GL_MODELVIEW);
318 //glClear(GL_COLOR_BUFFER_BIT);
319 handle_drawing_requests(drawing_requests);
320 drawing_requests.clear();
323 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
325 glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
328 glTexCoord2f(0, lightmap_uv_bottom);
331 glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
332 glVertex2f(SCREEN_WIDTH, 0);
334 glTexCoord2f(lightmap_uv_right, 0);
335 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
338 glVertex2f(0, SCREEN_HEIGHT);
342 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
345 assert_gl("drawing");
347 SDL_GL_SwapBuffers();
351 DrawingContext::handle_drawing_requests(DrawingRequests& requests)
353 std::stable_sort(requests.begin(), requests.end());
355 for(DrawingRequests::iterator i = requests.begin();
356 i != requests.end(); ++i) {
360 const Surface* surface = (const Surface*) i->request_data;
361 surface->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
365 draw_surface_part(*i);
374 draw_filled_rect(*i);
381 DrawingContext::push_transform()
383 transformstack.push_back(transform);
387 DrawingContext::pop_transform()
389 assert(!transformstack.empty());
391 transform = transformstack.back();
392 transformstack.pop_back();
396 DrawingContext::set_drawing_effect(DrawingEffect effect)
398 transform.drawing_effect = effect;
402 DrawingContext::get_drawing_effect() const
404 return transform.drawing_effect;
408 DrawingContext::set_alpha(float alpha)
410 transform.alpha = alpha;
414 DrawingContext::get_alpha() const
416 return transform.alpha;
420 DrawingContext::push_target()
422 target_stack.push_back(target);
426 DrawingContext::pop_target()
428 set_target(target_stack.back());
429 target_stack.pop_back();
433 DrawingContext::set_target(Target target)
435 this->target = target;
436 if(target == LIGHTMAP)
437 requests = &lightmap_requests;
439 requests = &drawing_requests;