ge211  2021.5.1
A student game engine
ge211_base.hxx
1 #pragma once
2 
3 #include "ge211_audio.hxx"
4 #include "ge211_color.hxx"
5 #include "ge211_error.hxx"
6 #include "ge211_event.hxx"
7 #include "ge211_forward.hxx"
8 #include "ge211_geometry.hxx"
9 #include "ge211_noexcept.hxx"
10 #include "ge211_random.hxx"
11 #include "ge211_resource.hxx"
12 #include "ge211_session.hxx"
13 #include "ge211_time.hxx"
14 
15 #include <memory>
16 #include <string>
17 
18 namespace ge211 {
19 
131 {
132 public:
133 
136  void run();
137 
143 
146  static const char* const default_window_title;
147 
152 
154  virtual ~Abstract_game() {}
155 
156 protected:
159 
170  virtual void draw(Sprite_set&) = 0;
171 
175  virtual void on_frame(double last_frame_seconds) {
176  (void) last_frame_seconds;
177  }
178 
183  virtual void on_key(Key) { }
184 
195  virtual void on_key_down(Key);
196 
199  virtual void on_key_up(Key) { }
200 
203 
205  virtual void on_mouse_up(Mouse_button, Posn<int>) { }
206 
208  virtual void on_mouse_move(Posn<int>) { }
209 
214  virtual void on_start() { }
215 
226  virtual void on_quit() { }
227 
231  virtual Dims<int> initial_window_dimensions() const;
232 
235  virtual std::string initial_window_title() const;
236 
238 
241 
243  void quit() NOEXCEPT;
244 
250  Window& get_window() const;
251 
254  Mixer& mixer() const
255  {
256  return *mixer_;
257  }
258 
263  { return frame_start_.start_time(); }
264 
268  { return prev_frame_length_; }
269 
273  double get_frame_rate() const NOEXCEPT
274  { return fps_; }
275 
277  double get_load_percent() const NOEXCEPT
278  { return load_; }
279 
289  void prepare(const sprites::Sprite&) const;
290 
292 
297 
298 private:
299  friend class detail::Engine;
300 
301  void mark_present_() NOEXCEPT;
302  void mark_frame_() NOEXCEPT;
303 
304  void poll_channels_();
305 
306  detail::Session session_;
307  detail::lazy_ptr<Mixer> mixer_;
308  detail::Engine* engine_ = nullptr;
309 
310  bool quit_ = false;
311 
312  Timer frame_start_;
313  Duration prev_frame_length_;
314 
315  double fps_ {0};
316  double load_ {100};
317 
318  int sample_counter_ {0};
319  Timer real_time_;
320  Pausable_timer busy_time_;
321 };
322 
323 }
ge211::Abstract_game::get_frame_rate
double get_frame_rate() const
Returns an approximation of the current frame rate in Hz.
Definition: ge211_base.hxx:273
ge211::Sprite_set
A collection of positioned Sprites ready to be rendered to the screen.
Definition: ge211_sprites.hxx:509
ge211::events::Mouse_button
Mouse_button
A representation of a mouse button.
Definition: ge211_event.hxx:20
std::string
ge211::time::Duration
A length of time.
Definition: ge211_time.hxx:31
ge211::Abstract_game::draw
virtual void draw(Sprite_set &)=0
You must override this function in the derived class to specify how to draw your scene.
ge211::Abstract_game::~Abstract_game
virtual ~Abstract_game()
Polymorphic classes should have virtual destructors.
Definition: ge211_base.hxx:154
ge211::Abstract_game::on_mouse_move
virtual void on_mouse_move(Posn< int >)
Called by the game engine each time the mouse moves.
Definition: ge211_base.hxx:208
ge211::Abstract_game::on_key_down
virtual void on_key_down(Key)
Called by the game engine each time a key is depressed.
Definition: ge211_base.cxx:83
ge211::Abstract_game::default_window_dimensions
static const Dims< int > default_window_dimensions
The default window dimensions, in pixels.
Definition: ge211_base.hxx:151
ge211::time::Time_point
A point in time.
Definition: ge211_time.hxx:175
ge211::Abstract_game::initial_window_title
virtual std::string initial_window_title() const
Override this function to specify the initial title of the game.
Definition: ge211_base.cxx:24
ge211::events::Key
Represents a key on the keyboard.
Definition: ge211_event.hxx:128
ge211
The game engine namespace.
Definition: ge211.hxx:4
ge211::sprites::Sprite
A sprite is an image that knows how to render itself to the screen at a given location,...
Definition: ge211_sprites.hxx:39
ge211::Abstract_game::initial_window_dimensions
virtual Dims< int > initial_window_dimensions() const
Override this function to specify the initial dimensions of the game's window.
Definition: ge211_base.cxx:19
ge211::Abstract_game::get_load_percent
double get_load_percent() const
Returns an approximation of the current machine load due to GE211.
Definition: ge211_base.hxx:277
ge211::geometry::Dims< int >
ge211::Window
Provides access to the game window and its properties.
Definition: ge211_window.hxx:17
ge211::Abstract_game::background_color
Color background_color
Assign this member variable to change the window's background color in subsequent frames.
Definition: ge211_base.hxx:296
ge211::Abstract_game::on_mouse_up
virtual void on_mouse_up(Mouse_button, Posn< int >)
Called by the game engine each time a mouse button is released.
Definition: ge211_base.hxx:205
ge211::Abstract_game::on_key
virtual void on_key(Key)
Called by the game engine for each keypress.
Definition: ge211_base.hxx:183
ge211::time::Timer
A class for timing intervals. The result is a Duration.
Definition: ge211_time.hxx:268
ge211::Abstract_game::mixer
Mixer & mixer() const
Gets access to the audio mixer, which can be used to play music and sound effects.
Definition: ge211_base.hxx:254
ge211::Abstract_game::default_window_title
static const char *const default_window_title
The default initial window title.
Definition: ge211_base.hxx:146
ge211::Abstract_game::on_mouse_down
virtual void on_mouse_down(Mouse_button, Posn< int >)
Called by the game engine each time a mouse button is depressed.
Definition: ge211_base.hxx:202
ge211::Abstract_game::get_frame_start_time
Time_point get_frame_start_time() const
Gets the time point at which the current frame started.
Definition: ge211_base.hxx:262
ge211::Abstract_game::default_background_color
static const Color default_background_color
The default background color of the window, if not changed by the derived class.
Definition: ge211_base.hxx:142
ge211::Abstract_game::on_start
virtual void on_start()
Called by the game engine after initializing the game but before commencing the event loop.
Definition: ge211_base.hxx:214
ge211::Abstract_game::get_window
Window & get_window() const
Gets the Window that the game is running in.
Definition: ge211_base.cxx:39
ge211::Color
For representing colors.
Definition: ge211_color.hxx:24
ge211::audio::Mixer
The entity that coordinates playing all audio tracks.
Definition: ge211_audio.hxx:191
ge211::Abstract_game::prepare
void prepare(const sprites::Sprite &) const
Prepares a sprites::Sprite for rendering, without actually including it in the scene.
Definition: ge211_base.cxx:47
ge211::geometry::Posn< int >
ge211::Abstract_game::on_quit
virtual void on_quit()
Called by the game engine after exiting the event loop but before the game instance is destroyed.
Definition: ge211_base.hxx:226
ge211::Abstract_game::run
void run()
Runs the game.
Definition: ge211_base.cxx:29
ge211::time::Timer::start_time
Time_point start_time() const
Returns the actual time when this timer was started or most recently reset.
Definition: ge211_time.hxx:296
ge211::Abstract_game::on_key_up
virtual void on_key_up(Key)
Called by the game engine each time a key is released.
Definition: ge211_base.hxx:199
ge211::Abstract_game::get_prev_frame_length
Duration get_prev_frame_length() const
Returns the duration of the frame right before the frame currently running.
Definition: ge211_base.hxx:267
ge211::Abstract_game::quit
void quit()
Causes the event loop to quit after the current frame finishes.
Definition: ge211_base.cxx:34
ge211::Abstract_game
This is the abstract base class for deriving games.
Definition: ge211_base.hxx:131
ge211::Abstract_game::on_frame
virtual void on_frame(double last_frame_seconds)
Called by the game engine once per frame.
Definition: ge211_base.hxx:175