Added a Jump 'n Bump like way to show statistics.
[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 "special/timer.h"
26 #include "special/base.h"
27 #include "special/frame_rate.h"
28
29 using namespace SuperTux;
30
31 /* GameLoop modes */
32
33 #define ST_GL_PLAY 0
34 #define ST_GL_TEST 1
35 #define ST_GL_LOAD_GAME 2
36 #define ST_GL_LOAD_LEVEL_FILE  3
37 #define ST_GL_DEMO_GAME  4
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
49 namespace SuperTux {
50 class DrawingContext;
51 }
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 private:
59   Timer fps_timer;
60   Timer frame_timer;
61   Timer endsequence_timer;
62   Level* level;
63   Sector* currentsector;
64
65   int st_gl_mode;
66   int levelnb;
67   float fps_fps;
68   FrameRate frame_rate;
69   int pause_menu_frame;
70
71   /** If true the end_sequence will be played, user input will be
72       ignored while doing that */
73   enum EndSequenceState {
74     NO_ENDSEQUENCE,
75     ENDSEQUENCE_RUNNING, // tux is running right
76     ENDSEQUENCE_WAITING  // waiting for the end of the music
77   };
78   EndSequenceState end_sequence;
79   float last_x_pos;
80
81   bool game_pause;
82
83   std::string levelname;
84   bool flip_level;
85
86   // the sector and spawnpoint we shoudl spawn after this frame
87   std::string newsector;
88   std::string newspawnpoint;
89
90 public:
91   enum ExitStatus { ES_NONE, ES_LEVEL_FINISHED, ES_GAME_OVER, ES_LEVEL_ABORT };
92 private:
93   ExitStatus exit_status;
94 public:
95   DrawingContext* context;
96   Timer time_left;
97
98   GameSession(const std::string& level, int mode, bool flip_level_ = false);
99   ~GameSession();
100
101   /** Enter the busy loop */
102   ExitStatus run();
103
104   void draw();
105   void action(double frame_ratio);
106
107   void set_current()
108   { current_ = this; }
109   static GameSession* current() { return current_; }
110
111   void respawn(const std::string& sectorname,
112       const std::string& spawnpointname);
113   Sector* get_current_sector()
114   { return currentsector; }
115   
116 private:
117   static GameSession* current_;
118
119   // for cheating
120   std::string last_keys;
121   // for fire works
122   Timer random_timer;
123
124   void restart_level();
125
126   void check_end_conditions();
127   void start_timers();
128   void process_events();
129
130   void levelintro();
131   void drawstatus(DrawingContext& context);
132   void drawendscreen();
133   void drawresultscreen(void);
134
135   void on_escape_press();
136   void process_menu();
137 };
138
139 std::string slotinfo(int slot);
140
141 bool rectcollision(base_type* one, base_type* two);
142 void bumpbrick(float x, float y);
143
144 /** Return true if the gameloop() was entered, false otherwise */
145 bool process_load_game_menu();
146
147 #endif /*SUPERTUX_GAMELOOP_H*/
148