From: Ondřej Hošek Date: Tue, 25 Oct 2005 15:03:33 +0000 (+0000) Subject: Added a "graceful exit" exception to be called when the user asks to prematurely... X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=ba9907babbb17fadee52f1653f7ad6adf362c6fd;p=supertux.git Added a "graceful exit" exception to be called when the user asks to prematurely close down the game. Currently thrown at SDL_QUIT, i.e. when the user closes the window. SVN-Revision: 2915 --- diff --git a/src/exceptions.hpp b/src/exceptions.hpp new file mode 100644 index 000000000..5fa82a5fd --- /dev/null +++ b/src/exceptions.hpp @@ -0,0 +1,33 @@ +// $Id$ +// +// SuperTux +// Copyright (C) 2005 Matthias Braun +// +// 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 __EXCEPTIONS_H__ +#define __EXCEPTIONS_H__ + +#include + +/** Throw this exception to gracefully shut SuperTux down. + */ +class graceful_shutdown : public std::exception +{ +public: + explicit graceful_shutdown() {}; +}; + +#endif diff --git a/src/game_session.cpp b/src/game_session.cpp index 0c3211e6f..247991e33 100644 --- a/src/game_session.cpp +++ b/src/game_session.cpp @@ -63,6 +63,7 @@ #include "file_system.hpp" #include "gameconfig.hpp" #include "gettext.hpp" +#include "exceptions.hpp" // the engine will be run with a logical framerate of 64fps. // We chose 64fps here because it is a power of 2, so 1/64 gives an "even" @@ -270,7 +271,7 @@ GameSession::process_events() Menu::current()->event(event); main_controller->process_event(event); if(event.type == SDL_QUIT) - throw std::runtime_error("Received window close"); + throw graceful_shutdown(); } // playback a demo? @@ -674,7 +675,7 @@ GameSession::display_info_box(const std::string& text) while (SDL_PollEvent(&event)) { main_controller->process_event(event); if(event.type == SDL_QUIT) - throw std::runtime_error("Received window close event"); + throw graceful_shutdown(); } if(main_controller->pressed(Controller::JUMP) diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index 5993bf4dd..94564e5d9 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -37,6 +37,7 @@ #include "main.hpp" #include "resources.hpp" #include "control/joystickkeyboardcontroller.hpp" +#include "exceptions.hpp" static const int MENU_REPEAT_INITIAL = 400; static const int MENU_REPEAT_RATE = 200; @@ -78,7 +79,7 @@ bool confirm_dialog(Surface *background, std::string text) SDL_Event event; while (SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) - throw std::runtime_error("received window close event"); + throw graceful_shutdown(); main_controller->process_event(event); dialog->event(event); } diff --git a/src/main.cpp b/src/main.cpp index caf7812f7..f89805ddc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,6 +50,7 @@ #include "game_session.hpp" #include "file_system.hpp" #include "physfs/physfs_sdl.hpp" +#include "exceptions.hpp" SDL_Surface* screen = 0; JoystickKeyboardController* main_controller = 0; @@ -229,10 +230,10 @@ static void parse_commandline(int argc, char** argv) config->record_demo = argv[++i]; } else if(arg == "--help") { print_usage(argv[0]); - throw std::runtime_error(""); + throw graceful_shutdown(); } else if(arg == "--version") { std::cerr << PACKAGE_NAME << " " << PACKAGE_VERSION << "\n"; - throw std::runtime_error(""); + throw graceful_shutdown(); } else if(arg[0] != '-') { config->start_level = arg; } else { @@ -405,7 +406,7 @@ void wait_for_event(float min_delay, float max_delay) while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_QUIT: - throw std::runtime_error("received window close"); + throw graceful_shutdown(); case SDL_KEYDOWN: case SDL_JOYBUTTONDOWN: case SDL_MOUSEBUTTONDOWN: @@ -449,7 +450,8 @@ int main(int argc, char** argv) } else { // normal game title(); - } + } + } catch(graceful_shutdown& e) { } catch(std::exception& e) { std::cerr << "Unexpected exception: " << e.what() << std::endl; return 1; diff --git a/src/textscroller.cpp b/src/textscroller.cpp index 0c4406218..176ecab6f 100644 --- a/src/textscroller.cpp +++ b/src/textscroller.cpp @@ -30,6 +30,7 @@ #include "audio/sound_manager.hpp" #include "main.hpp" #include "control/joystickkeyboardcontroller.hpp" +#include "exceptions.hpp" static const float DEFAULT_SPEED = .02; static const float SCROLL = 60; @@ -120,7 +121,7 @@ void display_text_file(const std::string& filename) while(SDL_PollEvent(&event)) { main_controller->process_event(event); if(event.type == SDL_QUIT) - throw std::runtime_error("received window close"); + throw graceful_shutdown(); } if(main_controller->hold(Controller::UP)) { diff --git a/src/title.cpp b/src/title.cpp index 97efc08bd..66ffb3c27 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -59,6 +59,7 @@ #include "control/joystickkeyboardcontroller.hpp" #include "control/codecontroller.hpp" #include "main.hpp" +#include "exceptions.hpp" static Surface* bkg_title; static Surface* logo; @@ -328,7 +329,7 @@ void title() } main_controller->process_event(event); if (event.type == SDL_QUIT) - throw std::runtime_error("Received window close"); + throw graceful_shutdown(); } /* Draw the background: */ diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 86498528c..ae846fdc7 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -52,6 +52,7 @@ #include "object/background.hpp" #include "object/tilemap.hpp" #include "scripting/script_interpreter.hpp" +#include "exceptions.hpp" Menu* worldmap_menu = 0; @@ -595,7 +596,7 @@ WorldMap::get_input() Menu::current()->event(event); main_controller->process_event(event); if(event.type == SDL_QUIT) - throw std::runtime_error("Received window close"); + throw graceful_shutdown(); } }