Messaging subsystem rewrite, step I
[supertux.git] / src / video / font.cpp
index 72c2ff4..7394441 100644 (file)
 #include "screen.hpp"
 #include "font.hpp"
 #include "drawing_context.hpp"
+#include "msg.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.
@@ -154,7 +133,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 +187,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 +211,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
+        msg_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
+      msg_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;
   }
 }