X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=lib%2Fvideo%2Ffont.cpp;h=12d96c741889941adee471478debbe3fcf0b1b22;hb=e3bb6e46812f108f093e9ad0751a945c34b18cd3;hp=78cfd39abacbed9c2f702ca95b3e25e20da3d2b2;hpb=e4d4375bf4b6802321d956f5f3886320b7275cf0;p=supertux.git diff --git a/lib/video/font.cpp b/lib/video/font.cpp index 78cfd39ab..12d96c741 100644 --- a/lib/video/font.cpp +++ b/lib/video/font.cpp @@ -22,12 +22,14 @@ #include #include +#include #include "app/globals.h" +#include "lisp/parser.h" +#include "lisp/lisp.h" #include "screen.h" #include "font.h" #include "drawing_context.h" -#include "utils/lispreader.h" using namespace SuperTux; @@ -202,23 +204,32 @@ Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos, #define SCROLL 60 #define ITEMS_SPACE 4 -void SuperTux::display_text_file(const std::string& file, float scroll_speed, Font* heading_font, Font* normal_font, Font* small_font, Font* reference_font ) +void SuperTux::display_text_file(const std::string& file, float scroll_speed, + Font* heading_font, Font* normal_font, Font* small_font, + Font* reference_font) { std::string text; + std::string background_file; std::vector names; - LispReader* reader = LispReader::load(datadir + "/" + file, "supertux-text"); + std::string filename = datadir + "/" + file; + lisp::Parser parser; + try { + std::auto_ptr root (parser.parse(filename)); - if(!reader) - { - std::cerr << "Error: Could not open text. Ignoring...\n"; + const lisp::Lisp* text_lisp = root->get_lisp("supertux-text"); + if(!text_lisp) + throw std::runtime_error("File isn't a supertux-text file"); + + if(!text_lisp->get("text", text)) + throw std::runtime_error("file doesn't contain a text field"); + if(!text_lisp->get("background", background_file)) + throw std::runtime_error("file doesn't contain a background file"); + } catch(std::exception& e) { + std::cerr << "Couldn't load file '" << filename << "': " << e.what() << + "\n"; return; - } - - reader->read_string("text", text, true); - std::string background_file; - reader->read_string("background", background_file, true); - delete reader; + } // Split text string lines into a vector names.clear(); @@ -249,6 +260,7 @@ void SuperTux::display_text_file(const std::string& file, float scroll_speed, Fo int done = 0; float scroll = 0; float speed = scroll_speed / 50; + float left_border = 50; DrawingContext context; SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); @@ -305,18 +317,32 @@ void SuperTux::display_text_file(const std::string& file, float scroll_speed, Fo } Font* font = 0; + bool center = true; switch(names[i][0]) { case ' ': font = small_font; break; case '\t': font = normal_font; break; case '-': font = heading_font; break; case '*': font = reference_font; break; - default: font = reference_font; break; + case '#': font = normal_font; center = false; break; + default: + break; } - - context.draw_text(font, - names[i].substr(1, names[i].size()-1), - Vector(screen->w/2, screen->h + y - scroll), CENTER_ALLIGN, LAYER_FOREGROUND1); + + if(font) { + if(center) { + context.draw_text(font, + names[i].substr(1, names[i].size()-1), + Vector(screen->w/2, screen->h + y - scroll), + CENTER_ALLIGN, LAYER_FOREGROUND1); + } else { + context.draw_text(font, + names[i].substr(1, names[i].size()-1), + Vector(left_border, screen->h + y - scroll), + LEFT_ALLIGN, LAYER_FOREGROUND1); + } + } + y += font->get_height() + ITEMS_SPACE; }