refactored some supertux mainloops
[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 "screen.hpp"
27 #include "timer.hpp"
28 #include "statistics.hpp"
29 #include "math/vector.hpp"
30 #include "console.hpp"
31
32 /* GameLoop modes */
33 enum GameSessionMode {
34   ST_GL_PLAY,
35   ST_GL_TEST,
36   ST_GL_LOAD_GAME,
37   ST_GL_LOAD_LEVEL_FILE,
38   ST_GL_DEMO_GAME
39 };
40
41 enum GameMenuIDs {
42   MNID_CONTINUE,
43   MNID_ABORTLEVEL
44 };
45
46 extern int game_started;
47
48 class Level;
49 class Sector;
50 class Statistics;
51 class DrawingContext;
52 class CodeController;
53
54 /**
55  * The GameSession class controlls the controll flow of the Game (the part
56  * where you actually play a level)
57  */
58 class GameSession : public Screen, public ConsoleCommandReceiver
59 {
60 public:
61   GameSession(const std::string& levelfile, GameSessionMode mode,
62               Statistics* statistics = NULL);
63   ~GameSession();
64
65   void record_demo(const std::string& filename);
66   void play_demo(const std::string& filename);
67
68   void draw(DrawingContext& context);
69   void update(float frame_ratio);
70   void setup();
71
72   void set_current()
73   { current_ = this; }
74   static GameSession* current()
75   { return current_; }
76
77   /// ends the current level
78   void finish(bool win = true);
79   void respawn(const std::string& sectorname,
80       const std::string& spawnpointname);
81   void set_reset_point(const std::string& sectorname,
82       const Vector& pos);
83   void display_info_box(const std::string& text);
84   
85   Sector* get_current_sector()
86   { return currentsector; }
87
88   Level* get_current_level()
89   { return level; }
90
91   void start_sequence(const std::string& sequencename);
92
93   /** returns the "working directory" usually this is the directory where the
94    * currently played level resides. This is used when locating additional
95    * resources for the current level/world
96    */
97   std::string get_working_directory();
98   bool consoleCommand(std::string command); /**< callback from Console; return false if command was unknown, true otherwise */
99
100 private:
101   void restart_level(bool fromBeginning = true);
102
103   void check_end_conditions();
104   void process_events();
105   void capture_demo_step();
106
107   void levelintro();
108   void drawstatus(DrawingContext& context);
109   void draw_pause(DrawingContext& context);
110
111   void on_escape_press();
112   void process_menu();
113
114   Timer endsequence_timer;
115   Level* level;
116   Sector* currentsector;
117
118   GameSessionMode mode;
119   int levelnb;
120   float fps_fps;
121   int pause_menu_frame;
122
123   /** If true the end_sequence will be played, user input will be
124       ignored while doing that */
125   enum EndSequenceState {
126     NO_ENDSEQUENCE,
127     ENDSEQUENCE_RUNNING, // tux is running right
128     ENDSEQUENCE_WAITING  // waiting for the end of the music
129   };
130   EndSequenceState end_sequence;
131   float last_x_pos;
132   CodeController* end_sequence_controller;
133
134   bool game_pause;
135
136   std::string levelfile;
137
138   // reset point (the point where tux respawns if he dies)
139   std::string reset_sector;
140   Vector reset_pos;
141
142   // the sector and spawnpoint we should spawn after this frame
143   std::string newsector;
144   std::string newspawnpoint;
145
146   static GameSession* current_;
147
148   Statistics* best_level_statistics;
149
150   std::ostream* capture_demo_stream;
151   std::string capture_file;
152   std::istream* playback_demo_stream;
153   CodeController* demo_controller;
154   Console* console;
155 };
156
157 #endif /*SUPERTUX_GAMELOOP_H*/
158