fix cr/lfs and remove trailing whitespaces...
[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 {
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::draw_surface_part(DrawingRequest& request)
242 {
243   SurfacePartRequest* surfacepartrequest
244     = (SurfacePartRequest*) request.request_data;
245
246   surfacepartrequest->surface->draw_part(
247       surfacepartrequest->source.x, surfacepartrequest->source.y,
248       request.pos.x, request.pos.y,
249       surfacepartrequest->size.x, surfacepartrequest->size.y,
250       request.alpha, request.drawing_effect);
251
252   delete surfacepartrequest;
253 }
254
255 void
256 DrawingContext::draw_gradient(DrawingRequest& request)
257 {
258   GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
259   const Color& top = gradientrequest->top;
260   const Color& bottom = gradientrequest->bottom;
261
262   glDisable(GL_TEXTURE_2D);
263   glBegin(GL_QUADS);
264   glColor4f(top.red, top.green, top.blue, top.alpha);
265   glVertex2f(0, 0);
266   glVertex2f(SCREEN_WIDTH, 0);
267   glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
268   glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
269   glVertex2f(0, SCREEN_HEIGHT);
270   glEnd();
271   glEnable(GL_TEXTURE_2D);
272
273   delete gradientrequest;
274 }
275
276 void
277 DrawingContext::draw_text(DrawingRequest& request)
278 {
279   TextRequest* textrequest = (TextRequest*) request.request_data;
280
281   textrequest->font->draw(textrequest->text, request.pos,
282       textrequest->alignment, request.drawing_effect, request.alpha);
283
284   delete textrequest;
285 }
286
287 void
288 DrawingContext::draw_filled_rect(DrawingRequest& request)
289 {
290   FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
291
292   float x = request.pos.x;
293   float y = request.pos.y;
294   float w = fillrectrequest->size.x;
295   float h = fillrectrequest->size.y;
296
297   glDisable(GL_TEXTURE_2D);
298   glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
299             fillrectrequest->color.blue, fillrectrequest->color.alpha);
300
301   glBegin(GL_QUADS);
302   glVertex2f(x, y);
303   glVertex2f(x+w, y);
304   glVertex2f(x+w, y+h);
305   glVertex2f(x, y+h);
306   glEnd();
307   glEnable(GL_TEXTURE_2D);
308
309   delete fillrectrequest;
310 }
311
312 void
313 DrawingContext::do_drawing()
314 {
315 #ifdef DEBUG
316   assert(transformstack.empty());
317   assert(target_stack.empty());
318 #endif
319   transformstack.clear();
320   target_stack.clear();
321
322   bool use_lightmap = lightmap_requests.size() != 0;
323
324   // PART1: create lightmap
325   if(use_lightmap) {
326     glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
327     glMatrixMode(GL_PROJECTION);
328     glLoadIdentity();
329     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
330     glMatrixMode(GL_MODELVIEW);
331     glLoadIdentity();
332
333     // FIXME: Add ambient light support here
334     glClearColor(0.3, 0.3, 0.4, 1);
335     glClear(GL_COLOR_BUFFER_BIT);
336     handle_drawing_requests(lightmap_requests);
337     lightmap_requests.clear();
338
339     glDisable(GL_BLEND);
340     glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
341     glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
342
343     glViewport(0, 0, screen->w, screen->h);
344     glMatrixMode(GL_PROJECTION);
345     glLoadIdentity();
346     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
347     glMatrixMode(GL_MODELVIEW);
348     glLoadIdentity();
349     glEnable(GL_BLEND);
350   }
351
352   //glClear(GL_COLOR_BUFFER_BIT);
353   handle_drawing_requests(drawing_requests);
354   drawing_requests.clear();
355
356   if(use_lightmap) {
357     // multiple the lightmap with the framebuffer
358     glBlendFunc(GL_DST_COLOR, GL_ZERO);
359
360     glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
361     glBegin(GL_QUADS);
362
363     glTexCoord2f(0, lightmap_uv_bottom);
364     glVertex2f(0, 0);
365
366     glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
367     glVertex2f(SCREEN_WIDTH, 0);
368
369     glTexCoord2f(lightmap_uv_right, 0);
370     glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
371
372     glTexCoord2f(0, 0);
373     glVertex2f(0, SCREEN_HEIGHT);
374
375     glEnd();
376
377     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
378   }
379
380   assert_gl("drawing");
381
382   SDL_GL_SwapBuffers();
383 }
384
385 void
386 DrawingContext::handle_drawing_requests(DrawingRequests& requests)
387 {
388   std::stable_sort(requests.begin(), requests.end());
389
390   for(DrawingRequests::iterator i = requests.begin();
391       i != requests.end(); ++i) {
392     switch(i->type) {
393       case SURFACE:
394       {
395         const Surface* surface = (const Surface*) i->request_data;
396         if (i->angle == 0.0f)
397           surface->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
398         else
399           surface->draw(i->pos.x, i->pos.y, i->alpha, i->angle, i->color, i->blend, i->drawing_effect);
400         break;
401       }
402       case SURFACE_PART:
403         draw_surface_part(*i);
404         break;
405       case GRADIENT:
406         draw_gradient(*i);
407         break;
408       case TEXT:
409         draw_text(*i);
410         break;
411       case FILLRECT:
412         draw_filled_rect(*i);
413         break;
414     }
415   }
416 }
417
418 void
419 DrawingContext::push_transform()
420 {
421   transformstack.push_back(transform);
422 }
423
424 void
425 DrawingContext::pop_transform()
426 {
427   assert(!transformstack.empty());
428
429   transform = transformstack.back();
430   transformstack.pop_back();
431 }
432
433 void
434 DrawingContext::set_drawing_effect(DrawingEffect effect)
435 {
436   transform.drawing_effect = effect;
437 }
438
439 DrawingEffect
440 DrawingContext::get_drawing_effect() const
441 {
442   return transform.drawing_effect;
443 }
444
445 void
446 DrawingContext::set_alpha(float alpha)
447 {
448   transform.alpha = alpha;
449 }
450
451 float
452 DrawingContext::get_alpha() const
453 {
454   return transform.alpha;
455 }
456
457 void
458 DrawingContext::push_target()
459 {
460   target_stack.push_back(target);
461 }
462
463 void
464 DrawingContext::pop_target()
465 {
466   set_target(target_stack.back());
467   target_stack.pop_back();
468 }
469
470 void
471 DrawingContext::set_target(Target target)
472 {
473   this->target = target;
474   if(target == LIGHTMAP)
475     requests = &lightmap_requests;
476   else
477     requests = &drawing_requests;
478 }