- Reworked Surface class and drawing stuff:
[supertux.git] / src / video / drawing_context.cpp
index 7c83695..6f1aee1 100644 (file)
 #include <algorithm>
 #include <cassert>
 #include <iostream>
+#include <SDL_image.h>
+#include <GL/gl.h>
+#include <GL/glu.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"
 
-DrawingContext::DrawingContext(SDL_Surface* targetsurface)
+#define LIGHTMAP_DIV 4
+
+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()
+{
+  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
@@ -53,16 +81,16 @@ DrawingContext::draw_surface(const Surface* surface, const Vector& position,
   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;
-  request.zoom = transform.zoom;
   request.alpha = transform.alpha;
   request.request_data = const_cast<Surface*> (surface);  
 
-  drawingrequests.push_back(request);
+  requests->push_back(request);
 }
 
 void
@@ -101,7 +129,7 @@ DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
   }
   request.request_data = surfacepartrequest;
 
-  drawingrequests.push_back(request);
+  requests->push_back(request);
 }
 
 void
@@ -114,7 +142,6 @@ DrawingContext::draw_text(const Font* font, const std::string& text,
   request.pos = transform.apply(position);
   request.layer = layer;
   request.drawing_effect = transform.drawing_effect;
-  request.zoom = transform.zoom;
   request.alpha = transform.alpha;
 
   TextRequest* textrequest = new TextRequest;
@@ -123,7 +150,7 @@ DrawingContext::draw_text(const Font* font, const std::string& text,
   textrequest->alignment = alignment;
   request.request_data = textrequest;
 
-  drawingrequests.push_back(request);
+  requests->push_back(request);
 }
 
 void
@@ -135,7 +162,7 @@ DrawingContext::draw_center_text(const Font* font, const std::string& text,
 }
 
 void
-DrawingContext::draw_gradient(Color top, Color bottom, int layer)
+DrawingContext::draw_gradient(const Color& top, const Color& bottom, int layer)
 {
   DrawingRequest request;
 
@@ -144,7 +171,6 @@ DrawingContext::draw_gradient(Color top, Color bottom, int layer)
   request.layer = layer;
 
   request.drawing_effect = transform.drawing_effect;
-  request.zoom = transform.zoom;
   request.alpha = transform.alpha;
 
   GradientRequest* gradientrequest = new GradientRequest;
@@ -152,12 +178,12 @@ DrawingContext::draw_gradient(Color top, Color bottom, int layer)
   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;
 
@@ -166,18 +192,15 @@ DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
   request.layer = layer;
 
   request.drawing_effect = transform.drawing_effect;
-  request.zoom = transform.zoom;
   request.alpha = transform.alpha;                    
 
   FillRectRequest* fillrectrequest = new FillRectRequest;
   fillrectrequest->size = size;
   fillrectrequest->color = color;
-  fillrectrequest->color.alpha
-      = (int) ((float) fillrectrequest->color.alpha 
-              * ((float) transform.alpha / 255.0));
+  fillrectrequest->color.alpha = color.alpha * transform.alpha;
   request.request_data = fillrectrequest;
 
-  drawingrequests.push_back(request);
+  requests->push_back(request);
 }
 
 void
@@ -186,11 +209,11 @@ DrawingContext::draw_surface_part(DrawingRequest& request)
   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;
 }
@@ -203,10 +226,10 @@ DrawingContext::draw_gradient(DrawingRequest& request)
   const Color& bottom = gradientrequest->bottom;
   
   glBegin(GL_QUADS);
-  glColor3ub(top.red, top.green, top.blue);
+  glColor4f(top.red, top.green, top.blue, top.alpha);
   glVertex2f(0, 0);
   glVertex2f(SCREEN_WIDTH, 0);
-  glColor3ub(bottom.red, bottom.green, bottom.blue);
+  glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
   glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
   glVertex2f(0, SCREEN_HEIGHT);
   glEnd();
@@ -235,18 +258,17 @@ DrawingContext::draw_filled_rect(DrawingRequest& request)
   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);
+  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();
-  glDisable(GL_BLEND);
+  glEnable(GL_TEXTURE_2D);
 
   delete fillrectrequest;
 }
@@ -256,24 +278,84 @@ DrawingContext::do_drawing()
 {
 #ifdef DEBUG
   assert(transformstack.empty());
+  assert(target_stack.empty());
 #endif
   transformstack.clear();
+  target_stack.clear();
+
+  bool use_lightmap = lightmap_requests.size() != 0;
+  
+  // 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(1.0f, 1.0f, 1.0f, 1.0f);
+    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);
+  }
+
+  //glClear(GL_COLOR_BUFFER_BIT);
+  handle_drawing_requests(drawing_requests);
+  drawing_requests.clear();
+
+  if(use_lightmap) {
+    glBlendFunc(GL_DST_COLOR, GL_ZERO);
+
+    glBindTexture(GL_TEXTURE_2D, lightmap->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);
     
-  std::stable_sort(drawingrequests.begin(), drawingrequests.end());
+    glEnd();
+
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+  }
+
+  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);
-        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->drawing_effect);
         break;
       }
       case SURFACE_PART:
@@ -290,11 +372,6 @@ DrawingContext::do_drawing()
         break;
     }
   }
-
-  drawingrequests.clear();
-
-  // update screen
-  SDL_GL_SwapBuffers();
 }
 
 void
@@ -313,31 +390,49 @@ DrawingContext::pop_transform()
 }
 
 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.zoom = zoom;
+  transform.alpha = alpha;
+}
+
+float
+DrawingContext::get_alpha() const
+{
+  return transform.alpha;
 }
 
 void
-DrawingContext::set_alpha(uint8_t alpha)
+DrawingContext::push_target()
 {
-  transform.alpha = alpha;
+  target_stack.push_back(target);
 }
 
-uint8_t
-DrawingContext::get_alpha() const
+void
+DrawingContext::pop_target()
 {
-  return transform.alpha;
+  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;
 }
+