From: Ingo Ruhnke Date: Mon, 19 Apr 2004 21:21:00 +0000 (+0000) Subject: - fixed problem with last_menu not being able to handle menues deeper than two submenues X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=63d73bc2cd3c356ffcf25af79256fbf2af1dd07c;p=supertux.git - fixed problem with last_menu not being able to handle menues deeper than two submenues - misc other cleanup SVN-Revision: 580 --- diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 4c2bfd2cc..cfdefc16d 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -131,22 +131,14 @@ GameSession::start_timers() void GameSession::on_escape_press() { - if(!game_pause) + if(st_gl_mode == ST_GL_TEST) { - if(st_gl_mode == ST_GL_TEST) - { - exit_status = LEVEL_ABORT; - } - else if (!Menu::current()) - { - Menu::set_current(game_menu); - st_pause_ticks_stop(); - } - else - { - Menu::set_current(NULL); - st_pause_ticks_start(); - } + exit_status = LEVEL_ABORT; + } + else if (!Menu::current()) + { + Menu::set_current(game_menu); + st_pause_ticks_stop(); } } @@ -160,151 +152,153 @@ GameSession::process_events() { /* Check for menu-events, if the menu is shown */ if (Menu::current()) - Menu::current()->event(event); - - switch(event.type) { - case SDL_QUIT: /* Quit event - quit: */ - st_abort("Received window close", ""); - break; - - case SDL_KEYDOWN: /* A keypress! */ - { - SDLKey key = event.key.keysym.sym; - - if(tux.key_event(key,DOWN)) + Menu::current()->event(event); + } + else + { + switch(event.type) + { + case SDL_QUIT: /* Quit event - quit: */ + st_abort("Received window close", ""); break; - switch(key) + case SDL_KEYDOWN: /* A keypress! */ { - case SDLK_ESCAPE: /* Escape: Open/Close the menu: */ - on_escape_press(); - break; - default: - break; - } - } - break; - case SDL_KEYUP: /* A keyrelease! */ - { - SDLKey key = event.key.keysym.sym; + SDLKey key = event.key.keysym.sym; + + if(tux.key_event(key,DOWN)) + break; - if(tux.key_event(key, UP)) + switch(key) + { + case SDLK_ESCAPE: /* Escape: Open/Close the menu: */ + on_escape_press(); + break; + default: + break; + } + } break; - - switch(key) + case SDL_KEYUP: /* A keyrelease! */ { - case SDLK_p: - if(!Menu::current()) + SDLKey key = event.key.keysym.sym; + + if(tux.key_event(key, UP)) + break; + + switch(key) { - if(game_pause) - { - game_pause = false; - st_pause_ticks_stop(); - } - else + case SDLK_p: + if(!Menu::current()) { - game_pause = true; - st_pause_ticks_start(); + if(game_pause) + { + game_pause = false; + st_pause_ticks_stop(); + } + else + { + game_pause = true; + st_pause_ticks_start(); + } } - } - break; - case SDLK_TAB: - if(debug_mode) - { - tux.size = !tux.size; - if(tux.size == BIG) + break; + case SDLK_TAB: + if(debug_mode) { - tux.base.height = 64; + tux.size = !tux.size; + if(tux.size == BIG) + { + tux.base.height = 64; + } + else + tux.base.height = 32; } + break; + case SDLK_END: + if(debug_mode) + player_status.distros += 50; + break; + case SDLK_DELETE: + if(debug_mode) + tux.got_coffee = 1; + break; + case SDLK_INSERT: + if(debug_mode) + tux.invincible_timer.start(TUX_INVINCIBLE_TIME); + break; + case SDLK_l: + if(debug_mode) + --player_status.lives; + break; + case SDLK_s: + if(debug_mode) + player_status.score += 1000; + case SDLK_f: + if(debug_fps) + debug_fps = false; else - tux.base.height = 32; + debug_fps = true; + break; + default: + break; } - break; - case SDLK_END: - if(debug_mode) - player_status.distros += 50; - break; - case SDLK_DELETE: - if(debug_mode) - tux.got_coffee = 1; - break; - case SDLK_INSERT: - if(debug_mode) - tux.invincible_timer.start(TUX_INVINCIBLE_TIME); - break; - case SDLK_l: - if(debug_mode) - --player_status.lives; - break; - case SDLK_s: - if(debug_mode) - player_status.score += 1000; - case SDLK_f: - if(debug_fps) - debug_fps = false; - else - debug_fps = true; - break; - default: - break; } - } - break; + break; - case SDL_JOYAXISMOTION: - switch(event.jaxis.axis) - { - case JOY_X: - if (event.jaxis.value < -JOYSTICK_DEAD_ZONE) - { - tux.input.left = DOWN; - tux.input.right = UP; - } - else if (event.jaxis.value > JOYSTICK_DEAD_ZONE) - { - tux.input.left = UP; - tux.input.right = DOWN; - } - else + case SDL_JOYAXISMOTION: + switch(event.jaxis.axis) { - tux.input.left = DOWN; - tux.input.right = DOWN; + case JOY_X: + if (event.jaxis.value < -JOYSTICK_DEAD_ZONE) + { + tux.input.left = DOWN; + tux.input.right = UP; + } + else if (event.jaxis.value > JOYSTICK_DEAD_ZONE) + { + tux.input.left = UP; + tux.input.right = DOWN; + } + else + { + tux.input.left = DOWN; + tux.input.right = DOWN; + } + break; + case JOY_Y: + if (event.jaxis.value > JOYSTICK_DEAD_ZONE) + tux.input.down = DOWN; + else if (event.jaxis.value < -JOYSTICK_DEAD_ZONE) + tux.input.down = UP; + else + tux.input.down = UP; + + break; + default: + break; } break; - case JOY_Y: - if (event.jaxis.value > JOYSTICK_DEAD_ZONE) - tux.input.down = DOWN; - else if (event.jaxis.value < -JOYSTICK_DEAD_ZONE) - tux.input.down = UP; - else - tux.input.down = UP; - - break; - default: + case SDL_JOYBUTTONDOWN: + if (event.jbutton.button == JOY_A) + tux.input.up = DOWN; + else if (event.jbutton.button == JOY_B) + tux.input.fire = DOWN; + else if (event.jbutton.button == JOY_START) + on_escape_press(); + break; + case SDL_JOYBUTTONUP: + if (event.jbutton.button == JOY_A) + tux.input.up = UP; + else if (event.jbutton.button == JOY_B) + tux.input.fire = UP; break; - } - break; - case SDL_JOYBUTTONDOWN: - if (event.jbutton.button == JOY_A) - tux.input.up = DOWN; - else if (event.jbutton.button == JOY_B) - tux.input.fire = DOWN; - else if (event.jbutton.button == JOY_START) - on_escape_press(); - break; - case SDL_JOYBUTTONUP: - if (event.jbutton.button == JOY_A) - tux.input.up = UP; - else if (event.jbutton.button == JOY_B) - tux.input.fire = UP; - break; - - default: - break; - - } /* switch */ + default: + break; + } /* switch */ + } } /* while */ } @@ -331,14 +325,6 @@ GameSession::check_end_conditions() { // No more lives!? if(st_gl_mode != ST_GL_TEST) drawendscreen(); - - if(st_gl_mode != ST_GL_TEST) - { - // FIXME: highscore soving doesn't make sense in its - // current form - //if (player_status.score > hs_score) - //save_hs(player_status.score); - } exit_status = GAME_OVER; } @@ -572,13 +558,7 @@ GameSession::drawstatus() white_text->draw("SCORE", 0, 0, 1); gold_text->draw(str, 96, 0, 1); - if(st_gl_mode != ST_GL_TEST) - { - sprintf(str, "%d", hs_score); - white_text->draw("HIGH", 0, 20, 1); - gold_text->draw(str, 96, 20, 1); - } - else + if(st_gl_mode == ST_GL_TEST) { white_text->draw("Press ESC To Return",0,20,1); } diff --git a/src/menu.cpp b/src/menu.cpp index 6c9fe4eca..044d94a23 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -1,20 +1,28 @@ -/* - menu.c - - Super Tux - Menu - - by Tobias Glaesser - tobi.web@gmx.de - http://www.newbreedsoftware.com/supertux/ - - December 20, 2003 - March 15, 2004 -*/ +// $Id$ +// +// SuperTux +// Copyright (C) 2004 Tobias Glaesser +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef WIN32 #include #include #endif +#include #include #include #include @@ -47,6 +55,7 @@ Menu* save_game_menu = 0; Menu* contrib_menu = 0; Menu* contrib_subset_menu = 0; +std::stack Menu::last_menus; Menu* Menu::current_ = 0; void @@ -54,7 +63,9 @@ Menu::set_current(Menu* menu) { if (menu) { - menu->last_menu = current_; + if (last_menus.empty() || menu != last_menus.top()) + last_menus.push(current_); + menu->effect.start(500); } @@ -136,10 +147,8 @@ Menu::Menu() pos_x = screen->w/2; pos_y = screen->h/2; has_backitem = false; - last_menu = 0; arrange_left = 0; active_item = 0; - last_menu = 0; effect.init(false); } @@ -242,8 +251,11 @@ Menu::action() break; case MN_BACK: - if(last_menu != NULL) - Menu::set_current(last_menu); + if (!last_menus.empty()) + { + Menu::set_current(last_menus.top()); + last_menus.pop(); + } break; default: break; @@ -581,8 +593,11 @@ Menu::event(SDL_Event& event) case SDLK_ESCAPE: if(Menu::current()) { - if (has_backitem == true && last_menu != NULL) - Menu::set_current(last_menu); + if (has_backitem == true && !last_menus.empty()) + { + Menu::set_current(last_menus.top()); + last_menus.pop(); + } else Menu::set_current(0); } diff --git a/src/menu.h b/src/menu.h index 8a7ef5534..8c30c4400 100644 --- a/src/menu.h +++ b/src/menu.h @@ -1,20 +1,28 @@ -/* - menu.h - - Super Tux - Menu - - by Tobias Glaesser - tobi.web@gmx.de - http://www.newbreedsoftware.com/supertux/ - - December 20, 2003 -*/ +// $Id$ +// +// SuperTux +// Copyright (C) 2004 Tobias Glaesser +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef SUPERTUX_MENU_H #define SUPERTUX_MENU_H #include #include +#include #include "texture.h" #include "timer.h" #include "type.h" @@ -55,6 +63,16 @@ public: class Menu { +private: + static std::stack last_menus; + static Menu* current_; +public: + /** Set the current menu, if pmenu is NULL, hide the current menu */ + static void set_current(Menu* pmenu); + + /** Return the current active menu or NULL if none is active */ + static Menu* current() { return current_; } + private: /* Action done on the menu */ enum MenuAction { @@ -73,27 +91,22 @@ private: int pos_y; bool has_backitem; - /** input event for the menu */ + /** input event for the menu (up, down, left, right, etc.) */ MenuAction menuaction; /* input implementation variables */ int delete_character; char mn_input_char; - Menu* last_menu; int width(); int height(); - - static Menu* current_; + public: Timer effect; int arrange_left; int active_item; - std::vector item; - /** Set the current menu, if pmenu is NULL, hide the current menu */ - static void set_current(Menu* pmenu); - static Menu* current() { return current_; } + std::vector item; Menu(); ~Menu(); @@ -105,8 +118,11 @@ public: /** Remove all entries from the menu */ void clear(); - /** Check, if the value of the active menu item has changed. */ + /** Check, if the value of the active menu item has changed. FIXME: + Somebody should document the exact meaning of this function a + bit more */ int check (); + void draw (); void draw_item(int index, int menu_width, int menu_height); void set_pos(int x, int y, float rw = 0, float rh = 0); diff --git a/src/supertux.cpp b/src/supertux.cpp index 028d48227..0e9c7ce1a 100644 --- a/src/supertux.cpp +++ b/src/supertux.cpp @@ -27,8 +27,6 @@ int main(int argc, char * argv[]) { - bool done; - st_directory_setup(); parseargs(argc, argv); @@ -51,11 +49,7 @@ int main(int argc, char * argv[]) } else { - done = false; - while (!done) - { - done = title(); - } + title(); } clearscreen(0, 0, 0); diff --git a/src/title.cpp b/src/title.cpp index 7a3581513..eaebd1248 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -49,8 +49,6 @@ static Surface* img_choose_subset; static bool walking; static Timer random_timer; -static SDL_Event event; -static SDLKey key; static int frame, i; static unsigned int last_update_time; static unsigned int update_time; @@ -217,7 +215,7 @@ void draw_demo(GameSession* session, double frame_ratio) } /* --- TITLE SCREEN --- */ -bool title(void) +void title(void) { st_subset subset; random_timer.init(true); @@ -232,24 +230,21 @@ bool title(void) updatescreen(); /* Load images: */ - bkg_title = new Surface(datadir + "/images/title/background.jpg", IGNORE_ALPHA); logo = new Surface(datadir + "/images/title/logo.png", USE_ALPHA); img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", USE_ALPHA); /* --- Main title loop: --- */ - bool done = 0; frame = 0; /* Draw the title background: */ bkg_title->draw_bg(); - load_hs(); update_time = st_get_ticks(); random_timer.start(rand() % 2000 + 2000); Menu::set_current(main_menu); - while (!done) + while (Menu::current()) { // Calculate the movement-factor double frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE); @@ -258,29 +253,18 @@ bool title(void) /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */ frame_ratio /= 2; - /* Handle events: */ - + SDL_Event event; while (SDL_PollEvent(&event)) { - if (event.type == SDL_QUIT) + if (Menu::current()) { - done = true; + Menu::current()->event(event); } - else if (event.type == SDL_KEYDOWN) + else { - /* Keypress... */ - key = event.key.keysym.sym; - - if (Menu::current()) - { - Menu::current()->event(event); - } - - if (!Menu::current()) - { - /* Escape: Quit: */ - done = true; - } + // FIXME: QUIT signal should be handled more generic, not locally + if (event.type == SDL_QUIT) + Menu::set_current(0); } } @@ -299,7 +283,7 @@ bool title(void) 0, 420, 0); /* Don't draw menu, if quit is true */ - if(!done) + if(Menu::current()) { Menu::current()->action(); Menu::current()->draw(); @@ -318,15 +302,15 @@ bool title(void) update_contrib_menu(); break; case 3: - done = true; - done = leveleditor(1); + leveleditor(1); Menu::set_current(main_menu); break; case 4: display_credits(); + Menu::set_current(main_menu); break; case 5: - done = true; + Menu::set_current(0); break; } } @@ -371,9 +355,6 @@ bool title(void) delete bkg_title; delete logo; - - /* Return to main! */ - return done; } #define MAX_VEL 10 @@ -427,6 +408,7 @@ void display_credits() while(done == 0) { /* in case of input, exit */ + SDL_Event event; while(SDL_PollEvent(&event)) switch(event.type) { diff --git a/src/title.h b/src/title.h index 280e75cdf..5f02938bf 100644 --- a/src/title.h +++ b/src/title.h @@ -10,4 +10,4 @@ April 11, 2000 - March 15, 2004 */ -bool title(void); +void title(void); diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 424b62778..a89b0286c 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -364,8 +364,7 @@ WorldMap::load_map() void WorldMap::on_escape_press() { - std::cout << "on escape press" << std::endl; - + // Show or hide the menu if(!Menu::current()) Menu::set_current(worldmap_menu); else