- just doing some C++ifying
[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)
85 {
86   if(shadowsize > 0)
87     draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize));
88
89   draw_chars(chars, text, pos);
90 }
91
92 void
93 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos)
94 {
95   SurfaceImpl* impl = pchars->impl;
96
97   Vector p = pos;
98   for(size_t i = 0; i < text.size(); ++i)
99   {
100     int c = (unsigned char) text[i];
101     if(c > 127) // correct for the 32 controlchars at 128-159
102       c -= 32;
103     // a non-printable character?
104     if(c == '\n') {
105       p.x = pos.x;
106       p.y += h + 2;
107       continue;
108     }
109     if(c == ' ' || c < first_char || c > last_char) {
110       p.x += w;
111       continue;
112     }
113     
114     int index = c - first_char;
115     int source_x = (index % 16) * w;
116     int source_y = (index / 16) * h;
117
118     impl->draw_part(source_x, source_y, p.x, p.y, w, h, 255);
119     p.x += w;
120   }
121 }
122
123 /* --- SCROLL TEXT FUNCTION --- */
124
125 #define MAX_VEL     10
126 #define SPEED_INC   0.01
127 #define SCROLL      60
128 #define ITEMS_SPACE 4
129
130 void display_text_file(const std::string& file, const std::string& surface, float scroll_speed)
131 {
132   Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
133   display_text_file(file, sur, scroll_speed);
134   delete sur;
135 }
136
137 void display_text_file(const std::string& file, Surface* surface, float scroll_speed)
138 {
139   int done;
140   float scroll;
141   float speed;
142   FILE* fi;
143   char temp[1024];
144   std::vector<std::string> names;
145   char filename[1024];
146   
147   sprintf(filename,"%s/%s", datadir.c_str(), file.c_str());
148   if((fi = fopen(filename,"r")) != NULL)
149     {
150       while(fgets(temp, sizeof(temp), fi) != NULL)
151         {
152           temp[strlen(temp)-1]='\0';
153           names.push_back(temp);
154         }
155       fclose(fi);
156     }
157   else
158     {
159       names.push_back("File was not found!");
160       names.push_back(filename);
161       names.push_back("Shame on the guy, who");
162       names.push_back("forgot to include it");
163       names.push_back("in your SuperTux distribution.");
164     }
165
166   scroll = 0;
167   speed = scroll_speed / 50;
168   done = 0;
169
170   DrawingContext context;
171   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
172
173   Uint32 lastticks = SDL_GetTicks();
174   while(!done)
175     {
176       /* in case of input, exit */
177       SDL_Event event;
178       while(SDL_PollEvent(&event))
179         switch(event.type)
180           {
181           case SDL_KEYDOWN:
182             switch(event.key.keysym.sym)
183               {
184               case SDLK_UP:
185                 speed -= SPEED_INC;
186                 break;
187               case SDLK_DOWN:
188                 speed += SPEED_INC;
189                 break;
190               case SDLK_SPACE:
191               case SDLK_RETURN:
192                 if(speed >= 0)
193                   scroll += SCROLL;
194                 break;
195               case SDLK_ESCAPE:
196                 done = 1;
197                 break;
198               default:
199                 break;
200               }
201             break;
202           case SDL_QUIT:
203             done = 1;
204             break;
205           default:
206             break;
207           }
208
209       if(speed > MAX_VEL)
210         speed = MAX_VEL;
211       else if(speed < -MAX_VEL)
212         speed = -MAX_VEL;
213
214       /* draw the credits */
215       context.draw_surface(surface, Vector(0,0), 0);
216
217       float y = 0;
218       for(size_t i = 0; i < names.size(); i++) {
219         if(names[i].size() == 0) {
220           y += white_text->get_height() + ITEMS_SPACE;
221           continue;
222         }
223
224         Font* font = 0;
225         switch(names[i][0])
226         {
227           case ' ': font = white_small_text; break;
228           case '\t': font = white_text; break;
229           case '-': font = white_big_text; break;
230           case '*': font = blue_text; break;
231           default: font = blue_text; break;
232         }
233
234         context.draw_text_center(font,
235             names[i].substr(1, names[i].size()-1),
236             Vector(0, screen->h + y - scroll), LAYER_FOREGROUND1);
237         y += font->get_height() + ITEMS_SPACE;
238       }
239
240       context.do_drawing();
241
242       if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
243         done = 1;
244
245       Uint32 ticks = SDL_GetTicks();
246       scroll += speed * (ticks - lastticks);
247       lastticks = ticks;
248       if(scroll < 0)
249         scroll = 0;
250
251       SDL_Delay(10);
252     }
253
254   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
255   Menu::set_current(main_menu);
256 }
257