Added GameManager class to keep track of the World objects, should fix the crashing...
[supertux.git] / src / supertux / main.cpp
index e39e24b..eee10dc 100644 (file)
@@ -14,6 +14,8 @@
 //  You should have received a copy of the GNU General Public License
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+#include "supertux/main.hpp"
+
 #include <config.h>
 #include <version.h>
 
@@ -28,26 +30,27 @@ extern "C" {
 #include <findlocale.h>
 }
 
-#include "video/renderer.hpp"
-#include "supertux/main.hpp"
-
 #include "addon/addon_manager.hpp"
 #include "audio/sound_manager.hpp"
-#include "control/joystickkeyboardcontroller.hpp"
+#include "control/input_manager.hpp"
 #include "math/random_generator.hpp"
 #include "physfs/ifile_stream.hpp"
-#include "physfs/physfs_sdl.hpp"
 #include "physfs/physfs_file_system.hpp"
+#include "physfs/physfs_sdl.hpp"
 #include "scripting/squirrel_util.hpp"
+#include "supertux/game_manager.hpp"
 #include "supertux/gameconfig.hpp"
 #include "supertux/globals.hpp"
 #include "supertux/player_status.hpp"
-#include "supertux/screen_manager.hpp"
 #include "supertux/resources.hpp"
+#include "supertux/screen_fade.hpp"
+#include "supertux/screen_manager.hpp"
 #include "supertux/title_screen.hpp"
 #include "util/file_system.hpp"
 #include "util/gettext.hpp"
 #include "video/drawing_context.hpp"
+#include "video/lightmap.hpp"
+#include "video/renderer.hpp"
 #include "worldmap/worldmap.hpp"
 
 namespace { DrawingContext *context_pointer; }
@@ -390,24 +393,13 @@ Main::parse_commandline(int argc, char** argv)
 void
 Main::init_sdl()
 {
-  if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
+  if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0) {
     std::stringstream msg;
     msg << "Couldn't initialize SDL: " << SDL_GetError();
     throw std::runtime_error(msg.str());
   }
   // just to be sure
   atexit(SDL_Quit);
-
- // SDL_EnableUNICODE(1); //old code, mofif by giby 
- //   SDL_JoystickID myID = SDL_JoystickInstanceID(myOpenedStick);
-  
-
-  // wait 100ms and clear SDL event queue because sometimes we have random
-  // joystick events in the queue on startup...
-  SDL_Delay(100);
-  SDL_Event dummy;
-  while(SDL_PollEvent(&dummy))
-    ;
 }
 
 void
@@ -437,7 +429,7 @@ Main::init_video()
     SDL_SetWindowIcon(Renderer::instance()->get_window(), icon);
     SDL_FreeSurface(icon);
   }
-  //SDL_ShowCursor(0);
+  SDL_ShowCursor(0);
 
   log_info << (g_config->use_fullscreen?"fullscreen ":"window ")
            << " Window: "     << g_config->window_size
@@ -463,47 +455,6 @@ Main::quit_audio()
   }
 }
 
-void
-Main::wait_for_event(float min_delay, float max_delay)
-{
-  assert(min_delay <= max_delay);
-
-  Uint32 min = (Uint32) (min_delay * 1000);
-  Uint32 max = (Uint32) (max_delay * 1000);
-
-  Uint32 ticks = SDL_GetTicks();
-  while(SDL_GetTicks() - ticks < min) {
-    SDL_Delay(10);
-    sound_manager->update();
-  }
-
-  // clear event queue
-  SDL_Event event;
-  while (SDL_PollEvent(&event))
-  {}
-
-  /* Handle events: */
-  bool running = false;
-  ticks = SDL_GetTicks();
-  while(running) {
-    while(SDL_PollEvent(&event)) {
-      switch(event.type) {
-        case SDL_QUIT:
-          g_screen_manager->quit();
-          break;
-        case SDL_KEYDOWN:
-        case SDL_JOYBUTTONDOWN:
-        case SDL_MOUSEBUTTONDOWN:
-          running = false;
-      }
-    }
-    if(SDL_GetTicks() - ticks >= (max - min))
-      running = false;
-    sound_manager->update();
-    SDL_Delay(10);
-  }
-}
-
 static Uint32 last_timelog_ticks = 0;
 static const char* last_timelog_component = 0;
 
@@ -536,7 +487,7 @@ Main::run(int argc, char** argv)
     Console::instance = new Console();
 
     timelog("controller");
-    g_jk_controller = new JoystickKeyboardController();
+    g_input_manager = new InputManager();
 
     timelog("config");
     init_config();
@@ -573,6 +524,7 @@ Main::run(int argc, char** argv)
 
     const std::unique_ptr<PlayerStatus> default_playerstatus(new PlayerStatus());
 
+    GameManager game_manager;
     g_screen_manager = new ScreenManager();
 
     init_rand();
@@ -591,8 +543,9 @@ Main::run(int argc, char** argv)
 
       if(g_config->start_level.size() > 4 &&
          g_config->start_level.compare(g_config->start_level.size() - 5, 5, ".stwm") == 0) {
-        g_screen_manager->push_screen(new worldmap::WorldMap(
-                                 FileSystem::basename(g_config->start_level), default_playerstatus.get()));
+        g_screen_manager->push_screen(std::unique_ptr<Screen>(
+                                        new worldmap::WorldMap(
+                                          FileSystem::basename(g_config->start_level), default_playerstatus.get())));
       } else {
         std::unique_ptr<GameSession> session (
           new GameSession(FileSystem::basename(g_config->start_level), default_playerstatus.get()));
@@ -605,10 +558,10 @@ Main::run(int argc, char** argv)
 
         if(g_config->record_demo != "")
           session->record_demo(g_config->record_demo);
-        g_screen_manager->push_screen(session.release());
+        g_screen_manager->push_screen(std::move(session));
       }
     } else {
-      g_screen_manager->push_screen(new TitleScreen(default_playerstatus.get()));
+      g_screen_manager->push_screen(std::unique_ptr<Screen>(new TitleScreen(default_playerstatus.get())));
     }
 
     g_screen_manager->run(context);
@@ -630,8 +583,8 @@ Main::run(int argc, char** argv)
     g_config->save();
   delete g_config;
   g_config = NULL;
-  delete g_jk_controller;
-  g_jk_controller = NULL;
+  delete g_input_manager;
+  g_input_manager = NULL;
   delete Console::instance;
   Console::instance = NULL;
   scripting::exit_squirrel();