Added a "graceful exit" exception to be called when the user asks to prematurely...
authorOndřej Hošek <ondra.hosek@gmail.com>
Tue, 25 Oct 2005 15:03:33 +0000 (15:03 +0000)
committerOndřej Hošek <ondra.hosek@gmail.com>
Tue, 25 Oct 2005 15:03:33 +0000 (15:03 +0000)
SVN-Revision: 2915

src/exceptions.hpp [new file with mode: 0644]
src/game_session.cpp
src/gui/menu.cpp
src/main.cpp
src/textscroller.cpp
src/title.cpp
src/worldmap.cpp

diff --git a/src/exceptions.hpp b/src/exceptions.hpp
new file mode 100644 (file)
index 0000000..5fa82a5
--- /dev/null
@@ -0,0 +1,33 @@
+//  $Id$
+// 
+//  SuperTux
+//  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
+//
+//  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 <exception>
+
+/** Throw this exception to gracefully shut SuperTux down.
+ */
+class graceful_shutdown : public std::exception
+{
+public:
+  explicit graceful_shutdown() {};
+};
+
+#endif
index 0c3211e..247991e 100644 (file)
@@ -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)
index 5993bf4..94564e5 100644 (file)
@@ -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);
       }
index caf7812..f89805d 100644 (file)
@@ -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;
index 0c44062..176ecab 100644 (file)
@@ -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)) {
index 97efc08..66ffb3c 100644 (file)
@@ -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: */
index 8649852..ae846fd 100644 (file)
@@ -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();
   }
 }