-// $Id: drawing_context.cpp 2334 2005-04-04 16:26:14Z grumbel $
+// $Id$
//
-// SuperTux - A Jump'n Run
-// Copyright (C) 2004 Matthias Braun <matze@braunis.de
+// SuperTux
+// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <config.h>
+#include <functional>
#include <algorithm>
#include <cassert>
#include <iostream>
+#include <SDL_image.h>
+#include <sstream>
+#include <iomanip>
+#include <physfs.h>
#include "drawing_context.hpp"
+#include "drawing_request.hpp"
+#include "video_systems.hpp"
+#include "renderer.hpp"
+#include "lightmap.hpp"
#include "surface.hpp"
-#include "font.hpp"
#include "main.hpp"
#include "gameconfig.hpp"
+#include "texture.hpp"
+#include "texture_manager.hpp"
+#include "obstack/obstackpp.hpp"
-DrawingContext::DrawingContext(SDL_Surface* targetsurface)
+static inline int next_po2(int val)
{
- if(targetsurface) {
- screen = targetsurface;
- } else {
- screen = SDL_GetVideoSurface();
- }
+ int result = 1;
+ while(result < val)
+ result *= 2;
+
+ return result;
+}
+
+DrawingContext::DrawingContext() :
+ renderer(0), lightmap(0), ambient_color(1.0f, 1.0f, 1.0f, 1.0f), target(NORMAL), screenshot_requested(false)
+{
+ requests = &drawing_requests;
+ obstack_init(&obst);
}
DrawingContext::~DrawingContext()
{
+ delete renderer;
+ delete lightmap;
+
+ obstack_free(&obst, NULL);
+}
+
+void
+DrawingContext::init_renderer()
+{
+ delete renderer;
+ delete lightmap;
+
+ renderer = new_renderer();
+ lightmap = new_lightmap();
}
void
DrawingContext::draw_surface(const Surface* surface, const Vector& position,
- int layer)
+ float angle, const Color& color, const Blend& blend,
+ int layer)
{
assert(surface != 0);
-
- DrawingRequest request;
- request.type = SURFACE;
- request.pos = transform.apply(position);
+ DrawingRequest* request = new(obst) DrawingRequest();
- if(request.pos.x >= SCREEN_WIDTH || request.pos.y >= SCREEN_HEIGHT
- || request.pos.x + surface->w < 0 || request.pos.y + surface->h < 0)
+ request->target = target;
+ request->type = SURFACE;
+ request->pos = transform.apply(position);
+
+ if(request->pos.x >= SCREEN_WIDTH || request->pos.y >= SCREEN_HEIGHT
+ || request->pos.x + surface->get_width() < 0
+ || request->pos.y + surface->get_height() < 0)
return;
- request.layer = layer;
- request.drawing_effect = transform.drawing_effect;
- request.zoom = transform.zoom;
- request.alpha = transform.alpha;
- request.request_data = const_cast<Surface*> (surface);
+ request->layer = layer;
+ request->drawing_effect = transform.drawing_effect;
+ request->alpha = transform.alpha;
+ request->angle = angle;
+ request->color = color;
+ request->blend = blend;
+
+ request->request_data = const_cast<Surface*> (surface);
- drawingrequests.push_back(request);
+ requests->push_back(request);
+}
+
+void
+DrawingContext::draw_surface(const Surface* surface, const Vector& position,
+ int layer)
+{
+ draw_surface(surface, position, 0.0f, Color(1.0f, 1.0f, 1.0f), Blend(), layer);
}
void
{
assert(surface != 0);
- DrawingRequest request;
+ DrawingRequest* request = new(obst) DrawingRequest();
+
+ request->target = target;
+ request->type = SURFACE_PART;
+ request->pos = transform.apply(dest);
+ request->layer = layer;
+ request->drawing_effect = transform.drawing_effect;
+ request->alpha = transform.alpha;
- request.type = SURFACE_PART;
- request.pos = transform.apply(dest);
- request.layer = layer;
- request.drawing_effect = transform.drawing_effect;
- request.alpha = transform.alpha;
-
- SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
+ SurfacePartRequest* surfacepartrequest = new(obst) SurfacePartRequest();
surfacepartrequest->size = size;
surfacepartrequest->source = source;
surfacepartrequest->surface = surface;
// clip on screen borders
- if(request.pos.x < 0) {
- surfacepartrequest->size.x += request.pos.x;
+ if(request->pos.x < 0) {
+ surfacepartrequest->size.x += request->pos.x;
if(surfacepartrequest->size.x <= 0)
return;
- surfacepartrequest->source.x -= request.pos.x;
- request.pos.x = 0;
+ surfacepartrequest->source.x -= request->pos.x;
+ request->pos.x = 0;
}
- if(request.pos.y < 0) {
- surfacepartrequest->size.y += request.pos.y;
+ if(request->pos.y < 0) {
+ surfacepartrequest->size.y += request->pos.y;
if(surfacepartrequest->size.y <= 0)
return;
- surfacepartrequest->source.y -= request.pos.y;
- request.pos.y = 0;
+ surfacepartrequest->source.y -= request->pos.y;
+ request->pos.y = 0;
}
- request.request_data = surfacepartrequest;
+ request->request_data = surfacepartrequest;
- drawingrequests.push_back(request);
+ requests->push_back(request);
}
void
DrawingContext::draw_text(const Font* font, const std::string& text,
const Vector& position, FontAlignment alignment, int layer)
{
- DrawingRequest request;
+ DrawingRequest* request = new(obst) DrawingRequest();
- request.type = TEXT;
- request.pos = transform.apply(position);
- request.layer = layer;
- request.drawing_effect = transform.drawing_effect;
- request.zoom = transform.zoom;
- request.alpha = transform.alpha;
+ request->target = target;
+ request->type = TEXT;
+ request->pos = transform.apply(position);
+ request->layer = layer;
+ request->drawing_effect = transform.drawing_effect;
+ request->alpha = transform.alpha;
- TextRequest* textrequest = new TextRequest;
+ TextRequest* textrequest = new(obst) TextRequest();
textrequest->font = font;
textrequest->text = text;
textrequest->alignment = alignment;
- request.request_data = textrequest;
+ request->request_data = textrequest;
- drawingrequests.push_back(request);
+ requests->push_back(request);
}
void
const Vector& position, int layer)
{
draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
- CENTER_ALLIGN, layer);
+ ALIGN_CENTER, layer);
}
void
-DrawingContext::draw_gradient(Color top, Color bottom, int layer)
+DrawingContext::draw_gradient(const Color& top, const Color& bottom, int layer)
{
- DrawingRequest request;
+ DrawingRequest* request = new(obst) DrawingRequest();
- request.type = GRADIENT;
- request.pos = Vector(0,0);
- request.layer = layer;
+ request->target = target;
+ request->type = GRADIENT;
+ request->pos = Vector(0,0);
+ request->layer = layer;
- request.drawing_effect = transform.drawing_effect;
- request.zoom = transform.zoom;
- request.alpha = transform.alpha;
+ request->drawing_effect = transform.drawing_effect;
+ request->alpha = transform.alpha;
- GradientRequest* gradientrequest = new GradientRequest;
+ GradientRequest* gradientrequest = new(obst) GradientRequest();
gradientrequest->top = top;
gradientrequest->bottom = bottom;
- request.request_data = gradientrequest;
+ request->request_data = gradientrequest;
- drawingrequests.push_back(request);
+ requests->push_back(request);
}
void
DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
- Color color, int layer)
+ const Color& color, int layer)
{
- DrawingRequest request;
+ DrawingRequest* request = new(obst) DrawingRequest();
- request.type = FILLRECT;
- request.pos = transform.apply(topleft);
- request.layer = layer;
+ request->target = target;
+ request->type = FILLRECT;
+ request->pos = transform.apply(topleft);
+ request->layer = layer;
- request.drawing_effect = transform.drawing_effect;
- request.zoom = transform.zoom;
- request.alpha = transform.alpha;
+ request->drawing_effect = transform.drawing_effect;
+ request->alpha = transform.alpha;
- FillRectRequest* fillrectrequest = new FillRectRequest;
+ FillRectRequest* fillrectrequest = new(obst) FillRectRequest();
fillrectrequest->size = size;
fillrectrequest->color = color;
- fillrectrequest->color.alpha
- = (int) ((float) fillrectrequest->color.alpha
- * ((float) transform.alpha / 255.0));
- request.request_data = fillrectrequest;
+ fillrectrequest->color.alpha = color.alpha * transform.alpha;
+ fillrectrequest->radius = 0.0f;
+ request->request_data = fillrectrequest;
- drawingrequests.push_back(request);
+ requests->push_back(request);
}
void
-DrawingContext::draw_surface_part(DrawingRequest& request)
+DrawingContext::draw_filled_rect(const Rect& rect, const Color& color,
+ int layer)
{
- SurfacePartRequest* surfacepartrequest
- = (SurfacePartRequest*) request.request_data;
-
- surfacepartrequest->surface->impl->draw_part(
- surfacepartrequest->source.x, surfacepartrequest->source.y,
- request.pos.x, request.pos.y,
- surfacepartrequest->size.x, surfacepartrequest->size.y, request.alpha,
- request.drawing_effect);
-
- delete surfacepartrequest;
+ draw_filled_rect(rect, color, 0.0f, layer);
}
void
-DrawingContext::draw_gradient(DrawingRequest& request)
+DrawingContext::draw_filled_rect(const Rect& rect, const Color& color, float radius, int layer)
{
- GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
- const Color& top = gradientrequest->top;
- const Color& bottom = gradientrequest->bottom;
-
- glBegin(GL_QUADS);
- glColor3ub(top.red, top.green, top.blue);
- glVertex2f(0, 0);
- glVertex2f(SCREEN_WIDTH, 0);
- glColor3ub(bottom.red, bottom.green, bottom.blue);
- glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
- glVertex2f(0, SCREEN_HEIGHT);
- glEnd();
-
- delete gradientrequest;
-}
+ DrawingRequest* request = new(obst) DrawingRequest();
-void
-DrawingContext::draw_text(DrawingRequest& request)
-{
- TextRequest* textrequest = (TextRequest*) request.request_data;
+ request->target = target;
+ request->type = FILLRECT;
+ request->pos = transform.apply(rect.p1);
+ request->layer = layer;
- textrequest->font->draw(textrequest->text, request.pos,
- textrequest->alignment, request.drawing_effect, request.alpha);
+ request->drawing_effect = transform.drawing_effect;
+ request->alpha = transform.alpha;
- delete textrequest;
+ FillRectRequest* fillrectrequest = new(obst) FillRectRequest;
+ fillrectrequest->size = Vector(rect.get_width(), rect.get_height());
+ fillrectrequest->color = color;
+ fillrectrequest->color.alpha = color.alpha * transform.alpha;
+ fillrectrequest->radius = radius;
+ request->request_data = fillrectrequest;
+
+ requests->push_back(request);
}
void
-DrawingContext::draw_filled_rect(DrawingRequest& request)
+DrawingContext::get_light(const Vector& position, Color* color)
{
- FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
-
- float x = request.pos.x;
- float y = request.pos.y;
- float w = fillrectrequest->size.x;
- float h = fillrectrequest->size.y;
-
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glColor4ub(fillrectrequest->color.red, fillrectrequest->color.green,
- fillrectrequest->color.blue, fillrectrequest->color.alpha);
-
- glBegin(GL_POLYGON);
- glVertex2f(x, y);
- glVertex2f(x+w, y);
- glVertex2f(x+w, y+h);
- glVertex2f(x, y+h);
- glEnd();
- glDisable(GL_BLEND);
-
- delete fillrectrequest;
+ if( ambient_color.red == 1.0f && ambient_color.green == 1.0f
+ && ambient_color.blue == 1.0f ) {
+ *color = Color( 1.0f, 1.0f, 1.0f);
+ return;
+ }
+
+ DrawingRequest* request = new(obst) DrawingRequest();
+ request->target = target;
+ request->type = GETLIGHT;
+ request->pos = transform.apply(position);
+
+ //There is no light offscreen.
+ if(request->pos.x >= SCREEN_WIDTH || request->pos.y >= SCREEN_HEIGHT
+ || request->pos.x < 0 || request->pos.y < 0){
+ *color = Color( 0, 0, 0);
+ return;
+ }
+
+ request->layer = LAYER_GUI; //make sure all get_light requests are handled last.
+ GetLightRequest* getlightrequest = new(obst) GetLightRequest();
+ getlightrequest->color_ptr = color;
+ request->request_data = getlightrequest;
+ lightmap_requests.push_back(request);
}
void
{
#ifdef DEBUG
assert(transformstack.empty());
+ assert(target_stack.empty());
#endif
transformstack.clear();
-
- std::stable_sort(drawingrequests.begin(), drawingrequests.end());
-
- for(DrawingRequests::iterator i = drawingrequests.begin();
- i != drawingrequests.end(); ++i) {
- switch(i->type) {
- case SURFACE:
- {
- const Surface* surface = (const Surface*) i->request_data;
-
- if(i->zoom != 1.0)
- surface->impl->draw_stretched(i->pos.x * i->zoom, i->pos.y * i->zoom,
- (int)(surface->w * i->zoom), (int)(surface->h * i->zoom),
- i->alpha, i->drawing_effect);
- else
- surface->impl->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
- break;
- }
- case SURFACE_PART:
- draw_surface_part(*i);
- break;
- case GRADIENT:
- draw_gradient(*i);
- break;
- case TEXT:
- draw_text(*i);
+ target_stack.clear();
+
+ //Use Lightmap if ambient color is not white.
+ bool use_lightmap = ( ambient_color.red != 1.0f || ambient_color.green != 1.0f ||
+ ambient_color.blue != 1.0f );
+
+ // PART1: create lightmap
+ if(use_lightmap) {
+ lightmap->start_draw(ambient_color);
+ handle_drawing_requests(lightmap_requests);
+ lightmap->end_draw();
+ }
+ lightmap_requests.clear();
+
+ handle_drawing_requests(drawing_requests);
+ drawing_requests.clear();
+ if(use_lightmap) {
+ lightmap->do_draw();
+ }
+ obstack_free(&obst, NULL);
+ obstack_init(&obst);
+
+ // if a screenshot was requested, take one
+ if (screenshot_requested) {
+ renderer->do_take_screenshot();
+ screenshot_requested = false;
+ }
+
+ renderer->flip();
+}
+
+class RequestPtrCompare
+ : public std::binary_function<const DrawingRequest*,
+ const DrawingRequest*,
+ bool>
+{
+public:
+ bool operator()(const DrawingRequest* r1, const DrawingRequest* r2) const
+ {
+ return *r1 < *r2;
+ }
+};
+
+void
+DrawingContext::handle_drawing_requests(DrawingRequests& requests)
+{
+ std::stable_sort(requests.begin(), requests.end(), RequestPtrCompare());
+
+ DrawingRequests::const_iterator i;
+ for(i = requests.begin(); i != requests.end(); ++i) {
+ const DrawingRequest& request = **i;
+
+ switch(request.target) {
+ case NORMAL:
+ switch(request.type) {
+ case SURFACE:
+ renderer->draw_surface(request);
+ break;
+ case SURFACE_PART:
+ renderer->draw_surface_part(request);
+ break;
+ case GRADIENT:
+ renderer->draw_gradient(request);
+ break;
+ case TEXT:
+ {
+ const TextRequest* textrequest = (TextRequest*) request.request_data;
+ textrequest->font->draw(renderer, textrequest->text, request.pos,
+ textrequest->alignment, request.drawing_effect, request.alpha);
+ }
+ break;
+ case FILLRECT:
+ renderer->draw_filled_rect(request);
+ break;
+ case GETLIGHT:
+ lightmap->get_light(request);
+ break;
+ }
break;
- case FILLRECT:
- draw_filled_rect(*i);
+ case LIGHTMAP:
+ switch(request.type) {
+ case SURFACE:
+ lightmap->draw_surface(request);
+ break;
+ case SURFACE_PART:
+ lightmap->draw_surface_part(request);
+ break;
+ case GRADIENT:
+ lightmap->draw_gradient(request);
+ break;
+ case TEXT:
+ {
+ const TextRequest* textrequest = (TextRequest*) request.request_data;
+ textrequest->font->draw(renderer, textrequest->text, request.pos,
+ textrequest->alignment, request.drawing_effect, request.alpha);
+ }
+ break;
+ case FILLRECT:
+ lightmap->draw_filled_rect(request);
+ break;
+ case GETLIGHT:
+ lightmap->get_light(request);
+ break;
+ }
break;
}
}
-
- drawingrequests.clear();
-
- // update screen
- SDL_GL_SwapBuffers();
}
void
}
void
-DrawingContext::set_drawing_effect(uint32_t effect)
+DrawingContext::set_drawing_effect(DrawingEffect effect)
{
transform.drawing_effect = effect;
}
-uint32_t
+DrawingEffect
DrawingContext::get_drawing_effect() const
{
return transform.drawing_effect;
}
void
-DrawingContext::set_zooming(float zoom)
+DrawingContext::set_alpha(float alpha)
+{
+ transform.alpha = alpha;
+}
+
+float
+DrawingContext::get_alpha() const
+{
+ return transform.alpha;
+}
+
+void
+DrawingContext::push_target()
{
- transform.zoom = zoom;
+ target_stack.push_back(target);
}
void
-DrawingContext::set_alpha(uint8_t alpha)
+DrawingContext::pop_target()
{
- transform.alpha = alpha;
+ set_target(target_stack.back());
+ target_stack.pop_back();
}
-uint8_t
-DrawingContext::get_alpha() const
+void
+DrawingContext::set_target(Target target)
{
- return transform.alpha;
+ this->target = target;
+ if(target == LIGHTMAP) {
+ requests = &lightmap_requests;
+ } else {
+ assert(target == NORMAL);
+ requests = &drawing_requests;
+ }
+}
+
+void
+DrawingContext::set_ambient_color( Color new_color )
+{
+ ambient_color = new_color;
}
+
+void
+DrawingContext::take_screenshot()
+{
+ screenshot_requested = true;
+}
+