- increased Tux's safe time to 1250ms
[supertux.git] / src / text.cpp
index 6560327..6e66f3d 100644 (file)
@@ -70,6 +70,12 @@ Text::Text(const std::string& file, int kind_, int w_, int h_)
   SDL_FreeSurface(conv);
 }
 
+Text::~Text()
+{
+  delete chars;
+  delete shadow_chars;
+}
+
 void
 Text::draw(const  char* text, int x, int y, int shadowsize, int update)
 {
@@ -95,14 +101,18 @@ Text::draw_chars(Surface* pchars,const  char* text, int x, int y, int update)
     {
       for( i = 0, j = 0; i < len; ++i,++j)
         {
-          if( text[i] >= 'A' && text[i] <= 'Z')
-            pchars->draw_part((int)(text[i] - 'A')*w, 0, x+(j*w), y, w, h, 255,  update);
-          else if( text[i] >= 'a' && text[i] <= 'z')
-            pchars->draw_part((int)(text[i] - 'a')*w, h, x+(j*w), y, w, h, 255,  update);
-          else if ( text[i] >= '!' && text[i] <= '9')
-            pchars->draw_part((int)(text[i] - '!')*w, h*2, x+(j*w), y, w, h, 255,  update);
-          else if ( text[i] == '?')
-            pchars->draw_part(25*w, h*2, x+(j*w), y, w, h, 255,  update);
+          if( text[i] >= ' ' && text[i] <= '/')
+            pchars->draw_part((int)(text[i] - ' ')*w,  0 , x+(j*w), y, w, h, 255,  update);
+          else if( text[i] >= '0' && text[i] <= '?')
+            pchars->draw_part((int)(text[i] - '0')*w, h*1, x+(j*w), y, w, h, 255,  update);
+          else if ( text[i] >= '@' && text[i] <= 'O')
+            pchars->draw_part((int)(text[i] - '@')*w, h*2, x+(j*w), y, w, h, 255,  update);
+          else if ( text[i] >= 'P' && text[i] <= '_')
+            pchars->draw_part((int)(text[i] - 'P')*w, h*3, x+(j*w), y, w, h, 255,  update);
+          else if ( text[i] >= '`' && text[i] <= 'o')
+            pchars->draw_part((int)(text[i] - '`')*w, h*4, x+(j*w), y, w, h, 255,  update);
+          else if ( text[i] >= 'p' && text[i] <= '~')
+            pchars->draw_part((int)(text[i] - 'p')*w, h*5, x+(j*w), y, w, h, 255,  update);
           else if ( text[i] == '\n')
             {
               y += h + 2;
@@ -182,14 +192,6 @@ Text::drawf(const  char* text, int x, int y,
     }
 }
 
-Text::~Text()
-{
-  if(kind == TEXT_TEXT)
-    delete chars;
-  else if(kind == TEXT_NUM)
-    delete chars;
-}
-
 /* --- ERASE TEXT: --- */
 
 void
@@ -223,22 +225,23 @@ Text::erasecenteredtext(const  char * text, int y, Surface * ptexture, int updat
 
 /* --- SCROLL TEXT FUNCTION --- */
 
-#define MAX_VEL 10
-#define SPEED   1
-#define SCROLL  60
+#define MAX_VEL     10
+#define SPEED_INC   0.01
+#define SCROLL      60
 #define ITEMS_SPACE 4
 
-void display_text_file(const std::string& file, const std::string& surface)
+void display_text_file(const std::string& file, const std::string& surface, float scroll_speed)
 {
   Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
-  display_text_file(file, sur);
+  display_text_file(file, sur, scroll_speed);
   delete sur;
 }
 
-void display_text_file(const std::string& file, Surface* surface)
+void display_text_file(const std::string& file, Surface* surface, float scroll_speed)
 {
   int done;
-  int scroll, speed;
+  float scroll;
+  float speed;
   int y;
   int length;
   FILE* fi;
@@ -246,7 +249,7 @@ void display_text_file(const std::string& file, Surface* surface)
   string_list_type names;
   char filename[1024];
   string_list_init(&names);
-  sprintf(filename,"%s/%s", datadir.c_str(), file);
+  sprintf(filename,"%s/%s", datadir.c_str(), file.c_str());
   if((fi = fopen(filename,"r")) != NULL)
     {
       while(fgets(temp, sizeof(temp), fi) != NULL)
@@ -267,13 +270,14 @@ void display_text_file(const std::string& file, Surface* surface)
 
 
   scroll = 0;
-  speed = 2;
+  speed = scroll_speed / 50;
   done = 0;
 
   length = names.num_items;
 
   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
 
+  Uint32 lastticks = SDL_GetTicks();
   while(done == 0)
     {
       /* in case of input, exit */
@@ -285,10 +289,10 @@ void display_text_file(const std::string& file, Surface* surface)
             switch(event.key.keysym.sym)
               {
               case SDLK_UP:
-                speed -= SPEED;
+                speed -= SPEED_INC;
                 break;
               case SDLK_DOWN:
-                speed += SPEED;
+                speed += SPEED_INC;
                 break;
               case SDLK_SPACE:
               case SDLK_RETURN:
@@ -323,19 +327,23 @@ void display_text_file(const std::string& file, Surface* surface)
         switch(names.item[i][0])
           {
           case ' ':
-            white_small_text->drawf(names.item[i]+1, 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
+            white_small_text->drawf(names.item[i]+1, 0, screen->h+y-int(scroll),
+                A_HMIDDLE, A_TOP, 1);
             y += white_small_text->h+ITEMS_SPACE;
             break;
           case '       ':
-            white_text->drawf(names.item[i]+1, 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
+            white_text->drawf(names.item[i]+1, 0, screen->h+y-int(scroll),
+                A_HMIDDLE, A_TOP, 1);
             y += white_text->h+ITEMS_SPACE;
             break;
           case '-':
-            white_big_text->drawf(names.item[i]+1, 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 3);
+            white_big_text->drawf(names.item[i]+1, 0, screen->h+y-int(scroll),
+                A_HMIDDLE, A_TOP, 3);
             y += white_big_text->h+ITEMS_SPACE;
             break;
           default:
-            blue_text->drawf(names.item[i], 0, 60+screen->h+y-scroll, A_HMIDDLE, A_TOP, 1);
+            blue_text->drawf(names.item[i], 0, screen->h+y-int(scroll),
+                A_HMIDDLE, A_TOP, 1);
             y += blue_text->h+ITEMS_SPACE;
             break;
           }
@@ -343,14 +351,16 @@ void display_text_file(const std::string& file, Surface* surface)
 
       flipscreen();
 
-      if(60+screen->h+y-scroll < 0 && 20+60+screen->h+y-scroll < 0)
+      if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
         done = 1;
 
-      scroll += speed;
+      Uint32 ticks = SDL_GetTicks();
+      scroll += speed * (ticks - lastticks);
+      lastticks = ticks;
       if(scroll < 0)
         scroll = 0;
 
-      SDL_Delay(35);
+      SDL_Delay(10);
     }
   string_list_free(&names);