830428df9a2f7cb1b2e173d34da90381d68d2c64
[supertux.git] / src / game_session.hpp
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 <string>
25 #include <SDL.h>
26 #include "timer.hpp"
27 #include "statistics.hpp"
28 #include "math/vector.hpp"
29 #include "console.hpp"
30
31 /* GameLoop modes */
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 : public ConsoleCommandReceiver
57 {
58 public:
59   enum ExitStatus { ES_NONE, ES_LEVEL_FINISHED, /*ES_GAME_OVER,*/ ES_LEVEL_ABORT };
60
61 public:
62   DrawingContext* context;
63
64   GameSession(const std::string& levelfile, GameSessionMode mode,
65               Statistics* statistics=0);
66   ~GameSession();
67
68   /** Enter the busy loop */
69   ExitStatus run();
70
71   void record_demo(const std::string& filename);
72   void play_demo(const std::string& filename);
73   void draw();
74   void update(float frame_ratio);
75
76   void set_current()
77   { current_ = this; }
78   static GameSession* current() { return current_; }
79
80   /// ends the current level
81   void finish(bool win = true);
82   void respawn(const std::string& sectorname,
83       const std::string& spawnpointname);
84   void set_reset_point(const std::string& sectorname,
85       const Vector& pos);
86   void display_info_box(const std::string& text);
87   Sector* get_current_sector()
88   { return currentsector; }
89
90   Level* get_current_level()
91   { return level; }
92
93   void start_sequence(const std::string& sequencename);
94
95   /** returns the "working directory" usually this is the directory where the
96    * currently played level resides. This is used when locating additional
97    * resources for the current level/world
98    */
99   std::string get_working_directory();
100   bool consoleCommand(std::string command); /**< callback from Console; return false if command was unknown, true otherwise */
101
102 private:
103   void restart_level(bool fromBeginning = true);
104
105   void check_end_conditions();
106   void process_events();
107   void capture_demo_step();
108
109   void levelintro();
110   void drawstatus(DrawingContext& context);
111   void drawendscreen();
112   void drawresultscreen();
113   void draw_pause();
114
115   void on_escape_press();
116   void process_menu();
117
118   Timer endsequence_timer;
119   Level* level;
120   Sector* currentsector;
121
122   GameSessionMode mode;
123   int levelnb;
124   float fps_fps;
125   int pause_menu_frame;
126
127   /** If true the end_sequence will be played, user input will be
128       ignored while doing that */
129   enum EndSequenceState {
130     NO_ENDSEQUENCE,
131     ENDSEQUENCE_RUNNING, // tux is running right
132     ENDSEQUENCE_WAITING  // waiting for the end of the music
133   };
134   EndSequenceState end_sequence;
135   float last_x_pos;
136   CodeController* end_sequence_controller;
137
138   bool game_pause;
139
140   std::string levelfile;
141
142   // reset point (the point where tux respawns if he dies)
143   std::string reset_sector;
144   Vector reset_pos;
145
146   // the sector and spawnpoint we should spawn after this frame
147   std::string newsector;
148   std::string newspawnpoint;
149
150   static GameSession* current_;
151
152   Statistics* best_level_statistics;
153   ExitStatus exit_status;
154
155   std::ostream* capture_demo_stream;
156   std::string capture_file;
157   std::istream* playback_demo_stream;
158   CodeController* demo_controller;
159   Console* console;
160 };
161
162 std::string slotinfo(int slot);
163
164 /** Return true if the gameloop() was entered, false otherwise */
165 bool process_load_game_menu();
166
167 #endif /*SUPERTUX_GAMELOOP_H*/
168