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