-// $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
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
#include <config.h>
#include <algorithm>
#include <cassert>
#include <iostream>
+#include <SDL_image.h>
+#include <GL/gl.h>
+
+#include "drawing_context.hpp"
+#include "surface.hpp"
+#include "font.hpp"
+#include "main.hpp"
+#include "gameconfig.hpp"
+#include "glutil.hpp"
+#include "texture.hpp"
+#include "texture_manager.hpp"
+#define LIGHTMAP_DIV 5
+
+static inline int next_po2(int val)
+{
+ int result = 1;
+ while(result < val)
+ result *= 2;
-#include "drawing_context.h"
-#include "surface.h"
-#include "font.h"
-#include "main.h"
-#include "gameconfig.h"
+ return result;
+}
-DrawingContext::DrawingContext(SDL_Surface* targetsurface)
+DrawingContext::DrawingContext()
+ : ambient_color( 1.0f, 1.0f, 1.0f, 1.0f )
{
- if(targetsurface) {
- screen = targetsurface;
- } else {
- screen = SDL_GetVideoSurface();
- }
+ screen = SDL_GetVideoSurface();
+
+ lightmap_width = screen->w / LIGHTMAP_DIV;
+ lightmap_height = screen->h / LIGHTMAP_DIV;
+ unsigned int width = next_po2(lightmap_width);
+ unsigned int height = next_po2(lightmap_height);
+
+ lightmap = new Texture(width, height, GL_RGB);
+
+ lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
+ lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
+ texture_manager->register_texture(lightmap);
+
+ requests = &drawing_requests;
}
DrawingContext::~DrawingContext()
{
+ texture_manager->remove_texture(lightmap);
+ delete lightmap;
}
void
DrawingContext::draw_surface(const Surface* surface, const Vector& position,
- int layer, uint32_t drawing_effect)
+ float angle, const Color& color, const Blend& blend,
+ int layer)
{
assert(surface != 0);
-
+
DrawingRequest request;
request.type = SURFACE;
request.pos = transform.apply(position);
if(request.pos.x >= SCREEN_WIDTH || request.pos.y >= SCREEN_HEIGHT
- || request.pos.x + surface->w < 0 || request.pos.y + surface->h < 0)
+ || request.pos.x + surface->get_width() < 0
+ || request.pos.y + surface->get_height() < 0)
return;
request.layer = layer;
- request.drawing_effect = transform.drawing_effect | drawing_effect;
- request.zoom = transform.zoom;
+ request.drawing_effect = transform.drawing_effect;
request.alpha = transform.alpha;
- request.request_data = const_cast<Surface*> (surface);
+ request.angle = angle;
+ request.color = color;
+ request.blend = blend;
- drawingrequests.push_back(request);
+ request.request_data = const_cast<Surface*> (surface);
+
+ 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
DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
- const Vector& size, const Vector& dest, int layer, uint32_t drawing_effect)
+ const Vector& size, const Vector& dest, int layer)
{
assert(surface != 0);
request.type = SURFACE_PART;
request.pos = transform.apply(dest);
request.layer = layer;
- request.drawing_effect = transform.drawing_effect | drawing_effect;
+ request.drawing_effect = transform.drawing_effect;
request.alpha = transform.alpha;
-
+
SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
surfacepartrequest->size = size;
surfacepartrequest->source = source;
}
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,
- uint32_t drawing_effect)
+ const Vector& position, FontAlignment alignment, int layer)
{
DrawingRequest request;
request.type = TEXT;
request.pos = transform.apply(position);
request.layer = layer;
- request.drawing_effect = transform.drawing_effect | drawing_effect;
- request.zoom = transform.zoom;
+ request.drawing_effect = transform.drawing_effect;
request.alpha = transform.alpha;
TextRequest* textrequest = new TextRequest;
textrequest->alignment = alignment;
request.request_data = textrequest;
- drawingrequests.push_back(request);
+ requests->push_back(request);
}
void
DrawingContext::draw_center_text(const Font* font, const std::string& text,
- const Vector& position, int layer, uint32_t drawing_effect)
+ const Vector& position, int layer)
{
draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
- CENTER_ALLIGN, layer, drawing_effect);
+ 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;
request.layer = layer;
request.drawing_effect = transform.drawing_effect;
- request.zoom = transform.zoom;
request.alpha = transform.alpha;
GradientRequest* gradientrequest = new GradientRequest;
gradientrequest->bottom = bottom;
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;
request.layer = layer;
request.drawing_effect = transform.drawing_effect;
- request.zoom = transform.zoom;
- request.alpha = transform.alpha;
+ request.alpha = transform.alpha;
FillRectRequest* fillrectrequest = new FillRectRequest;
fillrectrequest->size = size;
fillrectrequest->color = color;
+ fillrectrequest->color.alpha = color.alpha * transform.alpha;
request.request_data = fillrectrequest;
- drawingrequests.push_back(request);
+ requests->push_back(request);
+}
+
+void
+DrawingContext::draw_filled_rect(const Rect& rect, const Color& color,
+ int layer)
+{
+ DrawingRequest request;
+
+ request.type = FILLRECT;
+ request.pos = transform.apply(rect.p1);
+ request.layer = layer;
+
+ request.drawing_effect = transform.drawing_effect;
+ request.alpha = transform.alpha;
+
+ FillRectRequest* fillrectrequest = new FillRectRequest;
+ fillrectrequest->size = Vector(rect.get_width(), rect.get_height());
+ fillrectrequest->color = color;
+ fillrectrequest->color.alpha = color.alpha * transform.alpha;
+ request.request_data = fillrectrequest;
+
+ requests->push_back(request);
+}
+
+void
+DrawingContext::get_light(const Vector& position, Color* color)
+{
+ 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;
+ 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 GetLightRequest;
+ getlightrequest->color_ptr = color;
+ request.request_data = getlightrequest;
+ lightmap_requests.push_back(request);
+}
+
+void
+DrawingContext::get_light(DrawingRequest& request)
+{
+ GetLightRequest* getlightrequest = (GetLightRequest*) request.request_data;
+
+ float pixels[3];
+ for( int i = 0; i<3; i++)
+ pixels[i] = 0.0f; //set to black
+
+ float posX = request.pos.x * lightmap_width / SCREEN_WIDTH;
+ float posY = screen->h - request.pos.y * lightmap_height / SCREEN_HEIGHT;
+ glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
+ *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);
+ //printf("get_light %f/%f =>%f/%f r%f g%f b%f\n", request.pos.x, request.pos.y, posX, posY, pixels[0], pixels[1], pixels[2]);
+
+ delete getlightrequest;
}
void
SurfacePartRequest* surfacepartrequest
= (SurfacePartRequest*) request.request_data;
- surfacepartrequest->surface->impl->draw_part(
+ surfacepartrequest->surface->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);
+ surfacepartrequest->size.x, surfacepartrequest->size.y,
+ request.alpha, request.drawing_effect);
delete surfacepartrequest;
}
GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
const Color& top = gradientrequest->top;
const Color& bottom = gradientrequest->bottom;
-
-#ifndef NOOPENGL
- if(config->use_gl)
- {
- 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();
- }
- else
- {
-#endif
- if(&top == &bottom)
- {
- fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, top.red, top.green, top.blue);
- }
- else
- {
- float redstep = (float(bottom.red)-float(top.red)) / float(SCREEN_HEIGHT);
- float greenstep = (float(bottom.green)-float(top.green)) / float(SCREEN_HEIGHT);
- float bluestep = (float(bottom.blue) - float(top.blue)) / float(SCREEN_HEIGHT);
-
- for(float y = 0; y < SCREEN_HEIGHT; y += 2)
- fillrect(0, (int)y, SCREEN_WIDTH, 2,
- int(float(top.red) + redstep * y),
- int(float(top.green) + greenstep * y),
- int(float(top.blue) + bluestep * y), 255);
- }
-#ifndef NOOPENGL
- }
-#endif
+ glDisable(GL_TEXTURE_2D);
+ glBegin(GL_QUADS);
+ glColor4f(top.red, top.green, top.blue, top.alpha);
+ glVertex2f(0, 0);
+ glVertex2f(SCREEN_WIDTH, 0);
+ glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
+ glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
+ glVertex2f(0, SCREEN_HEIGHT);
+ glEnd();
+ glEnable(GL_TEXTURE_2D);
delete gradientrequest;
}
float w = fillrectrequest->size.x;
float h = fillrectrequest->size.y;
-#ifndef NOOPENGL
- if(config->use_gl)
- {
- 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);
- }
- else
- {
-#endif
- SDL_Rect src, rect;
- SDL_Surface *temp = NULL;
-
- rect.x = (int)x;
- rect.y = (int)y;
- rect.w = (int)w;
- rect.h = (int)h;
-
- if(fillrectrequest->color.alpha != 255)
- {
- temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
- screen->format->Rmask,
- screen->format->Gmask,
- screen->format->Bmask,
- screen->format->Amask);
-
-
- src.x = 0;
- src.y = 0;
- src.w = rect.w;
- src.h = rect.h;
-
- SDL_FillRect(temp, &src, SDL_MapRGB(screen->format,
- fillrectrequest->color.red, fillrectrequest->color.green,
- fillrectrequest->color.blue));
-
- SDL_SetAlpha(temp, SDL_SRCALPHA, fillrectrequest->color.alpha);
-
- SDL_BlitSurface(temp,0,screen,&rect);
-
- SDL_FreeSurface(temp);
- }
- else
- SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format,
- fillrectrequest->color.red, fillrectrequest->color.green,
- fillrectrequest->color.blue));
-
-#ifndef NOOPENGL
-
- }
-#endif
+ glDisable(GL_TEXTURE_2D);
+ glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
+ fillrectrequest->color.blue, fillrectrequest->color.alpha);
+
+ glBegin(GL_QUADS);
+ glVertex2f(x, y);
+ glVertex2f(x+w, y);
+ glVertex2f(x+w, y+h);
+ glVertex2f(x, y+h);
+ glEnd();
+ glEnable(GL_TEXTURE_2D);
delete fillrectrequest;
}
void
+DrawingContext::draw_lightmap(DrawingRequest& request)
+{
+ const Texture* texture = reinterpret_cast<Texture*> (request.request_data);
+
+ // multiple the lightmap with the framebuffer
+ glBlendFunc(GL_DST_COLOR, GL_ZERO);
+
+ glBindTexture(GL_TEXTURE_2D, texture->get_handle());
+ glBegin(GL_QUADS);
+
+ glTexCoord2f(0, lightmap_uv_bottom);
+ glVertex2f(0, 0);
+
+ glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
+ glVertex2f(SCREEN_WIDTH, 0);
+
+ glTexCoord2f(lightmap_uv_right, 0);
+ glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
+
+ glTexCoord2f(0, 0);
+ glVertex2f(0, SCREEN_HEIGHT);
+
+ glEnd();
+
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+}
+
+void
DrawingContext::do_drawing()
{
#ifdef DEBUG
assert(transformstack.empty());
+ assert(target_stack.empty());
#endif
transformstack.clear();
-
- std::stable_sort(drawingrequests.begin(), drawingrequests.end());
+ 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) {
+ glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+
+ glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
+ glClear(GL_COLOR_BUFFER_BIT);
+ handle_drawing_requests(lightmap_requests);
+ lightmap_requests.clear();
+
+ glDisable(GL_BLEND);
+ glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
+ glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
+
+ glViewport(0, 0, screen->w, screen->h);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ glEnable(GL_BLEND);
+
+ // add a lightmap drawing request into the queue
+ DrawingRequest request;
+ request.type = LIGHTMAPREQUEST;
+ request.layer = LAYER_HUD - 1;
+ request.request_data = lightmap;
+ requests->push_back(request);
+ }
+
+ //glClear(GL_COLOR_BUFFER_BIT);
+ handle_drawing_requests(drawing_requests);
+ drawing_requests.clear();
+ assert_gl("drawing");
- for(DrawingRequests::iterator i = drawingrequests.begin();
- i != drawingrequests.end(); ++i) {
+ SDL_GL_SwapBuffers();
+}
+
+void
+DrawingContext::handle_drawing_requests(DrawingRequests& requests)
+{
+ std::stable_sort(requests.begin(), requests.end());
+
+ for(DrawingRequests::iterator i = requests.begin();
+ i != requests.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);
+ if (i->angle == 0.0f &&
+ i->color.red == 1.0f && i->color.green == 1.0f &&
+ i->color.blue == 1.0f && i->color.alpha == 1.0f )
+ surface->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
else
- surface->impl->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
+ surface->draw(i->pos.x, i->pos.y, i->alpha, i->angle, i->color, i->blend, i->drawing_effect);
break;
}
case SURFACE_PART:
case FILLRECT:
draw_filled_rect(*i);
break;
+ case LIGHTMAPREQUEST:
+ draw_lightmap(*i);
+ break;
+ case GETLIGHT:
+ get_light(*i);
+ break;
}
}
-
- // update screen
- if(config->use_gl)
- SDL_GL_SwapBuffers();
- else
- SDL_Flip(screen);
-
- drawingrequests.clear();
}
void
}
void
-DrawingContext::set_drawing_effect(int effect)
+DrawingContext::set_drawing_effect(DrawingEffect effect)
{
transform.drawing_effect = effect;
}
-void
-DrawingContext::set_zooming(float zoom)
+DrawingEffect
+DrawingContext::get_drawing_effect() const
{
- transform.zoom = zoom;
+ return transform.drawing_effect;
}
void
-DrawingContext::set_alpha(int alpha)
+DrawingContext::set_alpha(float alpha)
{
transform.alpha = alpha;
}
+
+float
+DrawingContext::get_alpha() const
+{
+ return transform.alpha;
+}
+
+void
+DrawingContext::push_target()
+{
+ target_stack.push_back(target);
+}
+
+void
+DrawingContext::pop_target()
+{
+ set_target(target_stack.back());
+ target_stack.pop_back();
+}
+
+void
+DrawingContext::set_target(Target target)
+{
+ this->target = target;
+ if(target == LIGHTMAP)
+ requests = &lightmap_requests;
+ else
+ requests = &drawing_requests;
+}
+
+void
+DrawingContext::set_ambient_color( Color new_color )
+{
+ ambient_color = new_color;
+}