Ooops, forgot to change something after testing.
[supertux.git] / src / gameloop.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
22 #ifndef SUPERTUX_GAMELOOP_H
23 #define SUPERTUX_GAMELOOP_H
24
25 #include "sound.h"
26 #include "type.h"
27
28 /* GameLoop modes */
29
30 #define ST_GL_PLAY 0
31 #define ST_GL_TEST 1
32 #define ST_GL_LOAD_GAME 2
33 #define ST_GL_LOAD_LEVEL_FILE  3
34 #define ST_GL_DEMO_GAME  4
35
36 extern int game_started;
37
38 class Level;
39 class Sector;
40 class DrawingContext;
41
42 /** The GameSession class controlls the controll flow of a World, ie.
43     present the menu on specifc keypresses, render and update it while
44     keeping the speed and framerate sane, etc. */
45 class GameSession
46 {
47 private:
48   Timer fps_timer;
49   Timer frame_timer;
50   Timer endsequence_timer;
51   Level* level;
52   Sector* currentsector;
53
54   int st_gl_mode;
55   int levelnb;
56   float fps_fps;
57   unsigned int last_update_time;
58   unsigned int update_time;
59   int pause_menu_frame;
60   int debug_fps;
61
62   /** If true the end_sequence will be played, user input will be
63       ignored while doing that */
64   enum EndSequenceState {
65     NO_ENDSEQUENCE,
66     ENDSEQUENCE_RUNNING, // tux is running right
67     ENDSEQUENCE_WAITING  // waiting for the end of the music
68   };
69   EndSequenceState end_sequence;
70   float last_x_pos;
71
72   bool game_pause;
73
74   std::string levelname;
75   bool flip_level;
76
77   // the sector and spawnpoint we shoudl spawn after this frame
78   std::string newsector;
79   std::string newspawnpoint;
80 public:
81   enum ExitStatus { ES_NONE, ES_LEVEL_FINISHED, ES_GAME_OVER, ES_LEVEL_ABORT };
82 private:
83   ExitStatus exit_status;
84 public:
85   DrawingContext* context;
86   Timer time_left;
87
88   GameSession(const std::string& level, int mode, bool flip_level_ = false);
89   ~GameSession();
90
91   /** Enter the busy loop */
92   ExitStatus run();
93
94   void draw();
95   void action(double frame_ratio);
96
97   void set_current()
98   { current_ = this; }
99   static GameSession* current() { return current_; }
100
101   void respawn(const std::string& sectorname,
102       const std::string& spawnpointname);
103   Sector* get_current_sector()
104   { return currentsector; }
105   
106 private:
107   static GameSession* current_;
108
109   void restart_level();
110
111   void check_end_conditions();
112   void start_timers();
113   void process_events();
114
115   void levelintro();
116   void drawstatus(DrawingContext& context);
117   void drawendscreen();
118   void drawresultscreen(void);
119
120 private:
121   void on_escape_press();
122   void process_menu();
123 };
124
125 std::string slotinfo(int slot);
126
127 bool rectcollision(base_type* one, base_type* two);
128 void bumpbrick(float x, float y);
129
130 #endif /*SUPERTUX_GAMELOOP_H*/
131