ge211  2021.5.1
A student game engine
ge211_base.cxx
1 #include "ge211_base.hxx"
2 #include "ge211_engine.hxx"
3 #include "ge211_error.hxx"
4 
5 #include <SDL.h>
6 
7 namespace ge211 {
8 
9 using namespace detail;
10 
11 // Storage for constexprs, just in case.
12 const Dims<int> Abstract_game::default_window_dimensions{800, 600};
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 = 30;
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 
47 void Abstract_game::prepare(const sprites::Sprite& sprite) const
48 {
49  if (engine_)
50  engine_->prepare(sprite);
51  else {
53  << "Abstract_game::prepare: Could not prepare sprite "
54  << "because engine is not initialized";
55  }
56 }
57 
58 void Abstract_game::mark_present_() NOEXCEPT
59 {
60  busy_time_.pause();
61 }
62 
63 void Abstract_game::mark_frame_() NOEXCEPT
64 {
65  busy_time_.resume();
66  prev_frame_length_ = frame_start_.reset();
67 
68  if (++sample_counter_ == frames_per_sample) {
69  auto sample_duration = real_time_.reset().seconds();
70  auto busy_duration = busy_time_.reset().seconds();
71  fps_ = frames_per_sample / sample_duration;
72  load_ = 100 * busy_duration / sample_duration;
73  sample_counter_ = 0;
74  }
75 }
76 
77 void Abstract_game::poll_channels_()
78 {
79  if (mixer_.is_forced())
80  mixer_->poll_channels_();
81 }
82 
84 {
85  if (key.code() == '\u001B') quit();
86 }
87 
88 } // end namespace ge211
std::string
ge211::internal::logging::warn
Log_message warn(std::string reason="")
Returns a warn-level log message.
Definition: ge211_error.cxx:176
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::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::geometry::Dims< int >
ge211::Window
Provides access to the game window and its properties.
Definition: ge211_window.hxx:17
ge211::exceptions::Client_logic_error
An exception that indicates that a logic error was performed by the client.
Definition: ge211_error.hxx:48
ge211::Abstract_game::default_window_title
static const char *const default_window_title
The default initial window title.
Definition: ge211_base.hxx:146
ge211::events::Key::code
static Key code(char32_t c)
Constructs a key with the given Unicode code point code.
Definition: ge211_event.hxx:141
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::Color::black
static constexpr Color black()
Solid black.
Definition: ge211_color.hxx:64
ge211::Abstract_game::get_window
Window & get_window() const
Gets the Window that the game is running in.
Definition: ge211_base.cxx:39
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::Abstract_game::run
void run()
Runs the game.
Definition: ge211_base.cxx:29
ge211::Abstract_game::quit
void quit()
Causes the event loop to quit after the current frame finishes.
Definition: ge211_base.cxx:34