Added a function to draw text on center of screen for comodity.
[supertux.git] / lib / video / drawing_context.cpp
index f6e9af1..889b863 100644 (file)
@@ -30,6 +30,8 @@ using namespace SuperTux;
 DrawingContext::DrawingContext()
 {
 transform.draw_effect = NONE_EFFECT;
+transform.zoom = 1;
+transform.alpha = 255;
 }
 
 DrawingContext::~DrawingContext()
@@ -50,6 +52,8 @@ DrawingContext::draw_surface(const Surface* surface, const Vector& position,
   request.pos = transform.apply(position);
   request.drawing_effect = drawing_effect;
   request.drawing_effect = transform.draw_effect | drawing_effect;
+  request.zoom = transform.zoom;
+  request.alpha = transform.alpha;
 
   drawingrequests.push_back(request);
 }
@@ -66,6 +70,7 @@ DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
   request.layer = layer;
   request.pos = transform.apply(dest);
   request.drawing_effect = drawing_effect;
+  request.alpha = transform.alpha;
   
   SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
   surfacepartrequest->size = size;
@@ -78,7 +83,8 @@ DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
 
 void
 DrawingContext::draw_text(Font* font, const std::string& text,
-    const Vector& position, int layer, Uint32 drawing_effect)
+    const Vector& position, int allignment, int layer,
+    Uint32 drawing_effect)
 {
   DrawingRequest request;
 
@@ -86,33 +92,23 @@ DrawingContext::draw_text(Font* font, const std::string& text,
   request.layer = layer;
   request.pos = transform.apply(position);
   request.drawing_effect = drawing_effect;
+  request.alpha = transform.alpha;
 
   TextRequest* textrequest = new TextRequest;
   textrequest->font = font;
   textrequest->text = text;
+  textrequest->allignment = allignment;
   request.request_data = textrequest;
 
   drawingrequests.push_back(request);
 }
 
 void
-DrawingContext::draw_text_center(Font* font, const std::string& text,
+DrawingContext::draw_center_text(Font* font, const std::string& text,
     const Vector& position, int layer, Uint32 drawing_effect)
 {
-  DrawingRequest request;
-
-  request.type = TEXT;
-  request.layer = layer;
-  request.pos = transform.apply(position) + Vector(screen->w/2 - 
-      font->get_text_width(text)/2, 0);
-  request.drawing_effect = drawing_effect;
-
-  TextRequest* textrequest = new TextRequest;
-  textrequest->font = font;
-  textrequest->text = text;
-  request.request_data = textrequest;
-
-  drawingrequests.push_back(request);
+draw_text(font, text, Vector(position.x + screen->w/2, position.y),
+          CENTER_ALLIGN, layer, drawing_effect);
 }
 
 void
@@ -159,7 +155,7 @@ DrawingContext::draw_surface_part(DrawingRequest& request)
   surfacepartrequest->surface->impl->draw_part(
       surfacepartrequest->source.x, surfacepartrequest->source.y,
       request.pos.x, request.pos.y,
-      surfacepartrequest->size.x, surfacepartrequest->size.y, 255,
+      surfacepartrequest->size.x, surfacepartrequest->size.y, request.alpha,
       request.drawing_effect);
 
   delete surfacepartrequest;
@@ -215,8 +211,8 @@ void
 DrawingContext::draw_text(DrawingRequest& request)
 {
   TextRequest* textrequest = (TextRequest*) request.request_data;
-  
-  textrequest->font->draw(textrequest->text, request.pos, request.drawing_effect);
+
+  textrequest->font->draw(textrequest->text, request.pos, textrequest->allignment, request.drawing_effect, request.alpha);
 
   delete textrequest;
 }
@@ -306,7 +302,13 @@ DrawingContext::do_drawing()
       case SURFACE:
       {
         const Surface* surface = (const Surface*) i->request_data;
-        surface->impl->draw(i->pos.x, i->pos.y, 255, i->drawing_effect);
+
+        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:
@@ -353,3 +355,15 @@ DrawingContext::set_drawing_effect(int effect)
 {
   transform.draw_effect = effect;
 }
+
+void
+DrawingContext::set_zooming(float zoom)
+{
+  transform.zoom = zoom;
+}
+
+void
+DrawingContext::set_alpha(int alpha)
+{
+  transform.alpha = alpha;
+}