Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[supertux.git] / src / video / font.cpp
index 72c2ff4..5fe050a 100644 (file)
@@ -1,7 +1,7 @@
-//  $Id: font.cpp 2298 2005-03-30 12:01:02Z matzebraun $
-// 
+//  $Id$
+//
 //  SuperTux
-//  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
 //  This program is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU General Public License
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 //  GNU General Public License for more details.
-// 
+//
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-//  02111-1307, USA.
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
 #include <config.h>
 
 #include <cstdlib>
 #include "screen.hpp"
 #include "font.hpp"
 #include "drawing_context.hpp"
+#include "log.hpp"
 
-Font::Font(const std::string& file, FontType ntype, int nw, int nh,
-        int nshadowsize)
-    : chars(0), shadow_chars(0), type(ntype), w(nw), h(nh),
-      shadowsize(nshadowsize)
+Font::Font(const std::string& file, const std::string& shadowfile,
+           int w, int h, int shadowsize)
+    : chars(0), shadow_chars(0), w(w), h(h), shadowsize(shadowsize)
 {
-  chars = new Surface(file, true);
+  chars = new Surface(file);
+  shadow_chars = new Surface(shadowfile);
  
-  switch(type) {
-    case TEXT:
-      first_char = 32;
-      break;
-    case NUM:
-      first_char = 48;
-      break;
-  }
-  char_count = (chars->h / h) * 16;
-   
-  // Load shadow font.
-  if(shadowsize > 0) {
-    SDL_Surface* conv = SDL_DisplayFormatAlpha(chars->impl->get_sdl_surface());
-    int pixels = conv->w * conv->h;
-    SDL_LockSurface(conv);
-    for(int i = 0; i < pixels; ++i) {
-      Uint32 *p = (Uint32 *)conv->pixels + i;
-      *p = *p & conv->format->Amask;
-    }
-    SDL_UnlockSurface(conv);
-    SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
-    shadow_chars = new Surface(conv, true);
-    SDL_FreeSurface(conv);
-  }
+  first_char = 32;
+  char_count = ((int) chars->get_height() / h) * 16;
 }
 
 Font::~Font()
@@ -86,7 +65,7 @@ Font::get_text_width(const std::string& text) const
   if(hl == 0)
     hl = text.size();
 
-  for (uint i = 0; i < text.size(); i++)
+  for (unsigned int i = 0; i < text.size(); i++)
     if ((unsigned char) text[i] > 0xC2 && (unsigned char) text[i] < 0xC6)
       hl--;  // control characters are a WASTE.
 
@@ -118,7 +97,7 @@ Font::get_height() const
 
 void
 Font::draw(const std::string& text, const Vector& pos_, FontAlignment alignment,
-    uint32_t drawing_effect, uint8_t alpha) const
+           DrawingEffect drawing_effect, float alpha) const
 {
   /* Cut lines changes into seperate strings, needed to support center/right text
      alignments with break lines.
@@ -135,6 +114,9 @@ Font::draw(const std::string& text, const Vector& pos_, FontAlignment alignment,
       l = text.size();
       done = true;
     }
+
+    if(l > sizeof(temp)-1)
+      l = sizeof(temp)-1;
     
     temp[text.copy(temp, l - i, i)] = '\0';
     
@@ -154,7 +136,7 @@ Font::draw(const std::string& text, const Vector& pos_, FontAlignment alignment,
 
 void
 Font::draw_text(const std::string& text, const Vector& pos, 
-    uint32_t drawing_effect, uint8_t alpha) const
+                DrawingEffect drawing_effect, float alpha) const
 {
   if(shadowsize > 0)
     draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize),
@@ -208,10 +190,8 @@ uint32_t decode_utf8(const std::string& text, size_t& p)
 
 void
 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
-                 uint32_t drawing_effect, uint8_t alpha) const
+                 DrawingEffect drawing_effect, float alpha) const
 {
-  SurfaceImpl* impl = pchars->impl;
-
   Vector p = pos;
   size_t i = 0;
   while(i < text.size()) {
@@ -234,23 +214,20 @@ Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
     if(c >= 0x80) {
       font_index -= 32;
       if(c <= 0xa0) {
-#ifdef DEBUG
-        std::cout << "Unsupported utf-8 character '" << c << "' found\n";
-#endif
+        log_debug << "Unsupported utf-8 character '" << c << "' found" << std::endl;
         font_index = 0;
       }
     }
         
     if(font_index < 0 || font_index >= (ssize_t) char_count) {
-#ifdef DEBUG
-      std::cout << "Unsupported utf-8 character found\n";
-#endif
+      log_debug << "Unsupported utf-8 character found" << std::endl;
       font_index = 0;
     }                   
 
     int source_x = (font_index % 16) * w;
     int source_y = (font_index / 16) * h;
-    impl->draw_part(source_x, source_y, p.x, p.y, w, h, alpha, drawing_effect);
+    pchars->draw_part(source_x, source_y, p.x, p.y, w, h, alpha,
+                      drawing_effect);
     p.x += w;
   }
 }