ge211
ge211_base.cpp
1 #include "ge211_base.h"
2 #include "ge211_engine.h"
3 #include "ge211_error.h"
4 
5 #include <SDL.h>
6 
7 namespace ge211 {
8 
9 using namespace detail;
10 
11 // Storage for constexprs, just in case.
13 const char* const Abstract_game::default_window_title = "ge211 window";
15 
16 // How many frames to run before calculating the frame rate.
17 static int const frames_per_sample = 60;
18 
20 {
21  return default_window_dimensions;
22 }
23 
25 {
26  return default_window_title;
27 }
28 
30 {
31  Engine(*this).run();
32 }
33 
34 void Abstract_game::quit() noexcept
35 {
36  quit_ = true;
37 }
38 
40 {
41  if (engine_) return engine_->get_window();
42 
43  throw Client_logic_error{"Abstract_game::window: Window does not exist "
44  "until engine is initialized"};
45 }
46 
48 {
49  return rng_;
50 }
51 
53 {
54  return mixer_.get();
55 }
56 
57 void Abstract_game::prepare(const sprites::Sprite& sprite) const
58 {
59  if (engine_)
60  engine_->prepare(sprite);
61  else {
62  warn() << "Abstract_game::prepare: Could not prepare sprite "
63  << "because engine is not initialized";
64  }
65 }
66 
67 void Abstract_game::mark_frame_() noexcept
68 {
69  prev_frame_length_ = frame_start_.reset();
70 
71  if (! (fps_sample_count_ = (fps_sample_count_ + 1) % frames_per_sample))
72  fps_ = frames_per_sample / fps_sample_start_.reset().seconds();
73 }
74 
76 {
77  if (key.code() == '\u001B') quit();
78 }
79 
80 } // end namespace ge211
Represents a key on the keyboard.
Definition: ge211_event.h:119
virtual void on_key_down(Key)
Called by the game engine each time a key is depressed.
Definition: ge211_base.cpp:75
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
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
static const Dimensions default_window_dimensions
The default window dimensions, in pixels.
Definition: ge211_base.h:115
Provides access to the game window and its properties.
Definition: ge211_window.h:12
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
static Key code(char32_t c)
Constructs a key with the given Unicode code point code.
Definition: ge211_event.h:133
static constexpr Color black() noexcept
Solid black.
Definition: ge211_color.h:63
An exception that indicates that a logic error was performed by the client.
Definition: ge211_error.h:46
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
Basic_dimensions< int > Dimensions
Type alias for the most common use of Basic_dimensions, which is with a coordinate type of int...
Definition: ge211_forward.h:77
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
void prepare(const sprites::Sprite &) const
Prepares a sprites::Sprite for rendering, without actually including it in the scene.
Definition: ge211_base.cpp:57