c231e4bb8b56b73d9121c745136e55adffd5cad7
[supertux.git] / src / video / drawing_context.cpp
1 //  $Id: drawing_context.cpp 2334 2005-04-04 16:26:14Z grumbel $
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 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 #include <config.h>
20
21 #include <algorithm>
22 #include <cassert>
23 #include <iostream>
24 #include <SDL_image.h>
25 #include <GL/gl.h>
26 #include <GL/glu.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_surface_part(DrawingRequest& request)
208 {
209   SurfacePartRequest* surfacepartrequest
210     = (SurfacePartRequest*) request.request_data;
211
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);
217
218   delete surfacepartrequest;
219 }
220
221 void
222 DrawingContext::draw_gradient(DrawingRequest& request)
223 {
224   GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
225   const Color& top = gradientrequest->top;
226   const Color& bottom = gradientrequest->bottom;
227   
228   glDisable(GL_TEXTURE_2D);
229   glBegin(GL_QUADS);
230   glColor4f(top.red, top.green, top.blue, top.alpha);
231   glVertex2f(0, 0);
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);
236   glEnd();
237   glEnable(GL_TEXTURE_2D);
238
239   delete gradientrequest;
240 }
241
242 void
243 DrawingContext::draw_text(DrawingRequest& request)
244 {
245   TextRequest* textrequest = (TextRequest*) request.request_data;
246
247   textrequest->font->draw(textrequest->text, request.pos,
248       textrequest->alignment, request.drawing_effect, request.alpha);
249
250   delete textrequest;
251 }
252
253 void
254 DrawingContext::draw_filled_rect(DrawingRequest& request)
255 {
256   FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
257
258   float x = request.pos.x;
259   float y = request.pos.y;
260   float w = fillrectrequest->size.x;
261   float h = fillrectrequest->size.y;
262
263   glDisable(GL_TEXTURE_2D);
264   glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
265             fillrectrequest->color.blue, fillrectrequest->color.alpha);
266  
267   glBegin(GL_QUADS);
268   glVertex2f(x, y);
269   glVertex2f(x+w, y);
270   glVertex2f(x+w, y+h);
271   glVertex2f(x, y+h);
272   glEnd();
273   glEnable(GL_TEXTURE_2D);
274
275   delete fillrectrequest;
276 }
277
278 void
279 DrawingContext::do_drawing()
280 {
281 #ifdef DEBUG
282   assert(transformstack.empty());
283   assert(target_stack.empty());
284 #endif
285   transformstack.clear();
286   target_stack.clear();
287
288   bool use_lightmap = lightmap_requests.size() != 0;
289   
290   // PART1: create lightmap
291   if(use_lightmap) {
292     glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
293     glMatrixMode(GL_PROJECTION);
294     glLoadIdentity();               
295     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
296     glMatrixMode(GL_MODELVIEW);
297     glLoadIdentity();
298
299     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
300     glClear(GL_COLOR_BUFFER_BIT);
301     handle_drawing_requests(lightmap_requests);
302     lightmap_requests.clear();
303   
304     glDisable(GL_BLEND);
305     glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
306     glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
307
308     glViewport(0, 0, screen->w, screen->h);
309     glMatrixMode(GL_PROJECTION);
310     glLoadIdentity();               
311     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
312     glMatrixMode(GL_MODELVIEW);    
313     glLoadIdentity();
314     glEnable(GL_BLEND);
315   }
316
317   //glClear(GL_COLOR_BUFFER_BIT);
318   handle_drawing_requests(drawing_requests);
319   drawing_requests.clear();
320
321   if(use_lightmap) {
322     glBlendFunc(GL_DST_COLOR, GL_ZERO);
323
324     glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
325     glBegin(GL_QUADS);
326
327     glTexCoord2f(0, lightmap_uv_bottom);
328     glVertex2f(0, 0);
329
330     glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
331     glVertex2f(SCREEN_WIDTH, 0);
332
333     glTexCoord2f(lightmap_uv_right, 0);
334     glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
335
336     glTexCoord2f(0, 0);
337     glVertex2f(0, SCREEN_HEIGHT);
338     
339     glEnd();
340
341     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
342   }
343
344   assert_gl("drawing");
345
346   SDL_GL_SwapBuffers();
347 }
348
349 void
350 DrawingContext::handle_drawing_requests(DrawingRequests& requests)
351 {
352   std::stable_sort(requests.begin(), requests.end());
353   
354   for(DrawingRequests::iterator i = requests.begin();
355       i != requests.end(); ++i) {
356     switch(i->type) {
357       case SURFACE:
358       {
359         const Surface* surface = (const Surface*) i->request_data;
360         surface->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
361         break;
362       }
363       case SURFACE_PART:
364         draw_surface_part(*i);
365         break;
366       case GRADIENT:
367         draw_gradient(*i);
368         break;
369       case TEXT:
370         draw_text(*i);
371         break;
372       case FILLRECT:
373         draw_filled_rect(*i);
374         break;
375     }
376   }
377 }
378
379 void
380 DrawingContext::push_transform()
381 {
382   transformstack.push_back(transform);
383 }
384
385 void
386 DrawingContext::pop_transform()
387 {
388   assert(!transformstack.empty());
389
390   transform = transformstack.back();
391   transformstack.pop_back();
392 }
393
394 void
395 DrawingContext::set_drawing_effect(DrawingEffect effect)
396 {
397   transform.drawing_effect = effect;
398 }
399
400 DrawingEffect
401 DrawingContext::get_drawing_effect() const
402 {
403   return transform.drawing_effect;
404 }
405
406 void
407 DrawingContext::set_alpha(float alpha)
408 {
409   transform.alpha = alpha;
410 }
411
412 float
413 DrawingContext::get_alpha() const
414 {
415   return transform.alpha;
416 }
417
418 void
419 DrawingContext::push_target()
420 {
421   target_stack.push_back(target);
422 }
423
424 void
425 DrawingContext::pop_target()
426 {
427   set_target(target_stack.back());
428   target_stack.pop_back();
429 }
430
431 void
432 DrawingContext::set_target(Target target)
433 {
434   this->target = target;
435   if(target == LIGHTMAP)
436     requests = &lightmap_requests;
437   else
438     requests = &drawing_requests;
439 }
440