Check if this fixes the jamming.
[supertux.git] / src / gameloop.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU General Public License
10 //  as published by the Free Software Foundation; either version 2
11 //  of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 // 
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
22 #include <iostream>
23 #include <sstream>
24 #include <cassert>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <cmath>
28 #include <cstring>
29 #include <cerrno>
30 #include <unistd.h>
31 #include <ctime>
32
33 #include "SDL.h"
34
35 #ifndef WIN32
36 #include <sys/types.h>
37 #include <ctype.h>
38 #endif
39
40 #include "defines.h"
41 #include "app/globals.h"
42 #include "gameloop.h"
43 #include "video/screen.h"
44 #include "app/setup.h"
45 #include "high_scores.h"
46 #include "gui/menu.h"
47 #include "badguy.h"
48 #include "sector.h"
49 #include "special.h"
50 #include "player.h"
51 #include "level.h"
52 #include "scene.h"
53 #include "collision.h"
54 #include "tile.h"
55 #include "particlesystem.h"
56 #include "resources.h"
57 #include "background.h"
58 #include "tilemap.h"
59 #include "app/gettext.h"
60 #include "worldmap.h"
61 #include "intro.h"
62 #include "misc.h"
63
64 GameSession* GameSession::current_ = 0;
65
66 GameSession::GameSession(const std::string& levelname_, int mode, bool flip_level_)
67   : level(0), currentsector(0), st_gl_mode(mode),
68     end_sequence(NO_ENDSEQUENCE), levelname(levelname_), flip_level(flip_level_)
69 {
70   current_ = this;
71   
72   global_frame_counter = 0;
73   game_pause = false;
74   fps_fps = 0;
75
76   fps_timer.init(true);            
77   frame_timer.init(true);
78   frame_rate.set_fps(100);
79
80   context = new DrawingContext();
81
82   if(flip_levels_mode)
83     flip_level = true;
84
85   restart_level();
86 }
87
88 void
89 GameSession::restart_level()
90 {
91   game_pause   = false;
92   exit_status  = ES_NONE;
93   end_sequence = NO_ENDSEQUENCE;
94
95   fps_timer.init(true);
96   frame_timer.init(true);
97
98 #if 0
99   float old_x_pos = -1;
100   if (world)
101     { // Tux has lost a life, so we try to respawn him at the nearest reset point
102       old_x_pos = world->get_tux()->base.x;
103     }
104 #endif
105   
106   delete level;
107   currentsector = 0;
108
109   level = new Level;
110   level->load(levelname);
111   if(flip_level)
112     level->do_vertical_flip();
113   currentsector = level->get_sector("main");
114   if(!currentsector)
115     Termination::abort("Level has no main sector.", "");
116   currentsector->activate("main");
117
118 #if 0 // TODO
119   // Set Tux to the nearest reset point
120   if (old_x_pos != -1)
121     {
122       ResetPoint best_reset_point = { -1, -1 };
123       for(std::vector<ResetPoint>::iterator i = get_level()->reset_points.begin();
124           i != get_level()->reset_points.end(); ++i)
125         {
126           if (i->x < old_x_pos && best_reset_point.x < i->x)
127             best_reset_point = *i;
128         }
129       
130       if (best_reset_point.x != -1)
131         {
132           world->get_tux()->base.x = best_reset_point.x;
133           world->get_tux()->base.y = best_reset_point.y;
134         }
135     }
136 #endif
137     
138   if (st_gl_mode != ST_GL_DEMO_GAME)
139     {
140       if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
141         levelintro();
142     }
143
144   time_left.init(true);
145   start_timers();
146   currentsector->play_music(LEVEL_MUSIC);
147 }
148
149 GameSession::~GameSession()
150 {
151   delete level;
152   delete context;
153 }
154
155 void
156 GameSession::levelintro(void)
157 {
158   SoundManager::get()->halt_music();
159   
160   char str[60];
161
162   DrawingContext context;
163   currentsector->background->draw(context);
164
165   context.draw_text_center(gold_text, level->get_name(), Vector(0, 220),
166       LAYER_FOREGROUND1);
167
168   sprintf(str, "TUX x %d", player_status.lives);
169   context.draw_text_center(white_text, str, Vector(0, 240),
170       LAYER_FOREGROUND1);
171
172   if(level->get_author().size())
173     context.draw_text_center(white_small_text,
174       std::string(_("by ")) + level->get_author(), 
175       Vector(0, 400), LAYER_FOREGROUND1);
176
177
178   if(flip_level)
179     context.draw_text_center(white_text,
180       _("Level Vertically Flipped!"),
181       Vector(0, 310), LAYER_FOREGROUND1);
182
183   context.do_drawing();
184
185   SDL_Event event;
186   wait_for_event(event,1000,3000,true);
187 }
188
189 /* Reset Timers */
190 void
191 GameSession::start_timers()
192 {
193   time_left.start(level->time_left*1000);
194   Ticks::pause_init();
195   frame_rate.start();
196 }
197
198 void
199 GameSession::on_escape_press()
200 {
201   if(currentsector->player->dying || end_sequence != NO_ENDSEQUENCE)
202     return;   // don't let the player open the menu, when he is dying
203   
204   if(game_pause)
205     return;
206
207   if(st_gl_mode == ST_GL_TEST)
208     {
209       exit_status = ES_LEVEL_ABORT;
210     }
211   else if (!Menu::current())
212     {
213       /* Tell Tux that the keys are all down, otherwise
214         it could have nasty bugs, like going allways to the right
215         or whatever that key does */
216       Player& tux = *(currentsector->player);
217       tux.key_event((SDLKey)keymap.jump, UP);
218       tux.key_event((SDLKey)keymap.duck, UP);
219       tux.key_event((SDLKey)keymap.left, UP);
220       tux.key_event((SDLKey)keymap.right, UP);
221       tux.key_event((SDLKey)keymap.fire, UP);
222
223       Menu::set_current(game_menu);
224       Ticks::pause_start();
225     }
226 }
227
228 void
229 GameSession::process_events()
230 {
231   if (end_sequence != NO_ENDSEQUENCE)
232     {
233       Player& tux = *currentsector->player;
234          
235       tux.input.fire  = UP;
236       tux.input.left  = UP;
237       tux.input.right = DOWN;
238       tux.input.down  = UP; 
239
240       if (int(last_x_pos) == int(tux.base.x))
241         tux.input.up    = DOWN; 
242       else
243         tux.input.up    = UP; 
244
245       last_x_pos = tux.base.x;
246
247       SDL_Event event;
248       while (SDL_PollEvent(&event))
249         {
250           /* Check for menu-events, if the menu is shown */
251           if (Menu::current())
252             {
253               Menu::current()->event(event);
254               if(!Menu::current())
255               Ticks::pause_stop();
256             }
257
258           switch(event.type)
259             {
260             case SDL_QUIT:        /* Quit event - quit: */
261               Termination::abort("Received window close", "");
262               break;
263               
264             case SDL_KEYDOWN:     /* A keypress! */
265               {
266                 SDLKey key = event.key.keysym.sym;
267            
268                 switch(key)
269                   {
270                   case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
271                     on_escape_press();
272                     break;
273                   default:
274                     break;
275                   }
276               }
277           
278             case SDL_JOYBUTTONDOWN:
279               if (event.jbutton.button == joystick_keymap.start_button)
280                 on_escape_press();
281               break;
282             }
283         }
284     }
285   else // normal mode
286     {
287       if(!Menu::current() && !game_pause)
288         Ticks::pause_stop();
289
290       SDL_Event event;
291       while (SDL_PollEvent(&event))
292         {
293           /* Check for menu-events, if the menu is shown */
294           if (Menu::current())
295             {
296               Menu::current()->event(event);
297               if(!Menu::current())
298                 Ticks::pause_stop();
299             }
300           else
301             {
302               Player& tux = *currentsector->player;
303   
304               switch(event.type)
305                 {
306                 case SDL_QUIT:        /* Quit event - quit: */
307                   Termination::abort("Received window close", "");
308                   break;
309
310                 case SDL_KEYDOWN:     /* A keypress! */
311                   {
312                     SDLKey key = event.key.keysym.sym;
313             
314                     if(tux.key_event(key,DOWN))
315                       break;
316
317                     switch(key)
318                       {
319                       case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
320                         on_escape_press();
321                         break;
322                       default:
323                         break;
324                       }
325                   }
326                   break;
327                 case SDL_KEYUP:      /* A keyrelease! */
328                   {
329                     SDLKey key = event.key.keysym.sym;
330
331                     if(tux.key_event(key, UP))
332                       break;
333
334                     switch(key)
335                       {
336                       case SDLK_a:
337                         if(debug_mode)
338                         {
339                           char buf[160];
340                           snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
341                               tux.base.x, tux.base.y);
342                           context->draw_text(white_text, buf,
343                               Vector(0, screen->h - white_text->get_height()),
344                               LAYER_FOREGROUND1);
345                           context->do_drawing();
346                           SDL_Delay(1000);
347                         }
348                         break;
349                       case SDLK_p:
350                         if(!Menu::current())
351                           {
352                             if(game_pause)
353                               {
354                                 game_pause = false;
355                                 Ticks::pause_stop();
356                               }
357                             else
358                               {
359                                 game_pause = true;
360                                 Ticks::pause_start();
361                               }
362                           }
363                         break;
364                       case SDLK_TAB:
365                         if(debug_mode)
366                           {
367                             tux.grow(false);
368                           }
369                         break;
370                       case SDLK_END:
371                         if(debug_mode)
372                           player_status.distros += 50;
373                         break;
374                       case SDLK_DELETE:
375                         if(debug_mode)
376                           tux.got_power = tux.FIRE_POWER;
377                         break;
378                       case SDLK_HOME:
379                         if(debug_mode)
380                           tux.got_power = tux.ICE_POWER;
381                         break;
382                       case SDLK_INSERT:
383                         if(debug_mode)
384                           tux.invincible_timer.start(TUX_INVINCIBLE_TIME);
385                         break;
386                       case SDLK_l:
387                         if(debug_mode)
388                           --player_status.lives;
389                         break;
390                       case SDLK_s:
391                         if(debug_mode)
392                           player_status.score += 1000;
393                       case SDLK_f:
394                         if(debug_fps)
395                           debug_fps = false;
396                         else
397                           debug_fps = true;
398                         break;
399                       default:
400                         break;
401                       }
402                   }
403                   break;
404
405                 case SDL_JOYAXISMOTION:
406                   if (event.jaxis.axis == joystick_keymap.x_axis)
407                     {
408                       if (event.jaxis.value < -joystick_keymap.dead_zone)
409                         {
410                           tux.input.left  = DOWN;
411                           tux.input.right = UP;
412                         }
413                       else if (event.jaxis.value > joystick_keymap.dead_zone)
414                         {
415                           tux.input.left  = UP;
416                           tux.input.right = DOWN;
417                         }
418                       else
419                         {
420                           tux.input.left  = DOWN;
421                           tux.input.right = DOWN;
422                         }
423                     }
424                   else if (event.jaxis.axis == joystick_keymap.y_axis)
425                     {
426                       if (event.jaxis.value > joystick_keymap.dead_zone)
427                         tux.input.down = DOWN;
428                       else if (event.jaxis.value < -joystick_keymap.dead_zone)
429                         tux.input.down = UP;
430                       else
431                         tux.input.down = UP;
432                     }
433                   break;
434             
435                 case SDL_JOYBUTTONDOWN:
436                   if (event.jbutton.button == joystick_keymap.a_button)
437                     tux.input.up = DOWN;
438                   else if (event.jbutton.button == joystick_keymap.b_button)
439                     tux.input.fire = DOWN;
440                   else if (event.jbutton.button == joystick_keymap.start_button)
441                     on_escape_press();
442                   break;
443                 case SDL_JOYBUTTONUP:
444                   if (event.jbutton.button == joystick_keymap.a_button)
445                     tux.input.up = UP;
446                   else if (event.jbutton.button == joystick_keymap.b_button)
447                     tux.input.fire = UP;
448                   break;
449
450                 default:
451                   break;
452                 }  /* switch */
453             }
454         } /* while */
455     }
456 }
457
458 void
459 GameSession::check_end_conditions()
460 {
461   Player* tux = currentsector->player;
462
463   /* End of level? */
464   Tile* endtile = collision_goal(tux->base);
465
466   if(end_sequence && !endsequence_timer.check())
467     {
468       exit_status = ES_LEVEL_FINISHED;
469       return;
470     }
471   else if(end_sequence == ENDSEQUENCE_RUNNING && endtile && endtile->data >= 1)
472     {
473       end_sequence = ENDSEQUENCE_WAITING;
474     }
475   else if(!end_sequence && endtile && endtile->data == 0)
476     {
477       end_sequence = ENDSEQUENCE_RUNNING;
478       last_x_pos = -1;
479       SoundManager::get()->play_music(level_end_song, 0);
480       endsequence_timer.start(7000); // 5 seconds until we finish the map
481       tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.)
482     }
483   else if (!end_sequence && tux->is_dead())
484     {
485       player_status.bonus = PlayerStatus::NO_BONUS;
486
487       if (player_status.lives < 0)
488         { // No more lives!?
489           exit_status = ES_GAME_OVER;
490         }
491       else
492         { // Still has lives, so reset Tux to the levelstart
493           restart_level();
494         }
495
496       return;
497     }
498 }
499
500 void
501 GameSession::action(double frame_ratio)
502 {
503   if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
504     {
505       // Update Tux and the World
506       currentsector->action(frame_ratio);
507     }
508
509   // respawning in new sector?
510   if(newsector != "" && newspawnpoint != "") {
511     Sector* sector = level->get_sector(newsector);
512     currentsector = sector;
513     currentsector->activate(newspawnpoint);
514     currentsector->play_music(LEVEL_MUSIC);
515     newsector = newspawnpoint = "";
516   }
517 }
518
519 void 
520 GameSession::draw()
521 {
522   currentsector->draw(*context);
523   drawstatus(*context);
524
525   if(game_pause)
526     {
527       int x = screen->h / 20;
528       for(int i = 0; i < x; ++i)
529         {
530           context->draw_filled_rect(
531               Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
532                 -((pause_menu_frame * i)%screen->w)
533                 ,(i*20+pause_menu_frame)%screen->h),
534               Vector(screen->w,10),
535               Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
536         }
537       context->draw_filled_rect(
538           Vector(0,0), Vector(screen->w, screen->h),
539           Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
540       context->draw_text_center(blue_text, _("PAUSE - Press 'P' To Play"),
541           Vector(0, 230), LAYER_FOREGROUND1+2);
542
543       char str1[60];
544       char str2[124];
545       sprintf(str1, _("Playing: "));
546       sprintf(str2, level->name.c_str());
547
548       context->draw_text(blue_text, str1,
549           Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
550           LAYER_FOREGROUND1+2);
551       context->draw_text(white_text, str2,
552           Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
553           LAYER_FOREGROUND1+2);
554     }
555
556   if(Menu::current())
557     {
558       Menu::current()->draw(*context);
559       mouse_cursor->draw(*context);
560     }
561
562   context->do_drawing();
563 }
564
565 void
566 GameSession::process_menu()
567 {
568   Menu* menu = Menu::current();
569   if(menu)
570     {
571       menu->action();
572
573       if(menu == game_menu)
574         {
575           switch (game_menu->check())
576             {
577             case MNID_CONTINUE:
578               Ticks::pause_stop();
579               break;
580             case MNID_ABORTLEVEL:
581               Ticks::pause_stop();
582               exit_status = ES_LEVEL_ABORT;
583               break;
584             }
585         }
586       else if(menu == options_menu)
587         {
588           process_options_menu();
589         }
590       else if(menu == load_game_menu )
591         {
592           process_load_game_menu();
593         }
594     }
595 }
596
597 GameSession::ExitStatus
598 GameSession::run()
599 {
600   Menu::set_current(0);
601   current_ = this;
602   
603   int fps_cnt = 0;
604
605   frame_rate.start();
606
607   // Eat unneeded events
608   SDL_Event event;
609   while (SDL_PollEvent(&event)) {}
610
611   draw();
612
613   while (exit_status == ES_NONE)
614     {
615       /* Calculate the movement-factor */
616       double frame_ratio = frame_rate.get();
617
618       if(!frame_timer.check())
619         {
620           frame_timer.start(25);
621           ++global_frame_counter;
622         }
623
624       /* Handle events: */
625       currentsector->player->input.old_fire 
626         = currentsector->player->input.fire;
627
628       process_events();
629       process_menu();
630
631       // Update the world state and all objects in the world
632       // Do that with a constante time-delta so that the game will run
633       // determistic and not different on different machines
634       if(!game_pause && !Menu::current())
635         {
636           // Update the world
637           check_end_conditions();
638           if (end_sequence == ENDSEQUENCE_RUNNING)
639              action(frame_ratio/2);
640           else if(end_sequence == NO_ENDSEQUENCE)
641              action(frame_ratio);
642         }
643       else
644         {
645           ++pause_menu_frame;
646           SDL_Delay(50);
647         }
648
649       draw();
650
651       /* Time stops in pause mode */
652       if(game_pause || Menu::current())
653         {
654           continue;
655         }
656
657       frame_rate.update();
658
659       /* Handle time: */
660       if (!time_left.check() && currentsector->player->dying == DYING_NOT
661               && !end_sequence)
662         currentsector->player->kill(Player::KILL);
663
664       /* Handle music: */
665       if(currentsector->player->invincible_timer.check() && !end_sequence)
666         {
667           currentsector->play_music(HERRING_MUSIC);
668         }
669       /* are we low on time ? */
670       else if (time_left.get_left() < TIME_WARNING && !end_sequence)
671         {
672           currentsector->play_music(HURRYUP_MUSIC);
673         }
674       /* or just normal music? */
675       else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
676         {
677           currentsector->play_music(LEVEL_MUSIC);
678         }
679
680       /* Calculate frames per second */
681       if(show_fps)
682         {
683           ++fps_cnt;
684           fps_fps = (1000.0 / (float)fps_timer.get_gone()) * (float)fps_cnt;
685
686           if(!fps_timer.check())
687             {
688               fps_timer.start(1000);
689               fps_cnt = 0;
690             }
691         }
692     }
693   
694   return exit_status;
695 }
696
697 void
698 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
699 {
700   newsector = sector;
701   newspawnpoint = spawnpoint;
702 }
703
704 /* Bounce a brick: */
705 void bumpbrick(float x, float y)
706 {
707   Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32,
708                          (int)(y / 32) * 32));
709
710   SoundManager::get()->play_sound(IDToSound(SND_BRICK), Vector(x, y), Sector::current()->player->get_pos());
711 }
712
713 /* (Status): */
714 void
715 GameSession::drawstatus(DrawingContext& context)
716 {
717   char str[60];
718   
719   snprintf(str, 60, " %d", player_status.score);
720   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
721   context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
722
723   if(st_gl_mode == ST_GL_TEST)
724     {
725       context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
726           LAYER_FOREGROUND1);
727     }
728
729   if(!time_left.check()) {
730     context.draw_text_center(white_text, _("TIME's UP"), Vector(0, 0),
731         LAYER_FOREGROUND1);
732   } else if (time_left.get_left() > TIME_WARNING || (global_frame_counter % 10) < 5) {
733     sprintf(str, " %d", time_left.get_left() / 1000 );
734     context.draw_text_center(white_text, _("TIME"),
735         Vector(0, 0), LAYER_FOREGROUND1);
736     context.draw_text_center(gold_text, str,
737         Vector(4*16, 0), LAYER_FOREGROUND1);
738   }
739
740   sprintf(str, " %d", player_status.distros);
741   context.draw_text(white_text, _("COINS"),
742       Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width("   99"), 0),
743         LAYER_FOREGROUND1);
744   context.draw_text(gold_text, str,
745       Vector(screen->w - gold_text->get_text_width(" 99"), 0),LAYER_FOREGROUND1);
746
747   if (player_status.lives >= 5)
748     {
749       sprintf(str, "%dx", player_status.lives);
750       float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
751       context.draw_text(gold_text, str, Vector(x, 20), LAYER_FOREGROUND1);
752       context.draw_surface(tux_life, Vector(screen->w - 16, 20),
753           LAYER_FOREGROUND1);
754     }
755   else
756     {
757       for(int i= 0; i < player_status.lives; ++i)
758         context.draw_surface(tux_life, 
759             Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
760             LAYER_FOREGROUND1);
761     }
762
763   context.draw_text(white_text, _("LIVES"),
764       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 20),
765       LAYER_FOREGROUND1);
766
767   if(show_fps)
768     {
769       sprintf(str, "%2.1f", fps_fps);
770       context.draw_text(white_text, "FPS", 
771           Vector(screen->w - white_text->get_text_width("FPS     "), 40),
772           LAYER_FOREGROUND1);
773       context.draw_text(gold_text, str,
774           Vector(screen->w-4*16, 40), LAYER_FOREGROUND1);
775     }
776 }
777
778 void
779 GameSession::drawresultscreen(void)
780 {
781   char str[80];
782
783   DrawingContext context;
784   currentsector->background->draw(context);  
785
786   context.draw_text_center(blue_text, _("Result:"), Vector(0, 200),
787       LAYER_FOREGROUND1);
788
789   sprintf(str, _("SCORE: %d"), player_status.score);
790   context.draw_text_center(gold_text, str, Vector(0, 224), LAYER_FOREGROUND1);
791
792   sprintf(str, _("COINS: %d"), player_status.distros);
793   context.draw_text_center(gold_text, str, Vector(0, 256), LAYER_FOREGROUND1);
794
795   context.do_drawing();
796   
797   SDL_Event event;
798   wait_for_event(event,2000,5000,true);
799 }
800
801 std::string slotinfo(int slot)
802 {
803   std::string tmp;
804   std::string slotfile;
805   std::string title;
806   std::stringstream stream;
807   stream << slot;
808   slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
809
810   lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
811   if (savegame)
812     {
813       LispReader reader(lisp_cdr(savegame));
814       reader.read_string("title", title);
815       lisp_free(savegame);
816     }
817
818   if (access(slotfile.c_str(), F_OK) == 0)
819     {
820       if (!title.empty())
821         tmp = "Slot " + stream.str() + " - " + title;
822       else
823         tmp = "Slot " + stream.str() + " - Savegame";
824     }
825   else
826     tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
827
828   return tmp;
829 }
830
831 bool process_load_game_menu()
832 {
833   int slot = load_game_menu->check();
834
835   if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
836     {
837       std::stringstream stream;
838       stream << slot;
839       std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
840
841       if (access(slotfile.c_str(), F_OK) != 0)
842         {
843           draw_intro();
844         }
845
846       // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000);
847       fadeout(256);
848
849       DrawingContext context;
850       context.draw_text_center(white_text, "Loading...",
851                                Vector(0, screen->h/2), LAYER_FOREGROUND1);
852       context.do_drawing();
853
854       WorldMapNS::WorldMap worldmap;
855
856       // Load the game or at least set the savegame_file variable
857       worldmap.loadgame(slotfile);
858
859       worldmap.display();
860
861       Menu::set_current(main_menu);
862
863       Ticks::pause_stop();
864       return true;
865     }
866   else
867     {
868       return false;
869     }
870 }