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
28 #include "drawing_context.h"
30 Font::Font(const std::string& file, FontType ntype, int nw, int nh,
32 : chars(0), shadow_chars(0), type(ntype), w(nw), h(nh),
33 shadowsize(nshadowsize)
35 chars = new Surface(file, USE_ALPHA);
45 last_char = first_char + (chars->h / h) * 16;
46 if(last_char > 127) // we have left out some control chars at 128-159
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;
58 SDL_UnlockSurface(conv);
59 SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
60 shadow_chars = new Surface(conv, USE_ALPHA);
61 SDL_FreeSurface(conv);
72 Font::get_height() const
78 Font::get_text_width(const std::string& text) const
80 return text.size() * w;
84 Font::draw(const std::string& text, const Vector& pos)
87 draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize));
89 draw_chars(chars, text, pos);
93 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos)
95 SurfaceImpl* impl = pchars->impl;
98 for(size_t i = 0; i < text.size(); ++i)
100 int c = (unsigned char) text[i];
101 if(c > 127) // correct for the 32 controlchars at 128-159
103 // a non-printable character?
109 if(c == ' ' || c < first_char || c > last_char) {
114 int index = c - first_char;
115 int source_x = (index % 16) * w;
116 int source_y = (index / 16) * h;
118 impl->draw_part(source_x, source_y, p.x, p.y, w, h, 255);
123 /* --- SCROLL TEXT FUNCTION --- */
126 #define SPEED_INC 0.01
128 #define ITEMS_SPACE 4
130 void display_text_file(const std::string& file, const std::string& surface, float scroll_speed)
132 Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
133 display_text_file(file, sur, scroll_speed);
137 void display_text_file(const std::string& file, Surface* surface, float scroll_speed)
144 std::vector<std::string> names;
147 sprintf(filename,"%s/%s", datadir.c_str(), file.c_str());
148 if((fi = fopen(filename,"r")) != NULL)
150 while(fgets(temp, sizeof(temp), fi) != NULL)
152 temp[strlen(temp)-1]='\0';
153 names.push_back(temp);
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.");
167 speed = scroll_speed / 50;
170 DrawingContext context;
171 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
173 Uint32 lastticks = SDL_GetTicks();
176 /* in case of input, exit */
178 while(SDL_PollEvent(&event))
182 switch(event.key.keysym.sym)
211 else if(speed < -MAX_VEL)
214 /* draw the credits */
215 context.draw_surface(surface, Vector(0,0), 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;
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;
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;
240 context.do_drawing();
242 if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
245 Uint32 ticks = SDL_GetTicks();
246 scroll += speed * (ticks - lastticks);
254 SDL_EnableKeyRepeat(0, 0); // disables key repeating
255 Menu::set_current(main_menu);