Changed parameter from set_action().
[supertux.git] / lib / video / font.cpp
index a0f7d73..f6a3254 100644 (file)
@@ -71,15 +71,46 @@ Font::~Font()
 }
 
 float
-Font::get_height() const
+Font::get_text_width(const std::string& text) const
 {
-  return h;
+  /** Let's calculate the size of the biggest paragraph */
+  int l, hl;
+  hl = 0; l = -1;
+  while(true)
+    {
+    l = text.find("\n", l+1);
+    if(l == (int)std::string::npos)
+      break;
+    if(hl < l)
+      hl = l;
+    }
+  if(hl == 0)
+    hl = text.size();
+
+  return hl * w;
 }
 
 float
-Font::get_text_width(const std::string& text) const
+Font::get_text_height(const std::string& text) const
 {
-  return text.size() * w;
+  /** Let's calculate height of the text */
+  int l, hh;
+  hh = h; l = -1;
+  while(true)
+    {
+    l = text.find("\n", l+1);
+    if(l == (int)std::string::npos)
+      break;
+    hh += h + 2;
+    }
+
+  return hh;
+}
+
+float
+Font::get_height() const
+{
+  return h;
 }
 
 void
@@ -93,6 +124,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)
 {