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