Made compiles on MacOS X a bit smoother, activated MacOS-specific code for CMake...
[supertux.git] / src / main.cpp
index c3b6805..8144ecd 100644 (file)
 #include <SDL.h>
 #include <SDL_image.h>
 
+#ifdef MACOSX
+namespace supertux_apple {
+#include <CoreFoundation/CoreFoundation.h>
+}
+#endif
+
 #include "gameconfig.hpp"
 #include "resources.hpp"
 #include "gettext.hpp"
@@ -55,7 +61,7 @@
 #include "worldmap/worldmap.hpp"
 #include "binreloc/binreloc.h"
 
-DrawingContext context;
+namespace { DrawingContext *context_pointer; }
 SDL_Surface *screen;
 JoystickKeyboardController* main_controller = 0;
 TinyGetText::DictionaryManager dictionary_manager;
@@ -164,9 +170,22 @@ static void init_physfs(const char* argv0)
   }
 
 #ifdef MACOSX
+{
+  using namespace supertux_apple;
+
   // when started from Application file on Mac OS X...
-  dir = PHYSFS_getBaseDir();
-  dir += "SuperTux.app/Contents/Resources/data";
+  char path[PATH_MAX];
+  CFBundleRef mainBundle = CFBundleGetMainBundle();
+  assert(mainBundle != 0);
+  CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
+  assert(mainBundleURL != 0);
+  CFStringRef pathStr = CFURLCopyFileSystemPath(mainBundleURL, kCFURLPOSIXPathStyle);
+  assert(pathStr != 0);
+  CFStringGetCString(pathStr, path, PATH_MAX, kCFStringEncodingUTF8);
+  CFRelease(mainBundleURL);
+  CFRelease(pathStr);
+
+  dir = std::string(path) + "/Contents/Resources/data";
   testfname = dir + "/credits.txt";
   sourcedir = false;
   f = fopen(testfname.c_str(), "r");
@@ -178,6 +197,7 @@ static void init_physfs(const char* argv0)
       sourcedir = true;
     }
   }
+}
 #endif
 
 #ifdef _WIN32
@@ -193,12 +213,13 @@ static void init_physfs(const char* argv0)
     br_init (NULL);
     dir = br_find_data_dir(APPDATADIR);
     datadir = dir;
-    datadir += "/" PACKAGE_NAME;
     free(dir);
 
 #else
     datadir = APPDATADIR;
 #endif
+    datadir += "/";
+    datadir += application;
     if(!PHYSFS_addToSearchPath(datadir.c_str(), 1)) {
       log_warning << "Couldn't add '" << datadir << "' to physfs searchpath: " << PHYSFS_getLastError() << std::endl;
     }
@@ -376,26 +397,6 @@ void init_video()
   }
 #endif
 
-  context.init_renderer();
-  screen = SDL_GetVideoSurface();
-
-  SDL_WM_SetCaption(PACKAGE_NAME " " PACKAGE_VERSION, 0);
-
-  // set icon
-  SDL_Surface* icon = IMG_Load_RW(
-      get_physfs_SDLRWops("images/engine/icons/supertux.xpm"), true);
-  if(icon != 0) {
-    SDL_WM_SetIcon(icon, 0);
-    SDL_FreeSurface(icon);
-  }
-#ifdef DEBUG
-  else {
-    log_warning << "Couldn't find icon 'images/engine/icons/supertux.xpm'" << std::endl;
-  }
-#endif
-
-  SDL_ShowCursor(0);
-
   double aspect_ratio = config->aspect_ratio;
 
   // try to guess aspect ratio of monitor if needed
@@ -416,6 +417,26 @@ void init_video()
     SCREEN_HEIGHT = static_cast<int> (600 * 1/aspect_ratio + 0.5);
   }
 
+  context_pointer->init_renderer();
+  screen = SDL_GetVideoSurface();
+
+  SDL_WM_SetCaption(PACKAGE_NAME " " PACKAGE_VERSION, 0);
+
+  // set icon
+  SDL_Surface* icon = IMG_Load_RW(
+      get_physfs_SDLRWops("images/engine/icons/supertux.xpm"), true);
+  if(icon != 0) {
+    SDL_WM_SetIcon(icon, 0);
+    SDL_FreeSurface(icon);
+  }
+#ifdef DEBUG
+  else {
+    log_warning << "Couldn't find icon 'images/engine/icons/supertux.xpm'" << std::endl;
+  }
+#endif
+
+  SDL_ShowCursor(0);
+
   log_info << (config->use_fullscreen?"fullscreen ":"window ") << SCREEN_WIDTH << "x" << SCREEN_HEIGHT << " Ratio: " << aspect_ratio << "\n";
 }
 
@@ -523,6 +544,8 @@ int main(int argc, char** argv)
     timelog("audio");
     init_audio();
     timelog("video");
+    DrawingContext context;
+    context_pointer = &context;
     init_video();
     Console::instance->init_graphics();
     timelog("scripting");
@@ -579,7 +602,6 @@ int main(int argc, char** argv)
   delete main_loop;
   main_loop = NULL;
 
-  free_options_menu();
   unload_shared();
   quit_audio();