Move some more globals to supertux/globals.hpp
[supertux.git] / src / supertux / main.cpp
index 4979871..bef5b08 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>
 
@@ -40,7 +42,7 @@ namespace supertux_apple {
 #include "scripting/squirrel_util.hpp"
 #include "supertux/gameconfig.hpp"
 #include "supertux/globals.hpp"
-#include "supertux/mainloop.hpp"
+#include "supertux/screen_manager.hpp"
 #include "supertux/resources.hpp"
 #include "supertux/title_screen.hpp"
 #include "util/file_system.hpp"
@@ -50,7 +52,8 @@ namespace supertux_apple {
 
 namespace { DrawingContext *context_pointer; }
 
-static void init_config()
+void 
+Main::init_config()
 {
   g_config = new Config();
   try {
@@ -60,21 +63,25 @@ static void init_config()
   }
 }
 
-static void init_tinygettext()
+void
+Main::init_tinygettext()
 {
+  dictionary_manager = new tinygettext::DictionaryManager();
   tinygettext::Log::set_log_info_callback(0);
-  dictionary_manager.set_filesystem(std::auto_ptr<tinygettext::FileSystem>(new PhysFSFileSystem));
+  dictionary_manager->set_filesystem(std::auto_ptr<tinygettext::FileSystem>(new PhysFSFileSystem));
 
-  dictionary_manager.add_directory("locale");
-  dictionary_manager.set_charset("UTF-8");
+  dictionary_manager->add_directory("locale");
+  dictionary_manager->set_charset("UTF-8");
 
   // Config setting "locale" overrides language detection
-  if (g_config->locale != "") {
-    dictionary_manager.set_language(tinygettext::Language::from_name(g_config->locale));
+  if (g_config->locale != "") 
+  {
+    dictionary_manager->set_language(tinygettext::Language::from_name(g_config->locale));
   }
 }
 
-static void init_physfs(const char* argv0)
+ void
+Main::init_physfs(const char* argv0)
 {
   if(!PHYSFS_init(argv0)) {
     std::stringstream msg;
@@ -196,7 +203,8 @@ static void init_physfs(const char* argv0)
   PHYSFS_freeList(searchpath);
 }
 
-static void print_usage(const char* argv0)
+void
+Main::print_usage(const char* argv0)
 {
   fprintf(stderr, _("Usage: %s [OPTIONS] [LEVELFILE]\n\n"), argv0);
   fprintf(stderr,
@@ -224,7 +232,8 @@ static void print_usage(const char* argv0)
 /**
  * Options that should be evaluated prior to any initializations at all go here
  */
-static bool pre_parse_commandline(int argc, char** argv)
+bool
+Main::pre_parse_commandline(int argc, char** argv)
 {
   for(int i = 1; i < argc; ++i) {
     std::string arg = argv[i];
@@ -245,7 +254,8 @@ static bool pre_parse_commandline(int argc, char** argv)
 /**
  * Options that should be evaluated after config is read go here
  */
-static bool parse_commandline(int argc, char** argv)
+bool
+Main::parse_commandline(int argc, char** argv)
 {
   for(int i = 1; i < argc; ++i) {
     std::string arg = argv[i];
@@ -374,7 +384,8 @@ static bool parse_commandline(int argc, char** argv)
   return false;
 }
 
-static void init_sdl()
+void
+Main::init_sdl()
 {
   if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
     std::stringstream msg;
@@ -394,7 +405,8 @@ static void init_sdl()
     ;
 }
 
-static void init_rand()
+void
+Main::init_rand()
 {
   g_config->random_seed = systemRandom.srand(g_config->random_seed);
 
@@ -402,7 +414,8 @@ static void init_rand()
   //log_info << "Using random seed " << config->random_seed << how << std::endl;
 }
 
-void init_video()
+void
+Main::init_video()
 {
   // FIXME: Add something here
   SCREEN_WIDTH  = 800;
@@ -430,7 +443,7 @@ void init_video()
     SDL_WM_SetIcon(icon, 0);
     SDL_FreeSurface(icon);
   }
-#ifdef DEBUG
+#ifndef NDEBUG
   else {
     log_warning << "Couldn't load icon '" << icon_fname << "'" << std::endl;
   }
@@ -444,7 +457,8 @@ void init_video()
            << " Area: "       << g_config->aspect_width     << "x" << g_config->aspect_height << std::endl;
 }
 
-static void init_audio()
+void
+Main::init_audio()
 {
   sound_manager = new SoundManager();
 
@@ -452,7 +466,8 @@ static void init_audio()
   sound_manager->enable_music(g_config->music_enabled);
 }
 
-static void quit_audio()
+void
+Main::quit_audio()
 {
   if(sound_manager != NULL) {
     delete sound_manager;
@@ -460,7 +475,8 @@ static void quit_audio()
   }
 }
 
-void wait_for_event(float min_delay, float max_delay)
+void
+Main::wait_for_event(float min_delay, float max_delay)
 {
   assert(min_delay <= max_delay);
 
@@ -485,7 +501,7 @@ void wait_for_event(float min_delay, float max_delay)
     while(SDL_PollEvent(&event)) {
       switch(event.type) {
         case SDL_QUIT:
-          g_main_loop->quit();
+          g_screen_manager->quit();
           break;
         case SDL_KEYDOWN:
         case SDL_JOYBUTTONDOWN:
@@ -500,7 +516,7 @@ void wait_for_event(float min_delay, float max_delay)
   }
 }
 
-#ifdef DEBUG
+#ifndef NDEBUG
 static Uint32 last_timelog_ticks = 0;
 static const char* last_timelog_component = 0;
 
@@ -521,17 +537,8 @@ static inline void timelog(const char* )
 }
 #endif
 
-std::istream* physfs_open_file(const char* name)
-{
-  return new IFileStream(name);
-}
-
-void physfs_free_list(char** name)
-{
-  PHYSFS_freeList(name);
-}
-
-int supertux_main(int argc, char** argv)
+int
+Main::main(int argc, char** argv)
 {
   int result = 0;
 
@@ -574,11 +581,11 @@ int supertux_main(int argc, char** argv)
     Scripting::init_squirrel(g_config->enable_script_debugger);
 
     timelog("resources");
-    load_shared();
+    Resources::load_shared();
 
     timelog(0);
 
-    g_main_loop = new MainLoop();
+    g_screen_manager = new ScreenManager();
     if(g_config->start_level != "") {
       // we have a normal path specified at commandline, not a physfs path.
       // So we simply mount that path here...
@@ -589,7 +596,7 @@ int supertux_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_main_loop->push_screen(new WorldMapNS::WorldMap(
+        g_screen_manager->push_screen(new WorldMapNS::WorldMap(
                                  FileSystem::basename(g_config->start_level)));
       } else {
         init_rand();//If level uses random eg. for
@@ -605,15 +612,15 @@ int supertux_main(int argc, char** argv)
 
         if(g_config->record_demo != "")
           session->record_demo(g_config->record_demo);
-        g_main_loop->push_screen(session.release());
+        g_screen_manager->push_screen(session.release());
       }
     } else {
       init_rand();
-      g_main_loop->push_screen(new TitleScreen());
+      g_screen_manager->push_screen(new TitleScreen());
     }
 
     //init_rand(); PAK: this call might subsume the above 3, but I'm chicken!
-    g_main_loop->run(context);
+    g_screen_manager->run(context);
   } catch(std::exception& e) {
     log_fatal << "Unexpected exception: " << e.what() << std::endl;
     result = 1;
@@ -622,10 +629,10 @@ int supertux_main(int argc, char** argv)
     result = 1;
   }
 
-  delete g_main_loop;
-  g_main_loop = NULL;
+  delete g_screen_manager;
+  g_screen_manager = NULL;
 
-  unload_shared();
+  Resources::unload_shared();
   quit_audio();
 
   if(g_config)