From b2d8d1eac9fd6328d3a8c61cf09d4b2882145042 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tobias=20Gl=C3=A4=C3=9Fer?= Date: Mon, 26 Jul 2004 15:48:38 +0000 Subject: [PATCH] Made the SuperTux library again a bit more selfstanding. Moved global fonts to src/ and they are handled as static variables inside Menu:: and Button:: . SVN-Revision: 1623 --- lib/app/globals.cpp | 8 - lib/app/globals.h | 7 - lib/app/setup.cpp | 23 -- lib/app/setup.h | 6 +- lib/gui/button.cpp | 9 +- lib/gui/button.h | 3 + lib/gui/menu.cpp | 765 ++++++++++++++++++++++++++------------------------- lib/gui/menu.h | 7 + lib/video/font.cpp | 14 +- lib/video/font.h | 9 +- lib/video/screen.cpp | 6 +- src/gameloop.cpp | 10 +- src/intro.cpp | 3 +- src/resources.cpp | 105 +++++-- src/resources.h | 8 + src/title.cpp | 2 +- src/worldmap.cpp | 2 +- 17 files changed, 512 insertions(+), 475 deletions(-) diff --git a/lib/app/globals.cpp b/lib/app/globals.cpp index cced05621..251fc29b8 100644 --- a/lib/app/globals.cpp +++ b/lib/app/globals.cpp @@ -43,14 +43,6 @@ JoystickKeymap::JoystickKeymap() JoystickKeymap joystick_keymap; SDL_Surface * screen; -Font* gold_text; -Font* blue_text; -Font* gray_text; -Font* yellow_nums; -Font* white_text; -Font* white_small_text; -Font* white_big_text; - MouseCursor * mouse_cursor; bool use_gl; diff --git a/lib/app/globals.h b/lib/app/globals.h index 0fb80b7d9..6f727b768 100644 --- a/lib/app/globals.h +++ b/lib/app/globals.h @@ -55,13 +55,6 @@ namespace SuperTux extern JoystickKeymap joystick_keymap; extern SDL_Surface* screen; - extern Font* gold_text; - extern Font* white_text; - extern Font* blue_text; - extern Font* gray_text; - extern Font* white_small_text; - extern Font* white_big_text; - extern Font* yellow_nums; extern MouseCursor * mouse_cursor; diff --git a/lib/app/setup.cpp b/lib/app/setup.cpp index 83be24430..de2e2a9ca 100644 --- a/lib/app/setup.cpp +++ b/lib/app/setup.cpp @@ -346,20 +346,6 @@ void Setup::general(void) SDL_EnableUNICODE(1); - /* Load global images: */ - gold_text = new Font(datadir + "/images/fonts/gold.png", Font::TEXT, 16,18); - blue_text = new Font(datadir + "/images/fonts/blue.png", Font::TEXT, 16,18,3); - white_text = new Font(datadir + "/images/fonts/white.png", - Font::TEXT, 16,18); - gray_text = new Font(datadir + "/images/fonts/gray.png", - Font::TEXT, 16,18); - white_small_text = new Font(datadir + "/images/fonts/white-small.png", - Font::TEXT, 8,9, 1); - white_big_text = new Font(datadir + "/images/fonts/white-big.png", - Font::TEXT, 20,22, 3); - yellow_nums = new Font(datadir + "/images/fonts/numbers.png", - Font::NUM, 32,32); - /* Load GUI/menu images: */ checkbox = new Surface(datadir + "/images/status/checkbox.png", true); checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true); @@ -376,15 +362,6 @@ void Setup::general(void) void Setup::general_free(void) { - /* Free global images: */ - delete gold_text; - delete white_text; - delete blue_text; - delete gray_text; - delete white_small_text; - delete white_big_text; - delete yellow_nums; - /* Free GUI/menu images: */ delete checkbox; delete checkbox_checked; diff --git a/lib/app/setup.h b/lib/app/setup.h index 60a0272b0..2eebb380f 100644 --- a/lib/app/setup.h +++ b/lib/app/setup.h @@ -45,12 +45,14 @@ struct Setup static void directories(void); static void general(void); static void general_free(); - static void video_sdl(unsigned int screen_w, unsigned int screen_h); - static void video_gl(unsigned int screen_w, unsigned int screen_h); static void video(unsigned int screen_w, unsigned int screen_h); static void audio(void); static void joystick(void); static void parseargs(int argc, char * argv[]); + + private: + static void video_sdl(unsigned int screen_w, unsigned int screen_h); + static void video_gl(unsigned int screen_w, unsigned int screen_h); }; /// Termination handling diff --git a/lib/gui/button.cpp b/lib/gui/button.cpp index a004cce30..7b0298555 100644 --- a/lib/gui/button.cpp +++ b/lib/gui/button.cpp @@ -30,6 +30,7 @@ using namespace SuperTux; Timer Button::popup_timer; +Font* Button::info_font = 0; Button::Button(Surface* button_image, const std::string& ninfo, SDLKey nshortcut, int x, int y, int mw, int mh) @@ -121,13 +122,13 @@ void Button::draw(DrawingContext& context) char str[80]; int i = -32; - if(0 > rect.x - white_small_text->get_text_width(info)) - i = rect.w + (int)white_small_text->get_text_width(info); + if(0 > rect.x - info_font->get_text_width(info)) + i = rect.w + (int)info_font->get_text_width(info); if(!info.empty()) - context.draw_text(white_small_text, info, Vector(i + rect.x - white_small_text->get_text_width(info), rect.y), LAYER_GUI); + context.draw_text(info_font, info, Vector(i + rect.x - info_font->get_text_width(info), rect.y), LAYER_GUI); sprintf(str,"(%s)", SDL_GetKeyName(shortcut)); - context.draw_text(white_small_text, str, Vector(i + rect.x - white_small_text->get_text_width(str), rect.y + white_small_text->get_height()+2), LAYER_GUI); + context.draw_text(info_font, str, Vector(i + rect.x - info_font->get_text_width(str), rect.y + info_font->get_height()+2), LAYER_GUI); } if(state == BUTTON_PRESSED || state == BUTTON_DEACTIVE) fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200); diff --git a/lib/gui/button.h b/lib/gui/button.h index 2b265b5be..3ad96750d 100644 --- a/lib/gui/button.h +++ b/lib/gui/button.h @@ -24,6 +24,7 @@ #include #include "../video/surface.h" +#include "../video/font.h" #include "../special/timer.h" namespace SuperTux @@ -81,6 +82,8 @@ namespace SuperTux // void set_drawable(Drawable* newdrawable) // { drawable = newdrawable; } + static Font* info_font; + private: static Timer popup_timer; // Drawable* drawable; diff --git a/lib/gui/menu.cpp b/lib/gui/menu.cpp index 57adf1002..941aa56bc 100644 --- a/lib/gui/menu.cpp +++ b/lib/gui/menu.cpp @@ -50,12 +50,17 @@ Surface* SuperTux::arrow_right; std::vector Menu::last_menus; Menu* Menu::current_ = 0; +Font* Menu::default_font; +Font* Menu::active_font; +Font* Menu::deactive_font; +Font* Menu::label_font; +Font* Menu::field_font; /* just displays a Yes/No text that can be used to confirm stuff */ bool SuperTux::confirm_dialog(Surface *background, std::string text) { //Surface* cap_screen = Surface::CaptureScreen(); - + Menu* dialog = new Menu; dialog->additem(MN_DEACTIVE, text,0,0); dialog->additem(MN_HL,"",0,0); @@ -68,44 +73,44 @@ bool SuperTux::confirm_dialog(Surface *background, std::string text) DrawingContext context; while(true) - { - SDL_Event event; - - while (SDL_PollEvent(&event)) { - dialog->event(event); - } + SDL_Event event; - if(background == NULL) - context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0); - else - context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0); + while (SDL_PollEvent(&event)) + { + dialog->event(event); + } - dialog->draw(context); - dialog->action(); + if(background == NULL) + context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0); + else + context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0); - switch (dialog->check()) - { - case true: - //delete cap_screen; - Menu::set_current(0); - delete dialog; - return true; - break; - case false: - //delete cap_screen; - Menu::set_current(0); - delete dialog; - return false; - break; - default: - break; - } + dialog->draw(context); + dialog->action(); - mouse_cursor->draw(context); - context.do_drawing(); - SDL_Delay(25); - } + switch (dialog->check()) + { + case true: + //delete cap_screen; + Menu::set_current(0); + delete dialog; + return true; + break; + case false: + //delete cap_screen; + Menu::set_current(0); + delete dialog; + return false; + break; + default: + break; + } + + mouse_cursor->draw(context); + context.do_drawing(); + SDL_Delay(25); + } return false; } @@ -124,16 +129,16 @@ void Menu::pop_current() { if (!last_menus.empty()) - { - current_ = last_menus.back(); - current_->effect.start(500); + { + current_ = last_menus.back(); + current_->effect.start(500); - last_menus.pop_back(); - } + last_menus.pop_back(); + } else - { - current_ = 0; - } + { + current_ = 0; + } } void @@ -167,10 +172,10 @@ MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* pnew_item->input[0] = '\0'; if(kind_ == MN_STRINGSELECT) - { - pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type)); - string_list_init(pnew_item->list); - } + { + pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type)); + string_list_init(pnew_item->list); + } else pnew_item->list = NULL; @@ -188,22 +193,22 @@ void MenuItem::change_text(const char *text_) { if (text_) - { - free(text); - text = (char*) malloc(sizeof(char )*(strlen(text_)+1)); - strcpy(text, text_); - } + { + free(text); + text = (char*) malloc(sizeof(char )*(strlen(text_)+1)); + strcpy(text, text_); + } } void MenuItem::change_input(const char *text_) { if(text) - { - free(input); - input = (char*) malloc(sizeof(char )*(strlen(text_)+1)); - strcpy(input, text_); - } + { + free(input); + input = (char*) malloc(sizeof(char )*(strlen(text_)+1)); + strcpy(input, text_); + } } std::string MenuItem::get_input_with_symbol(bool active_item) @@ -211,16 +216,16 @@ std::string MenuItem::get_input_with_symbol(bool active_item) if(!active_item) input_flickering = true; else - { - if(input_flickering_timer.get_left() < 0) { - if(input_flickering) - input_flickering = false; - else - input_flickering = true; - input_flickering_timer.start(FLICK_CURSOR_TIME); + if(input_flickering_timer.get_left() < 0) + { + if(input_flickering) + input_flickering = false; + else + input_flickering = true; + input_flickering_timer.start(FLICK_CURSOR_TIME); + } } - } char str[1024]; if(input_flickering) @@ -237,51 +242,51 @@ std::string MenuItem::get_input_with_symbol(bool active_item) void Menu::get_controlfield_key_into_input(MenuItem *item) { switch(*item->int_p) - { - case SDLK_UP: - item->change_input(_("Up cursor")); - break; - case SDLK_DOWN: - item->change_input(_("Down cursor")); - break; - case SDLK_LEFT: - item->change_input(_("Left cursor")); - break; - case SDLK_RIGHT: - item->change_input(_("Right cursor")); - break; - case SDLK_RETURN: - item->change_input(_("Return")); - break; - case SDLK_SPACE: - item->change_input(_("Space")); - break; - case SDLK_RSHIFT: - item->change_input(_("Right Shift")); - break; - case SDLK_LSHIFT: - item->change_input(_("Left Shift")); - break; - case SDLK_RCTRL: - item->change_input(_("Right Control")); - break; - case SDLK_LCTRL: - item->change_input(_("Left Control")); - break; - case SDLK_RALT: - item->change_input(_("Right Alt")); - break; - case SDLK_LALT: - item->change_input(_("Left Alt")); - break; - default: { - char tmp[64]; - snprintf(tmp, 64, "%d", *item->int_p); - item->change_input(tmp); + case SDLK_UP: + item->change_input(_("Up cursor")); + break; + case SDLK_DOWN: + item->change_input(_("Down cursor")); + break; + case SDLK_LEFT: + item->change_input(_("Left cursor")); + break; + case SDLK_RIGHT: + item->change_input(_("Right cursor")); + break; + case SDLK_RETURN: + item->change_input(_("Return")); + break; + case SDLK_SPACE: + item->change_input(_("Space")); + break; + case SDLK_RSHIFT: + item->change_input(_("Right Shift")); + break; + case SDLK_LSHIFT: + item->change_input(_("Left Shift")); + break; + case SDLK_RCTRL: + item->change_input(_("Right Control")); + break; + case SDLK_LCTRL: + item->change_input(_("Left Control")); + break; + case SDLK_RALT: + item->change_input(_("Right Alt")); + break; + case SDLK_LALT: + item->change_input(_("Left Alt")); + break; + default: + { + char tmp[64]; + snprintf(tmp, 64, "%d", *item->int_p); + item->change_input(tmp); + } + break; } - break; - } } /* Set ControlField for joystick button */ @@ -296,14 +301,14 @@ void Menu::get_controlfield_js_into_input(MenuItem *item) Menu::~Menu() { if(item.size() != 0) - { - for(unsigned int i = 0; i < item.size(); ++i) { - free(item[i].text); - free(item[i].input); - string_list_free(item[i].list); + for(unsigned int i = 0; i < item.size(); ++i) + { + free(item[i].text); + free(item[i].input); + string_list_free(item[i].list); + } } - } } @@ -355,133 +360,133 @@ Menu::action() { hit_item = -1; if(item.size() != 0) - { - switch(menuaction) { - case MENU_ACTION_UP: - if (active_item > 0) - --active_item; - else - active_item = int(item.size())-1; - break; - - case MENU_ACTION_DOWN: - if(active_item < int(item.size())-1) - ++active_item; - else - active_item = 0; - break; - - case MENU_ACTION_LEFT: - if(item[active_item].kind == MN_STRINGSELECT - && item[active_item].list->num_items != 0) - { - if(item[active_item].list->active_item > 0) - --item[active_item].list->active_item; - else - item[active_item].list->active_item = item[active_item].list->num_items-1; - } - break; - - case MENU_ACTION_RIGHT: - if(item[active_item].kind == MN_STRINGSELECT - && item[active_item].list->num_items != 0) - { - if(item[active_item].list->active_item < item[active_item].list->num_items-1) - ++item[active_item].list->active_item; - else - item[active_item].list->active_item = 0; - } - break; - - case MENU_ACTION_HIT: - { - hit_item = active_item; - switch (item[active_item].kind) + switch(menuaction) { - case MN_GOTO: - if (item[active_item].target_menu != NULL) - Menu::push_current(item[active_item].target_menu); + case MENU_ACTION_UP: + if (active_item > 0) + --active_item; else - puts("NULLL"); + active_item = int(item.size())-1; break; - case MN_TOGGLE: - item[active_item].toggled = !item[active_item].toggled; + case MENU_ACTION_DOWN: + if(active_item < int(item.size())-1) + ++active_item; + else + active_item = 0; break; - case MN_ACTION: - Menu::set_current(0); - item[active_item].toggled = true; - break; - case MN_TEXTFIELD: - case MN_NUMFIELD: - menuaction = MENU_ACTION_DOWN; - action(); + case MENU_ACTION_LEFT: + if(item[active_item].kind == MN_STRINGSELECT + && item[active_item].list->num_items != 0) + { + if(item[active_item].list->active_item > 0) + --item[active_item].list->active_item; + else + item[active_item].list->active_item = item[active_item].list->num_items-1; + } break; - case MN_BACK: - Menu::pop_current(); - break; - default: + case MENU_ACTION_RIGHT: + if(item[active_item].kind == MN_STRINGSELECT + && item[active_item].list->num_items != 0) + { + if(item[active_item].list->active_item < item[active_item].list->num_items-1) + ++item[active_item].list->active_item; + else + item[active_item].list->active_item = 0; + } break; - } - } - break; - - case MENU_ACTION_REMOVE: - if(item[active_item].kind == MN_TEXTFIELD - || item[active_item].kind == MN_NUMFIELD) - { - if(item[active_item].input != NULL) - { - int i = strlen(item[active_item].input); - while(delete_character > 0) /* remove charactes */ + case MENU_ACTION_HIT: { - item[active_item].input[i-1] = '\0'; - delete_character--; + hit_item = active_item; + switch (item[active_item].kind) + { + case MN_GOTO: + if (item[active_item].target_menu != NULL) + Menu::push_current(item[active_item].target_menu); + else + puts("NULLL"); + break; + + case MN_TOGGLE: + item[active_item].toggled = !item[active_item].toggled; + break; + + case MN_ACTION: + Menu::set_current(0); + item[active_item].toggled = true; + break; + case MN_TEXTFIELD: + case MN_NUMFIELD: + menuaction = MENU_ACTION_DOWN; + action(); + break; + + case MN_BACK: + Menu::pop_current(); + break; + default: + break; + } } - } - } - break; + break; - case MENU_ACTION_INPUT: - if(item[active_item].kind == MN_TEXTFIELD - || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9')) - { - if(item[active_item].input != NULL) - { - int i = strlen(item[active_item].input); - item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2)); - item[active_item].input[i] = mn_input_char; - item[active_item].input[i+1] = '\0'; - } - else - { - item[active_item].input = (char*) malloc(2*sizeof(char)); - item[active_item].input[0] = mn_input_char; - item[active_item].input[1] = '\0'; - } - } + case MENU_ACTION_REMOVE: + if(item[active_item].kind == MN_TEXTFIELD + || item[active_item].kind == MN_NUMFIELD) + { + if(item[active_item].input != NULL) + { + int i = strlen(item[active_item].input); + + while(delete_character > 0) /* remove charactes */ + { + item[active_item].input[i-1] = '\0'; + delete_character--; + } + } + } + break; - case MENU_ACTION_NONE: - break; + case MENU_ACTION_INPUT: + if(item[active_item].kind == MN_TEXTFIELD + || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9')) + { + if(item[active_item].input != NULL) + { + int i = strlen(item[active_item].input); + item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2)); + item[active_item].input[i] = mn_input_char; + item[active_item].input[i+1] = '\0'; + } + else + { + item[active_item].input = (char*) malloc(2*sizeof(char)); + item[active_item].input[0] = mn_input_char; + item[active_item].input[1] = '\0'; + } + } + + case MENU_ACTION_NONE: + break; + } } - } MenuItem& new_item = item[active_item]; if(new_item.kind == MN_DEACTIVE || new_item.kind == MN_LABEL || new_item.kind == MN_HL) - { - // Skip the horzontal line item - if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN) - menuaction = MENU_ACTION_DOWN; + { + // Skip the horzontal line item + if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN) + menuaction = MENU_ACTION_DOWN; - if (item.size() > 1) - action(); - } + if (item.size() > 1) + action(); + } menuaction = MENU_ACTION_NONE; @@ -500,8 +505,8 @@ Menu::check() void Menu::draw_item(DrawingContext& context, - int index, // Position of the current item in the menu - int menu_width, int menu_height) + int index, // Position of the current item in the menu + int menu_width, int menu_height) { MenuItem& pitem = item[index]; @@ -515,7 +520,7 @@ Menu::draw_item(DrawingContext& context, effect_offset = (index % 2) ? effect_time : -effect_time; } - Font* text_font = white_text; + Font* text_font = default_font; int x_pos = pos_x; int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset; int shadow_size = 2; @@ -528,182 +533,182 @@ Menu::draw_item(DrawingContext& context, x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2; if(index == active_item) - { - shadow_size = 3; - text_font = blue_text; - } - - switch (pitem.kind) - { - case MN_DEACTIVE: { - context.draw_text_center(gray_text, pitem.text, - Vector(0, y_pos - int(blue_text->get_height()/2)), - LAYER_GUI); - break; + shadow_size = 3; + text_font = active_font; } - case MN_HL: - { - // TODO - int x = pos_x - menu_width/2; - int y = y_pos - 12 - effect_offset; - /* Draw a horizontal line with a little 3d effect */ - context.draw_filled_rect(Vector(x, y + 6), - Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI); - context.draw_filled_rect(Vector(x, y + 6), - Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI); - break; - } - case MN_LABEL: - { - context.draw_text_center(white_big_text, - pitem.text, Vector(0, y_pos - int(white_big_text->get_height()/2)), - LAYER_GUI); - break; - } - case MN_TEXTFIELD: - case MN_NUMFIELD: - case MN_CONTROLFIELD_KB: - case MN_CONTROLFIELD_JS: + switch (pitem.kind) { - int width = text_width + input_width + 5; - int text_pos = screen->w/2 - width/2; - int input_pos = text_pos + text_width + 10; + case MN_DEACTIVE: + { + context.draw_text_center(deactive_font, pitem.text, + Vector(0, y_pos - int(deactive_font->get_height()/2)), + LAYER_GUI); + break; + } + + case MN_HL: + { + // TODO + int x = pos_x - menu_width/2; + int y = y_pos - 12 - effect_offset; + /* Draw a horizontal line with a little 3d effect */ + context.draw_filled_rect(Vector(x, y + 6), + Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI); + context.draw_filled_rect(Vector(x, y + 6), + Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI); + break; + } + case MN_LABEL: + { + context.draw_text_center(label_font, + pitem.text, Vector(0, y_pos - int(label_font->get_height()/2)), + LAYER_GUI); + break; + } + case MN_TEXTFIELD: + case MN_NUMFIELD: + case MN_CONTROLFIELD_KB: + case MN_CONTROLFIELD_JS: + { + int width = text_width + input_width + 5; + int text_pos = screen->w/2 - width/2; + int input_pos = text_pos + text_width + 10; - context.draw_filled_rect( + context.draw_filled_rect( Vector(input_pos - 5, y_pos - 10), Vector(input_width + 10, 20), Color(255,255,255,255), LAYER_GUI-5); - context.draw_filled_rect( + context.draw_filled_rect( Vector(input_pos - 4, y_pos - 9), Vector(input_width + 8, 18), Color(0,0,0,128), LAYER_GUI-4); - if(pitem.kind == MN_CONTROLFIELD_KB) - get_controlfield_key_into_input(&pitem); - else if (pitem.kind == MN_CONTROLFIELD_JS) - get_controlfield_js_into_input(&pitem); + if(pitem.kind == MN_CONTROLFIELD_KB) + get_controlfield_key_into_input(&pitem); + else if (pitem.kind == MN_CONTROLFIELD_JS) + get_controlfield_js_into_input(&pitem); - if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD) - { - if(active_item == index) - context.draw_text(gold_text, - pitem.get_input_with_symbol(true), - Vector(input_pos, y_pos - int(gold_text->get_height()/2)), - LAYER_GUI); + if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD) + { + if(active_item == index) + context.draw_text(field_font, + pitem.get_input_with_symbol(true), + Vector(input_pos, y_pos - int(field_font->get_height()/2)), + LAYER_GUI); + else + context.draw_text(field_font, + pitem.get_input_with_symbol(false), + Vector(input_pos, y_pos - int(field_font->get_height()/2)), + LAYER_GUI); + } else - context.draw_text(gold_text, - pitem.get_input_with_symbol(false), - Vector(input_pos, y_pos - int(gold_text->get_height()/2)), - LAYER_GUI); + context.draw_text(field_font, pitem.input, + Vector(input_pos, y_pos - int(field_font->get_height()/2)), + LAYER_GUI); + + context.draw_text(text_font, pitem.text, + Vector(text_pos, y_pos - int(text_font->get_height()/2)), + LAYER_GUI); + break; } - else - context.draw_text(gold_text, pitem.input, - Vector(input_pos, y_pos - int(gold_text->get_height()/2)), - LAYER_GUI); - - context.draw_text(text_font, pitem.text, - Vector(text_pos, y_pos - int(text_font->get_height()/2)), - LAYER_GUI); - break; - } - case MN_STRINGSELECT: - { - int list_pos_2 = list_width + 16; - int list_pos = list_width/2; - int text_pos = (text_width + 16)/2; - - /* Draw arrows */ - context.draw_surface(arrow_left, - Vector(x_pos - list_pos + text_pos - 17, y_pos - 8), - LAYER_GUI); - context.draw_surface(arrow_right, - Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8), - LAYER_GUI); - - /* Draw input background */ - context.draw_filled_rect( + case MN_STRINGSELECT: + { + int list_pos_2 = list_width + 16; + int list_pos = list_width/2; + int text_pos = (text_width + 16)/2; + + /* Draw arrows */ + context.draw_surface(arrow_left, + Vector(x_pos - list_pos + text_pos - 17, y_pos - 8), + LAYER_GUI); + context.draw_surface(arrow_right, + Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8), + LAYER_GUI); + + /* Draw input background */ + context.draw_filled_rect( Vector(x_pos - list_pos + text_pos - 1, y_pos - 10), Vector(list_pos_2 + 2, 20), Color(255,255,255,255), LAYER_GUI - 4); - context.draw_filled_rect( + context.draw_filled_rect( Vector(x_pos - list_pos + text_pos, y_pos - 9), Vector(list_pos_2, 18), Color(0,0,0,128), LAYER_GUI - 5); - context.draw_text_center(text_font, string_list_active(pitem.list), - Vector(text_pos, y_pos - int(text_font->get_height()/2)), - LAYER_GUI); - context.draw_text_center(text_font, pitem.text, - Vector(list_pos_2/2, y_pos - int(text_font->get_height()/2)), - LAYER_GUI); - break; - } - case MN_BACK: - { + context.draw_text_center(text_font, string_list_active(pitem.list), + Vector(text_pos, y_pos - int(text_font->get_height()/2)), + LAYER_GUI); + context.draw_text_center(text_font, pitem.text, + Vector(list_pos_2/2, y_pos - int(text_font->get_height()/2)), + LAYER_GUI); + break; + } + case MN_BACK: + { + context.draw_text_center(text_font, pitem.text, + Vector(0, y_pos - int(text_font->get_height()/2)), + LAYER_GUI); + context.draw_surface(back, + Vector(x_pos + text_width/2 + 16, y_pos - 8), + LAYER_GUI); + break; + } + + case MN_TOGGLE: + { + context.draw_text_center(text_font, pitem.text, + Vector(0, y_pos - (text_font->get_height()/2)), + LAYER_GUI); + + if(pitem.toggled) + context.draw_surface(checkbox_checked, + Vector(x_pos + (text_width+16)/2, y_pos - 8), + LAYER_GUI + 1); + else + context.draw_surface(checkbox, + Vector(x_pos + (text_width+16)/2, y_pos - 8), + LAYER_GUI + 1); + break; + } + case MN_ACTION: context.draw_text_center(text_font, pitem.text, - Vector(0, y_pos - int(text_font->get_height()/2)), - LAYER_GUI); - context.draw_surface(back, - Vector(x_pos + text_width/2 + 16, y_pos - 8), - LAYER_GUI); + Vector(0, y_pos - int(text_font->get_height()/2)), + LAYER_GUI); break; - } - case MN_TOGGLE: - { + case MN_GOTO: context.draw_text_center(text_font, pitem.text, - Vector(0, y_pos - (text_font->get_height()/2)), - LAYER_GUI); - - if(pitem.toggled) - context.draw_surface(checkbox_checked, - Vector(x_pos + (text_width+16)/2, y_pos - 8), - LAYER_GUI + 1); - else - context.draw_surface(checkbox, - Vector(x_pos + (text_width+16)/2, y_pos - 8), - LAYER_GUI + 1); + Vector(0, y_pos - int(text_font->get_height()/2)), + LAYER_GUI); break; } - case MN_ACTION: - context.draw_text_center(text_font, pitem.text, - Vector(0, y_pos - int(text_font->get_height()/2)), - LAYER_GUI); - break; - - case MN_GOTO: - context.draw_text_center(text_font, pitem.text, - Vector(0, y_pos - int(text_font->get_height()/2)), - LAYER_GUI); - break; - } } int Menu::get_width() const -{ - /* The width of the menu has to be more than the width of the text - with the most characters */ - int menu_width = 0; - for(unsigned int i = 0; i < item.size(); ++i) { - int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list)); - if( w > menu_width ) - { - menu_width = w; - if( item[i].kind == MN_TOGGLE) - menu_width += 2; - } - } + /* The width of the menu has to be more than the width of the text + with the most characters */ + int menu_width = 0; + for(unsigned int i = 0; i < item.size(); ++i) + { + int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list)); + if( w > menu_width ) + { + menu_width = w; + if( item[i].kind == MN_TOGGLE) + menu_width += 2; + } + } - return (menu_width * 16 + 24); -} + return (menu_width * 16 + 24); + } int Menu::get_height() const -{ - return item.size() * 24; -} + { + return item.size() * 24; + } /* Draw the current menu. */ void @@ -714,24 +719,24 @@ Menu::draw(DrawingContext& context) /* Draw a transparent background */ context.draw_filled_rect( - Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10), - Vector(menu_width,menu_height + 20), - Color(150,180,200,125), LAYER_GUI-10); + Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10), + Vector(menu_width,menu_height + 20), + Color(150,180,200,125), LAYER_GUI-10); for(unsigned int i = 0; i < item.size(); ++i) - { - draw_item(context, i, menu_width, menu_height); - } + { + draw_item(context, i, menu_width, menu_height); + } } MenuItem& Menu::get_item_by_id(int id) { for(std::vector::iterator i = item.begin(); i != item.end(); ++i) - { - if(i->id == id) - return *i; - } + { + if(i->id == id) + return *i; + } assert(false); static MenuItem dummyitem; @@ -833,7 +838,7 @@ Menu::event(SDL_Event& event) } } break; - + case SDL_JOYAXISMOTION: if(event.jaxis.axis == joystick_keymap.y_axis) { @@ -854,7 +859,7 @@ Menu::event(SDL_Event& event) case SDL_JOYBUTTONDOWN: if (item[active_item].kind == MN_CONTROLFIELD_JS) { - // FIXME: This next line does nothing useable, right? + // FIXME: This next line does nothing useable, right? // *item[active_item].int_p = key; menuaction = MENU_ACTION_DOWN; } @@ -867,9 +872,9 @@ Menu::event(SDL_Event& event) int y = event.motion.y; if(x > pos_x - get_width()/2 && - x < pos_x + get_width()/2 && - y > pos_y - get_height()/2 && - y < pos_y + get_height()/2) + x < pos_x + get_width()/2 && + y > pos_y - get_height()/2 && + y < pos_y + get_height()/2) { menuaction = MENU_ACTION_HIT; } @@ -882,9 +887,9 @@ Menu::event(SDL_Event& event) int y = event.motion.y; if(x > pos_x - get_width()/2 && - x < pos_x + get_width()/2 && - y > pos_y - get_height()/2 && - y < pos_y + get_height()/2) + x < pos_x + get_width()/2 && + y > pos_y - get_height()/2 && + y < pos_y + get_height()/2) { active_item = (y - (pos_y - get_height()/2)) / 24; mouse_cursor->set_state(MC_LINK); diff --git a/lib/gui/menu.h b/lib/gui/menu.h index eae2a4721..31ad4f9d5 100644 --- a/lib/gui/menu.h +++ b/lib/gui/menu.h @@ -25,6 +25,7 @@ #include "SDL.h" #include "../video/surface.h" +#include "../video/font.h" #include "../special/timer.h" #include "../special/base.h" #include "../special/stringlist.h" @@ -130,6 +131,12 @@ namespace SuperTux Timer joystick_timer; public: + static Font* default_font; + static Font* active_font; + static Font* deactive_font; + static Font* label_font; + static Font* field_font; + Timer effect; int arrange_left; int active_item; diff --git a/lib/video/font.cpp b/lib/video/font.cpp index 5b5ac85e7..a0f7d73e5 100644 --- a/lib/video/font.cpp +++ b/lib/video/font.cpp @@ -131,7 +131,7 @@ 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) +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::vector names; @@ -229,18 +229,18 @@ void SuperTux::display_text_file(const std::string& file, float scroll_speed) float y = 0; for(size_t i = 0; i < names.size(); i++) { if(names[i].size() == 0) { - y += white_text->get_height() + ITEMS_SPACE; + y += normal_font->get_height() + ITEMS_SPACE; continue; } Font* font = 0; switch(names[i][0]) { - case ' ': font = white_small_text; break; - case '\t': font = white_text; break; - case '-': font = white_big_text; break; - case '*': font = blue_text; break; - default: font = blue_text; break; + 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; } context.draw_text_center(font, diff --git a/lib/video/font.h b/lib/video/font.h index b32cd9050..9304cab72 100644 --- a/lib/video/font.h +++ b/lib/video/font.h @@ -29,10 +29,6 @@ namespace SuperTux { - /** Reads a text file (using LispReader, so it as to be in its formatting) - and displays it in a StarTrek fashion */ - void display_text_file(const std::string& file, float scroll_speed); - /// Font class Font { @@ -75,6 +71,11 @@ namespace SuperTux int last_char; }; + + /** Reads a text file (using LispReader, so it as to be in its formatting) + and displays it in a StarTrek fashion */ + void display_text_file(const std::string& file, float scroll_speed, Font* heading_font, Font* normal_font, Font* small_font, Font* reference_font ); + } //namespace SuperTux #endif /*SUPERTUX_FONT_H*/ diff --git a/lib/video/screen.cpp b/lib/video/screen.cpp index 284512cee..41245eebe 100644 --- a/lib/video/screen.cpp +++ b/lib/video/screen.cpp @@ -264,11 +264,7 @@ void SuperTux::fadeout(int fade_time) } fillrect(0, 0, screen->w, screen->h, 0, 0, 0, 255); - - DrawingContext context; - context.draw_text_center(white_text, "Loading...", - Vector(0, screen->h/2), LAYER_FOREGROUND1); - context.do_drawing(); + } void SuperTux::shrink_fade(const Vector& point, int fade_time) diff --git a/src/gameloop.cpp b/src/gameloop.cpp index a1ab9466d..a5c17b787 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -851,13 +851,19 @@ bool process_load_game_menu() // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000); fadeout(256); + + DrawingContext context; + context.draw_text_center(white_text, "Loading...", + Vector(0, screen->h/2), LAYER_FOREGROUND1); + context.do_drawing(); + WorldMapNS::WorldMap worldmap; - + // Load the game or at least set the savegame_file variable worldmap.loadgame(slotfile); worldmap.display(); - + Menu::set_current(main_menu); st_pause_ticks_stop(); diff --git a/src/intro.cpp b/src/intro.cpp index 372b7f3eb..a2a438a51 100644 --- a/src/intro.cpp +++ b/src/intro.cpp @@ -22,11 +22,12 @@ #include "app/defines.h" #include "video/font.h" #include "video/screen.h" +#include "resources.h" using namespace SuperTux; void draw_intro() { - display_text_file("intro.txt", 1); + display_text_file("intro.txt", 1, white_big_text , white_text, white_small_text, blue_text ); } diff --git a/src/resources.cpp b/src/resources.cpp index 74bd667d3..d139568e7 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -28,6 +28,7 @@ #include "audio/sound_manager.h" #include "app/setup.h" #include "door.h" +#include "gui/button.h" Surface* img_waves[3]; Surface* img_water; @@ -76,9 +77,41 @@ char * soundfilenames[NUM_SOUNDS] = { "/sounds/explosion.wav" }; + +Font* gold_text; +Font* blue_text; +Font* gray_text; +Font* yellow_nums; +Font* white_text; +Font* white_small_text; +Font* white_big_text; + /* Load graphics/sounds shared between all levels: */ void loadshared() { + + /* Load global images: */ + gold_text = new Font(datadir + "/images/fonts/gold.png", Font::TEXT, 16,18); + blue_text = new Font(datadir + "/images/fonts/blue.png", Font::TEXT, 16,18,3); + white_text = new Font(datadir + "/images/fonts/white.png", + Font::TEXT, 16,18); + gray_text = new Font(datadir + "/images/fonts/gray.png", + Font::TEXT, 16,18); + white_small_text = new Font(datadir + "/images/fonts/white-small.png", + Font::TEXT, 8,9, 1); + white_big_text = new Font(datadir + "/images/fonts/white-big.png", + Font::TEXT, 20,22, 3); + yellow_nums = new Font(datadir + "/images/fonts/numbers.png", + Font::NUM, 32,32); + + Menu::default_font = white_text; + Menu::active_font = blue_text; + Menu::deactive_font = gray_text; + Menu::label_font = white_big_text; + Menu::field_font = gold_text; + + Button::info_font = white_small_text; + int i; sprite_manager = new SpriteManager(datadir + "/supertux.strf"); @@ -90,13 +123,13 @@ void loadshared() char img_name[1024]; for (int i = 0; i < GROWING_FRAMES; i++) - { - sprintf(img_name, "%s/images/shared/tux-grow-left-%i.png", datadir.c_str(), i+1); - growingtux_left[i] = new Surface(img_name, false); + { + sprintf(img_name, "%s/images/shared/tux-grow-left-%i.png", datadir.c_str(), i+1); + growingtux_left[i] = new Surface(img_name, false); - sprintf(img_name, "%s/images/shared/tux-grow-right-%i.png", datadir.c_str(), i+1); - growingtux_right[i] = new Surface(img_name, false); - } + sprintf(img_name, "%s/images/shared/tux-grow-right-%i.png", datadir.c_str(), i+1); + growingtux_right[i] = new Surface(img_name, false); + } smalltux.stand_left = sprite_manager->load("smalltux-stand-left"); smalltux.stand_right = sprite_manager->load("smalltux-stand-right"); @@ -165,56 +198,56 @@ void loadshared() img_water = new Surface(datadir + "/images/shared/water.png", false); img_waves[0] = new Surface(datadir + "/images/shared/waves-0.png", - true); + true); img_waves[1] = new Surface(datadir + "/images/shared/waves-1.png", - true); + true); img_waves[2] = new Surface(datadir + "/images/shared/waves-2.png", - true); + true); /* Pole: */ img_pole = new Surface(datadir + "/images/shared/pole.png", true); img_poletop = new Surface(datadir + "/images/shared/poletop.png", - true); + true); /* Flag: */ img_flag[0] = new Surface(datadir + "/images/shared/flag-0.png", - true); + true); img_flag[1] = new Surface(datadir + "/images/shared/flag-1.png", - true); + true); /* Cloud: */ img_cloud[0][0] = new Surface(datadir + "/images/shared/cloud-00.png", - true); + true); img_cloud[0][1] = new Surface(datadir + "/images/shared/cloud-01.png", - true); + true); img_cloud[0][2] = new Surface(datadir + "/images/shared/cloud-02.png", - true); + true); img_cloud[0][3] = new Surface(datadir + "/images/shared/cloud-03.png", - true); + true); img_cloud[1][0] = new Surface(datadir + "/images/shared/cloud-10.png", - true); + true); img_cloud[1][1] = new Surface(datadir + "/images/shared/cloud-11.png", - true); + true); img_cloud[1][2] = new Surface(datadir + "/images/shared/cloud-12.png", - true); + true); img_cloud[1][3] = new Surface(datadir + "/images/shared/cloud-13.png", - true); + true); /* Bad guys: */ @@ -230,22 +263,22 @@ void loadshared() door = sprite_manager->load("door"); for (int i = 0; i < DOOR_OPENING_FRAMES; i++) { - sprintf(img_name, "%s/images/shared/door-%i.png", datadir.c_str(), i+1); - door_opening[i] = new Surface(img_name, false); + sprintf(img_name, "%s/images/shared/door-%i.png", datadir.c_str(), i+1); + door_opening[i] = new Surface(img_name, false); } /* Distros: */ img_distro[0] = new Surface(datadir + "/images/tilesets/coin1.png", - true); + true); img_distro[1] = new Surface(datadir + "/images/tilesets/coin2.png", - true); + true); img_distro[2] = new Surface(datadir + "/images/tilesets/coin3.png", - true); + true); img_distro[3] = new Surface(datadir + "/images/tilesets/coin2.png", - true); + true); /* Tux life: */ @@ -263,17 +296,29 @@ void loadshared() Send a mail to me: neoneurone@users.sf.net, if you have another opinion. :) */ for (i = 0; i < NUM_SOUNDS; i++) - SoundManager::get()->add_sound(SoundManager::get()->load_sound(datadir + soundfilenames[i]),i); + SoundManager::get + ()->add_sound(SoundManager::get + ()->load_sound(datadir + soundfilenames[i]),i); /* Herring song */ - herring_song = SoundManager::get()->load_music(datadir + "/music/SALCON.MOD"); - level_end_song = SoundManager::get()->load_music(datadir + "/music/leveldone.mod"); + herring_song = SoundManager::get + ()->load_music(datadir + "/music/SALCON.MOD"); + level_end_song = SoundManager::get + ()->load_music(datadir + "/music/leveldone.mod"); } - /* Free shared data: */ void unloadshared(void) { + /* Free global images: */ + delete gold_text; + delete white_text; + delete blue_text; + delete gray_text; + delete white_small_text; + delete white_big_text; + delete yellow_nums; + int i; free_special_gfx(); diff --git a/src/resources.h b/src/resources.h index d57d57b9f..306e2df5d 100644 --- a/src/resources.h +++ b/src/resources.h @@ -82,6 +82,14 @@ extern Menu* highscore_menu; extern Menu* load_game_menu; extern Menu* save_game_menu; +extern Font* gold_text; +extern Font* white_text; +extern Font* blue_text; +extern Font* gray_text; +extern Font* white_small_text; +extern Font* white_big_text; +extern Font* yellow_nums; + void loadshared(); void unloadshared(); diff --git a/src/title.cpp b/src/title.cpp index 802576310..6410e01d0 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -352,7 +352,7 @@ void title(void) update_time = st_get_ticks(); break; case MNID_CREDITS: - display_text_file("CREDITS", SCROLL_SPEED_CREDITS); + display_text_file("CREDITS", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text ); Menu::set_current(main_menu); break; case MNID_QUITMAINMENU: diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 8c11af927..f67109d4a 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -784,7 +784,7 @@ WorldMap::update(float delta) if (!level->extro_filename.empty()) { // Display a text file - display_text_file(level->extro_filename, SCROLL_SPEED_MESSAGE); + display_text_file(level->extro_filename, SCROLL_SPEED_MESSAGE, white_big_text , white_text, white_small_text, blue_text ); } if (level->swap_x != -1 && level->swap_y != -1) { -- 2.11.0