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"
29 #include "../lispreader.h"
31 Font::Font(const std::string& file, FontType ntype, int nw, int nh,
33 : chars(0), shadow_chars(0), type(ntype), w(nw), h(nh),
34 shadowsize(nshadowsize)
36 chars = new Surface(file, USE_ALPHA);
46 last_char = first_char + (chars->h / h) * 16;
47 if(last_char > 127) // we have left out some control chars at 128-159
52 SDL_Surface* conv = SDL_DisplayFormatAlpha(chars->impl->get_sdl_surface());
53 int pixels = conv->w * conv->h;
54 SDL_LockSurface(conv);
55 for(int i = 0; i < pixels; ++i) {
56 Uint32 *p = (Uint32 *)conv->pixels + i;
57 *p = *p & conv->format->Amask;
59 SDL_UnlockSurface(conv);
60 SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
61 shadow_chars = new Surface(conv, USE_ALPHA);
62 SDL_FreeSurface(conv);
73 Font::get_height() const
79 Font::get_text_width(const std::string& text) const
81 return text.size() * w;
85 Font::draw(const std::string& text, const Vector& pos, Uint32 drawing_effect)
88 draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize),
91 draw_chars(chars, text, pos, drawing_effect);
95 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
96 Uint32 drawing_effect)
98 SurfaceImpl* impl = pchars->impl;
101 for(size_t i = 0; i < text.size(); ++i)
103 int c = (unsigned char) text[i];
104 if(c > 127) // correct for the 32 controlchars at 128-159
106 // a non-printable character?
112 if(c == ' ' || c < first_char || c > last_char) {
117 int index = c - first_char;
118 int source_x = (index % 16) * w;
119 int source_y = (index / 16) * h;
121 impl->draw_part(source_x, source_y, p.x, p.y, w, h, 255, drawing_effect);
126 /* --- SCROLL TEXT FUNCTION --- */
129 #define SPEED_INC 0.01
131 #define ITEMS_SPACE 4
133 void display_text_file(const std::string& file, const std::string& surface, float scroll_speed)
135 Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
136 display_text_file(file, sur, scroll_speed);
140 void display_text_file(const std::string& file, Surface* surface, float scroll_speed)
143 std::vector<std::string> names;
145 lisp_object_t* root_obj = lisp_read_from_file(datadir + file);
146 lisp_object_t* cur = lisp_car(root_obj);
148 if (lisp_symbol_p(cur) && strcmp(lisp_symbol(cur), "text") == 0)
150 LispReader reader(lisp_cdr(root_obj));
151 reader.read_string("text", text);
155 std::cerr << "Error: Could not open text. Ignoring...\n";
164 l = text.find("\n", i);
165 if(l != std::string::npos)
169 text.copy(temp, l, i);
170 names.push_back(temp);
175 float speed = scroll_speed / 50;
177 DrawingContext context;
178 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
180 Uint32 lastticks = SDL_GetTicks();
183 /* in case of input, exit */
185 while(SDL_PollEvent(&event))
189 switch(event.key.keysym.sym)
218 else if(speed < -MAX_VEL)
221 /* draw the credits */
222 context.draw_surface(surface, Vector(0,0), 0);
225 for(size_t i = 0; i < names.size(); i++) {
226 if(names[i].size() == 0) {
227 y += white_text->get_height() + ITEMS_SPACE;
234 case ' ': font = white_small_text; break;
235 case '\t': font = white_text; break;
236 case '-': font = white_big_text; break;
237 case '*': font = blue_text; break;
238 default: font = blue_text; break;
241 context.draw_text_center(font,
242 names[i].substr(1, names[i].size()-1),
243 Vector(0, screen->h + y - scroll), LAYER_FOREGROUND1);
244 y += font->get_height() + ITEMS_SPACE;
247 context.do_drawing();
249 if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
252 Uint32 ticks = SDL_GetTicks();
253 scroll += speed * (ticks - lastticks);
261 SDL_EnableKeyRepeat(0, 0); // disables key repeating
262 Menu::set_current(main_menu);