[Super-tux-commit] supertux/src textscroller.cpp,NONE,1.1 textscroller.h,NONE,1.1 gameloop.cpp,1.214
Brought to you by:
wkendrick
From: Matze B. <mat...@us...> - 2005-03-30 12:01:12
|
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1392/src Modified Files: gameloop.cpp gameloop.h title.cpp title.h worldmap.cpp Added Files: textscroller.cpp textscroller.h Log Message: -Some cleanups in text scrolling code -Added the (temporary) bell graphics which I forgot yesterday -Implemented an infoblock object (the textbox isn't finished yet) and it also needs new graphics --- NEW FILE: textscroller.h --- #ifndef __TEXTSCROLLER_H__ #define __TEXTSCROLLER_H__ #include <vector> #include <string> namespace SuperTux { class DrawingContext; } using namespace SuperTux; /** This class is displaying a box with information text inside the game */ class InfoBox { public: InfoBox(const std::string& text); ~InfoBox(); void draw(DrawingContext& context); void scrolldown(); void scrollup(); void pagedown(); void pageup(); private: size_t firstline; std::vector<std::string> lines; }; /** Reads a text file (using LispReader, so it as to be in its formatting) * and scroll it over the screen * (this call blocks until all text scrolled through or the user aborted the * textscrolling) */ void display_text_file(const std::string& file); #endif Index: title.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/title.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- title.h 13 Mar 2005 23:45:02 -0000 1.7 +++ title.h 30 Mar 2005 12:00:57 -0000 1.8 @@ -31,14 +31,7 @@ MNID_QUITMAINMENU }; -/* Scrolling text speed */ - -#define SCROLL_SPEED_CREDITS 1.5 -#define SCROLL_SPEED_MESSAGE 1.0 - -void title(void); +void title(); #endif //SUPERTUX_TITLE_H -// EOF // - Index: gameloop.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/gameloop.h,v retrieving revision 1.68 retrieving revision 1.69 diff -u -d -r1.68 -r1.69 --- gameloop.h 30 Mar 2005 01:52:12 -0000 1.68 +++ gameloop.h 30 Mar 2005 12:00:55 -0000 1.69 @@ -22,6 +22,7 @@ #ifndef SUPERTUX_GAMELOOP_H #define SUPERTUX_GAMELOOP_H +#include <SDL.h> #include "timer.h" #include "statistics.h" @@ -79,6 +80,7 @@ const std::string& spawnpointname); void set_reset_point(const std::string& sectorname, const Vector& pos); + void display_info_box(const std::string& text); Sector* get_current_sector() { return currentsector; } @@ -95,12 +97,12 @@ void levelintro(); void drawstatus(DrawingContext& context); void drawendscreen(); - void drawresultscreen(void); + void drawresultscreen(); + void draw_pause(); void on_escape_press(); void process_menu(); - Uint32 fps_ticks; Timer2 endsequence_timer; Level* level; Index: title.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/title.cpp,v retrieving revision 1.141 retrieving revision 1.142 diff -u -d -r1.141 -r1.142 --- title.cpp 30 Mar 2005 01:52:13 -0000 1.141 +++ title.cpp 30 Mar 2005 12:00:56 -0000 1.142 @@ -60,6 +60,7 @@ #include "resources.h" #include "app/gettext.h" #include "misc.h" +#include "textscroller.h" static Surface* bkg_title; static Surface* logo; @@ -387,7 +388,7 @@ fadeout(500); credits_music = SoundManager::get()->load_music(datadir + "/music/credits.ogg"); SoundManager::get()->play_music(credits_music); - display_text_file("credits.txt", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text ); + display_text_file("credits.txt"); fadeout(500); Menu::set_current(main_menu); break; Index: worldmap.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/worldmap.cpp,v retrieving revision 1.140 retrieving revision 1.141 diff -u -d -r1.140 -r1.141 --- worldmap.cpp 25 Mar 2005 20:39:53 -0000 1.140 +++ worldmap.cpp 30 Mar 2005 12:00:58 -0000 1.141 @@ -44,6 +44,7 @@ #include "resources.h" #include "misc.h" #include "player_status.h" +#include "textscroller.h" #define map_message_TIME 2.8 @@ -835,8 +836,7 @@ if (!level->extro_filename.empty()) { // Display a text file std::string filename = levels_path + level->extro_filename; - display_text_file(filename, SCROLL_SPEED_MESSAGE, - white_big_text , white_text, white_small_text, blue_text ); + display_text_file(filename); } if (!level->next_worldmap.empty()) @@ -1040,8 +1040,7 @@ if(!intro_displayed && intro_filename != "") { std::string filename = levels_path + intro_filename; - display_text_file(filename, SCROLL_SPEED_MESSAGE, - white_big_text, white_text, white_small_text, blue_text); + display_text_file(filename); intro_displayed = true; } Index: gameloop.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/gameloop.cpp,v retrieving revision 1.214 retrieving revision 1.215 diff -u -d -r1.214 -r1.215 --- gameloop.cpp 30 Mar 2005 01:52:12 -0000 1.214 +++ gameloop.cpp 30 Mar 2005 12:00:53 -0000 1.215 @@ -62,6 +62,7 @@ #include "statistics.h" #include "timer.h" #include "object/fireworks.h" +#include "textscroller.h" GameSession* GameSession::current_ = 0; @@ -103,15 +104,6 @@ last_keys.clear(); -#if 0 - Vector tux_pos = Vector(-1,-1); - if (currentsector) - { - // Tux has lost a life, so we try to respawn him at the nearest reset point - tux_pos = currentsector->player->base; - } -#endif - delete level; currentsector = 0; @@ -571,11 +563,10 @@ GameSession::action(float elapsed_time) { // advance timers - if (exit_status == ES_NONE && !currentsector->player->growing_timer.check()) - { - // Update Tux and the World - currentsector->action(elapsed_time); - } + if(!currentsector->player->growing_timer.started()) { + // Update Tux and the World + currentsector->action(elapsed_time); + } // respawning in new sector? if(newsector != "" && newspawnpoint != "") { @@ -594,46 +585,46 @@ drawstatus(*context); if(game_pause) - { - int x = screen->h / 20; - for(int i = 0; i < x; ++i) - { - context->draw_filled_rect( - Vector(i % 2 ? (pause_menu_frame * i)%screen->w : - -((pause_menu_frame * i)%screen->w) - ,(i*20+pause_menu_frame)%screen->h), - Vector(screen->w,10), - Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1); - } - context->draw_filled_rect( - Vector(0,0), Vector(screen->w, screen->h), - Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1); - context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"), - Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2); - - char str1[60]; - char str2[124]; - sprintf(str1, _("Playing: ")); - sprintf(str2, level->name.c_str()); - - context->draw_text(blue_text, str1, - Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340), - LEFT_ALLIGN, LAYER_FOREGROUND1+2); - context->draw_text(white_text, str2, - Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340), - LEFT_ALLIGN, LAYER_FOREGROUND1+2); - } + draw_pause(); - if(Menu::current()) - { - Menu::current()->draw(*context); - mouse_cursor->draw(*context); - } + if(Menu::current()) { + Menu::current()->draw(*context); + mouse_cursor->draw(*context); + } context->do_drawing(); } void +GameSession::draw_pause() +{ + int x = screen->h / 20; + for(int i = 0; i < x; ++i) { + context->draw_filled_rect( + Vector(i % 2 ? (pause_menu_frame * i)%screen->w : + -((pause_menu_frame * i)%screen->w) + ,(i*20+pause_menu_frame)%screen->h), + Vector(screen->w,10), + Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1); + } + context->draw_filled_rect( + Vector(0,0), Vector(screen->w, screen->h), + Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1); + context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"), + Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2); + + const char* str1 = _("Playing: "); + const char* str2 = level->get_name().c_str(); + + context->draw_text(blue_text, str1, + Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340), + LEFT_ALLIGN, LAYER_FOREGROUND1+2); + context->draw_text(white_text, str2, + Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340), + LEFT_ALLIGN, LAYER_FOREGROUND1+2); +} + +void GameSession::process_menu() { Menu* menu = Menu::current(); @@ -781,6 +772,29 @@ } void +GameSession::display_info_box(const std::string& text) +{ + InfoBox* box = new InfoBox(text); + + bool running = true; + while(running) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + switch(event.type) { + case SDL_KEYDOWN: + running = false; + break; + } + } + + box->draw(*context); + draw(); + } + + delete box; +} + +void GameSession::start_sequence(const std::string& sequencename) { if(sequencename == "endsequence" || sequencename == "fireworks") { --- NEW FILE: textscroller.cpp --- #include <config.h> #include "textscroller.h" #include <stdexcept> #include "resources.h" #include "video/font.h" #include "video/drawing_context.h" #include "app/globals.h" #include "lisp/parser.h" #include "lisp/lisp.h" static const float DEFAULT_SPEED = 1.0; static const float MAX_VEL = 10; static const float SPEED_INC = 0.01; static const float SCROLL = 60; static const float ITEMS_SPACE = 4; static void split_text(const std::string& text, std::vector<std::string>& lines) { // Split text string lines into a vector lines.clear(); std::string::size_type i, l; i = 0; while(true) { l = text.find("\n", i); if(l == std::string::npos) { lines.push_back(text.substr(i, text.size()-i)); break; } lines.push_back(text.substr(i, l-i)); i = l+1; } } void display_text_file(const std::string& file) { const Font* heading_font = white_big_text; const Font* normal_font = white_text; const Font* small_font = white_small_text; const Font* reference_font = blue_text; float speed = DEFAULT_SPEED; std::string text; std::string background_file; std::vector<std::string> lines; std::string filename = datadir + "/" + file; lisp::Parser parser; try { std::auto_ptr<lisp::Lisp> root (parser.parse(filename)); 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"); text_lisp->get("speed", speed); } catch(std::exception& e) { std::cerr << "Couldn't load file '" << filename << "': " << e.what() << "\n"; return; } // Split text string lines into a vector split_text(text, lines); // load background image Surface* background = new Surface( get_resource_filename("images/background/" + background_file), false); int done = 0; float scroll = 0; speed /= 50.0; float left_border = 50; DrawingContext context; SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); Uint32 lastticks = SDL_GetTicks(); while(!done) { /* in case of input, exit */ SDL_Event event; while(SDL_PollEvent(&event)) switch(event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_UP: speed -= SPEED_INC; break; case SDLK_DOWN: speed += SPEED_INC; break; case SDLK_SPACE: case SDLK_RETURN: if(speed >= 0) scroll += SCROLL; break; case SDLK_ESCAPE: done = 1; break; default: break; } break; case SDL_QUIT: done = 1; break; default: break; } if(speed > MAX_VEL) speed = MAX_VEL; else if(speed < -MAX_VEL) speed = -MAX_VEL; /* draw the credits */ context.draw_surface(background, Vector(0,0), 0); float y = 0; for(size_t i = 0; i < lines.size(); i++) { const std::string& line = lines[i]; if(line.size() == 0) { y += normal_font->get_height() + ITEMS_SPACE; continue; } const Font* font = 0; bool center = true; switch(line[0]) { case ' ': font = small_font; break; case '\t': font = normal_font; break; case '-': font = heading_font; break; case '*': font = reference_font; break; case '#': font = normal_font; center = false; break; default: std::cerr << "Warning: text contains an unformated line.\n"; font = normal_font; center = false; break; } if(center) { context.draw_text(font, line.substr(1, line.size()-1), Vector(screen->w/2, screen->h + y - scroll), CENTER_ALLIGN, LAYER_FOREGROUND1); } else { context.draw_text(font, line.substr(1, line.size()-1), Vector(left_border, screen->h + y - scroll), LEFT_ALLIGN, LAYER_FOREGROUND1); } y += font->get_height() + ITEMS_SPACE; } context.do_drawing(); if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0) done = 1; Uint32 ticks = SDL_GetTicks(); scroll += speed * (ticks - lastticks); lastticks = ticks; if(scroll < 0) scroll = 0; SDL_Delay(10); } SDL_EnableKeyRepeat(0, 0); // disables key repeating delete background; } InfoBox::InfoBox(const std::string& text) : firstline(0) { split_text(text, lines); } InfoBox::~InfoBox() { } void InfoBox::draw(DrawingContext& context) { const Font* heading_font = white_big_text; const Font* normal_font = white_text; const Font* small_font = white_small_text; const Font* reference_font = blue_text; float x1 = 200; float y1 = 100; float width = 400; float height = 200; context.draw_filled_rect(Vector(x1, y1), Vector(width, height), Color(150, 180, 200, 125), LAYER_GUI-1); float y = y1; for(size_t i = firstline; i < lines.size(); ++i) { const std::string& line = lines[i]; if(y >= y1 + height) break; if(line.size() == 0) { y += normal_font->get_height() + ITEMS_SPACE; continue; } const Font* font = 0; bool center = true; switch(line[0]) { case ' ': font = small_font; break; case '\t': font = normal_font; break; case '-': font = heading_font; break; case '*': font = reference_font; break; case '#': font = normal_font; center = false; break; default: std::cerr << "Warning: text contains an unformated line.\n"; font = normal_font; center = false; break; } if(center) { context.draw_text(font, line.substr(1, line.size()-1), Vector(screen->w/2, y), CENTER_ALLIGN, LAYER_GUI); } else { context.draw_text(font, line.substr(1, line.size()-1), Vector(x1, y), LEFT_ALLIGN, LAYER_GUI); } y += font->get_height() + ITEMS_SPACE; } } void InfoBox::scrollup() { if(firstline > 0) firstline--; } void InfoBox::scrolldown() { if(firstline < lines.size()-1) firstline++; } void InfoBox::pageup() { } void InfoBox::pagedown() { } |