59023b36ec8e751b96e973c915eb908aae177fbe
[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 <string>
25 #include <SDL.h>
26 #include "timer.h"
27 #include "statistics.h"
28 #include "math/vector.h"
29
30 /* GameLoop modes */
31 enum GameSessionMode {
32   ST_GL_PLAY,
33   ST_GL_TEST,
34   ST_GL_LOAD_GAME,
35   ST_GL_LOAD_LEVEL_FILE,
36   ST_GL_DEMO_GAME
37 };
38
39 enum GameMenuIDs {
40   MNID_CONTINUE,
41   MNID_ABORTLEVEL
42 };
43
44 extern int game_started;
45
46 class Level;
47 class Sector;
48 class Statistics;
49 class DrawingContext;
50 class CodeController;
51
52 /** The GameSession class controlls the controll flow of a World, ie.
53     present the menu on specifc keypresses, render and update it while
54     keeping the speed and framerate sane, etc. */
55 class GameSession
56 {
57 public:
58   enum ExitStatus { ES_NONE, ES_LEVEL_FINISHED, ES_GAME_OVER, ES_LEVEL_ABORT };
59
60 public:
61   DrawingContext* context;
62
63   GameSession(const std::string& levelfile, GameSessionMode mode,
64               Statistics* statistics=0);
65   ~GameSession();
66
67   /** Enter the busy loop */
68   ExitStatus run();
69
70   void record_demo(const std::string& filename);
71   void play_demo(const std::string& filename);
72   void draw();
73   void update(float frame_ratio);
74
75   void set_current()
76   { current_ = this; }
77   static GameSession* current() { return current_; }
78
79   /// ends the current level
80   void finish(bool win = true);
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   /** 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   
99 private:
100   void restart_level();
101
102   void check_end_conditions();
103   void process_events();
104   void capture_demo_step();
105
106   void levelintro();
107   void drawstatus(DrawingContext& context);
108   void drawendscreen();
109   void drawresultscreen();
110   void draw_pause();
111
112   void on_escape_press();
113   void process_menu();
114
115   Timer endsequence_timer;
116   Level* level;
117   Sector* currentsector;
118
119   GameSessionMode mode;
120   int levelnb;
121   float fps_fps;
122   int pause_menu_frame;
123
124   /** If true the end_sequence will be played, user input will be
125       ignored while doing that */
126   enum EndSequenceState {
127     NO_ENDSEQUENCE,
128     ENDSEQUENCE_RUNNING, // tux is running right
129     ENDSEQUENCE_WAITING  // waiting for the end of the music
130   };
131   EndSequenceState end_sequence;
132   float last_x_pos;
133   CodeController* end_sequence_controller;
134
135   bool game_pause;
136
137   std::string levelfile;
138
139   // reset point (the point where tux respawns if he dies)
140   std::string reset_sector;
141   Vector reset_pos;
142
143   // the sector and spawnpoint we should spawn after this frame
144   std::string newsector;
145   std::string newspawnpoint;
146
147   static GameSession* current_;
148
149   Statistics* best_level_statistics;
150   ExitStatus exit_status;
151
152   std::ostream* capture_demo_stream;
153   std::string capture_file;
154   std::istream* playback_demo_stream;
155   CodeController* demo_controller;
156 };
157
158 std::string slotinfo(int slot);
159
160 /** Return true if the gameloop() was entered, false otherwise */
161 bool process_load_game_menu();
162
163 #endif /*SUPERTUX_GAMELOOP_H*/
164