New sector attribute 'ambient-light'.
[supertux.git] / src / video / drawing_context.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 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
20 #include <config.h>
21
22 #include <algorithm>
23 #include <cassert>
24 #include <iostream>
25 #include <SDL_image.h>
26 #include <GL/gl.h>
27
28 #include "drawing_context.hpp"
29 #include "surface.hpp"
30 #include "font.hpp"
31 #include "main.hpp"
32 #include "gameconfig.hpp"
33 #include "glutil.hpp"
34 #include "texture.hpp"
35 #include "texture_manager.hpp"
36
37 #define LIGHTMAP_DIV 4
38
39 static inline int next_po2(int val)
40 {
41   int result = 1;
42   while(result < val)
43     result *= 2;
44
45   return result;
46 }
47
48 DrawingContext::DrawingContext(): 
49   ambient_color( 1.0f, 1.0f, 1.0f, 1.0f )
50 {
51   screen = SDL_GetVideoSurface();
52
53   lightmap_width = screen->w / LIGHTMAP_DIV;
54   lightmap_height = screen->h / LIGHTMAP_DIV;
55   unsigned int width = next_po2(lightmap_width);
56   unsigned int height = next_po2(lightmap_height);
57
58   lightmap = new Texture(width, height, GL_RGB);
59
60   lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
61   lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
62   texture_manager->register_texture(lightmap);
63
64   requests = &drawing_requests;
65 }
66
67 DrawingContext::~DrawingContext()
68 {
69   texture_manager->remove_texture(lightmap);
70   delete lightmap;
71 }
72
73 void
74 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
75                              float angle, const Color& color, const Blend& blend,
76                              int layer)
77 {
78   assert(surface != 0);
79
80   DrawingRequest request;
81
82   request.type = SURFACE;
83   request.pos = transform.apply(position);
84
85   if(request.pos.x >= SCREEN_WIDTH || request.pos.y >= SCREEN_HEIGHT
86       || request.pos.x + surface->get_width() < 0
87       || request.pos.y + surface->get_height() < 0)
88     return;
89
90   request.layer = layer;
91   request.drawing_effect = transform.drawing_effect;
92   request.alpha = transform.alpha;
93   request.angle = angle;
94   request.color = color;
95   request.blend = blend;
96
97   request.request_data = const_cast<Surface*> (surface);
98
99   requests->push_back(request);
100 }
101
102 void
103 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
104     int layer)
105 {
106   draw_surface(surface, position, 0.0f, Color(1.0f, 1.0f, 1.0f), Blend(), layer);
107 }
108
109 void
110 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
111     const Vector& size, const Vector& dest, int layer)
112 {
113   assert(surface != 0);
114
115   DrawingRequest request;
116
117   request.type = SURFACE_PART;
118   request.pos = transform.apply(dest);
119   request.layer = layer;
120   request.drawing_effect = transform.drawing_effect;
121   request.alpha = transform.alpha;
122
123   SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
124   surfacepartrequest->size = size;
125   surfacepartrequest->source = source;
126   surfacepartrequest->surface = surface;
127
128   // clip on screen borders
129   if(request.pos.x < 0) {
130     surfacepartrequest->size.x += request.pos.x;
131     if(surfacepartrequest->size.x <= 0)
132       return;
133     surfacepartrequest->source.x -= request.pos.x;
134     request.pos.x = 0;
135   }
136   if(request.pos.y < 0) {
137     surfacepartrequest->size.y += request.pos.y;
138     if(surfacepartrequest->size.y <= 0)
139       return;
140     surfacepartrequest->source.y -= request.pos.y;
141     request.pos.y = 0;
142   }
143   request.request_data = surfacepartrequest;
144
145   requests->push_back(request);
146 }
147
148 void
149 DrawingContext::draw_text(const Font* font, const std::string& text,
150     const Vector& position, FontAlignment alignment, int layer)
151 {
152   DrawingRequest request;
153
154   request.type = TEXT;
155   request.pos = transform.apply(position);
156   request.layer = layer;
157   request.drawing_effect = transform.drawing_effect;
158   request.alpha = transform.alpha;
159
160   TextRequest* textrequest = new TextRequest;
161   textrequest->font = font;
162   textrequest->text = text;
163   textrequest->alignment = alignment;
164   request.request_data = textrequest;
165
166   requests->push_back(request);
167 }
168
169 void
170 DrawingContext::draw_center_text(const Font* font, const std::string& text,
171     const Vector& position, int layer)
172 {
173   draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
174       CENTER_ALLIGN, layer);
175 }
176
177 void
178 DrawingContext::draw_gradient(const Color& top, const Color& bottom, int layer)
179 {
180   DrawingRequest request;
181
182   request.type = GRADIENT;
183   request.pos = Vector(0,0);
184   request.layer = layer;
185
186   request.drawing_effect = transform.drawing_effect;
187   request.alpha = transform.alpha;
188
189   GradientRequest* gradientrequest = new GradientRequest;
190   gradientrequest->top = top;
191   gradientrequest->bottom = bottom;
192   request.request_data = gradientrequest;
193
194   requests->push_back(request);
195 }
196
197 void
198 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
199                                  const Color& color, int layer)
200 {
201   DrawingRequest request;
202
203   request.type = FILLRECT;
204   request.pos = transform.apply(topleft);
205   request.layer = layer;
206
207   request.drawing_effect = transform.drawing_effect;
208   request.alpha = transform.alpha;
209
210   FillRectRequest* fillrectrequest = new FillRectRequest;
211   fillrectrequest->size = size;
212   fillrectrequest->color = color;
213   fillrectrequest->color.alpha = color.alpha * transform.alpha;
214   request.request_data = fillrectrequest;
215
216   requests->push_back(request);
217 }
218
219 void
220 DrawingContext::draw_filled_rect(const Rect& rect, const Color& color,
221                                  int layer)
222 {
223   DrawingRequest request;
224
225   request.type = FILLRECT;
226   request.pos = transform.apply(rect.p1);
227   request.layer = layer;
228
229   request.drawing_effect = transform.drawing_effect;
230   request.alpha = transform.alpha;
231
232   FillRectRequest* fillrectrequest = new FillRectRequest;
233   fillrectrequest->size = Vector(rect.get_width(), rect.get_height());
234   fillrectrequest->color = color;
235   fillrectrequest->color.alpha = color.alpha * transform.alpha;
236   request.request_data = fillrectrequest;
237
238   requests->push_back(request);
239 }
240
241 void
242 DrawingContext::draw_surface_part(DrawingRequest& request)
243 {
244   SurfacePartRequest* surfacepartrequest
245     = (SurfacePartRequest*) request.request_data;
246
247   surfacepartrequest->surface->draw_part(
248       surfacepartrequest->source.x, surfacepartrequest->source.y,
249       request.pos.x, request.pos.y,
250       surfacepartrequest->size.x, surfacepartrequest->size.y,
251       request.alpha, request.drawing_effect);
252
253   delete surfacepartrequest;
254 }
255
256 void
257 DrawingContext::draw_gradient(DrawingRequest& request)
258 {
259   GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
260   const Color& top = gradientrequest->top;
261   const Color& bottom = gradientrequest->bottom;
262
263   glDisable(GL_TEXTURE_2D);
264   glBegin(GL_QUADS);
265   glColor4f(top.red, top.green, top.blue, top.alpha);
266   glVertex2f(0, 0);
267   glVertex2f(SCREEN_WIDTH, 0);
268   glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
269   glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
270   glVertex2f(0, SCREEN_HEIGHT);
271   glEnd();
272   glEnable(GL_TEXTURE_2D);
273
274   delete gradientrequest;
275 }
276
277 void
278 DrawingContext::draw_text(DrawingRequest& request)
279 {
280   TextRequest* textrequest = (TextRequest*) request.request_data;
281
282   textrequest->font->draw(textrequest->text, request.pos,
283       textrequest->alignment, request.drawing_effect, request.alpha);
284
285   delete textrequest;
286 }
287
288 void
289 DrawingContext::draw_filled_rect(DrawingRequest& request)
290 {
291   FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
292
293   float x = request.pos.x;
294   float y = request.pos.y;
295   float w = fillrectrequest->size.x;
296   float h = fillrectrequest->size.y;
297
298   glDisable(GL_TEXTURE_2D);
299   glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
300             fillrectrequest->color.blue, fillrectrequest->color.alpha);
301
302   glBegin(GL_QUADS);
303   glVertex2f(x, y);
304   glVertex2f(x+w, y);
305   glVertex2f(x+w, y+h);
306   glVertex2f(x, y+h);
307   glEnd();
308   glEnable(GL_TEXTURE_2D);
309
310   delete fillrectrequest;
311 }
312
313 void
314 DrawingContext::do_drawing()
315 {
316 #ifdef DEBUG
317   assert(transformstack.empty());
318   assert(target_stack.empty());
319 #endif
320   transformstack.clear();
321   target_stack.clear();
322
323   //Use Lightmap if ambient color is not white.
324   bool use_lightmap = ( ambient_color.red != 1.0f   || ambient_color.green != 1.0f ||
325                         ambient_color.blue  != 1.0f );
326
327   // PART1: create lightmap
328   if(use_lightmap) {
329     glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
330     glMatrixMode(GL_PROJECTION);
331     glLoadIdentity();
332     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
333     glMatrixMode(GL_MODELVIEW);
334     glLoadIdentity();
335
336     glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
337     glClear(GL_COLOR_BUFFER_BIT);
338     handle_drawing_requests(lightmap_requests);
339     lightmap_requests.clear();
340
341     glDisable(GL_BLEND);
342     glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
343     glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
344
345     glViewport(0, 0, screen->w, screen->h);
346     glMatrixMode(GL_PROJECTION);
347     glLoadIdentity();
348     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
349     glMatrixMode(GL_MODELVIEW);
350     glLoadIdentity();
351     glEnable(GL_BLEND);
352   }
353
354   //glClear(GL_COLOR_BUFFER_BIT);
355   handle_drawing_requests(drawing_requests);
356   drawing_requests.clear();
357
358   if(use_lightmap) {
359     // multiple the lightmap with the framebuffer
360     glBlendFunc(GL_DST_COLOR, GL_ZERO);
361
362     glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
363     glBegin(GL_QUADS);
364
365     glTexCoord2f(0, lightmap_uv_bottom);
366     glVertex2f(0, 0);
367
368     glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
369     glVertex2f(SCREEN_WIDTH, 0);
370
371     glTexCoord2f(lightmap_uv_right, 0);
372     glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
373
374     glTexCoord2f(0, 0);
375     glVertex2f(0, SCREEN_HEIGHT);
376
377     glEnd();
378
379     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
380   }
381
382   assert_gl("drawing");
383
384   SDL_GL_SwapBuffers();
385 }
386
387 void
388 DrawingContext::handle_drawing_requests(DrawingRequests& requests)
389 {
390   std::stable_sort(requests.begin(), requests.end());
391
392   for(DrawingRequests::iterator i = requests.begin();
393       i != requests.end(); ++i) {
394     switch(i->type) {
395       case SURFACE:
396       {
397         const Surface* surface = (const Surface*) i->request_data;
398         if (i->angle == 0.0f)
399           surface->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
400         else
401           surface->draw(i->pos.x, i->pos.y, i->alpha, i->angle, i->color, i->blend, i->drawing_effect);
402         break;
403       }
404       case SURFACE_PART:
405         draw_surface_part(*i);
406         break;
407       case GRADIENT:
408         draw_gradient(*i);
409         break;
410       case TEXT:
411         draw_text(*i);
412         break;
413       case FILLRECT:
414         draw_filled_rect(*i);
415         break;
416     }
417   }
418 }
419
420 void
421 DrawingContext::push_transform()
422 {
423   transformstack.push_back(transform);
424 }
425
426 void
427 DrawingContext::pop_transform()
428 {
429   assert(!transformstack.empty());
430
431   transform = transformstack.back();
432   transformstack.pop_back();
433 }
434
435 void
436 DrawingContext::set_drawing_effect(DrawingEffect effect)
437 {
438   transform.drawing_effect = effect;
439 }
440
441 DrawingEffect
442 DrawingContext::get_drawing_effect() const
443 {
444   return transform.drawing_effect;
445 }
446
447 void
448 DrawingContext::set_alpha(float alpha)
449 {
450   transform.alpha = alpha;
451 }
452
453 float
454 DrawingContext::get_alpha() const
455 {
456   return transform.alpha;
457 }
458
459 void
460 DrawingContext::push_target()
461 {
462   target_stack.push_back(target);
463 }
464
465 void
466 DrawingContext::pop_target()
467 {
468   set_target(target_stack.back());
469   target_stack.pop_back();
470 }
471
472 void
473 DrawingContext::set_target(Target target)
474 {
475   this->target = target;
476   if(target == LIGHTMAP)
477     requests = &lightmap_requests;
478   else
479     requests = &drawing_requests;
480 }
481
482 void
483 DrawingContext::set_ambient_color( Color new_color )
484 {
485   ambient_color = new_color;
486 }