4ea0469f55498f8f3c97df3a3122ced3f961f0da
[supertux.git] / src / screen / font.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #include <cstdlib>
22 #include <cstring>
23
24 #include "globals.h"
25 #include "defines.h"
26 #include "screen.h"
27 #include "font.h"
28 #include "drawing_context.h"
29
30 Font::Font(const std::string& file, FontType ntype, int nw, int nh,
31         int nshadowsize)
32     : chars(0), shadow_chars(0), type(ntype), w(nw), h(nh),
33       shadowsize(nshadowsize)
34 {
35   chars = new Surface(file, USE_ALPHA);
36  
37   switch(type) {
38     case TEXT:
39       first_char = 32;
40       break;
41     case NUM:
42       first_char = 48;
43       break;
44   }
45   last_char = first_char + (chars->h / h) * 16;
46   if(last_char > 127) // we have left out some control chars at 128-159
47     last_char += 32;
48    
49   // Load shadow font.
50   if(shadowsize > 0) {
51     SDL_Surface* conv = SDL_DisplayFormatAlpha(chars->impl->get_sdl_surface());
52     int pixels = conv->w * conv->h;
53     SDL_LockSurface(conv);
54     for(int i = 0; i < pixels; ++i) {
55       Uint32 *p = (Uint32 *)conv->pixels + i;
56       *p = *p & conv->format->Amask;
57     }
58     SDL_UnlockSurface(conv);
59     SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
60     shadow_chars = new Surface(conv, USE_ALPHA);
61     SDL_FreeSurface(conv);
62   }
63 }
64
65 Font::~Font()
66 {
67   delete chars;
68   delete shadow_chars;
69 }
70
71 float
72 Font::get_height() const
73 {
74   return h;
75 }
76
77 float
78 Font::get_text_width(const std::string& text) const
79 {
80   return text.size() * w;
81 }
82
83 void
84 Font::draw(const std::string& text, const Vector& pos, Uint32 drawing_effect)
85 {
86   if(shadowsize > 0)
87     draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize),
88                drawing_effect);
89
90   draw_chars(chars, text, pos, drawing_effect);
91 }
92
93 void
94 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
95                  Uint32 drawing_effect)
96 {
97   SurfaceImpl* impl = pchars->impl;
98
99   Vector p = pos;
100   for(size_t i = 0; i < text.size(); ++i)
101   {
102     int c = (unsigned char) text[i];
103     if(c > 127) // correct for the 32 controlchars at 128-159
104       c -= 32;
105     // a non-printable character?
106     if(c == '\n') {
107       p.x = pos.x;
108       p.y += h + 2;
109       continue;
110     }
111     if(c == ' ' || c < first_char || c > last_char) {
112       p.x += w;
113       continue;
114     }
115     
116     int index = c - first_char;
117     int source_x = (index % 16) * w;
118     int source_y = (index / 16) * h;
119
120     impl->draw_part(source_x, source_y, p.x, p.y, w, h, 255, drawing_effect);
121     p.x += w;
122   }
123 }
124
125 /* --- SCROLL TEXT FUNCTION --- */
126
127 #define MAX_VEL     10
128 #define SPEED_INC   0.01
129 #define SCROLL      60
130 #define ITEMS_SPACE 4
131
132 void display_text_file(const std::string& file, const std::string& surface, float scroll_speed)
133 {
134   Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
135   display_text_file(file, sur, scroll_speed);
136   delete sur;
137 }
138
139 void display_text_file(const std::string& file, Surface* surface, float scroll_speed)
140 {
141   int done;
142   float scroll;
143   float speed;
144   FILE* fi;
145   char temp[1024];
146   std::vector<std::string> names;
147   char filename[1024];
148   
149   sprintf(filename,"%s/%s", datadir.c_str(), file.c_str());
150   if((fi = fopen(filename,"r")) != NULL)
151     {
152       while(fgets(temp, sizeof(temp), fi) != NULL)
153         {
154           temp[strlen(temp)-1]='\0';
155           names.push_back(temp);
156         }
157       fclose(fi);
158     }
159   else
160     {
161       names.push_back("File was not found!");
162       names.push_back(filename);
163       names.push_back("Shame on the guy, who");
164       names.push_back("forgot to include it");
165       names.push_back("in your SuperTux distribution.");
166     }
167
168   scroll = 0;
169   speed = scroll_speed / 50;
170   done = 0;
171
172   DrawingContext context;
173   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
174
175   Uint32 lastticks = SDL_GetTicks();
176   while(!done)
177     {
178       /* in case of input, exit */
179       SDL_Event event;
180       while(SDL_PollEvent(&event))
181         switch(event.type)
182           {
183           case SDL_KEYDOWN:
184             switch(event.key.keysym.sym)
185               {
186               case SDLK_UP:
187                 speed -= SPEED_INC;
188                 break;
189               case SDLK_DOWN:
190                 speed += SPEED_INC;
191                 break;
192               case SDLK_SPACE:
193               case SDLK_RETURN:
194                 if(speed >= 0)
195                   scroll += SCROLL;
196                 break;
197               case SDLK_ESCAPE:
198                 done = 1;
199                 break;
200               default:
201                 break;
202               }
203             break;
204           case SDL_QUIT:
205             done = 1;
206             break;
207           default:
208             break;
209           }
210
211       if(speed > MAX_VEL)
212         speed = MAX_VEL;
213       else if(speed < -MAX_VEL)
214         speed = -MAX_VEL;
215
216       /* draw the credits */
217       context.draw_surface(surface, Vector(0,0), 0);
218
219       float y = 0;
220       for(size_t i = 0; i < names.size(); i++) {
221         if(names[i].size() == 0) {
222           y += white_text->get_height() + ITEMS_SPACE;
223           continue;
224         }
225
226         Font* font = 0;
227         switch(names[i][0])
228         {
229           case ' ': font = white_small_text; break;
230           case '\t': font = white_text; break;
231           case '-': font = white_big_text; break;
232           case '*': font = blue_text; break;
233           default: font = blue_text; break;
234         }
235
236         context.draw_text_center(font,
237             names[i].substr(1, names[i].size()-1),
238             Vector(0, screen->h + y - scroll), LAYER_FOREGROUND1);
239         y += font->get_height() + ITEMS_SPACE;
240       }
241
242       context.do_drawing();
243
244       if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
245         done = 1;
246
247       Uint32 ticks = SDL_GetTicks();
248       scroll += speed * (ticks - lastticks);
249       lastticks = ticks;
250       if(scroll < 0)
251         scroll = 0;
252
253       SDL_Delay(10);
254     }
255
256   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
257   Menu::set_current(main_menu);
258 }
259