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, FontType ntype, int nw, int nh,
31 : chars(0), shadow_chars(0), type(ntype), w(nw), h(nh),
32 shadowsize(nshadowsize)
34 chars = new Surface(file, USE_ALPHA);
44 last_char = first_char + (chars->h / h) * 16;
45 if(last_char > 127) // we have left out some control chars at 128-159
50 SDL_Surface* conv = SDL_DisplayFormatAlpha(chars->impl->get_sdl_surface());
51 int pixels = conv->w * conv->h;
52 SDL_LockSurface(conv);
53 for(int i = 0; i < pixels; ++i) {
54 Uint32 *p = (Uint32 *)conv->pixels + i;
55 *p = *p & conv->format->Amask;
57 SDL_UnlockSurface(conv);
58 SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
59 shadow_chars = new Surface(conv, USE_ALPHA);
60 SDL_FreeSurface(conv);
71 Font::get_height() const
77 Font::get_text_width(const std::string& text) const
79 return text.size() * w;
83 Font::draw(const std::string& text, const Vector& pos)
86 draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize));
88 draw_chars(chars, text, pos);
92 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos)
94 SurfaceImpl* impl = pchars->impl;
97 for(size_t i = 0; i < text.size(); ++i)
99 int c = (unsigned char) text[i];
100 if(c > 127) // correct for the 32 controlchars at 128-159
102 // a non-printable character?
108 if(c == ' ' || c < first_char || c > last_char) {
113 int index = c - first_char;
114 int source_x = (index % 16) * w;
115 int source_y = (index / 16) * h;
117 impl->draw_part(source_x, source_y, p.x, p.y, w, h, 255);
122 /* --- SCROLL TEXT FUNCTION --- */
125 #define SPEED_INC 0.01
127 #define ITEMS_SPACE 4
129 void display_text_file(const std::string& file, const std::string& surface, float scroll_speed)
131 Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
132 display_text_file(file, sur, scroll_speed);
136 void display_text_file(const std::string& file, Surface* surface, float scroll_speed)
143 std::vector<std::string> names;
146 sprintf(filename,"%s/%s", datadir.c_str(), file.c_str());
147 if((fi = fopen(filename,"r")) != NULL)
149 while(fgets(temp, sizeof(temp), fi) != NULL)
151 temp[strlen(temp)-1]='\0';
152 names.push_back(temp);
158 names.push_back("File was not found!");
159 names.push_back(filename);
160 names.push_back("Shame on the guy, who");
161 names.push_back("forgot to include it");
162 names.push_back("in your SuperTux distribution.");
166 speed = scroll_speed / 50;
169 DrawingContext context;
170 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
172 Uint32 lastticks = SDL_GetTicks();
175 /* in case of input, exit */
177 while(SDL_PollEvent(&event))
181 switch(event.key.keysym.sym)
210 else if(speed < -MAX_VEL)
213 /* draw the credits */
214 context.draw_surface(surface, Vector(0,0), 0);
217 for(size_t i = 0; i < names.size(); i++) {
218 if(names[i].size() == 0) {
219 y += white_text->get_height() + ITEMS_SPACE;
226 case ' ': font = white_small_text; break;
227 case '\t': font = white_text; break;
228 case '-': font = white_big_text; break;
229 case '*': font = blue_text; break;
230 default: font = blue_text; break;
233 context.draw_text_center(font,
234 names[i].substr(1, names[i].size()-1),
235 Vector(0, screen->h + y - scroll), LAYER_FOREGROUND1);
236 y += font->get_height() + ITEMS_SPACE;
239 context.do_drawing();
241 if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
244 Uint32 ticks = SDL_GetTicks();
245 scroll += speed * (ticks - lastticks);
253 SDL_EnableKeyRepeat(0, 0); // disables key repeating
254 Menu::set_current(main_menu);