remove outdated comment
[supertux.git] / lib / video / font.cpp
index a0f7d73..e01cedc 100644 (file)
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //  02111-1307, USA.
 
+#include <config.h>
+
 #include <cstdlib>
 #include <cstring>
+#include <stdexcept>
 
-#include "../app/globals.h"
-#include "../video/screen.h"
-#include "../video/font.h"
-#include "../video/drawing_context.h"
-#include "../utils/lispreader.h"
+#include "app/globals.h"
+#include "lisp/parser.h"
+#include "lisp/lisp.h"
+#include "screen.h"
+#include "font.h"
+#include "drawing_context.h"
 
 using namespace SuperTux;
 
@@ -71,30 +75,101 @@ Font::~Font()
 }
 
 float
+Font::get_text_width(const std::string& text) const
+{
+  /** Let's calculate the size of the biggest paragraph */
+  std::string::size_type l, hl, ol;
+  hl = 0; l = 0;
+  while(true)
+    {
+    ol = l;
+    l = text.find("\n", l+1);
+    if(l == std::string::npos)
+      break;
+    if(hl < l-ol)
+      hl = l-ol;
+    }
+  if(hl == 0)
+    hl = text.size();
+
+  return hl * w;
+}
+
+float
+Font::get_text_height(const std::string& text) const
+{
+  /** Let's calculate height of the text */
+  std::string::size_type l, hh;
+  hh = h; l = 0;
+  while(true)
+    {
+    l = text.find("\n", l+1);
+    if(l == std::string::npos)
+      break;
+    hh += h + 2;
+    }
+
+  return hh;
+}
+
+float
 Font::get_height() const
 {
   return h;
 }
 
-float
-Font::get_text_width(const std::string& text) const
+void
+Font::draw(const std::string& text, const Vector& pos_, FontAlignment alignment,
+    uint32_t drawing_effect, uint8_t alpha) const
 {
-  return text.size() * w;
+  /* Cut lines changes into seperate strings, needed to support center/right text
+     alignments with break lines.
+     Feel free to replace this hack with a more elegant solution
+  */
+  char temp[1024];
+  std::string::size_type l, i, y;
+  bool done = false;
+  i = y = 0;
+
+  while(!done)
+    {
+    l = text.find("\n", i);
+    if(l == std::string::npos)
+      {
+      l = text.size();
+      done = true;
+      }
+
+    temp[text.copy(temp, l - i, i)] = '\0';
+
+    // calculate X positions based on the alignment type
+    Vector pos = Vector(pos_);
+    if(alignment == CENTER_ALLIGN)
+      pos.x -= get_text_width(temp) / 2;
+    else if(alignment == RIGHT_ALLIGN)
+      pos.x -= get_text_width(temp);
+
+    draw_text(temp, pos + Vector(0,y), drawing_effect, alpha);
+
+    i = l+1;
+    y += h + 2;
+    }
 }
 
 void
-Font::draw(const std::string& text, const Vector& pos, Uint32 drawing_effect)
+Font::draw_text(const std::string& text, const Vector& pos, 
+    uint32_t drawing_effect, uint8_t alpha) const
 {
   if(shadowsize > 0)
     draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize),
-               drawing_effect);
+               drawing_effect, alpha);
 
-  draw_chars(chars, text, pos, drawing_effect);
+  draw_chars(chars, text, pos, drawing_effect, alpha);
 }
 
 void
 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
-                 Uint32 drawing_effect)
+                 uint32_t drawing_effect, uint8_t alpha) const
 {
   SurfaceImpl* impl = pchars->impl;
 
@@ -119,151 +194,8 @@ Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
     int source_x = (index % 16) * w;
     int source_y = (index / 16) * h;
 
-    impl->draw_part(source_x, source_y, p.x, p.y, w, h, 255, drawing_effect);
+    impl->draw_part(source_x, source_y, p.x, p.y, w, h, alpha, drawing_effect);
     p.x += w;
   }
 }
 
-/* --- SCROLL TEXT FUNCTION --- */
-
-#define MAX_VEL     10
-#define SPEED_INC   0.01
-#define SCROLL      60
-#define ITEMS_SPACE 4
-
-void SuperTux::display_text_file(const std::string& file, float scroll_speed, Font* heading_font, Font* normal_font, Font* small_font, Font* reference_font )
-{
-  std::string text;
-  std::vector<std::string> names;
-
-  LispReader* reader = LispReader::load(datadir + "/" + file, "supertux-text");
-
-  if(!reader)
-    {
-    std::cerr << "Error: Could not open text. Ignoring...\n";
-    return;
-    }
-
-  reader->read_string("text", text, true);
-  std::string background_file;
-  reader->read_string("background", background_file, true);
-  delete reader;
-
-  // Split text string lines into a vector
-  names.clear();
-  unsigned int i, l;
-  i = 0;
-  while(true)
-    {
-    l = text.find("\n", i);
-
-    if(l == std::string::npos)
-      {
-      char temp[1024];
-      temp[text.copy(temp, text.size() - i, i)] = '\0';
-      names.push_back(temp);
-      break;
-      }
-
-    char temp[1024];
-    temp[text.copy(temp, l-i, i)] = '\0';
-    names.push_back(temp);
-
-    i = l+1;
-    }
-
-  // load background image
-  Surface* background = new Surface(datadir + "/images/background/" + background_file, false);
-
-  int done = 0;
-  float scroll = 0;
-  float speed = scroll_speed / 50;
-
-  DrawingContext context;
-  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
-
-  Uint32 lastticks = SDL_GetTicks();
-  while(!done)
-    {
-      /* in case of input, exit */
-      SDL_Event event;
-      while(SDL_PollEvent(&event))
-        switch(event.type)
-          {
-          case SDL_KEYDOWN:
-            switch(event.key.keysym.sym)
-              {
-              case SDLK_UP:
-                speed -= SPEED_INC;
-                break;
-              case SDLK_DOWN:
-                speed += SPEED_INC;
-                break;
-              case SDLK_SPACE:
-              case SDLK_RETURN:
-                if(speed >= 0)
-                  scroll += SCROLL;
-                break;
-              case SDLK_ESCAPE:
-                done = 1;
-                break;
-              default:
-                break;
-              }
-            break;
-          case SDL_QUIT:
-            done = 1;
-            break;
-          default:
-            break;
-          }
-
-      if(speed > MAX_VEL)
-        speed = MAX_VEL;
-      else if(speed < -MAX_VEL)
-        speed = -MAX_VEL;
-
-      /* draw the credits */
-      context.draw_surface(background, Vector(0,0), 0);
-
-      float y = 0;
-      for(size_t i = 0; i < names.size(); i++) {
-        if(names[i].size() == 0) {
-          y += normal_font->get_height() + ITEMS_SPACE;
-          continue;
-        }
-
-        Font* font = 0;
-        switch(names[i][0])
-        {
-          case ' ': font = small_font; break;
-          case '\t': font = normal_font; break;
-          case '-': font = heading_font; break;
-          case '*': font = reference_font; break;
-          default: font = reference_font; break;
-        }
-
-        context.draw_text_center(font,
-            names[i].substr(1, names[i].size()-1),
-            Vector(0, screen->h + y - scroll), LAYER_FOREGROUND1);
-        y += font->get_height() + ITEMS_SPACE;
-      }
-
-      context.do_drawing();
-
-      if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
-        done = 1;
-
-      Uint32 ticks = SDL_GetTicks();
-      scroll += speed * (ticks - lastticks);
-      lastticks = ticks;
-      if(scroll < 0)
-        scroll = 0;
-
-      SDL_Delay(10);
-    }
-
-  SDL_EnableKeyRepeat(0, 0);    // disables key repeating
-  delete background;
-}
-