supertux/main, control/haptic_manager: Add SDL 1.2 compatibility code.
[supertux.git] / src / supertux / main.cpp
index 0f6e414..1a05161 100644 (file)
@@ -17,6 +17,7 @@
 #include <config.h>
 #include <version.h>
 
+#include <SDL.h>
 #include <SDL_image.h>
 #include <physfs.h>
 #include <iostream>
@@ -34,6 +35,7 @@ namespace supertux_apple {
 #include "addon/addon_manager.hpp"
 #include "audio/sound_manager.hpp"
 #include "control/joystickkeyboardcontroller.hpp"
+#include "control/haptic_manager.hpp"
 #include "math/random_generator.hpp"
 #include "physfs/ifile_stream.hpp"
 #include "physfs/physfs_sdl.hpp"
@@ -41,6 +43,7 @@ namespace supertux_apple {
 #include "scripting/squirrel_util.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/title_screen.hpp"
@@ -92,17 +95,40 @@ Main::init_physfs(const char* argv0)
   PHYSFS_permitSymbolicLinks(1);
 
   // Initialize physfs (this is a slightly modified version of
-  // PHYSFS_setSaneConfig
-  const char* application = "supertux2"; //instead of PACKAGE_NAME so we can coexist with MS1
+  // PHYSFS_setSaneConfig)
+  const char* application = PACKAGE_NAME;
   const char* userdir = PHYSFS_getUserDir();
-  char* writedir = new char[strlen(userdir) + strlen(application) + 2];
+
+  char* writedir = new char[strlen(userdir) + strlen(application) + 
+#ifndef _WIN32
+                                                                    2];
+#else
+                                                                    1];
+#endif
 
   // Set configuration directory
-  sprintf(writedir, "%s.%s", userdir, application);
+  sprintf(writedir, 
+#ifndef _WIN32
+                    "%s.%s",
+#else
+                    "%s%s",
+#endif
+                             userdir, application);
   if(!PHYSFS_setWriteDir(writedir)) {
     // try to create the directory
-    char* mkdir = new char[strlen(application) + 2];
-    sprintf(mkdir, ".%s", application);
+    char* mkdir = new char[strlen(application) +
+#ifndef _WIN32
+                                                 2];
+#else
+                                                 1];
+#endif
+    sprintf(mkdir,
+#ifndef _WIN32
+                   ".%s",
+#else
+                   "%s",
+#endif
+                          application);
     if(!PHYSFS_setWriteDir(userdir) || !PHYSFS_mkdir(mkdir)) {
       std::ostringstream msg;
       msg << "Failed creating configuration directory '"
@@ -176,23 +202,21 @@ Main::init_physfs(const char* argv0)
 #endif
 
   if(!sourcedir) {
-#if defined(APPDATADIR) || defined(ENABLE_BINRELOC)
-    std::string datadir;
+    std::string datadir = PHYSFS_getBaseDir();
+    datadir = datadir.substr(0, datadir.rfind(INSTALL_SUBDIR_BIN));
+    datadir += "/" INSTALL_SUBDIR_SHARE;
 #ifdef ENABLE_BINRELOC
 
     char* dir;
     br_init (NULL);
-    dir = br_find_data_dir(APPDATADIR);
+    dir = br_find_data_dir(datadir.c_str());
     datadir = dir;
     free(dir);
 
-#else
-    datadir = APPDATADIR;
 #endif
     if(!PHYSFS_addToSearchPath(datadir.c_str(), 1)) {
       log_warning << "Couldn't add '" << datadir << "' to physfs searchpath: " << PHYSFS_getLastError() << std::endl;
     }
-#endif
   }
 
   //show search Path
@@ -335,7 +359,7 @@ Main::parse_commandline(int argc, char** argv)
       } 
       else 
       {
-        g_config->video = get_video_system(argv[i]);
+        g_config->video = VideoSystem::get_video_system(argv[i]);
       }
     } else if(arg == "--show-fps") {
       g_config->show_fps = true;
@@ -377,7 +401,14 @@ Main::parse_commandline(int argc, char** argv)
 void
 Main::init_sdl()
 {
-  if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
+  int init_flags;
+
+  init_flags = SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK;
+#ifdef SDL_INIT_HAPTIC
+  init_flags |= SDL_INIT_HAPTIC;
+#endif
+
+  if(SDL_Init(init_flags) < 0) {
     std::stringstream msg;
     msg << "Couldn't initialize SDL: " << SDL_GetError();
     throw std::runtime_error(msg.str());
@@ -433,11 +464,9 @@ Main::init_video()
     SDL_WM_SetIcon(icon, 0);
     SDL_FreeSurface(icon);
   }
-#ifndef NDEBUG
   else {
     log_warning << "Couldn't load icon '" << icon_fname << "'" << std::endl;
   }
-#endif
 
   SDL_ShowCursor(0);
 
@@ -506,7 +535,6 @@ Main::wait_for_event(float min_delay, float max_delay)
   }
 }
 
-#ifndef NDEBUG
 static Uint32 last_timelog_ticks = 0;
 static const char* last_timelog_component = 0;
 
@@ -521,14 +549,9 @@ static inline void timelog(const char* component)
   last_timelog_ticks = current_ticks;
   last_timelog_component = component;
 }
-#else
-static inline void timelog(const char* )
-{
-}
-#endif
 
 int
-Main::main(int argc, char** argv)
+Main::run(int argc, char** argv)
 {
   int result = 0;
 
@@ -541,6 +564,9 @@ Main::main(int argc, char** argv)
     init_physfs(argv[0]);
     init_sdl();
 
+    timelog("haptic");
+    g_haptic_manager = new HapticManager();
+
     timelog("controller");
     g_main_controller = new JoystickKeyboardController();
 
@@ -575,7 +601,12 @@ Main::main(int argc, char** argv)
 
     timelog(0);
 
+    const std::auto_ptr<PlayerStatus> default_playerstatus(new PlayerStatus());
+
     g_screen_manager = new ScreenManager();
+
+    init_rand();
+
     if(g_config->start_level != "") {
       // we have a normal path specified at commandline, not a physfs path.
       // So we simply mount that path here...
@@ -585,14 +616,11 @@ Main::main(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) {
-        init_rand();
         g_screen_manager->push_screen(new worldmap::WorldMap(
-                                 FileSystem::basename(g_config->start_level)));
+                                 FileSystem::basename(g_config->start_level), default_playerstatus.get()));
       } else {
-        init_rand();//If level uses random eg. for
-        // rain particles before we do this:
         std::auto_ptr<GameSession> session (
-          new GameSession(FileSystem::basename(g_config->start_level)));
+          new GameSession(FileSystem::basename(g_config->start_level), default_playerstatus.get()));
 
         g_config->random_seed =session->get_demo_random_seed(g_config->start_demo);
         init_rand();//initialise generator with seed from session
@@ -605,11 +633,9 @@ Main::main(int argc, char** argv)
         g_screen_manager->push_screen(session.release());
       }
     } else {
-      init_rand();
-      g_screen_manager->push_screen(new TitleScreen());
+      g_screen_manager->push_screen(new TitleScreen(default_playerstatus.get()));
     }
 
-    //init_rand(); PAK: this call might subsume the above 3, but I'm chicken!
     g_screen_manager->run(context);
   } catch(std::exception& e) {
     log_fatal << "Unexpected exception: " << e.what() << std::endl;