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