- moved loadshared() to the right point
[supertux.git] / src / setup.cpp
index 54f1c76..155b5ab 100644 (file)
@@ -11,6 +11,7 @@
 */
 
 #include <assert.h>
+#include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "texture.h"
 #include "menu.h"
 #include "gameloop.h"
+#include "configfile.h"
+#include "scene.h"
 
 #ifdef WIN32
 #define mkdir(dir, mode)    mkdir(dir)
 // on win32 we typically don't want LFS paths
 #undef DATA_PREFIX
-#define DATA_PREFIX "./data"
+#define DATA_PREFIX "./data/"
 #endif
 
 /* Local function prototypes: */
@@ -79,7 +82,7 @@ int fwriteable(const char *filename)
   return true;
 }
 
-/* Makes sure a directory is created in either the SuperTux base directory or the SuperTux base directory.*/
+/* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
 int fcreatedir(const char* relative_dir)
 {
   char path[1024];
@@ -102,6 +105,36 @@ int fcreatedir(const char* relative_dir)
     }
 }
 
+FILE * opendata(const char * rel_filename, const char * mode)
+{
+  char * filename = NULL;
+  FILE * fi;
+
+  filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
+                                             strlen(rel_filename) + 1));
+
+  strcpy(filename, st_dir);
+  /* Open the high score file: */
+
+  strcat(filename, rel_filename);
+
+  /* Try opening the file: */
+  fi = fopen(filename, mode);
+
+  if (fi == NULL)
+    {
+      fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename);
+
+      if (strcmp(mode, "r") == 0)
+        fprintf(stderr, "for read!!!\n");
+      else if (strcmp(mode, "w") == 0)
+        fprintf(stderr, "for write!!!\n");
+    }
+  free( filename );
+
+  return(fi);
+}
+
 /* Get all names of sub-directories in a certain directory. */
 /* Returns the number of sub-directories found. */
 /* Note: The user has to free the allocated space. */
@@ -310,7 +343,7 @@ void st_directory_setup(void)
             }
         }
 #else
-       datadir = DATA_PREFIX;
+  datadir = DATA_PREFIX;
 #endif
     }
   printf("Datadir: %s\n", datadir.c_str());
@@ -319,138 +352,176 @@ void st_directory_setup(void)
 /* Create and setup menus. */
 void st_menu(void)
 {
-  menu_init(&main_menu);
-  menu_additem(&main_menu, MN_LABEL,"Main Menu",0,0);
-  menu_additem(&main_menu, MN_HL,"",0,0);
-  menu_additem(&main_menu, MN_ACTION,"Start Game",0,0);
-  menu_additem(&main_menu, MN_GOTO,"Load Game",0,&load_game_menu);
-  menu_additem(&main_menu, MN_GOTO,"Options",0,&options_menu);
-  menu_additem(&main_menu, MN_ACTION,"Level editor",0,0);
-  menu_additem(&main_menu, MN_ACTION,"Credits",0,0);
-  menu_additem(&main_menu, MN_HL,"",0,0);
-  menu_additem(&main_menu, MN_ACTION,"Quit",0,0);
-
-  menu_init(&options_menu);
-  menu_additem(&options_menu, MN_LABEL,"Options",0,0);
-  menu_additem(&options_menu, MN_HL,"",0,0);
-  menu_additem(&options_menu, MN_TOGGLE,"Fullscreen",use_fullscreen,0);
+  main_menu      = new Menu();
+  options_menu   = new Menu();
+  options_controls_menu   = new Menu();
+  load_game_menu = new Menu();
+  save_game_menu = new Menu();
+  game_menu      = new Menu();
+  highscore_menu = new Menu();
+  contrib_menu   = new Menu();
+
+  main_menu->set_pos(screen->w/2, 335);
+  main_menu->additem(MN_GOTO, "Start Game",0,load_game_menu);
+  main_menu->additem(MN_GOTO, "Contrib Levels",0,contrib_menu);
+  main_menu->additem(MN_GOTO, "Options",0,options_menu);
+  main_menu->additem(MN_ACTION,"Level editor",0,0);
+  main_menu->additem(MN_ACTION,"Credits",0,0);
+  main_menu->additem(MN_ACTION,"Quit",0,0);
+
+  contrib_menu->additem(MN_LABEL,"Contrib Levels",0,0);
+  contrib_menu->additem(MN_HL,"",0,0);
+  contrib_menu->additem(MN_ACTION, "Some Levelset", 0, 0);
+  contrib_menu->additem(MN_ACTION, "Someother Levelset", 0, 0);
+  contrib_menu->additem(MN_ACTION, "Yet another Levelset", 0, 0);
+  contrib_menu->additem(MN_HL,"",0,0);
+  contrib_menu->additem(MN_BACK,"Back",0,0);
+
+  options_menu->additem(MN_LABEL,"Options",0,0);
+  options_menu->additem(MN_HL,"",0,0);
+  options_menu->additem(MN_TOGGLE,"Fullscreen",use_fullscreen,0);
   if(audio_device)
     {
-      menu_additem(&options_menu, MN_TOGGLE,"Sound     ",use_sound,0);
-      menu_additem(&options_menu, MN_TOGGLE,"Music     ",use_music,0);
+      options_menu->additem(MN_TOGGLE,"Sound     ",use_sound,0);
+      options_menu->additem(MN_TOGGLE,"Music     ",use_music,0);
     }
   else
     {
-      menu_additem(&options_menu, MN_DEACTIVE,"Sound     ",use_sound,0);
-      menu_additem(&options_menu, MN_DEACTIVE,"Music     ",use_music,0);
+      options_menu->additem(MN_DEACTIVE,"Sound     ",use_sound,0);
+      options_menu->additem(MN_DEACTIVE,"Music     ",use_music,0);
     }
-  menu_additem(&options_menu, MN_TOGGLE,"Show FPS  ",show_fps,0);
-  menu_additem(&options_menu, MN_HL,"",0,0);
-  menu_additem(&options_menu, MN_BACK,"Back",0,0);
-
-  menu_init(&load_game_menu);
-  menu_additem(&load_game_menu, MN_LABEL,"Load Game",0,0);
-  menu_additem(&load_game_menu, MN_HL,"",0,0);
-  menu_additem(&load_game_menu, MN_DEACTIVE,"Slot 1",0,0);
-  menu_additem(&load_game_menu, MN_DEACTIVE,"Slot 2",0,0);
-  menu_additem(&load_game_menu, MN_DEACTIVE,"Slot 3",0,0);
-  menu_additem(&load_game_menu, MN_DEACTIVE,"Slot 4",0,0);
-  menu_additem(&load_game_menu, MN_DEACTIVE,"Slot 5",0,0);
-  menu_additem(&load_game_menu, MN_HL,"",0,0);
-  menu_additem(&load_game_menu, MN_BACK,"Back",0,0);
-
-  menu_init(&save_game_menu);
-  menu_additem(&save_game_menu, MN_LABEL,"Save Game",0,0);
-  menu_additem(&save_game_menu, MN_HL,"",0,0);
-  menu_additem(&save_game_menu, MN_DEACTIVE,"Slot 1",0,0);
-  menu_additem(&save_game_menu, MN_DEACTIVE,"Slot 2",0,0);
-  menu_additem(&save_game_menu, MN_DEACTIVE,"Slot 3",0,0);
-  menu_additem(&save_game_menu, MN_DEACTIVE,"Slot 4",0,0);
-  menu_additem(&save_game_menu, MN_DEACTIVE,"Slot 5",0,0);
-  menu_additem(&save_game_menu, MN_HL,"",0,0);
-  menu_additem(&save_game_menu, MN_BACK,"Back",0,0);
-
-  menu_init(&game_menu);
-  menu_additem(&game_menu, MN_LABEL,"InGame Menu",0,0);
-  menu_additem(&game_menu, MN_HL,"",0,0);
-  menu_additem(&game_menu, MN_ACTION,"Return To Game",0,0);
-  menu_additem(&game_menu, MN_GOTO,"Save Game",0,&save_game_menu);
-  menu_additem(&game_menu, MN_GOTO,"Load Game",0,&load_game_menu);
-  menu_additem(&game_menu, MN_GOTO,"Options",0,&options_menu);
-  menu_additem(&game_menu, MN_HL,"",0,0);
-  menu_additem(&game_menu, MN_ACTION,"Quit Game",0,0);
-
-  menu_init(&highscore_menu);
-  menu_additem(&highscore_menu, MN_TEXTFIELD,"Enter your name:",0,0);
-
+  options_menu->additem(MN_TOGGLE,"Show FPS  ",show_fps,0);
+  options_menu->additem(MN_GOTO,"Controls  ",0,options_controls_menu);
+  options_menu->additem(MN_HL,"",0,0);
+  options_menu->additem(MN_BACK,"Back",0,0);
+  
+  options_controls_menu->additem(MN_LABEL,"Controls",0,0);
+  options_controls_menu->additem(MN_HL,"",0,0);
+  //FIXME:options_controls_menu->additem(MN_CONTROLFIELD,"Move Right", tux.keymap.right,0);
+  options_controls_menu->additem(MN_HL,"",0,0);
+  options_controls_menu->additem(MN_BACK,"Back",0,0);
+
+  load_game_menu->additem(MN_LABEL,"Start Game",0,0);
+  load_game_menu->additem(MN_HL,"",0,0);
+  load_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
+  load_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
+  load_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
+  load_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
+  load_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
+  load_game_menu->additem(MN_HL,"",0,0);
+  load_game_menu->additem(MN_BACK,"Back",0,0);
+
+  save_game_menu->additem(MN_LABEL,"Save Game",0,0);
+  save_game_menu->additem(MN_HL,"",0,0);
+  save_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
+  save_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
+  save_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
+  save_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
+  save_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
+  save_game_menu->additem(MN_HL,"",0,0);
+  save_game_menu->additem(MN_BACK,"Back",0,0);
+
+  game_menu->additem(MN_LABEL,"InGame Menu",0,0);
+  game_menu->additem(MN_HL,"",0,0);
+  game_menu->additem(MN_ACTION,"Return To Game",0,0);
+  game_menu->additem(MN_GOTO,"Save Game",0,save_game_menu);
+  game_menu->additem(MN_GOTO,"Load Game",0,load_game_menu);
+  game_menu->additem(MN_GOTO,"Options",0,options_menu);
+  game_menu->additem(MN_HL,"",0,0);
+  game_menu->additem(MN_ACTION,"Quit Game",0,0);
+
+  highscore_menu->additem(MN_TEXTFIELD,"Enter your name:",0,0);
 }
 
-void update_load_save_game_menu(menu_type* pmenu, int load)
+void update_load_save_game_menu(Menu* pmenu, int load)
 {
-  int i;
-
-  for(i = 2; i < 7; ++i)
+  for(int i = 2; i < 7; ++i)
     {
-      char *tmp;
-      slotinfo(&tmp,i-1);
-      if(load && strlen(tmp) == strlen("Slot X - Free") )
-        pmenu->item[i].kind = MN_DEACTIVE;
+      // FIXME: Insert a real savegame struct/class here instead of
+      // doing string vodoo
+      std::string tmp = slotinfo(i-1);
+
+      if(load && tmp.length() == strlen("Slot X - Free"))
+        pmenu->item[i].kind = MN_ACTION;
       else
         pmenu->item[i].kind = MN_ACTION;
-      menu_item_change_text(&pmenu->item[i],tmp);
-      free(tmp);
+      menu_item_change_text(&pmenu->item[i], tmp.c_str());
     }
 }
 
-void process_save_load_game_menu(int save)
+void process_save_game_menu()
 {
-  int slot;
-  switch (slot = menu_check(save ? &save_game_menu : &load_game_menu))
+  int slot = save_game_menu->check();
+  if (slot != -1)
+    GameSession::current()->savegame(slot - 1);
+}
+
+bool process_load_game_menu()
+{
+  int slot = load_game_menu->check();
+
+  if(slot != -1)
     {
-    default:
-      if(slot != -1)
-        {
-          if(save == true)
+      // FIXME: Insert a real savegame struct/class here instead of
+      // doing string vodoo
+      std::string tmp = slotinfo(slot-1);
+
+      if (tmp.length() == strlen("Slot X - Free"))
+        { // Slot is free, so start a new game
+          GameSession session("default", 1, ST_GL_PLAY);
+          session.run();
+
+          show_menu = true;
+          Menu::set_current(main_menu);
+        }
+      else
+        { 
+          puts("Warning: Loading games isn't supported at the moment");
+#if 0
+          // Slot contains a level, so load it
+          if (game_started)
             {
-              savegame(slot - 1);
+              GameSession session("default",slot - 1,ST_GL_LOAD_GAME);
+              session.run();
+
+              show_menu = true;
+              Menu::set_current(main_menu);
             }
           else
             {
-              if(game_started == false)
-                {
-                  gameloop("default",slot - 1,ST_GL_LOAD_GAME);
-                  show_menu = true;
-                  menu_set_current(&main_menu);
-                }
-              else
-                loadgame(slot - 1);
+              //loadgame(slot - 1);
             }
-          st_pause_ticks_stop();
+#endif
         }
-      break;
+      st_pause_ticks_stop();
+      return true;
+    }
+  else
+    {
+      return false;
     }
 }
 
 /* Handle changes made to global settings in the options menu. */
 void process_options_menu(void)
 {
-  switch (menu_check(&options_menu))
+  switch (options_menu->check())
     {
     case 2:
-      if(use_fullscreen != options_menu.item[2].toggled)
+      if(use_fullscreen != options_menu->item[2].toggled)
         {
           use_fullscreen = !use_fullscreen;
           st_video_setup();
         }
       break;
     case 3:
-      if(use_sound != options_menu.item[3].toggled)
+      if(use_sound != options_menu->item[3].toggled)
         use_sound = !use_sound;
       break;
     case 4:
-      if(use_music != options_menu.item[4].toggled)
+      if(use_music != options_menu->item[4].toggled)
         {
-          if(use_music == true)
+          if(use_music)
             {
               if(playing_music())
                 {
@@ -469,7 +540,7 @@ void process_options_menu(void)
         }
       break;
     case 5:
-      if(show_fps != options_menu.item[5].toggled)
+      if(show_fps != options_menu->item[5].toggled)
         show_fps = !show_fps;
       break;
     }
@@ -507,6 +578,9 @@ void st_general_setup(void)
   texture_load(&arrow_left, datadir + "/images/icons/left.png", USE_ALPHA);
   texture_load(&arrow_right, datadir + "/images/icons/right.png", USE_ALPHA);
 
+  /* Load the mouse-cursor */
+  mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
+  
 }
 
 void st_general_free(void)
@@ -529,15 +603,16 @@ void st_general_free(void)
   texture_free(&arrow_left);
   texture_free(&arrow_right);
 
+  /* Free mouse-cursor */
+  delete mouse_cursor;
+  
   /* Free menus */
-
-  menu_free(&main_menu);
-  menu_free(&game_menu);
-  menu_free(&options_menu);
-  menu_free(&highscore_menu);
-  menu_free(&save_game_menu);
-  menu_free(&load_game_menu);
-
+  delete main_menu;
+  delete game_menu;
+  delete options_menu;
+  delete highscore_menu;
+  delete save_game_menu;
+  delete load_game_menu;
 }
 
 void st_video_setup(void)
@@ -568,7 +643,7 @@ void st_video_setup(void)
 
   /* Set window manager stuff: */
 
-  SDL_WM_SetCaption("Super Tux", "Super Tux");
+  SDL_WM_SetCaption("SuperTux " VERSION, "SuperTux");
 
 }
 
@@ -576,7 +651,7 @@ void st_video_setup_sdl(void)
 {
   SDL_FreeSurface(screen);
 
-  if (use_fullscreen == true)
+  if (use_fullscreen)
     {
       screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
       if (screen == NULL)
@@ -614,7 +689,7 @@ void st_video_setup_gl(void)
   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 
-  if (use_fullscreen == true)
+  if (use_fullscreen)
     {
       screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
       if (screen == NULL)
@@ -728,7 +803,7 @@ void st_audio_setup(void)
 
   /* Init SDL Audio silently even if --disable-sound : */
 
-  if (audio_device == true)
+  if (audio_device)
     {
       if (SDL_Init(SDL_INIT_AUDIO) < 0)
         {
@@ -755,14 +830,14 @@ void st_audio_setup(void)
 
   /* Open sound silently regarless the value of "use_sound": */
 
-  if (audio_device == true)
+  if (audio_device)
     {
       if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
         {
           /* only print out message if sound or music
              was not disabled at command-line
            */
-          if ((use_sound == true) || (use_music == true))
+          if (use_sound || use_music)
             {
               fprintf(stderr,
                       "\nWarning: I could not set up audio for 44100 Hz "
@@ -785,16 +860,16 @@ void st_shutdown(void)
 {
   close_audio();
   SDL_Quit();
+  saveconfig();
 }
 
-
 /* --- ABORT! --- */
 
 void st_abort(const std::string& reason, const std::string& details)
 {
   fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
   st_shutdown();
-  exit(1);
+  abort();
 }
 
 
@@ -845,17 +920,7 @@ void parseargs(int argc, char * argv[])
 {
   int i;
 
-  /* Set defaults: */
-
-
-  debug_mode = false;
-  use_fullscreen = false;
-  show_fps = false;
-  use_gl = false;
-
-  use_sound = true;
-  use_music = true;
-  audio_device = true;
+  loadconfig();
 
   /* Parse arguments: */
 
@@ -897,8 +962,11 @@ void parseargs(int argc, char * argv[])
 
           use_gl = true;
 #endif
-
         }
+      else if (strcmp(argv[i], "--sdl") == 0)
+          {
+            use_gl = false;
+          }
       else if (strcmp(argv[i], "--usage") == 0)
         {
           /* Show usage: */
@@ -908,8 +976,7 @@ void parseargs(int argc, char * argv[])
       else if (strcmp(argv[i], "--version") == 0)
         {
           /* Show version: */
-
-          printf("Super Tux - version " VERSION "\n");
+          printf("SuperTux " VERSION "\n");
           exit(0);
         }
       else if (strcmp(argv[i], "--disable-sound") == 0)
@@ -931,7 +998,7 @@ void parseargs(int argc, char * argv[])
 
         }
       else if (strcmp(argv[i], "--help") == 0)
-        {        /* Show help: */
+        {     /* Show help: */
           puts("Super Tux " VERSION "\n"
                "  Please see the file \"README.txt\" for more details.\n");
           printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
@@ -939,6 +1006,7 @@ void parseargs(int argc, char * argv[])
                "  --fullscreen        Run in fullscreen mode.\n"
                "  --opengl            If opengl support was compiled in, this will enable\n"
                "                      the EXPERIMENTAL OpenGL mode.\n"
+               "  --sdl               Use non-opengl renderer\n"
                "\n"
                "Sound Options:\n"
                "  --disable-sound     If sound support was compiled in,  this will\n"