yeti cleanup and death animation rework, hitbox fix ups
[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     int layer)
75 {
76   assert(surface != 0);
77   
78   DrawingRequest request;
79
80   request.type = SURFACE;
81   request.pos = transform.apply(position);
82
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)
86     return;
87
88   request.layer = layer;
89   request.drawing_effect = transform.drawing_effect;
90   request.alpha = transform.alpha;
91   request.request_data = const_cast<Surface*> (surface);  
92
93   requests->push_back(request);
94 }
95
96 void
97 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
98     const Vector& size, const Vector& dest, int layer)
99 {
100   assert(surface != 0);
101
102   DrawingRequest request;
103
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;
109   
110   SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
111   surfacepartrequest->size = size;
112   surfacepartrequest->source = source;
113   surfacepartrequest->surface = surface;
114
115   // clip on screen borders
116   if(request.pos.x < 0) {
117     surfacepartrequest->size.x += request.pos.x;
118     if(surfacepartrequest->size.x <= 0)
119       return;
120     surfacepartrequest->source.x -= request.pos.x;
121     request.pos.x = 0;
122   }
123   if(request.pos.y < 0) {
124     surfacepartrequest->size.y += request.pos.y;
125     if(surfacepartrequest->size.y <= 0)
126       return;
127     surfacepartrequest->source.y -= request.pos.y;
128     request.pos.y = 0;
129   }
130   request.request_data = surfacepartrequest;
131
132   requests->push_back(request);
133 }
134
135 void
136 DrawingContext::draw_text(const Font* font, const std::string& text,
137     const Vector& position, FontAlignment alignment, int layer)
138 {
139   DrawingRequest request;
140
141   request.type = TEXT;
142   request.pos = transform.apply(position);
143   request.layer = layer;
144   request.drawing_effect = transform.drawing_effect;
145   request.alpha = transform.alpha;
146
147   TextRequest* textrequest = new TextRequest;
148   textrequest->font = font;
149   textrequest->text = text;
150   textrequest->alignment = alignment;
151   request.request_data = textrequest;
152
153   requests->push_back(request);
154 }
155
156 void
157 DrawingContext::draw_center_text(const Font* font, const std::string& text,
158     const Vector& position, int layer)
159 {
160   draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
161       CENTER_ALLIGN, layer);
162 }
163
164 void
165 DrawingContext::draw_gradient(const Color& top, const Color& bottom, int layer)
166 {
167   DrawingRequest request;
168
169   request.type = GRADIENT;
170   request.pos = Vector(0,0);
171   request.layer = layer;
172
173   request.drawing_effect = transform.drawing_effect;
174   request.alpha = transform.alpha;
175
176   GradientRequest* gradientrequest = new GradientRequest;
177   gradientrequest->top = top;
178   gradientrequest->bottom = bottom;
179   request.request_data = gradientrequest;
180
181   requests->push_back(request);
182 }
183
184 void
185 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
186                                  const Color& color, int layer)
187 {
188   DrawingRequest request;
189
190   request.type = FILLRECT;
191   request.pos = transform.apply(topleft);
192   request.layer = layer;
193
194   request.drawing_effect = transform.drawing_effect;
195   request.alpha = transform.alpha;                    
196
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;
202
203   requests->push_back(request);
204 }
205
206 void
207 DrawingContext::draw_filled_rect(const Rect& rect, const Color& color,
208                                  int layer)
209 {
210   DrawingRequest request;
211
212   request.type = FILLRECT;
213   request.pos = transform.apply(rect.p1);
214   request.layer = layer;
215
216   request.drawing_effect = transform.drawing_effect;
217   request.alpha = transform.alpha;                    
218
219   FillRectRequest* fillrectrequest = new FillRectRequest;
220   fillrectrequest->size = Vector(rect.get_width(), rect.get_height());
221   fillrectrequest->color = color;
222   fillrectrequest->color.alpha = color.alpha * transform.alpha;
223   request.request_data = fillrectrequest;
224
225   requests->push_back(request);
226 }
227
228 void
229 DrawingContext::draw_surface_part(DrawingRequest& request)
230 {
231   SurfacePartRequest* surfacepartrequest
232     = (SurfacePartRequest*) request.request_data;
233
234   surfacepartrequest->surface->draw_part(
235       surfacepartrequest->source.x, surfacepartrequest->source.y,
236       request.pos.x, request.pos.y,
237       surfacepartrequest->size.x, surfacepartrequest->size.y,
238       request.alpha, request.drawing_effect);
239
240   delete surfacepartrequest;
241 }
242
243 void
244 DrawingContext::draw_gradient(DrawingRequest& request)
245 {
246   GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
247   const Color& top = gradientrequest->top;
248   const Color& bottom = gradientrequest->bottom;
249   
250   glDisable(GL_TEXTURE_2D);
251   glBegin(GL_QUADS);
252   glColor4f(top.red, top.green, top.blue, top.alpha);
253   glVertex2f(0, 0);
254   glVertex2f(SCREEN_WIDTH, 0);
255   glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
256   glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
257   glVertex2f(0, SCREEN_HEIGHT);
258   glEnd();
259   glEnable(GL_TEXTURE_2D);
260
261   delete gradientrequest;
262 }
263
264 void
265 DrawingContext::draw_text(DrawingRequest& request)
266 {
267   TextRequest* textrequest = (TextRequest*) request.request_data;
268
269   textrequest->font->draw(textrequest->text, request.pos,
270       textrequest->alignment, request.drawing_effect, request.alpha);
271
272   delete textrequest;
273 }
274
275 void
276 DrawingContext::draw_filled_rect(DrawingRequest& request)
277 {
278   FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
279
280   float x = request.pos.x;
281   float y = request.pos.y;
282   float w = fillrectrequest->size.x;
283   float h = fillrectrequest->size.y;
284
285   glDisable(GL_TEXTURE_2D);
286   glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
287             fillrectrequest->color.blue, fillrectrequest->color.alpha);
288  
289   glBegin(GL_QUADS);
290   glVertex2f(x, y);
291   glVertex2f(x+w, y);
292   glVertex2f(x+w, y+h);
293   glVertex2f(x, y+h);
294   glEnd();
295   glEnable(GL_TEXTURE_2D);
296
297   delete fillrectrequest;
298 }
299
300 void
301 DrawingContext::do_drawing()
302 {
303 #ifdef DEBUG
304   assert(transformstack.empty());
305   assert(target_stack.empty());
306 #endif
307   transformstack.clear();
308   target_stack.clear();
309
310   bool use_lightmap = lightmap_requests.size() != 0;
311   
312   // PART1: create lightmap
313   if(use_lightmap) {
314     glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
315     glMatrixMode(GL_PROJECTION);
316     glLoadIdentity();               
317     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
318     glMatrixMode(GL_MODELVIEW);
319     glLoadIdentity();
320
321     //glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
322     glClearColor(0, 0, 0, 1);
323     glClear(GL_COLOR_BUFFER_BIT);
324     handle_drawing_requests(lightmap_requests);
325     lightmap_requests.clear();
326   
327     glDisable(GL_BLEND);
328     glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
329     glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
330
331     glViewport(0, 0, screen->w, screen->h);
332     glMatrixMode(GL_PROJECTION);
333     glLoadIdentity();               
334     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
335     glMatrixMode(GL_MODELVIEW);    
336     glLoadIdentity();
337     glEnable(GL_BLEND);
338   }
339
340   //glClear(GL_COLOR_BUFFER_BIT);
341   handle_drawing_requests(drawing_requests);
342   drawing_requests.clear();
343
344   if(use_lightmap) {
345     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
346
347     glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
348     glBegin(GL_QUADS);
349
350     glTexCoord2f(0, lightmap_uv_bottom);
351     glVertex2f(0, 0);
352
353     glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
354     glVertex2f(SCREEN_WIDTH, 0);
355
356     glTexCoord2f(lightmap_uv_right, 0);
357     glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
358
359     glTexCoord2f(0, 0);
360     glVertex2f(0, SCREEN_HEIGHT);
361     
362     glEnd();
363
364     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
365   }
366
367   assert_gl("drawing");
368
369   SDL_GL_SwapBuffers();
370 }
371
372 void
373 DrawingContext::handle_drawing_requests(DrawingRequests& requests)
374 {
375   std::stable_sort(requests.begin(), requests.end());
376   
377   for(DrawingRequests::iterator i = requests.begin();
378       i != requests.end(); ++i) {
379     switch(i->type) {
380       case SURFACE:
381       {
382         const Surface* surface = (const Surface*) i->request_data;
383         surface->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
384         break;
385       }
386       case SURFACE_PART:
387         draw_surface_part(*i);
388         break;
389       case GRADIENT:
390         draw_gradient(*i);
391         break;
392       case TEXT:
393         draw_text(*i);
394         break;
395       case FILLRECT:
396         draw_filled_rect(*i);
397         break;
398     }
399   }
400 }
401
402 void
403 DrawingContext::push_transform()
404 {
405   transformstack.push_back(transform);
406 }
407
408 void
409 DrawingContext::pop_transform()
410 {
411   assert(!transformstack.empty());
412
413   transform = transformstack.back();
414   transformstack.pop_back();
415 }
416
417 void
418 DrawingContext::set_drawing_effect(DrawingEffect effect)
419 {
420   transform.drawing_effect = effect;
421 }
422
423 DrawingEffect
424 DrawingContext::get_drawing_effect() const
425 {
426   return transform.drawing_effect;
427 }
428
429 void
430 DrawingContext::set_alpha(float alpha)
431 {
432   transform.alpha = alpha;
433 }
434
435 float
436 DrawingContext::get_alpha() const
437 {
438   return transform.alpha;
439 }
440
441 void
442 DrawingContext::push_target()
443 {
444   target_stack.push_back(target);
445 }
446
447 void
448 DrawingContext::pop_target()
449 {
450   set_target(target_stack.back());
451   target_stack.pop_back();
452 }
453
454 void
455 DrawingContext::set_target(Target target)
456 {
457   this->target = target;
458   if(target == LIGHTMAP)
459     requests = &lightmap_requests;
460   else
461     requests = &drawing_requests;
462 }
463