- some more work on the new unisolid snow tiles and a test level
[supertux.git] / src / video / drawing_context.cpp
index 7fcb205..4c5f179 100644 (file)
@@ -33,8 +33,7 @@
 #include "glutil.hpp"
 #include "texture.hpp"
 #include "texture_manager.hpp"
-
-#define LIGHTMAP_DIV 4
+#define LIGHTMAP_DIV 5
 
 static inline int next_po2(int val)
 {
@@ -45,8 +44,8 @@ static inline int next_po2(int val)
   return result;
 }
 
-DrawingContext::DrawingContext()
-  ambient_color( 1.0f, 1.0f, 1.0f, 1.0f )
+DrawingContext::DrawingContext()
+  ambient_color( 1.0f, 1.0f, 1.0f, 1.0f )
 {
   screen = SDL_GetVideoSurface();
 
@@ -171,7 +170,7 @@ DrawingContext::draw_center_text(const Font* font, const std::string& text,
     const Vector& position, int layer)
 {
   draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
-      CENTER_ALLIGN, layer);
+      ALIGN_CENTER, layer);
 }
 
 void
@@ -239,6 +238,51 @@ DrawingContext::draw_filled_rect(const Rect& rect, const Color& color,
 }
 
 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
 DrawingContext::draw_surface_part(DrawingRequest& request)
 {
   SurfacePartRequest* surfacepartrequest
@@ -311,6 +355,34 @@ DrawingContext::draw_filled_rect(DrawingRequest& request)
 }
 
 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
@@ -349,36 +421,18 @@ DrawingContext::do_drawing()
     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();
-
-  if(use_lightmap) {
-    // multiple the lightmap with the framebuffer
-    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);
-
-    glEnd();
-
-    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-  }
-
   assert_gl("drawing");
 
   SDL_GL_SwapBuffers();
@@ -395,7 +449,9 @@ DrawingContext::handle_drawing_requests(DrawingRequests& requests)
       case SURFACE:
       {
         const Surface* surface = (const Surface*) i->request_data;
-        if (i->angle == 0.0f)
+        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->draw(i->pos.x, i->pos.y, i->alpha, i->angle, i->color, i->blend, i->drawing_effect);
@@ -413,6 +469,12 @@ DrawingContext::handle_drawing_requests(DrawingRequests& requests)
       case FILLRECT:
         draw_filled_rect(*i);
         break;
+      case LIGHTMAPREQUEST:
+        draw_lightmap(*i);
+        break;
+      case GETLIGHT:
+        get_light(*i);
+        break;
     }
   }
 }