Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[supertux.git] / src / main.cpp
index 8874b00..71aa862 100644 (file)
@@ -1,7 +1,7 @@
 //  $Id$
-// 
+//
 //  SuperTux
-//  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
+//  Copyright (C) 2006 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
@@ -12,7 +12,7 @@
 //  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
@@ -20,7 +20,7 @@
 #include <config.h>
 #include <assert.h>
 
-#include "msg.hpp"
+#include "log.hpp"
 #include "main.hpp"
 
 #include <stdexcept>
@@ -35,7 +35,7 @@
 #include <physfs.h>
 #include <SDL.h>
 #include <SDL_image.h>
-#include <SDL_opengl.h>
+#include <GL/gl.h>
 
 #include "gameconfig.hpp"
 #include "resources.hpp"
@@ -65,7 +65,7 @@ static void init_config()
   try {
     config->load();
   } catch(std::exception& e) {
-    msg_info << "Couldn't load config file: " << e.what() << ", using default settings" << std::endl;
+    log_info << "Couldn't load config file: " << e.what() << ", using default settings" << std::endl;
   }
 }
 
@@ -148,11 +148,28 @@ static void init_physfs(const char* argv0)
   if(f) {
     fclose(f);
     if(!PHYSFS_addToSearchPath(dir.c_str(), 1)) {
+      log_warning << "Couldn't add '" << dir << "' to physfs searchpath: " << PHYSFS_getLastError() << std::endl;
+    } else {
+      sourcedir = true;
+    }
+  }
+
+#ifdef MACOSX
+  // when started from Application file on Mac OS X...
+  dir = PHYSFS_getBaseDir();
+  dir += "SuperTux.app/Contents/Resources/data";
+  testfname = dir + "/credits.txt";
+  sourcedir = false;
+  f = fopen(testfname.c_str(), "r");
+  if(f) {
+    fclose(f);
+    if(!PHYSFS_addToSearchPath(dir.c_str(), 1)) {
       msg_warning << "Couldn't add '" << dir << "' to physfs searchpath: " << PHYSFS_getLastError() << std::endl;
     } else {
       sourcedir = true;
     }
   }
+#endif
 
   if(!sourcedir) {
 #if defined(APPDATADIR) || defined(ENABLE_BINRELOC)
@@ -165,7 +182,7 @@ static void init_physfs(const char* argv0)
     datadir = APPDATADIR;
 #endif
     if(!PHYSFS_addToSearchPath(datadir.c_str(), 1)) {
-      msg_warning << "Couldn't add '" << datadir << "' to physfs searchpath: " << PHYSFS_getLastError() << std::endl;
+      log_warning << "Couldn't add '" << datadir << "' to physfs searchpath: " << PHYSFS_getLastError() << std::endl;
     }
 #endif
   }
@@ -175,7 +192,7 @@ static void init_physfs(const char* argv0)
 
   //show search Path
   for(char** i = PHYSFS_getSearchPath(); *i != NULL; i++)
-    msg_info << "[" << *i << "] is in the search path" << std::endl;
+    log_info << "[" << *i << "] is in the search path" << std::endl;
 }
 
 static void print_usage(const char* argv0)
@@ -237,12 +254,12 @@ static bool parse_commandline(int argc, char** argv)
       print_usage(argv[0]);
       return true;
     } else if(arg == "--version") {
-      msg_info << PACKAGE_NAME << " " << PACKAGE_VERSION << std::endl;
+      log_info << PACKAGE_NAME << " " << PACKAGE_VERSION << std::endl;
       return true;
     } else if(arg[0] != '-') {
       config->start_level = arg;
     } else {
-      msg_warning << "Unknown option '" << arg << "'. Use --help to see a list of options" << std::endl;
+      log_warning << "Unknown option '" << arg << "'. Use --help to see a list of options" << std::endl;
     }
   }
 
@@ -256,6 +273,8 @@ static void init_sdl()
     msg << "Couldn't initialize SDL: " << SDL_GetError();
     throw std::runtime_error(msg.str());
   }
+  // just to be sure
+  atexit(SDL_Quit);
 
   SDL_EnableUNICODE(1);
 
@@ -341,7 +360,7 @@ void init_video()
   }
 #ifdef DEBUG
   else {
-    msg_warning << "Couldn't find icon 'images/engine/icons/supertux.xpm'" << std::endl;
+    log_warning << "Couldn't find icon 'images/engine/icons/supertux.xpm'" << std::endl;
   }
 #endif
 
@@ -379,9 +398,9 @@ static void init_audio()
 
 static void init_scripting()
 {
-  script_manager = new ScriptManager();
+  ScriptManager::instance = new ScriptManager();
 
-  HSQUIRRELVM vm = script_manager->get_global_vm();
+  HSQUIRRELVM vm = ScriptManager::instance->get_vm();
   sq_pushroottable(vm); 
   expose_object(vm, -1, new Scripting::Sound(), "Sound", true);
   expose_object(vm, -1, new Scripting::Level(), "Level", true);
@@ -445,7 +464,7 @@ static inline void timelog(const char* component)
   Uint32 current_ticks = SDL_GetTicks();
   
   if(last_timelog_component != 0) {
-    msg_info << "Component '" << last_timelog_component <<  "' finished after " << (current_ticks - last_timelog_ticks) / 1000.0 << " seconds" << std::endl;
+    log_info << "Component '" << last_timelog_component <<  "' finished after " << (current_ticks - last_timelog_ticks) / 1000.0 << " seconds" << std::endl;
   }
 
   last_timelog_ticks = current_ticks;
@@ -459,6 +478,8 @@ static inline void timelog(const char* )
 
 int main(int argc, char** argv) 
 {
+  int result = 0;
+    
   try {
     srand(time(0));
     init_physfs(argv[0]);
@@ -476,13 +497,14 @@ int main(int argc, char** argv)
     init_audio();
     timelog("video");
     init_video();
+    Console::instance = new Console(); 
     timelog("scripting");
     init_scripting();
 
     timelog("menu");
     setup_menu();
     timelog("resources");
-    load_shared();
+    load_shared(); 
     timelog(0);
 
     main_loop = new MainLoop(); 
@@ -504,21 +526,20 @@ int main(int argc, char** argv)
     }
 
     main_loop->run();
-
-    delete main_loop;
-    main_loop = NULL;
   } catch(std::exception& e) {
-    msg_fatal << "Unexpected exception: " << e.what() << std::endl;
-    return 1;
+    log_fatal << "Unexpected exception: " << e.what() << std::endl;
+    result = 1;
   } catch(...) {
-    msg_fatal << "Unexpected exception" << std::endl;
-    return 1;
+    log_fatal << "Unexpected exception" << std::endl;
+    result = 1;
   }
 
+  delete main_loop;
+  main_loop = NULL;
+
   free_menu();
-  delete script_manager;
-  script_manager = NULL;
-  printf("crashunloadshared?\n");
+  delete ScriptManager::instance;
+  ScriptManager::instance = NULL;
   unload_shared();
   quit_audio();
 
@@ -528,10 +549,12 @@ int main(int argc, char** argv)
   config = NULL;
   delete main_controller;
   main_controller = NULL;
+  delete Console::instance;
+  Console::instance = NULL;
   delete texture_manager;
   texture_manager = NULL;
   SDL_Quit();
   PHYSFS_deinit();
   
-  return 0;
+  return result;
 }