Merge branch 'feature/c++11'
[supertux.git] / src / video / drawing_context.cpp
index 89b925c..96d06ab 100644 (file)
 //  You should have received a copy of the GNU General Public License
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+#include "video/drawing_context.hpp"
+
 #include <algorithm>
 #include <config.h>
 
-#include "video/drawing_context.hpp"
-
-#include "obstack/obstackpp.hpp"
+#include "math/sizef.hpp"
 #include "supertux/gameconfig.hpp"
-#include "supertux/main.hpp"
+#include "supertux/globals.hpp"
+#include "util/obstackpp.hpp"
 #include "video/drawing_request.hpp"
 #include "video/lightmap.hpp"
 #include "video/renderer.hpp"
 #include "video/texture_manager.hpp"
 #include "video/video_systems.hpp"
 
-static inline int next_po2(int val)
-{
-  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)
+DrawingContext::DrawingContext(Renderer& renderer, Lightmap& lightmap) :
+  renderer(renderer),
+  lightmap(lightmap),
+  transformstack(),
+  transform(),
+  blend_stack(),
+  blend_mode(),
+  drawing_requests(),
+  lightmap_requests(),
+  requests(),
+  ambient_color(1.0f, 1.0f, 1.0f, 1.0f),
+  target(NORMAL),
+  target_stack(),
+  obst(),
+  screenshot_requested(false)
 {
   requests = &drawing_requests;
   obstack_init(&obst);
@@ -48,24 +53,11 @@ DrawingContext::DrawingContext() :
 
 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,
+DrawingContext::draw_surface(SurfacePtr surface, const Vector& position,
                              float angle, const Color& color, const Blend& blend,
                              int layer)
 {
@@ -89,20 +81,20 @@ DrawingContext::draw_surface(const Surface* surface, const Vector& position,
   request->color = color;
   request->blend = blend;
 
-  request->request_data = const_cast<Surface*> (surface);
+  request->request_data = surface.get();
 
   requests->push_back(request);
 }
 
 void
-DrawingContext::draw_surface(const Surface* surface, const Vector& position,
+DrawingContext::draw_surface(SurfacePtr 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,
+DrawingContext::draw_surface_part(SurfacePtr surface, const Vector& source,
                                   const Vector& size, const Vector& dest, int layer)
 {
   assert(surface != 0);
@@ -119,7 +111,7 @@ DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
   SurfacePartRequest* surfacepartrequest = new(obst) SurfacePartRequest();
   surfacepartrequest->size = size;
   surfacepartrequest->source = source;
-  surfacepartrequest->surface = surface;
+  surfacepartrequest->surface = surface.get();
 
   // clip on screen borders
   if(request->pos.x < 0) {
@@ -142,7 +134,7 @@ DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
 }
 
 void
-DrawingContext::draw_text(const Font* font, const std::string& text,
+DrawingContext::draw_text(FontPtr font, const std::string& text,
                           const Vector& position, FontAlignment alignment, int layer, Color color)
 {
   DrawingRequest* request = new(obst) DrawingRequest();
@@ -156,7 +148,7 @@ DrawingContext::draw_text(const Font* font, const std::string& text,
   request->color = color;
 
   TextRequest* textrequest = new(obst) TextRequest();
-  textrequest->font = font;
+  textrequest->font = font.get();
   textrequest->text = text;
   textrequest->alignment = alignment;
   request->request_data = textrequest;
@@ -165,7 +157,7 @@ DrawingContext::draw_text(const Font* font, const std::string& text,
 }
 
 void
-DrawingContext::draw_center_text(const Font* font, const std::string& text,
+DrawingContext::draw_center_text(FontPtr font, const std::string& text,
                                  const Vector& position, int layer, Color color)
 {
   draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
@@ -218,14 +210,14 @@ DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
 }
 
 void
-DrawingContext::draw_filled_rect(const Rect& rect, const Color& color,
+DrawingContext::draw_filled_rect(const Rectf& rect, const Color& color,
                                  int layer)
 {
   draw_filled_rect(rect, color, 0.0f, layer);
 }
 
 void
-DrawingContext::draw_filled_rect(const Rect& rect, const Color& color, float radius, int layer)
+DrawingContext::draw_filled_rect(const Rectf& rect, const Color& color, float radius, int layer)
 {
   DrawingRequest* request = new(obst) DrawingRequest();
 
@@ -270,6 +262,14 @@ DrawingContext::draw_inverse_ellipse(const Vector& pos, const Vector& size, cons
   requests->push_back(request);     
 }
 
+Rectf
+DrawingContext::get_cliprect() const
+{
+  return Rectf(get_translation().x, get_translation().y,
+               get_translation().x + SCREEN_WIDTH, 
+               get_translation().y + SCREEN_HEIGHT);
+}
+
 void
 DrawingContext::get_light(const Vector& position, Color* color)
 {
@@ -301,10 +301,8 @@ DrawingContext::get_light(const Vector& position, Color* color)
 void
 DrawingContext::do_drawing()
 {
-#ifdef DEBUG
   assert(transformstack.empty());
   assert(target_stack.empty());
-#endif
   transformstack.clear();
   target_stack.clear();
 
@@ -314,9 +312,9 @@ DrawingContext::do_drawing()
 
   // PART1: create lightmap
   if(use_lightmap) {
-    lightmap->start_draw(ambient_color);
+    lightmap.start_draw(ambient_color);
     handle_drawing_requests(lightmap_requests);
-    lightmap->end_draw();
+    lightmap.end_draw();
 
     DrawingRequest* request = new(obst) DrawingRequest();
     request->target = NORMAL;
@@ -333,17 +331,14 @@ DrawingContext::do_drawing()
 
   // if a screenshot was requested, take one
   if (screenshot_requested) {
-    renderer->do_take_screenshot();
+    renderer.do_take_screenshot();
     screenshot_requested = false;
   }
 
-  renderer->flip();
+  renderer.flip();
 }
 
 class RequestPtrCompare
-  :  public std::binary_function<const DrawingRequest*,
-                                 const DrawingRequest*, 
-                                 bool>
 {
 public:
   bool operator()(const DrawingRequest* r1, const DrawingRequest* r2) const
@@ -365,64 +360,64 @@ DrawingContext::handle_drawing_requests(DrawingRequests& requests)
       case NORMAL:
         switch(request.type) {
           case SURFACE:
-            renderer->draw_surface(request);
+            renderer.draw_surface(request);
             break;
           case SURFACE_PART:
-            renderer->draw_surface_part(request);
+            renderer.draw_surface_part(request);
             break;
           case GRADIENT:
-            renderer->draw_gradient(request);
+            renderer.draw_gradient(request);
             break;
           case TEXT:
           {
             const TextRequest* textrequest = (TextRequest*) request.request_data;
-            textrequest->font->draw(renderer, textrequest->text, request.pos,
+            textrequest->font->draw(&renderer, textrequest->text, request.pos,
                                     textrequest->alignment, request.drawing_effect, request.color, request.alpha);
           }
           break;
           case FILLRECT:
-            renderer->draw_filled_rect(request);
+            renderer.draw_filled_rect(request);
             break;
           case INVERSEELLIPSE:
-            renderer->draw_inverse_ellipse(request);
+            renderer.draw_inverse_ellipse(request);
             break;
           case DRAW_LIGHTMAP:
-            lightmap->do_draw();
+            lightmap.do_draw();
             break;
           case GETLIGHT:
-            lightmap->get_light(request);
+            lightmap.get_light(request);
             break;
         }
         break;
       case LIGHTMAP:
         switch(request.type) {
           case SURFACE:
-            lightmap->draw_surface(request);
+            lightmap.draw_surface(request);
             break;
           case SURFACE_PART:
-            lightmap->draw_surface_part(request);
+            lightmap.draw_surface_part(request);
             break;
           case GRADIENT:
-            lightmap->draw_gradient(request);
+            lightmap.draw_gradient(request);
             break;
           case TEXT:
           {
             const TextRequest* textrequest = (TextRequest*) request.request_data;
-            textrequest->font->draw(renderer, textrequest->text, request.pos,
+            textrequest->font->draw(&renderer, textrequest->text, request.pos,
                                     textrequest->alignment, request.drawing_effect, request.color, request.alpha);
           }
           break;
           case FILLRECT:
-            lightmap->draw_filled_rect(request);
+            lightmap.draw_filled_rect(request);
             break;
           case INVERSEELLIPSE:
             assert(!"InverseEllipse doesn't make sense on the lightmap");
             break;
           case DRAW_LIGHTMAP:
-            lightmap->do_draw();
+            lightmap.do_draw();
             break;
           case GETLIGHT:
-            lightmap->get_light(request);
+            lightmap.get_light(request);
             break;
         }
         break;