1b4a4b38ff82b4be0f0595502e2cc3d2a5335cc2
[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 #define LIGHTMAP_DIV 5
37
38 static inline int next_po2(int val)
39 {
40   int result = 1;
41   while(result < val)
42     result *= 2;
43
44   return result;
45 }
46
47 DrawingContext::DrawingContext()
48   : ambient_color( 1.0f, 1.0f, 1.0f, 1.0f )
49 {
50   screen = SDL_GetVideoSurface();
51
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);
56
57   lightmap = new Texture(width, height, GL_RGB);
58
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);
62
63   requests = &drawing_requests;
64 }
65
66 DrawingContext::~DrawingContext()
67 {
68   texture_manager->remove_texture(lightmap);
69   delete lightmap;
70 }
71
72 void
73 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
74                              float angle, const Color& color, const Blend& blend,
75                              int layer)
76 {
77   assert(surface != 0);
78
79   DrawingRequest request;
80
81   request.type = SURFACE;
82   request.pos = transform.apply(position);
83
84   if(request.pos.x >= SCREEN_WIDTH || request.pos.y >= SCREEN_HEIGHT
85       || request.pos.x + surface->get_width() < 0
86       || request.pos.y + surface->get_height() < 0)
87     return;
88
89   request.layer = layer;
90   request.drawing_effect = transform.drawing_effect;
91   request.alpha = transform.alpha;
92   request.angle = angle;
93   request.color = color;
94   request.blend = blend;
95
96   request.request_data = const_cast<Surface*> (surface);
97
98   requests->push_back(request);
99 }
100
101 void
102 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
103     int layer)
104 {
105   draw_surface(surface, position, 0.0f, Color(1.0f, 1.0f, 1.0f), Blend(), layer);
106 }
107
108 void
109 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
110     const Vector& size, const Vector& dest, int layer)
111 {
112   assert(surface != 0);
113
114   DrawingRequest request;
115
116   request.type = SURFACE_PART;
117   request.pos = transform.apply(dest);
118   request.layer = layer;
119   request.drawing_effect = transform.drawing_effect;
120   request.alpha = transform.alpha;
121
122   SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
123   surfacepartrequest->size = size;
124   surfacepartrequest->source = source;
125   surfacepartrequest->surface = surface;
126
127   // clip on screen borders
128   if(request.pos.x < 0) {
129     surfacepartrequest->size.x += request.pos.x;
130     if(surfacepartrequest->size.x <= 0)
131       return;
132     surfacepartrequest->source.x -= request.pos.x;
133     request.pos.x = 0;
134   }
135   if(request.pos.y < 0) {
136     surfacepartrequest->size.y += request.pos.y;
137     if(surfacepartrequest->size.y <= 0)
138       return;
139     surfacepartrequest->source.y -= request.pos.y;
140     request.pos.y = 0;
141   }
142   request.request_data = surfacepartrequest;
143
144   requests->push_back(request);
145 }
146
147 void
148 DrawingContext::draw_text(const Font* font, const std::string& text,
149     const Vector& position, FontAlignment alignment, int layer)
150 {
151   DrawingRequest request;
152
153   request.type = TEXT;
154   request.pos = transform.apply(position);
155   request.layer = layer;
156   request.drawing_effect = transform.drawing_effect;
157   request.alpha = transform.alpha;
158
159   TextRequest* textrequest = new TextRequest;
160   textrequest->font = font;
161   textrequest->text = text;
162   textrequest->alignment = alignment;
163   request.request_data = textrequest;
164
165   requests->push_back(request);
166 }
167
168 void
169 DrawingContext::draw_center_text(const Font* font, const std::string& text,
170     const Vector& position, int layer)
171 {
172   draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
173       CENTER_ALLIGN, layer);
174 }
175
176 void
177 DrawingContext::draw_gradient(const Color& top, const Color& bottom, int layer)
178 {
179   DrawingRequest request;
180
181   request.type = GRADIENT;
182   request.pos = Vector(0,0);
183   request.layer = layer;
184
185   request.drawing_effect = transform.drawing_effect;
186   request.alpha = transform.alpha;
187
188   GradientRequest* gradientrequest = new GradientRequest;
189   gradientrequest->top = top;
190   gradientrequest->bottom = bottom;
191   request.request_data = gradientrequest;
192
193   requests->push_back(request);
194 }
195
196 void
197 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
198                                  const Color& color, int layer)
199 {
200   DrawingRequest request;
201
202   request.type = FILLRECT;
203   request.pos = transform.apply(topleft);
204   request.layer = layer;
205
206   request.drawing_effect = transform.drawing_effect;
207   request.alpha = transform.alpha;
208
209   FillRectRequest* fillrectrequest = new FillRectRequest;
210   fillrectrequest->size = size;
211   fillrectrequest->color = color;
212   fillrectrequest->color.alpha = color.alpha * transform.alpha;
213   request.request_data = fillrectrequest;
214
215   requests->push_back(request);
216 }
217
218 void
219 DrawingContext::draw_filled_rect(const Rect& rect, const Color& color,
220                                  int layer)
221 {
222   DrawingRequest request;
223
224   request.type = FILLRECT;
225   request.pos = transform.apply(rect.p1);
226   request.layer = layer;
227
228   request.drawing_effect = transform.drawing_effect;
229   request.alpha = transform.alpha;
230
231   FillRectRequest* fillrectrequest = new FillRectRequest;
232   fillrectrequest->size = Vector(rect.get_width(), rect.get_height());
233   fillrectrequest->color = color;
234   fillrectrequest->color.alpha = color.alpha * transform.alpha;
235   request.request_data = fillrectrequest;
236
237   requests->push_back(request);
238 }
239
240 void
241 DrawingContext::get_light(const Vector& position, Color* color)
242 {
243   if( ambient_color.red == 1.0f && ambient_color.green == 1.0f
244       && ambient_color.blue  == 1.0f ) {
245     *color = Color( 1.0f, 1.0f, 1.0f);
246     return;
247   }
248
249   DrawingRequest request;
250   request.type = GETLIGHT;
251   request.pos = transform.apply(position);
252
253   //There is no light offscreen. 
254   if(request.pos.x >= SCREEN_WIDTH || request.pos.y >= SCREEN_HEIGHT
255       || request.pos.x < 0 || request.pos.y < 0){
256     *color = Color( 0, 0, 0);
257     return;
258   }
259
260   request.layer = LAYER_GUI; //make sure all get_light requests are handled last.
261   GetLightRequest* getlightrequest = new GetLightRequest;
262   getlightrequest->color_ptr = color;
263   request.request_data = getlightrequest;
264   lightmap_requests.push_back(request);
265 }
266
267 void
268 DrawingContext::get_light(DrawingRequest& request)
269 {
270   GetLightRequest* getlightrequest = (GetLightRequest*) request.request_data;
271
272   float pixels[3];
273   for( int i = 0; i<3; i++)
274     pixels[i] = 0.0f; //set to black
275
276   float posX = request.pos.x /LIGHTMAP_DIV;
277   //TODO:this works when playing with 800x600 otherwise posY is wrong
278   float posY = screen->h - request.pos.y / LIGHTMAP_DIV;
279   glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
280     *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);  
281   //printf("get_light %f/%f =>%f/%f r%f g%f b%f\n", request.pos.x, request.pos.y, posX, posY, pixels[0], pixels[1], pixels[2]);
282
283   delete getlightrequest;
284 }
285
286 void
287 DrawingContext::draw_surface_part(DrawingRequest& request)
288 {
289   SurfacePartRequest* surfacepartrequest
290     = (SurfacePartRequest*) request.request_data;
291
292   surfacepartrequest->surface->draw_part(
293       surfacepartrequest->source.x, surfacepartrequest->source.y,
294       request.pos.x, request.pos.y,
295       surfacepartrequest->size.x, surfacepartrequest->size.y,
296       request.alpha, request.drawing_effect);
297
298   delete surfacepartrequest;
299 }
300
301 void
302 DrawingContext::draw_gradient(DrawingRequest& request)
303 {
304   GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
305   const Color& top = gradientrequest->top;
306   const Color& bottom = gradientrequest->bottom;
307
308   glDisable(GL_TEXTURE_2D);
309   glBegin(GL_QUADS);
310   glColor4f(top.red, top.green, top.blue, top.alpha);
311   glVertex2f(0, 0);
312   glVertex2f(SCREEN_WIDTH, 0);
313   glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
314   glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
315   glVertex2f(0, SCREEN_HEIGHT);
316   glEnd();
317   glEnable(GL_TEXTURE_2D);
318
319   delete gradientrequest;
320 }
321
322 void
323 DrawingContext::draw_text(DrawingRequest& request)
324 {
325   TextRequest* textrequest = (TextRequest*) request.request_data;
326
327   textrequest->font->draw(textrequest->text, request.pos,
328       textrequest->alignment, request.drawing_effect, request.alpha);
329
330   delete textrequest;
331 }
332
333 void
334 DrawingContext::draw_filled_rect(DrawingRequest& request)
335 {
336   FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
337
338   float x = request.pos.x;
339   float y = request.pos.y;
340   float w = fillrectrequest->size.x;
341   float h = fillrectrequest->size.y;
342
343   glDisable(GL_TEXTURE_2D);
344   glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
345             fillrectrequest->color.blue, fillrectrequest->color.alpha);
346
347   glBegin(GL_QUADS);
348   glVertex2f(x, y);
349   glVertex2f(x+w, y);
350   glVertex2f(x+w, y+h);
351   glVertex2f(x, y+h);
352   glEnd();
353   glEnable(GL_TEXTURE_2D);
354
355   delete fillrectrequest;
356 }
357
358 void
359 DrawingContext::draw_lightmap(DrawingRequest& request)
360 {
361   const Texture* texture = reinterpret_cast<Texture*> (request.request_data);
362
363   // multiple the lightmap with the framebuffer
364   glBlendFunc(GL_DST_COLOR, GL_ZERO);
365
366   glBindTexture(GL_TEXTURE_2D, texture->get_handle());
367   glBegin(GL_QUADS);
368
369   glTexCoord2f(0, lightmap_uv_bottom);
370   glVertex2f(0, 0);
371
372   glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
373   glVertex2f(SCREEN_WIDTH, 0);
374
375   glTexCoord2f(lightmap_uv_right, 0);
376   glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
377
378   glTexCoord2f(0, 0);
379   glVertex2f(0, SCREEN_HEIGHT);
380
381   glEnd();
382
383   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
384 }
385
386 void
387 DrawingContext::do_drawing()
388 {
389 #ifdef DEBUG
390   assert(transformstack.empty());
391   assert(target_stack.empty());
392 #endif
393   transformstack.clear();
394   target_stack.clear();
395
396   //Use Lightmap if ambient color is not white.
397   bool use_lightmap = ( ambient_color.red != 1.0f   || ambient_color.green != 1.0f ||
398                         ambient_color.blue  != 1.0f );
399
400   // PART1: create lightmap
401   if(use_lightmap) {
402     glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
403     glMatrixMode(GL_PROJECTION);
404     glLoadIdentity();
405     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
406     glMatrixMode(GL_MODELVIEW);
407     glLoadIdentity();
408
409     glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
410     glClear(GL_COLOR_BUFFER_BIT);
411     handle_drawing_requests(lightmap_requests);
412     lightmap_requests.clear();
413
414     glDisable(GL_BLEND);
415     glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
416     glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
417
418     glViewport(0, 0, screen->w, screen->h);
419     glMatrixMode(GL_PROJECTION);
420     glLoadIdentity();
421     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
422     glMatrixMode(GL_MODELVIEW);
423     glLoadIdentity();
424     glEnable(GL_BLEND);
425
426     // add a lightmap drawing request into the queue
427     DrawingRequest request;
428     request.type = LIGHTMAPREQUEST;
429     request.layer = LAYER_HUD - 1;
430     request.request_data = lightmap;
431     requests->push_back(request);
432   }
433
434   //glClear(GL_COLOR_BUFFER_BIT);
435   handle_drawing_requests(drawing_requests);
436   drawing_requests.clear();
437   assert_gl("drawing");
438
439   SDL_GL_SwapBuffers();
440 }
441
442 void
443 DrawingContext::handle_drawing_requests(DrawingRequests& requests)
444 {
445   std::stable_sort(requests.begin(), requests.end());
446
447   for(DrawingRequests::iterator i = requests.begin();
448       i != requests.end(); ++i) {
449     switch(i->type) {
450       case SURFACE:
451       {
452         const Surface* surface = (const Surface*) i->request_data;
453         if (i->angle == 0.0f && 
454             i->color.red == 1.0f && i->color.green == 1.0f  &&
455             i->color.blue == 1.0f &&  i->color.alpha == 1.0f  )
456           surface->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
457         else
458           surface->draw(i->pos.x, i->pos.y, i->alpha, i->angle, i->color, i->blend, i->drawing_effect);
459         break;
460       }
461       case SURFACE_PART:
462         draw_surface_part(*i);
463         break;
464       case GRADIENT:
465         draw_gradient(*i);
466         break;
467       case TEXT:
468         draw_text(*i);
469         break;
470       case FILLRECT:
471         draw_filled_rect(*i);
472         break;
473       case LIGHTMAPREQUEST:
474         draw_lightmap(*i);
475         break;
476       case GETLIGHT:
477         get_light(*i);
478         break;
479     }
480   }
481 }
482
483 void
484 DrawingContext::push_transform()
485 {
486   transformstack.push_back(transform);
487 }
488
489 void
490 DrawingContext::pop_transform()
491 {
492   assert(!transformstack.empty());
493
494   transform = transformstack.back();
495   transformstack.pop_back();
496 }
497
498 void
499 DrawingContext::set_drawing_effect(DrawingEffect effect)
500 {
501   transform.drawing_effect = effect;
502 }
503
504 DrawingEffect
505 DrawingContext::get_drawing_effect() const
506 {
507   return transform.drawing_effect;
508 }
509
510 void
511 DrawingContext::set_alpha(float alpha)
512 {
513   transform.alpha = alpha;
514 }
515
516 float
517 DrawingContext::get_alpha() const
518 {
519   return transform.alpha;
520 }
521
522 void
523 DrawingContext::push_target()
524 {
525   target_stack.push_back(target);
526 }
527
528 void
529 DrawingContext::pop_target()
530 {
531   set_target(target_stack.back());
532   target_stack.pop_back();
533 }
534
535 void
536 DrawingContext::set_target(Target target)
537 {
538   this->target = target;
539   if(target == LIGHTMAP)
540     requests = &lightmap_requests;
541   else
542     requests = &drawing_requests;
543 }
544
545 void
546 DrawingContext::set_ambient_color( Color new_color )
547 {
548   ambient_color = new_color;
549 }