4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
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.
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.
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
27 #include "drawing_context.h"
29 Font::Font(const std::string& file, int kind_, int w_, int h_, int shadowsize_)
34 shadowsize = shadowsize_;
46 else if(kind == TEXT_NUM)
57 chars = new Surface(file, USE_ALPHA);
60 conv = SDL_DisplayFormatAlpha(chars->impl->get_sdl_surface());
61 pixels = conv->w * conv->h;
62 SDL_LockSurface(conv);
63 for(i = 0; i < pixels; ++i)
65 Uint32 *p = (Uint32 *)conv->pixels + i;
66 *p = *p & conv->format->Amask;
68 SDL_UnlockSurface(conv);
69 SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
70 shadow_chars = new Surface(conv, USE_ALPHA);
72 SDL_FreeSurface(conv);
82 Font::get_height() const
88 Font::get_text_width(const std::string& text) const
90 return text.size() * w;
94 Font::draw(const std::string& text, const Vector& pos)
97 draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize));
99 draw_chars(chars, text, pos);
103 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos)
107 SurfaceImpl* impl = pchars->impl;
111 if(kind == TEXT_TEXT)
113 for( i = 0, j = 0; i < text.size(); ++i,++j)
115 if( text[i] >= ' ' && text[i] <= '/')
116 impl->draw_part((int)(text[i] - ' ')*w, 0 , x+(j*w), y, w, h, 255);
117 else if( text[i] >= '0' && text[i] <= '?')
118 impl->draw_part((int)(text[i] - '0')*w, h*1, x+(j*w), y, w, h, 255);
119 else if ( text[i] >= '@' && text[i] <= 'O')
120 impl->draw_part((int)(text[i] - '@')*w, h*2, x+(j*w), y, w, h, 255);
121 else if ( text[i] >= 'P' && text[i] <= '_')
122 impl->draw_part((int)(text[i] - 'P')*w, h*3, x+(j*w), y, w, h, 255);
123 else if ( text[i] >= '`' && text[i] <= 'o')
124 impl->draw_part((int)(text[i] - '`')*w, h*4, x+(j*w), y, w, h, 255);
125 else if ( text[i] >= 'p' && text[i] <= '~')
126 impl->draw_part((int)(text[i] - 'p')*w, h*5, x+(j*w), y, w, h, 255);
127 else if ( text[i] == '\n')
134 else if(kind == TEXT_NUM)
136 for( i = 0, j = 0; i < text.size(); ++i, ++j)
138 if ( text[i] >= '0' && text[i] <= '9')
139 impl->draw_part((int)(text[i] - '0')*w, 0, x+(j*w), y, w, h, 255);
140 else if ( text[i] == '\n')
149 /* --- SCROLL TEXT FUNCTION --- */
152 #define SPEED_INC 0.01
154 #define ITEMS_SPACE 4
156 void display_text_file(const std::string& file, const std::string& surface, float scroll_speed)
158 Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
159 display_text_file(file, sur, scroll_speed);
163 void display_text_file(const std::string& file, Surface* surface, float scroll_speed)
170 std::vector<std::string> names;
173 sprintf(filename,"%s/%s", datadir.c_str(), file.c_str());
174 if((fi = fopen(filename,"r")) != NULL)
176 while(fgets(temp, sizeof(temp), fi) != NULL)
178 temp[strlen(temp)-1]='\0';
179 names.push_back(temp);
185 names.push_back("File was not found!");
186 names.push_back(filename);
187 names.push_back("Shame on the guy, who");
188 names.push_back("forgot to include it");
189 names.push_back("in your SuperTux distribution.");
193 speed = scroll_speed / 50;
196 DrawingContext context;
197 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
199 Uint32 lastticks = SDL_GetTicks();
202 /* in case of input, exit */
204 while(SDL_PollEvent(&event))
208 switch(event.key.keysym.sym)
237 else if(speed < -MAX_VEL)
240 /* draw the credits */
241 context.draw_surface(surface, Vector(0,0), 0);
244 for(size_t i = 0; i < names.size(); i++) {
245 if(names[i].size() == 0) {
246 y += white_text->get_height() + ITEMS_SPACE;
253 case ' ': font = white_small_text; break;
254 case '\t': font = white_text; break;
255 case '-': font = white_big_text; break;
256 case '*': font = blue_text; break;
257 default: font = blue_text; break;
260 context.draw_text_center(font,
261 names[i].substr(1, names[i].size()-1),
262 Vector(0, screen->h + y - scroll), LAYER_FOREGROUND1);
263 y += font->get_height() + ITEMS_SPACE;
266 context.do_drawing();
268 if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
271 Uint32 ticks = SDL_GetTicks();
272 scroll += speed * (ticks - lastticks);
280 SDL_EnableKeyRepeat(0, 0); // disables key repeating
281 Menu::set_current(main_menu);