fixed save/load-game.
[supertux.git] / src / setup.c
index d7b67f6..336ae92 100644 (file)
@@ -23,8 +23,9 @@
 
 #ifdef LINUX
 #include <pwd.h>
-#include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/types.h>
+#include <dirent.h>
 #include <ctype.h>
 #endif
 
@@ -33,6 +34,8 @@
 #include "setup.h"
 #include "screen.h"
 #include "texture.h"
+#include "menu.h"
+#include "gameloop.h"
 
 /* Local function prototypes: */
 
@@ -44,22 +47,209 @@ int faccessible(char *filename)
 {
   struct stat filestat;
   if (stat(filename, &filestat) == -1)
+  {
     return NO;
+  }
   else
+  {
+  if(S_ISREG(filestat.st_mode))
     return YES;
+  else
+    return NO;
+  }
 }
 
+/* Can we write to this location? */
+int fwriteable(char *filename)
+{
+  FILE* fi;
+  fi = fopen(filename, "wa");
+  if (fi == NULL)
+    {
+      return NO;
+    }
+  return YES;
+}
 
-/* --- SETUP --- */
+/* Makes sure a directory is created in either the SuperTux base directory or the SuperTux base directory.*/
+int fcreatedir(char* relative_dir)
+{
+  char path[1024];
+  snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
+  if(mkdir(path,0755) != 0)
+    {
+      snprintf(path, 1024, "%s/%s/", DATA_PREFIX, relative_dir);
+      if(mkdir(path,0755) != 0)
+        {
+          return NO;
+        }
+      else
+        {
+          return YES;
+        }
+    }
+  else
+    {
+      return YES;
+    }
+}
 
-void st_directory_setup(void)
+/* 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. */
+string_list_type dsubdirs(char *rel_path, char* expected_file)
 {
+  DIR *dirStructP;
+  struct dirent *direntp;
+  int i = 0;
+  string_list_type sdirs;
+  char filename[1024];
+  char path[1024];
+
+  string_list_init(&sdirs);
+  sprintf(path,"%s/%s",st_dir,rel_path);
+  if((dirStructP = opendir(path)) != NULL)
+    {
+      while((direntp = readdir(dirStructP)) != NULL)
+        {
+          char absolute_filename[1024];
+          struct stat buf;
+
+          sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
+
+          if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
+            {
+              if(expected_file != NULL)
+                {
+                  sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
+                  if(!faccessible(filename))
+                    continue;
+                }
+
+              string_list_add_item(&sdirs,direntp->d_name);
+            }
+        }
+      closedir(dirStructP);
+    }
+
+  sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
+  if((dirStructP = opendir(path)) != NULL)
+    {
+      while((direntp = readdir(dirStructP)) != NULL)
+        {
+          char absolute_filename[1024];
+          struct stat buf;
 
-  /* Set SuperTux configuration and save directories */
+          sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
+
+          if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
+            {
+              if(expected_file != NULL)
+                {
+                  sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
+                  if(!faccessible(filename))
+                    {
+                      continue;
+                    }
+                  else
+                    {
+                      sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
+                      if(faccessible(filename))
+                        continue;
+                    }
+                }
+
+              string_list_add_item(&sdirs,direntp->d_name);
+            }
+        }
+      closedir(dirStructP);
+    }
+
+  return sdirs;
+}
+
+string_list_type dfiles(char *rel_path, char* glob, char* exception_str)
+{
+  DIR *dirStructP;
+  struct dirent *direntp;
+  int i = 0;
+  string_list_type sdirs;
+  char filename[1024];
+  char path[1024];
+
+  string_list_init(&sdirs);
+  sprintf(path,"%s/%s",st_dir,rel_path);
+  if((dirStructP = opendir(path)) != NULL)
+    {
+      while((direntp = readdir(dirStructP)) != NULL)
+        {
+          char absolute_filename[1024];
+          struct stat buf;
+
+          sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
+
+          if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
+            {
+              if(exception_str != NULL)
+                {
+                  if(strstr(direntp->d_name,exception_str) != NULL)
+                    continue;
+                }
+              if(glob != NULL)
+                if(strstr(direntp->d_name,glob) == NULL)
+                  continue;
+
+              string_list_add_item(&sdirs,direntp->d_name);
+            }
+        }
+      closedir(dirStructP);
+    }
+
+  sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
+  if((dirStructP = opendir(path)) != NULL)
+    {
+      while((direntp = readdir(dirStructP)) != NULL)
+        {
+          char absolute_filename[1024];
+          struct stat buf;
+
+          sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
+
+          if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
+            {
+              if(exception_str != NULL)
+                {
+                  if(strstr(direntp->d_name,exception_str) != NULL)
+                    continue;
+                }
+              if(glob != NULL)
+                if(strstr(direntp->d_name,glob) == NULL)
+                  continue;
+
+              string_list_add_item(&sdirs,direntp->d_name);
+            }
+        }
+      closedir(dirStructP);
+    }
+
+  return sdirs;
+}
+
+void free_strings(char **strings, int num)
+{
+  int i;
+  for(i=0; i < num; ++i)
+    free(strings[i]);
+}
 
+/* --- SETUP --- */
+/* Set SuperTux configuration and save directories */
+void st_directory_setup(void)
+{
+  char *home;
+  char str[1024];
   /* Get home directory (from $HOME variable)... if we can't determine it,
      use the current directory ("."): */
-  char *home;
   if (getenv("HOME") != NULL)
     home = getenv("HOME");
   else
@@ -69,24 +259,195 @@ void st_directory_setup(void)
                            strlen("/.supertux") + 1));
   strcpy(st_dir, home);
   strcat(st_dir, "/.supertux");
+  
+  /* Remove .supertux config-file from old SuperTux versions */
+  if(faccessible(st_dir))
+  {
+  remove(st_dir);
+  }
 
   st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
 
   strcpy(st_save_dir,st_dir);
   strcat(st_save_dir,"/save");
 
-  /* Create them. In the case they exist it won't destroy anything. */
+  /* Create them. In the case they exist they won't destroy anything. */
 #ifdef LINUX
 
   mkdir(st_dir, 0755);
   mkdir(st_save_dir, 0755);
+
+  sprintf(str, "%s/levels", st_dir);
+  mkdir(str, 0755);
 #else
   #ifdef WIN32
 
   mkdir(st_dir);
   mkdir(st_save_dir);
+  sprintf(str, "%s/levels", st_dir);
+  mkdir(str);
 #endif
 #endif
+
+}
+
+/* Create and setup menus. */
+void st_menu(void)
+{
+  menu_init(&main_menu);
+  menu_additem(&main_menu,menu_item_create(MN_LABEL,"Main Menu",0,0));
+  menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
+  menu_additem(&main_menu,menu_item_create(MN_ACTION,"Start Game",0,0));
+  menu_additem(&main_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
+  menu_additem(&main_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
+  menu_additem(&main_menu,menu_item_create(MN_ACTION,"Level editor",0,0));
+  menu_additem(&main_menu,menu_item_create(MN_ACTION,"Credits",0,0));
+  menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
+  menu_additem(&main_menu,menu_item_create(MN_ACTION,"Quit",0,0));
+
+  menu_init(&options_menu);
+  menu_additem(&options_menu,menu_item_create(MN_LABEL,"Options",0,0));
+  menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
+  menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Fullscreen",use_fullscreen,0));
+  if(audio_device == YES)
+    {
+      menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Sound     ",use_sound,0));
+      menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Music     ",use_music,0));
+    }
+  else
+    {
+      menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Sound     ",use_sound,0));
+      menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Music     ",use_music,0));
+    }
+  menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Show FPS  ",show_fps,0));
+  menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
+  menu_additem(&options_menu,menu_item_create(MN_BACK,"Back",0,0));
+
+  menu_init(&load_game_menu);
+  menu_additem(&load_game_menu,menu_item_create(MN_LABEL,"Load Game",0,0));
+  menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
+  menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
+  menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
+  menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
+  menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
+  menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
+  menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
+  menu_additem(&load_game_menu,menu_item_create(MN_BACK,"Back",0,0));
+
+  menu_init(&save_game_menu);
+  menu_additem(&save_game_menu,menu_item_create(MN_LABEL,"Save Game",0,0));
+  menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
+  menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
+  menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
+  menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
+  menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
+  menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
+  menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
+  menu_additem(&save_game_menu,menu_item_create(MN_BACK,"Back",0,0));
+
+  menu_init(&game_menu);
+  menu_additem(&game_menu,menu_item_create(MN_LABEL,"InGame Menu",0,0));
+  menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
+  menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
+  menu_additem(&game_menu,menu_item_create(MN_GOTO,"Save Game",0,&save_game_menu));
+  menu_additem(&game_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
+  menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
+  menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
+  menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
+
+  menu_init(&highscore_menu);
+  menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
+
+}
+
+void update_load_save_game_menu(menu_type* pmenu, int load)
+{
+  int i;
+
+  for(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;
+      else
+        pmenu->item[i].kind = MN_ACTION;
+      menu_item_change_text(&pmenu->item[i],tmp);
+      free(tmp);
+    }
+}
+
+void process_save_load_game_menu(int save)
+{
+  int slot;
+  switch (slot = menu_check(save ? &save_game_menu : &load_game_menu))
+    {
+    default:
+      if(slot != -1)
+        {
+          if(save == YES)
+            {
+              savegame(slot - 1);
+            }
+          else
+            {
+              if(game_started == NO)
+             {
+                gameloop("default",slot - 1,ST_GL_LOAD_GAME);
+               show_menu = YES;
+               menu_set_current(&main_menu);
+               }
+              else
+                loadgame(slot - 1);
+            }
+          st_pause_ticks_stop();
+        }
+      break;
+    }
+}
+
+/* Handle changes made to global settings in the options menu. */
+void process_options_menu(void)
+{
+  switch (menu_check(&options_menu))
+    {
+    case 2:
+      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)
+        use_sound = !use_sound;
+      break;
+    case 4:
+      if(use_music != options_menu.item[4].toggled)
+        {
+          if(use_music == YES)
+            {
+              if(playing_music())
+                {
+                  halt_music();
+                }
+              use_music = NO;
+            }
+          else
+            {
+              use_music = YES;
+              if (!playing_music())
+                {
+                  play_current_music();
+                }
+            }
+        }
+      break;
+    case 5:
+      if(show_fps != options_menu.item[5].toggled)
+        show_fps = !show_fps;
+      break;
+    }
 }
 
 void st_general_setup(void)
@@ -96,12 +457,23 @@ void st_general_setup(void)
   srand(SDL_GetTicks());
 
   /* Load global images: */
-          
- text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png");
- text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png");
- text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png");
- text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png");
+
+  text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18);
+  text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
+  text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
+  text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18);
+  text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18);
+  text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
+  text_load(&white_big_text,DATA_PREFIX "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
+  text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32);
+
+  /* Load GUI/menu images: */
+  texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA);
+  texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA);
+  texture_load(&back, DATA_PREFIX "/images/status/back.png", USE_ALPHA);
+  texture_load(&arrow_left, DATA_PREFIX "/images/icons/left.png", USE_ALPHA);
+  texture_load(&arrow_right, DATA_PREFIX "/images/icons/right.png", USE_ALPHA);
+
   /* Set icon image: */
 
   seticon();
@@ -109,11 +481,42 @@ void st_general_setup(void)
 
 }
 
+void st_general_free(void)
+{
+
+  /* Free global images: */
+
+  text_free(&black_text);
+  text_free(&gold_text);
+  text_free(&white_text);
+  text_free(&blue_text);
+  text_free(&red_text);
+  text_free(&white_small_text);
+  text_free(&white_big_text);
+
+  /* Free GUI/menu images: */
+  texture_free(&checkbox);
+  texture_free(&checkbox_checked);
+  texture_free(&back);
+  texture_free(&arrow_left);
+  texture_free(&arrow_right);
+  
+  /* 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);
+
+}
+
 void st_video_setup(void)
 {
 
-if(screen != NULL)
-   SDL_FreeSurface(screen); 
+  if(screen != NULL)
+    SDL_FreeSurface(screen);
 
   /* Init SDL Video: */
 
@@ -129,12 +532,12 @@ if(screen != NULL)
   /* Open display: */
 
   if(use_gl)
-  st_video_setup_gl();
+    st_video_setup_gl();
   else
-  st_video_setup_sdl();
-  
+    st_video_setup_sdl();
+
   texture_setup();
-    
+
   /* Set window manager stuff: */
 
   SDL_WM_SetCaption("Super Tux", "Super Tux");
@@ -143,6 +546,8 @@ if(screen != NULL)
 
 void st_video_setup_sdl(void)
 {
+  SDL_FreeSurface(screen);
+
   if (use_fullscreen == YES)
     {
       screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
@@ -156,7 +561,7 @@ void st_video_setup_sdl(void)
           use_fullscreen = NO;
         }
     }
-    else
+  else
     {
       screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
 
@@ -175,15 +580,15 @@ void st_video_setup_gl(void)
 {
 #ifndef NOOPENGL
 
-       SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
-       SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
-       SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
-       SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
-       SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
+  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
+  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
+  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
+  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 
   if (use_fullscreen == YES)
     {
-      screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL ) ; /* | SDL_HWSURFACE); */
+      screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
       if (screen == NULL)
         {
           fprintf(stderr,
@@ -194,9 +599,9 @@ void st_video_setup_gl(void)
           use_fullscreen = NO;
         }
     }
-    else
+  else
     {
-      screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_OPENGLBLIT  );
+      screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
 
       if (screen == NULL)
         {
@@ -207,23 +612,24 @@ void st_video_setup_gl(void)
           exit(1);
         }
     }
-       
-       /*
-        * Set up OpenGL for 2D rendering.
-        */
-       glDisable(GL_DEPTH_TEST);
-       glDisable(GL_CULL_FACE);
-
-       glViewport(0, 0, screen->w, screen->h);
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
-
-       glMatrixMode(GL_MODELVIEW);
-       glLoadIdentity();
-       glTranslatef(0.0f, 0.0f, 0.0f);
-       
+
+  /*
+   * Set up OpenGL for 2D rendering.
+   */
+  glDisable(GL_DEPTH_TEST);
+  glDisable(GL_CULL_FACE);
+
+  glViewport(0, 0, screen->w, screen->h);
+  glMatrixMode(GL_PROJECTION);
+  glLoadIdentity();
+  glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
+
+  glMatrixMode(GL_MODELVIEW);
+  glLoadIdentity();
+  glTranslatef(0.0f, 0.0f, 0.0f);
+
 #endif
+
 }
 
 void st_joystick_setup(void)
@@ -393,7 +799,7 @@ void seticon(void)
   /* Create mask: */
 
   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
-  mask = malloc(masklen * sizeof(Uint8));
+  mask = (Uint8*) malloc(masklen * sizeof(Uint8));
   memset(mask, 0xFF, masklen);
 
 
@@ -421,7 +827,7 @@ void parseargs(int argc, char * argv[])
   debug_mode = NO;
   use_fullscreen = NO;
   show_fps = NO;
-  use_gl = NO;    
+  use_gl = NO;
 
 #ifndef NOSOUND
 
@@ -453,13 +859,14 @@ void parseargs(int argc, char * argv[])
           show_fps = YES;
         }
       else if (strcmp(argv[i], "--opengl") == 0 ||
-          strcmp(argv[i], "-gl") == 0)
+               strcmp(argv[i], "-gl") == 0)
         {
-       #ifndef NOOPENGL
+#ifndef NOOPENGL
           /* Use OpengGL: */
 
           use_gl = YES;
-       #endif
+#endif
+
         }
       else if (strcmp(argv[i], "--usage") == 0)
         {
@@ -512,7 +919,7 @@ void parseargs(int argc, char * argv[])
           printf("----------  Command-line options  ----------\n\n");
 
           printf("  --opengl            - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
-         
+
           printf("  --disable-sound     - If sound support was compiled in,  this will\n                        disable sound for this session of the game.\n\n");
 
           printf("  --disable-music     - Like above, but this will disable music.\n\n");