Gameplay has been repaired a bit. A menu effect was added. OpenGL works without SDL_O...
[supertux.git] / src / gameloop.c
index 581386f..8593b13 100644 (file)
 
 /* extern variables */
 
-extern char* soundfilenames[NUM_SOUNDS];
 st_level current_level;
+int game_started = NO;
 
 /* Local variables: */
 
-texture_type img_waves[3], img_water, img_pole, img_poletop, img_flag[2];
-texture_type img_cloud[2][4];
-SDL_Event event;
-SDLKey key;
-char level_subset[100];
-char str[60];
-float fps_fps;
-int st_gl_mode;
-unsigned int last_update_time;
-unsigned int update_time;
-int pause_menu_frame;
+static texture_type img_waves[3], img_water, img_pole, img_poletop, img_flag[2];
+static texture_type img_cloud[2][4];
+static SDL_Event event;
+static SDLKey key;
+static char level_subset[100];
+static char str[60];
+static float fps_fps;
+static int st_gl_mode;
+static unsigned int last_update_time;
+static unsigned int update_time;
+static int pause_menu_frame;
 
 /* Local function prototypes: */
 
 void levelintro(void);
-void initgame(void);
 void loadshared(void);
 void unloadshared(void);
 void drawstatus(void);
@@ -134,7 +133,7 @@ void game_event(void)
           if(show_menu)
             menu_event(&event.key.keysym);
 
-          if(player_keydown_event(&tux,key))
+          if(player_key_event(&tux,key,DOWN))
             break;
 
           switch(key)
@@ -146,11 +145,13 @@ void game_event(void)
                     quit = 1;
                   else if(show_menu)
                     {
+                      menu_set_current(&game_menu);
                       show_menu = 0;
                       st_pause_ticks_stop();
                     }
                   else
                     {
+                      menu_set_current(&game_menu);
                       show_menu = 1;
                       st_pause_ticks_start();
                     }
@@ -163,7 +164,7 @@ void game_event(void)
         case SDL_KEYUP:      /* A keyrelease! */
           key = event.key.keysym.sym;
 
-          if(player_keyup_event(&tux,key))
+          if(player_key_event(&tux,key,UP))
             break;
 
           switch(key)
@@ -185,7 +186,15 @@ void game_event(void)
               break;
             case SDLK_TAB:
               if(debug_mode == YES)
-                tux.size = !tux.size;
+                {
+                  tux.size = !tux.size;
+                  if(tux.size == BIG)
+                    {
+                      tux.base.height = 64;
+                    }
+                  else
+                    tux.base.height = 32;
+                }
               break;
             case SDLK_END:
               if(debug_mode == YES)
@@ -295,7 +304,7 @@ int game_action(void)
           /* End of a level! */
           level++;
           next_level = 0;
-          if(st_gl_mode == ST_GL_PLAY)
+          if(st_gl_mode != ST_GL_TEST)
             drawresultscreen();
           player_level_begin(&tux);
         }
@@ -307,10 +316,10 @@ int game_action(void)
 
           if (tux.lives < 0)
             {
-              if(st_gl_mode == ST_GL_PLAY)
+              if(st_gl_mode != ST_GL_TEST)
                 drawendscreen();
 
-              if(st_gl_mode == ST_GL_PLAY)
+              if(st_gl_mode != ST_GL_TEST)
                 {
                   if (score > hs_score)
                     save_hs(score);
@@ -338,7 +347,7 @@ int game_action(void)
       level_load_gfx(&current_level);
       level_free_song();
       level_load_song(&current_level);
-      if(st_gl_mode == ST_GL_PLAY)
+      if(st_gl_mode != ST_GL_TEST)
         levelintro();
       start_timers();
     }
@@ -542,42 +551,47 @@ int gameloop(char * subset, int levelnb, int mode)
 {
   int fps_cnt, jump, done;
   timer_type fps_timer, frame_timer;
+  timer_init(&fps_timer, YES);
+  timer_init(&frame_timer, YES);
+  
+  game_started = YES;
 
-  level = levelnb;
   st_gl_mode = mode;
+  level = levelnb;
   strcpy(level_subset,subset);
 
-  /* Clear screen: */
-
-  clearscreen(0, 0, 0);
-  updatescreen();
-
-
-  /* Init the game: */
-  arrays_init();
+  if(st_gl_mode != ST_GL_LOAD_GAME)
+    {
+      /* Init the game: */
+      arrays_init();
+      set_defaults();
 
-  menu_reset();
-  menu_set_current(&game_menu);
+      if(level_load(&current_level,level_subset,level) != 0)
+        exit(1);
+      level_load_gfx(&current_level);
+      activate_bad_guys();
+      level_load_song(&current_level);
 
-  initgame();
-  loadshared();
-  set_defaults();
+    }
+    
+  player_init(&tux);
 
-  if(level_load(&current_level,level_subset,level) != 0)
-    exit(1);
-  level_load_gfx(&current_level);
-  activate_bad_guys();
-  level_load_song(&current_level);
-  if(st_gl_mode == ST_GL_PLAY)
+  if(st_gl_mode != ST_GL_TEST)
     load_hs();
 
-  player_init(&tux);
+  loadshared();
 
   if(st_gl_mode == ST_GL_PLAY)
     levelintro();
 
+
+    timer_init(&time_left,YES);
   start_timers();
 
+  if(st_gl_mode == ST_GL_LOAD_GAME)
+    loadgame(levelnb);
+
+
   /* --- MAIN GAME LOOP!!! --- */
 
   jump = NO;
@@ -585,10 +599,18 @@ int gameloop(char * subset, int levelnb, int mode)
   quit = 0;
   frame = 0;
   game_pause = 0;
-  timer_init(&fps_timer);
-  timer_init(&frame_timer);
+  timer_init(&fps_timer,YES);
+  timer_init(&frame_timer,YES);
   fps_cnt = 0;
 
+  /* Clear screen: */
+
+  clearscreen(0, 0, 0);
+  updatescreen();
+
+  while (SDL_PollEvent(&event))
+  {}
+
   game_draw();
   do
     {
@@ -618,13 +640,17 @@ int gameloop(char * subset, int levelnb, int mode)
             {
               switch (menu_check(&game_menu))
                 {
-                case 0:
+                case 2:
                   st_pause_ticks_stop();
                   break;
-                case 1:
-                  savegame();
+                case 3:
+                  update_load_save_game_menu(&save_game_menu, NO);
                   break;
                 case 4:
+                  update_load_save_game_menu(&load_game_menu, YES);
+                  break;
+                case 7:
+                  st_pause_ticks_stop();
                   done = 1;
                   break;
                 }
@@ -633,6 +659,14 @@ int gameloop(char * subset, int levelnb, int mode)
             {
               process_options_menu();
             }
+          else if(current_menu == &save_game_menu )
+            {
+              process_save_load_game_menu(YES);
+            }
+          else if(current_menu == &load_game_menu )
+            {
+              process_save_load_game_menu(NO);
+            }
         }
 
 
@@ -682,8 +716,8 @@ int gameloop(char * subset, int levelnb, int mode)
                 the results in SDL mode aren't perfect (thought the 100 FPS are reached), even on an AMD2500+. */
       if(last_update_time >= update_time - 12 && jump != YES )
         SDL_Delay(10);
-      //if((update_time - last_update_time) < 10)
-      //  SDL_Delay((11 - (update_time - last_update_time))/2);
+      /*if((update_time - last_update_time) < 10)
+          SDL_Delay((11 - (update_time - last_update_time))/2);*/
 
 
 
@@ -740,18 +774,12 @@ int gameloop(char * subset, int levelnb, int mode)
   unloadshared();
   arrays_free();
 
+  game_started = NO;
+
   return(quit);
 }
 
 
-/* Initialize the game stuff: */
-
-void initgame(void)
-{
-  score = 0;
-  distros = 0;
-}
-
 /* Load graphics/sounds shared between all levels: */
 
 void loadshared(void)
@@ -1135,7 +1163,7 @@ void loadshared(void)
 
   /* Herring song */
   herring_song_path = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) +
-                                      strlen("SALCON.MOD") + 8)); /* FIXME: We need a real herring_song! Thats a fake.:) */
+                                      strlen("SALCON.MOD") + 8));
 
   sprintf(herring_song_path, "%s/music/%s", DATA_PREFIX, "SALCON.MOD");
 
@@ -1375,7 +1403,7 @@ int issolid(float x, float y)
     {
       return YES;
     }
-
   return NO;
 }*/
 
@@ -1433,7 +1461,7 @@ void trybreakbrick(float x, float y)
         {
           /* Get a distro from it: */
 
-          add_bouncy_distro(((x + 1) / 32) * 32,
+          add_bouncy_distro(((int)(x + 1) / 32) * 32,
                             (int)(y / 32) * 32);
 
           if (counting_distros == NO)
@@ -1459,7 +1487,7 @@ void trybreakbrick(float x, float y)
 
       /* Replace it with broken bits: */
 
-      add_broken_brick(((x + 1) / 32) * 32,
+      add_broken_brick(((int)(x + 1) / 32) * 32,
                        (int)(y / 32) * 32);
 
 
@@ -1490,12 +1518,10 @@ void tryemptybox(float x, float y)
     {
       if (shape(x, y) == 'A')
         {
-       
-       DEBUG_MSG("Here I am");
-       
+
           /* Box with a distro! */
 
-          add_bouncy_distro(((x + 1) / 32) * 32,
+          add_bouncy_distro(((int)(x + 1) / 32) * 32,
                             (int)(y / 32) * 32 - 32);
 
           play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
@@ -1510,7 +1536,7 @@ void tryemptybox(float x, float y)
             {
               /* Tux is small, add mints! */
 
-              add_upgrade(((x + 1) / 32) * 32,
+              add_upgrade((int)((x + 1) / 32) * 32,
                           (int)(y / 32) * 32 - 32,
                           UPGRADE_MINTS);
             }
@@ -1518,7 +1544,7 @@ void tryemptybox(float x, float y)
             {
               /* Tux is big, add coffee: */
 
-              add_upgrade(((x + 1) / 32) * 32,
+              add_upgrade((int)((x + 1) / 32) * 32,
                           (int)(y / 32) * 32 - 32,
                           UPGRADE_COFFEE);
             }
@@ -1529,7 +1555,7 @@ void tryemptybox(float x, float y)
         {
           /* Add a golden herring */
 
-          add_upgrade(((x + 1) / 32) * 32,
+          add_upgrade((int)((x + 1) / 32) * 32,
                       (int)(y / 32) * 32 - 32,
                       UPGRADE_HERRING);
         }
@@ -1552,7 +1578,7 @@ void trygrabdistro(float x, float y, int bounciness)
 
       if (bounciness == BOUNCE)
         {
-          add_bouncy_distro(((x + 1) / 32) * 32,
+          add_bouncy_distro(((int)(x + 1) / 32) * 32,
                             (int)(y / 32) * 32);
         }
 
@@ -1611,13 +1637,13 @@ void drawstatus(void)
   text_draw(&white_text, "SCORE", 0, 0, 1, NO_UPDATE);
   text_draw(&gold_text, str, 96, 0, 1, NO_UPDATE);
 
-  if(st_gl_mode == ST_GL_PLAY)
+  if(st_gl_mode != ST_GL_TEST)
     {
       sprintf(str, "%d", hs_score);
       text_draw(&white_text, "HIGH", 0, 20, 1, NO_UPDATE);
       text_draw(&gold_text, str, 96, 20, 1, NO_UPDATE);
     }
-  else if(st_gl_mode == ST_GL_TEST)
+  else
     {
       text_draw(&white_text,"Press ESC To Return",0,20,1, NO_UPDATE);
     }
@@ -1685,61 +1711,63 @@ void drawresultscreen(void)
   SDL_Delay(2000);
 }
 
-void savegame(void)
+void savegame(int slot)
 {
-  char savefile[300];
-  time_t current_time = time(NULL);
-  struct tm* time_struct;
+  char savefile[1024];
   FILE* fi;
+  unsigned int ui;
 
-  time_struct = localtime(&current_time);
-  sprintf(savefile,"%s/%d-%d-%d-%d.save",st_save_dir,time_struct->tm_year+1900,time_struct->tm_mon,time_struct->tm_mday,time_struct->tm_hour);
-  printf("%s",savefile);
-
+  sprintf(savefile,"%s/slot%d.save",st_save_dir,slot);
 
   fi = fopen(savefile, "wb");
 
   if (fi == NULL)
     {
-      fprintf(stderr, "Warning: I could not open the high score file ");
+      fprintf(stderr, "Warning: I could not open the slot file ");
 
     }
   else
     {
-      fwrite(&level,4,1,fi);
-      fwrite(&score,4,1,fi);
-      fwrite(&distros,4,1,fi);
-      fwrite(&tux.base.x,4,1,fi);
-      fwrite(&tux.base.y,4,1,fi);
-      fwrite(&scroll_x,4,1,fi);
-      fwrite(&current_level.time_left,4,1,fi);
+      fputs(level_subset, fi);
+      fputs("\n", fi);
+      fwrite(&level,sizeof(int),1,fi);
+      fwrite(&score,sizeof(int),1,fi);
+      fwrite(&distros,sizeof(int),1,fi);
+      fwrite(&tux,sizeof(player_type),1,fi);
+      fwrite(&scroll_x,sizeof(float),1,fi);
+      fwrite(&time_left,sizeof(float),1,fi);
+      ui = st_get_ticks();
+      fwrite(&ui,sizeof(int),1,fi);
     }
   fclose(fi);
 
 }
 
-void loadgame(char* filename)
+void loadgame(int slot)
 {
-  char savefile[300];
+  char savefile[1024];
+  char str[100];
   FILE* fi;
-  time_t current_time = time(NULL);
-  struct tm* time_struct;
-
-  time_struct = localtime(&current_time);
-  sprintf(savefile,"%s/%d-%d-%d-%d.save",st_save_dir,time_struct->tm_year+1900,time_struct->tm_mon,time_struct->tm_mday,time_struct->tm_hour);
-  printf("%s",savefile);
+  unsigned int ui;
 
+  sprintf(savefile,"%s/slot%d.save",st_save_dir,slot);
 
   fi = fopen(savefile, "rb");
 
   if (fi == NULL)
     {
-      fprintf(stderr, "Warning: I could not open the high score file ");
+      fprintf(stderr, "Warning: I could not open the slot file ");
 
     }
   else
     {
-      player_level_begin(&tux);
+
+
+      fgets(str, 100, fi);
+      strcpy(level_subset, str);
+      level_subset[strlen(level_subset)-1] = '\0';
+      fread(&level,sizeof(int),1,fi);
+
       set_defaults();
       level_free(&current_level);
       if(level_load(&current_level,level_subset,level) != 0)
@@ -1752,16 +1780,58 @@ void loadgame(char* filename)
       level_free_song();
       level_load_song(&current_level);
       levelintro();
-      start_timers();
+      timer_start(&time_left,current_level.time_left*1000);
+      update_time = st_get_ticks();
 
-      fread(&level,4,1,fi);
-      fread(&score,4,1,fi);
-      fread(&distros,4,1,fi);
-      fread(&tux.base.x,4,1,fi);
-      fread(&tux.base.y,4,1,fi);
-      fread(&scroll_x,4,1,fi);
-      fread(&current_level.time_left,4,1,fi);
+      fread(&score,sizeof(int),1,fi);
+      fread(&distros,sizeof(int),1,fi);
+      fread(&tux,sizeof(player_type),1,fi);
+      fread(&scroll_x,sizeof(float),1,fi);
+      fread(&time_left,sizeof(float),1,fi);
+      fread(&ui,sizeof(int),1,fi);
+      time_left.time += st_get_ticks() - ui;
+      tux.invincible_timer.time += st_get_ticks() - ui;
+      tux.skidding_timer.time += st_get_ticks() - ui;
+      tux.safe_timer.time += st_get_ticks() - ui;
+      tux.vphysic.start_time += st_get_ticks() - ui;
+      tux.hphysic.start_time += st_get_ticks() - ui;
+      tux.vphysic.start_time += st_get_ticks() - ui;
       fclose(fi);
     }
 
 }
+
+void slotinfo(char **pinfo, int slot)
+{
+  FILE* fi;
+  char slotfile[1024];
+  char tmp[200];
+  char str[5];
+  int slot_level;
+  sprintf(slotfile,"%s/slot%d.save",st_save_dir,slot);
+
+  fi = fopen(slotfile, "rb");
+
+  sprintf(tmp,"Slot %d - ",slot);
+
+  if (fi == NULL)
+    {
+      strcat(tmp,"Free");
+    }
+  else
+    {
+      fgets(str, 100, fi);
+      str[strlen(str)-1] = '\0';
+      strcat(tmp, str);
+      strcat(tmp, " / Level:");
+      fread(&slot_level,sizeof(int),1,fi);
+      sprintf(str,"%d",slot_level);
+      strcat(tmp,str);
+      fclose(fi);
+    }
+
+  *pinfo = (char*) malloc(sizeof(char) * (strlen(tmp)+1));
+  strcpy(*pinfo,tmp);
+
+}
+