added experimental support for demo playback (together with an example demo...)
[supertux.git] / src / game_session.h
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Bill Kendrick <bill@newbreedsoftware.com>
5 //                     Tobias Glaesser <tobi.web@gmx.de>
6 //                     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 #ifndef SUPERTUX_GAMELOOP_H
22 #define SUPERTUX_GAMELOOP_H
23
24 #include <SDL.h>
25 #include "timer.h"
26 #include "statistics.h"
27
28 using namespace SuperTux;
29
30 /* GameLoop modes */
31
32 enum GameSessionMode {
33   ST_GL_PLAY,
34   ST_GL_TEST,
35   ST_GL_LOAD_GAME,
36   ST_GL_LOAD_LEVEL_FILE,
37   ST_GL_DEMO_GAME
38 };
39
40 enum GameMenuIDs {
41   MNID_CONTINUE,
42   MNID_ABORTLEVEL
43 };
44
45 extern int game_started;
46
47 class Level;
48 class Sector;
49 class Statistics;
50 class DrawingContext;
51 class CodeController;
52
53 /** The GameSession class controlls the controll flow of a World, ie.
54     present the menu on specifc keypresses, render and update it while
55     keeping the speed and framerate sane, etc. */
56 class GameSession
57 {
58 public:
59   enum ExitStatus { ES_NONE, ES_LEVEL_FINISHED, ES_GAME_OVER, ES_LEVEL_ABORT };
60
61 public:
62   DrawingContext* context;
63   Timer2 time_left;
64
65   GameSession(const std::string& levelfile, GameSessionMode mode,
66               Statistics* statistics=0);
67   ~GameSession();
68
69   /** Enter the busy loop */
70   ExitStatus run();
71
72   void record_demo(const std::string& filename);
73   void play_demo(const std::string& filename);
74   void draw();
75   void action(float frame_ratio);
76
77   void set_current()
78   { current_ = this; }
79   static GameSession* current() { return current_; }
80
81   void respawn(const std::string& sectorname,
82       const std::string& spawnpointname);
83   void set_reset_point(const std::string& sectorname,
84       const Vector& pos);
85   void display_info_box(const std::string& text);
86   Sector* get_current_sector()
87   { return currentsector; }
88
89   void start_sequence(const std::string& sequencename);
90   /// called by JoystickKeyboardController after an ascii key has been pressed
91   void try_cheats();
92   
93 private:
94   void restart_level();
95
96   void check_end_conditions();
97   void start_timers();
98   void process_events();
99   void capture_demo_step();
100
101   void levelintro();
102   void drawstatus(DrawingContext& context);
103   void drawendscreen();
104   void drawresultscreen();
105   void draw_pause();
106
107   void on_escape_press();
108   void process_menu();
109
110   Timer2 endsequence_timer;
111   Level* level;
112   Sector* currentsector;
113
114   GameSessionMode mode;
115   int levelnb;
116   float fps_fps;
117   int pause_menu_frame;
118
119   /** If true the end_sequence will be played, user input will be
120       ignored while doing that */
121   enum EndSequenceState {
122     NO_ENDSEQUENCE,
123     ENDSEQUENCE_RUNNING, // tux is running right
124     ENDSEQUENCE_WAITING  // waiting for the end of the music
125   };
126   EndSequenceState end_sequence;
127   float last_x_pos;
128   CodeController* end_sequence_controller;
129
130   bool game_pause;
131
132   std::string levelfile;
133
134   // reset point (the point where tux respawns if he dies)
135   std::string reset_sector;
136   Vector reset_pos;
137
138   // the sector and spawnpoint we should spawn after this frame
139   std::string newsector;
140   std::string newspawnpoint;
141
142   static GameSession* current_;
143
144   Statistics* best_level_statistics;
145   ExitStatus exit_status;
146
147   std::ostream* capture_demo_stream;
148   std::string capture_file;
149   std::istream* playback_demo_stream;
150   CodeController* demo_controller;
151 };
152
153 std::string slotinfo(int slot);
154
155 /** Return true if the gameloop() was entered, false otherwise */
156 bool process_load_game_menu();
157
158 #endif /*SUPERTUX_GAMELOOP_H*/
159