Initial integration, lots of broken stuff
[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 #include <config.h>
20
21 #include <functional>
22 #include <algorithm>
23 #include <cassert>
24 #include <iostream>
25 //#include <SDL_image.h>
26 #include <sstream>
27 #include <iomanip>
28 #include <physfs.h>
29
30 #include "drawing_context.hpp"
31 //#include "drawing_request.hpp"
32 //#include "video_systems.hpp"
33 //#include "renderer.hpp"
34 //#include "lightmap.hpp"
35 #include "surface.hpp"
36 #include "main.hpp"
37 #include "gameconfig.hpp"
38 //#include "texture.hpp"
39 //#include "texture_manager.hpp"
40 //#include "obstack/obstackpp.hpp"
41
42 #include "math/vector.hpp"
43 #include "math/rect.hpp"
44
45 #include <unison/video/Renderers.hpp>
46
47 /*static inline int next_po2(int val)
48 {
49   int result = 1;
50   while(result < val)
51     result *= 2;
52
53   return result;
54 }*/
55
56 DrawingContext::DrawingContext()
57   /*renderer(0), lightmap(0), ambient_color(1.0f, 1.0f, 1.0f, 1.0f), target(NORMAL), screenshot_requested(false)*/
58 {
59   ambient_color = Color(1.0f, 1.0f, 1.0f, 1.0f);
60   target = NORMAL;
61   screenshot_requested = false;
62   draw_target = &normal_list;
63   /*requests = &drawing_requests;
64   obstack_init(&obst);*/
65 }
66
67 DrawingContext::~DrawingContext()
68 {
69   /*delete renderer;
70   delete lightmap;
71
72   obstack_free(&obst, NULL);*/
73 }
74
75 void
76 DrawingContext::init_renderer()
77 {
78   Unison::Video::Renderers::get().set_renderer(config->video);
79   Unison::Video::Window::get().set_logical_size(Unison::Video::Area(SCREEN_WIDTH, SCREEN_HEIGHT));
80   Unison::Video::Window::get().open(Unison::Video::Area(config->screenwidth, config->screenheight), config->use_fullscreen);
81   lightmap = Unison::Video::Surface(Unison::Video::Area(SCREEN_WIDTH, SCREEN_HEIGHT));
82   /*delete renderer;
83   delete lightmap;
84
85   renderer = new_renderer();
86   lightmap = new_lightmap();*/
87 }
88
89 void
90 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
91                              float angle, const Color& color, const Blend& blend,
92                              int layer)
93 {
94   assert(surface != 0);
95
96   Unison::Video::RenderOptions options;
97   options.color = color.to_unison_color();
98   options.alpha = (unsigned char) transform.alpha * 0xff;
99   options.blend = blend.to_unison_blend();
100   options.h_flip = surface->get_flipx() != (transform.drawing_effect == HORIZONTAL_FLIP);
101   options.v_flip = (transform.drawing_effect == VERTICAL_FLIP);
102
103   Vector transformed = transform.apply(position);
104   Unison::Video::Point dst_pos((int) transformed.x, (int) transformed.y);
105
106   (*draw_target)[layer].blit_section(surface->get_texture(), dst_pos, options);
107
108   /*DrawingRequest* request = new(obst) DrawingRequest();
109
110   request->target = target;
111   request->type = SURFACE;
112   request->pos = transform.apply(position);
113
114   if(request->pos.x >= SCREEN_WIDTH || request->pos.y >= SCREEN_HEIGHT
115       || request->pos.x + surface->get_width() < 0
116       || request->pos.y + surface->get_height() < 0)
117     return;
118
119   request->layer = layer;
120   request->drawing_effect = transform.drawing_effect;
121   request->alpha = transform.alpha;
122   request->angle = angle;
123   request->color = color;
124   request->blend = blend;
125
126   request->request_data = const_cast<Surface*> (surface);
127
128   requests->push_back(request);*/
129 }
130
131 void
132 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
133     int layer)
134 {
135   draw_surface(surface, position, 0.0f, Color(1.0f, 1.0f, 1.0f), Blend(), layer);
136 }
137
138 void
139 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
140     const Vector& size, const Vector& dest, int layer)
141 {
142   assert(surface != 0);
143
144   Unison::Video::TextureSection texture = surface->get_texture();
145   texture.clip_rect.pos.x += (int) source.x;
146   texture.clip_rect.pos.y += (int) source.y;
147   texture.clip_rect.size.x += (unsigned int) size.x;
148   texture.clip_rect.size.y += (unsigned int) size.y;
149
150   Unison::Video::RenderOptions options;
151   options.alpha = (unsigned char) transform.alpha * 0xff;
152   options.h_flip = surface->get_flipx() != (transform.drawing_effect == HORIZONTAL_FLIP);
153   options.v_flip = (transform.drawing_effect == VERTICAL_FLIP);
154
155   Vector transformed = transform.apply(dest);
156   Unison::Video::Point dst_pos((int) transformed.x, (int) transformed.y);
157
158   (*draw_target)[layer].blit_section(texture, dst_pos, options);
159
160   /*DrawingRequest* request = new(obst) DrawingRequest();
161
162   request->target = target;
163   request->type = SURFACE_PART;
164   request->pos = transform.apply(dest);
165   request->layer = layer;
166   request->drawing_effect = transform.drawing_effect;
167   request->alpha = transform.alpha;
168
169   SurfacePartRequest* surfacepartrequest = new(obst) SurfacePartRequest();
170   surfacepartrequest->size = size;
171   surfacepartrequest->source = source;
172   surfacepartrequest->surface = surface;
173
174   // clip on screen borders
175   if(request->pos.x < 0) {
176     surfacepartrequest->size.x += request->pos.x;
177     if(surfacepartrequest->size.x <= 0)
178       return;
179     surfacepartrequest->source.x -= request->pos.x;
180     request->pos.x = 0;
181   }
182   if(request->pos.y < 0) {
183     surfacepartrequest->size.y += request->pos.y;
184     if(surfacepartrequest->size.y <= 0)
185       return;
186     surfacepartrequest->source.y -= request->pos.y;
187     request->pos.y = 0;
188   }
189   request->request_data = surfacepartrequest;
190
191   requests->push_back(request);*/
192 }
193
194 void
195 DrawingContext::draw_text(const Font* font, const std::string& text,
196     const Vector& position, FontAlignment alignment, int layer)
197 {
198   font->draw((*draw_target)[layer], text, transform.apply(position),
199       alignment, transform.drawing_effect, transform.alpha);
200
201   /*DrawingRequest* request = new(obst) DrawingRequest();
202
203   request->target = target;
204   request->type = TEXT;
205   request->pos = transform.apply(position);
206   request->layer = layer;
207   request->drawing_effect = transform.drawing_effect;
208   request->alpha = transform.alpha;
209
210   TextRequest* textrequest = new(obst) TextRequest();
211   textrequest->font = font;
212   textrequest->text = text;
213   textrequest->alignment = alignment;
214   request->request_data = textrequest;
215
216   requests->push_back(request);*/
217 }
218
219 void
220 DrawingContext::draw_center_text(const Font* font, const std::string& text,
221     const Vector& position, int layer)
222 {
223   draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
224       ALIGN_CENTER, layer);
225 }
226
227 namespace
228 {
229   class GradientRequest : public Unison::Video::DisplayList::Request
230   {
231     public:
232       GradientRequest(const Color &top, const Color &bottom) :
233         top(top),
234         bottom(bottom)
235       {
236       }
237
238       void do_request(Unison::Video::Blittable *dst) const
239       {
240         for(int y = 0;y < SCREEN_HEIGHT;++y)
241         {
242           Unison::Video::Color color;
243           color.red = (Uint8)((((float)(top.red-bottom.red)/(0-SCREEN_HEIGHT)) * y + top.red) * 255);
244           color.green = (Uint8)((((float)(top.green-bottom.green)/(0-SCREEN_HEIGHT)) * y + top.green) * 255);
245           color.green = (Uint8)((((float)(top.blue-bottom.blue)/(0-SCREEN_HEIGHT)) * y + top.blue) * 255);
246           color.alpha = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-SCREEN_HEIGHT)) * y + top.alpha) * 255);
247           dst->fill(color, Unison::Video::Rect(0, y, SCREEN_WIDTH, 1));
248         }
249       }
250     private:
251       Color top;
252       Color bottom;
253   };
254 }
255
256 void
257 DrawingContext::draw_gradient(const Color& top, const Color& bottom, int layer)
258 {
259   (*draw_target)[layer].add_request(new GradientRequest(top, bottom));
260
261   /*DrawingRequest* request = new(obst) DrawingRequest();
262
263   request->target = target;
264   request->type = GRADIENT;
265   request->pos = Vector(0,0);
266   request->layer = layer;
267
268   request->drawing_effect = transform.drawing_effect;
269   request->alpha = transform.alpha;
270
271   GradientRequest* gradientrequest = new(obst) GradientRequest();
272   gradientrequest->top = top;
273   gradientrequest->bottom = bottom;
274   request->request_data = gradientrequest;
275
276   requests->push_back(request);*/
277 }
278
279 void
280 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
281                                  const Color& color, int layer)
282 {
283   Vector transformed = transform.apply(topleft);
284
285   Unison::Video::Rect rect;
286   rect.pos = Unison::Video::Point((int) transformed.x, (int) transformed.y);
287   rect.size.x = (unsigned int) size.x;
288   rect.size.y = (unsigned int) size.y;
289
290   (*draw_target)[layer].fill_blend(color.to_unison_color(), rect);
291
292   /*DrawingRequest* request = new(obst) DrawingRequest();
293
294   request->target = target;
295   request->type = FILLRECT;
296   request->pos = transform.apply(topleft);
297   request->layer = layer;
298
299   request->drawing_effect = transform.drawing_effect;
300   request->alpha = transform.alpha;
301
302   FillRectRequest* fillrectrequest = new(obst) FillRectRequest();
303   fillrectrequest->size = size;
304   fillrectrequest->color = color;
305   fillrectrequest->color.alpha = color.alpha * transform.alpha;
306   request->request_data = fillrectrequest;
307
308   requests->push_back(request);*/
309 }
310
311 void
312 DrawingContext::draw_filled_rect(const Rect& rect, const Color& color,
313                                  int layer)
314 {
315   Vector transformed = transform.apply(rect.p1);
316
317   Unison::Video::Rect unison_rect;
318   unison_rect.pos.x = (int) transformed.x;
319   unison_rect.pos.y = (int) transformed.y;
320   unison_rect.size.x = (unsigned int) rect.get_width();
321   unison_rect.size.y = (unsigned int) rect.get_height();
322
323   (*draw_target)[layer].fill_blend(color.to_unison_color(), unison_rect);
324
325   /*DrawingRequest* request = new(obst) DrawingRequest();
326
327   request->target = target;
328   request->type = FILLRECT;
329   request->pos = transform.apply(rect.p1);
330   request->layer = layer;
331
332   request->drawing_effect = transform.drawing_effect;
333   request->alpha = transform.alpha;
334
335   FillRectRequest* fillrectrequest = new(obst) FillRectRequest;
336   fillrectrequest->size = Vector(rect.get_width(), rect.get_height());
337   fillrectrequest->color = color;
338   fillrectrequest->color.alpha = color.alpha * transform.alpha;
339   request->request_data = fillrectrequest;
340
341   requests->push_back(request);*/
342 }
343
344 void
345 DrawingContext::get_light(const Vector& position, Color* color)
346 {
347   if( ambient_color.red == 1.0f && ambient_color.green == 1.0f
348       && ambient_color.blue  == 1.0f ) {
349     *color = Color( 1.0f, 1.0f, 1.0f);
350     return;
351   }
352
353   /*DrawingRequest* request = new(obst) DrawingRequest();
354   request->target = target;
355   request->type = GETLIGHT;
356   request->pos = transform.apply(position);*/
357
358   //There is no light offscreen.
359   if(position.x >= SCREEN_WIDTH || position.y >= SCREEN_HEIGHT
360       || position.x < 0 || position.y < 0){
361     *color = Color( 0, 0, 0);
362     return;
363   }
364
365   Vector transformed = transform.apply(position);
366   Unison::Video::Point pos((int) transformed.x, (int) transformed.y);
367   get_light_requests.push_back(std::make_pair(pos, color));
368
369   /*request->layer = LAYER_GUI; //make sure all get_light requests are handled last.
370   GetLightRequest* getlightrequest = new(obst) GetLightRequest();
371   getlightrequest->color_ptr = color;
372   request->request_data = getlightrequest;
373   lightmap_requests.push_back(request);*/
374 }
375
376 void
377 DrawingContext::do_drawing()
378 {
379 #ifdef DEBUG
380   assert(transformstack.empty());
381   assert(target_stack.empty());
382 #endif
383   transformstack.clear();
384   target_stack.clear();
385
386   //Use Lightmap if ambient color is not white.
387   bool use_lightmap = ( ambient_color.red != 1.0f   || ambient_color.green != 1.0f ||
388                         ambient_color.blue  != 1.0f );
389
390   // PART1: create lightmap
391   if(use_lightmap) {
392     lightmap.fill(ambient_color.to_unison_color());
393     lightmap.draw(lightmap_list);
394     //lightmap->start_draw(ambient_color);
395     //handle_drawing_requests(lightmap_requests);
396     //lightmap->end_draw();
397   }
398
399   Unison::Video::Window::get().draw(normal_list);
400
401   //handle_drawing_requests(drawing_requests);
402   if(use_lightmap) {
403     Unison::Video::Window::get().blit(lightmap, Unison::Video::Point(), Unison::Video::Rect(), Unison::Video::BLEND_MOD);
404     //lightmap->do_draw();
405   }
406   //obstack_free(&obst, NULL);
407   //obstack_init(&obst);
408
409   // if a screenshot was requested, take one
410   if (screenshot_requested) {
411     // FIXME renderer->do_take_screenshot();
412     screenshot_requested = false;
413   }
414
415   //renderer->flip();
416   Unison::Video::Window::get().flip();
417
418   normal_list.clear();
419   lightmap_list.clear();
420 }
421
422 /*class RequestPtrCompare
423   :  public std::binary_function<const DrawingRequest*,
424   std::vector<std::pair<Unison::Video::Point, Color *> > get_light_requests;
425                                  const DrawingRequest*, 
426                                  bool>
427 {
428 public:
429   bool operator()(const DrawingRequest* r1, const DrawingRequest* r2) const
430   {
431     return *r1 < *r2;
432   }
433 };
434
435 void
436 DrawingContext::handle_drawing_requests(DrawingRequests& requests)
437 {
438   std::stable_sort(requests.begin(), requests.end(), RequestPtrCompare());
439
440   DrawingRequests::const_iterator i;
441   for(i = requests.begin(); i != requests.end(); ++i) {
442     const DrawingRequest& request = **i;
443
444     switch(request.target) {
445       case NORMAL:
446         switch(request.type) {
447           case SURFACE:
448             renderer->draw_surface(request);
449             break;
450           case SURFACE_PART:
451             renderer->draw_surface_part(request);
452             break;
453           case GRADIENT:
454             renderer->draw_gradient(request);
455             break;
456           case TEXT:
457             {
458               const TextRequest* textrequest = (TextRequest*) request.request_data;
459               textrequest->font->draw(renderer, textrequest->text, request.pos,
460                   textrequest->alignment, request.drawing_effect, request.alpha);
461             }
462             break;
463           case FILLRECT:
464             renderer->draw_filled_rect(request);
465             break;
466           case GETLIGHT:
467             lightmap->get_light(request);
468             break;
469         }
470         break;
471       case LIGHTMAP:
472         switch(request.type) {
473           case SURFACE:
474             lightmap->draw_surface(request);
475             break;
476           case SURFACE_PART:
477             lightmap->draw_surface_part(request);
478             break;
479           case GRADIENT:
480             lightmap->draw_gradient(request);
481             break;
482           case TEXT:
483             {
484               const TextRequest* textrequest = (TextRequest*) request.request_data;
485               textrequest->font->draw(renderer, textrequest->text, request.pos,
486                   textrequest->alignment, request.drawing_effect, request.alpha);
487             }
488             break;
489           case FILLRECT:
490             lightmap->draw_filled_rect(request);
491             break;
492           case GETLIGHT:
493             lightmap->get_light(request);
494             break;
495         }
496         break;
497     }
498   }
499   requests.clear();
500 }*/
501
502 void
503 DrawingContext::push_transform()
504 {
505   transformstack.push_back(transform);
506 }
507
508 void
509 DrawingContext::pop_transform()
510 {
511   assert(!transformstack.empty());
512
513   transform = transformstack.back();
514   transformstack.pop_back();
515 }
516
517 void
518 DrawingContext::set_drawing_effect(DrawingEffect effect)
519 {
520   transform.drawing_effect = effect;
521 }
522
523 DrawingEffect
524 DrawingContext::get_drawing_effect() const
525 {
526   return transform.drawing_effect;
527 }
528
529 void
530 DrawingContext::set_alpha(float alpha)
531 {
532   transform.alpha = alpha;
533 }
534
535 float
536 DrawingContext::get_alpha() const
537 {
538   return transform.alpha;
539 }
540
541 void
542 DrawingContext::push_target()
543 {
544   target_stack.push_back(target);
545 }
546
547 void
548 DrawingContext::pop_target()
549 {
550   set_target(target_stack.back());
551   target_stack.pop_back();
552 }
553
554 void
555 DrawingContext::set_target(Target target)
556 {
557   this->target = target;
558   if(target == LIGHTMAP) {
559     draw_target = &lightmap_list;
560   } else {
561     assert(target == NORMAL);
562     draw_target = &normal_list;
563   }
564 }
565
566 void
567 DrawingContext::set_ambient_color( Color new_color )
568 {
569   ambient_color = new_color;
570 }
571
572 void 
573 DrawingContext::take_screenshot()
574 {
575   screenshot_requested = true;
576 }
577