Added support for break lines in centered text.
authorRicardo Cruz <rick2@aeiou.pt>
Wed, 28 Jul 2004 14:57:24 +0000 (14:57 +0000)
committerRicardo Cruz <rick2@aeiou.pt>
Wed, 28 Jul 2004 14:57:24 +0000 (14:57 +0000)
The code is pretty hacky, but seems to work fine, and such a feature was really needed to avoid hacks (for instance, the one found in display_text_file()).

In the future, it would be a good idea to add break lines support for get_text_width() and get_text_height().

SVN-Revision: 1652

lib/video/drawing_context.cpp
lib/video/drawing_context.h
lib/video/font.cpp
lib/video/font.h

index f6e9af1..ea1020f 100644 (file)
@@ -90,6 +90,7 @@ DrawingContext::draw_text(Font* font, const std::string& text,
   TextRequest* textrequest = new TextRequest;
   textrequest->font = font;
   textrequest->text = text;
+  textrequest->center = false;
   request.request_data = textrequest;
 
   drawingrequests.push_back(request);
@@ -103,13 +104,13 @@ DrawingContext::draw_text_center(Font* font, const std::string& text,
 
   request.type = TEXT;
   request.layer = layer;
-  request.pos = transform.apply(position) + Vector(screen->w/2 - 
-      font->get_text_width(text)/2, 0);
+  request.pos = transform.apply(position);
   request.drawing_effect = drawing_effect;
 
   TextRequest* textrequest = new TextRequest;
   textrequest->font = font;
   textrequest->text = text;
+  textrequest->center = true;
   request.request_data = textrequest;
 
   drawingrequests.push_back(request);
@@ -215,8 +216,11 @@ void
 DrawingContext::draw_text(DrawingRequest& request)
 {
   TextRequest* textrequest = (TextRequest*) request.request_data;
-  
-  textrequest->font->draw(textrequest->text, request.pos, request.drawing_effect);
+
+  if(textrequest->center)
+    textrequest->font->draw_center(textrequest->text, request.pos, request.drawing_effect);
+  else
+    textrequest->font->draw(textrequest->text, request.pos, request.drawing_effect);
 
   delete textrequest;
 }
index 4674234..3c2b6ea 100644 (file)
@@ -129,6 +129,7 @@ namespace SuperTux
         {
           Font* font;
           std::string text;
+          bool center;
         };
 
       struct GradientRequest
@@ -161,6 +162,7 @@ namespace SuperTux
 
       void draw_surface_part(DrawingRequest& request);
       void draw_text(DrawingRequest& request);
+      void draw_text_center(DrawingRequest& request);
       void draw_gradient(DrawingRequest& request);
       void draw_filled_rect(DrawingRequest& request);
 
index a0f7d73..f0bdaff 100644 (file)
@@ -93,6 +93,35 @@ Font::draw(const std::string& text, const Vector& pos, Uint32 drawing_effect)
 }
 
 void
+Font::draw_center(const std::string& text, const Vector& pos, Uint32 drawing_effect)
+{
+  /* Cut lines changes into seperate strings, needed to support centering text
+     with break lines.
+     Feel free to replace this hack with a more elegant solution
+  */
+  char temp[1024];
+  unsigned int i, l, y;
+  i = y = 0;
+  while(true)
+    {
+    l = text.find("\n", i);
+    if(l == std::string::npos)
+      {
+      temp[text.copy(temp, text.size() - i, i)] = '\0';
+      draw(temp, Vector(screen->w/2 - get_text_width(temp)/2 + pos.x, pos.y + y),
+           drawing_effect);
+      break;
+      }
+    temp[text.copy(temp, l - i, i)] = '\0';
+    draw(temp, Vector(screen->w/2 - get_text_width(temp)/2 + pos.x, pos.y + y),
+         drawing_effect);
+
+    i = l+1;
+    y += h + 2;
+    }
+}
+
+void
 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
                  Uint32 drawing_effect)
 {
index 9304cab..dd6de77 100644 (file)
@@ -55,6 +55,8 @@ namespace SuperTux
 
       void draw(const std::string& text, const Vector& pos,
                 Uint32 drawing_effect = NONE_EFFECT);
+      void draw_center(const std::string& text, const Vector& pos,
+                Uint32 drawing_effect = NONE_EFFECT);
       void draw_chars(Surface* pchars, const std::string& text,
                       const Vector& position, Uint32 drawing_effect);