ge211
ge211_base.h
1 #pragma once
2 
3 #include "ge211_audio.h"
4 #include "ge211_color.h"
5 #include "ge211_error.h"
6 #include "ge211_event.h"
7 #include "ge211_forward.h"
8 #include "ge211_geometry.h"
9 #include "ge211_random.h"
10 #include "ge211_resource.h"
11 #include "ge211_session.h"
12 #include "ge211_time.h"
13 
14 #include <memory>
15 #include <string>
16 
17 namespace ge211 {
18 
95 {
96 public:
97 
100  void run();
101 
107 
110  static const char* const default_window_title;
111 
116 
117 protected:
120 
131  virtual void draw(Sprite_set&) = 0;
132 
136  virtual void on_frame(double last_frame_seconds) {
137  (void) last_frame_seconds;
138  }
139 
144  virtual void on_key(Key) { }
145 
156  virtual void on_key_down(Key);
157 
160  virtual void on_key_up(Key) { }
161 
164 
166  virtual void on_mouse_up(Mouse_button, Position) { }
167 
169  virtual void on_mouse_move(Position) { }
170 
175  virtual void on_start() { }
176 
187  virtual void on_quit() { }
188 
192  virtual Dimensions initial_window_dimensions() const;
193 
198  virtual std::string initial_window_title() const;
199 
201 
204 
206  void quit() noexcept;
207 
213  Window& get_window() const;
214 
218  Random& get_random() const noexcept;
219 
224  Mixer* get_mixer() const noexcept;
225 
230  { return frame_start_.start_time(); }
231 
235  { return prev_frame_length_; }
236 
240  double get_frame_rate() const noexcept
241  { return fps_; }
242 
252  void prepare(const sprites::Sprite&) const;
253 
255 
260 
261 private:
262  friend detail::Engine;
263 
264  void mark_frame_() noexcept;
265 
266  mutable Random rng_;
267  detail::Session session_;
268  std::unique_ptr<audio::Mixer> mixer_ = audio::Mixer::open_mixer();
269  detail::Engine* engine_ = nullptr;
270 
271  bool quit_ = false;
272 
273  Timer frame_start_;
274  Duration prev_frame_length_;
275  Timer fps_sample_start_;
276  int fps_sample_count_{0};
277  double fps_{0};
278 };
279 
280 }
Mouse_button
A representation of a mouse button.
Definition: ge211_event.h:18
Represents a key on the keyboard.
Definition: ge211_event.h:119
virtual void on_mouse_up(Mouse_button, Position)
Called by the game engine each time a mouse button is released.
Definition: ge211_base.h:166
virtual void on_key_down(Key)
Called by the game engine each time a key is depressed.
Definition: ge211_base.cpp:75
This is the abstract base class for deriving games.
Definition: ge211_base.h:94
A class for timing intervals. The result is a Duration.
Definition: ge211_time.h:242
virtual void on_mouse_down(Mouse_button, Position)
Called by the game engine each time a mouse button is depressed.
Definition: ge211_base.h:163
virtual std::string initial_window_title() const
Override this function to specify the initial title of the game.
Definition: ge211_base.cpp:24
The game engine namespace.
Definition: ge211.h:17
static const char *const default_window_title
The default initial window title.
Definition: ge211_base.h:110
A pseudo-random number generator.
Definition: ge211_random.h:63
virtual void on_start()
Called by the game engine after initializing the game but before commencing the event loop...
Definition: ge211_base.h:175
A point in time.
Definition: ge211_time.h:160
virtual void on_mouse_move(Position)
Called by the game engine each time the mouse moves.
Definition: ge211_base.h:169
Random & get_random() const noexcept
Gets the pseudo-random number generator associated with this game.
Definition: ge211_base.cpp:47
Mixer * get_mixer() const noexcept
Gets the audio mixer, which can be used to play music.
Definition: ge211_base.cpp:52
Duration get_prev_frame_length() const noexcept
Returns the duration of the frame right before the frame currently running.
Definition: ge211_base.h:234
static const Dimensions default_window_dimensions
The default window dimensions, in pixels.
Definition: ge211_base.h:115
Color background_color
Assign this member variable to change the window&#39;s background color in subsequent frames...
Definition: ge211_base.h:259
Provides access to the game window and its properties.
Definition: ge211_window.h:12
virtual void on_key(Key)
Called by the game engine for each keypress.
Definition: ge211_base.h:144
A sprite is an image that knows how to render itself to the screen at a given location, under a particular transformation.
Definition: ge211_sprites.h:33
The entity that coordinates playing all audio tracks.
Definition: ge211_audio.h:142
Represents the dimensions of an object, or more generally, the displacement between two Basic_positio...
Definition: ge211_forward.h:73
Window & get_window() const
Gets the Window that the game is running in.
Definition: ge211_base.cpp:39
A position in the T-valued Cartesian plane.
Definition: ge211_forward.h:74
double get_frame_rate() const noexcept
Returns an approximation of the current frame rate in Hz.
Definition: ge211_base.h:240
Time_point get_frame_start_time() const noexcept
Gets the time point at which the current frame started.
Definition: ge211_base.h:229
A length of time.
Definition: ge211_time.h:30
For representing colors.
Definition: ge211_color.h:22
virtual void on_quit()
Called by the game engine after exiting the event loop but before the game instance is destroyed...
Definition: ge211_base.h:187
virtual void on_frame(double last_frame_seconds)
Called by the game engine once per frame.
Definition: ge211_base.h:136
void run()
Runs the game.
Definition: ge211_base.cpp:29
virtual Dimensions initial_window_dimensions() const
Override this function to specify the initial dimensions of the game&#39;s window.
Definition: ge211_base.cpp:19
Time_point start_time() const
Returns the actual time when this timer was started or most recently reset.
Definition: ge211_time.h:271
A collection of positioned sprites ready to be rendered to the screen.
static const Color default_background_color
The default background color of the window, if not changed by the derived class.
Definition: ge211_base.h:106
void quit() noexcept
Causes the event loop to quit after the current frame finishes.
Definition: ge211_base.cpp:34
virtual void on_key_up(Key)
Called by the game engine each time a key is released.
Definition: ge211_base.h:160
void prepare(const sprites::Sprite &) const
Prepares a sprites::Sprite for rendering, without actually including it in the scene.
Definition: ge211_base.cpp:57
virtual void draw(Sprite_set &)=0
You must override this function in the derived class to specify how to draw your scene.