raylibstarter 0.1.0
game.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
5#include "scene.h"
6
7#define MAX(a, b) ((a)>(b)? (a) : (b))
8#define MIN(a, b) ((a)<(b)? (a) : (b))
9
10namespace game::core {
11 /**
12 * @brief The entry point into the game. The constructor is used to specify the basic settings such as the size of
13 * the drawing area in pixels. Finally, a first game scene can be started via the Run() method.
14 */
15 class Game final {
16 public:
17 Game() = delete;
18
19 /**
20 * Create a new Game instance
21 * @brief Constructor.
22 * @param stage_width Width of the game scene (unscaled). Fixed for the entire run time of the game.
23 * @param stage_height Height of the game scene (unscaled). Fixed for the entire run time of the game.
24 * @param full_screen Start in full screen or window mode.
25 * @param target_fps Target fps, usually 60 fps.
26 * @param window_flags Raylib window flags, preffered: GAME_CONFIG_FLAGS FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_MSAA_4X_HINT
27 * @param exit_key Game exit key. E.g. KEY_ESCAPE. Set it to KEY_NULL if the game app should not stop directly by pressing a key.
28 * @param mouse If true, the mouse position is available in game::core::Store::mouse_position. Please do not use the Raylib function GetMousePosition() which will not wok because of the scaling and letterboxing system.
29 * @param audio If true, Raylib will initialize the audio device.
30 * @param project_name Project name to be displayed as window title in Window mode.
31 */
32 Game(int stage_width, int stage_height, bool full_screen, int target_fps, int window_flags, int exit_key,
33 bool mouse, bool audio, const char *project_name);
34
35 Game(const game::core::Game &game) = delete;
36
37 Game &operator=(const Game &) = delete;
38
39 ~Game();
40
41 /**
42 * The method creates the main stage object of the game, which is accessible under game::core::Store::stage and
43 * transfers the scene object to the stage.
44 *
45 * The method also implements the main game loop. In this, the contents of the current game scene are drawn via
46 * the stage object game::core::Store::stage.
47 *
48 * Furthermore, the method increments the global game counter game::core::Store::ticks and updates the
49 * mouse pointer coordinates in game::core::Store::mouse_position if mouse support was enabled when
50 * instantiating the game object.
51 *
52 * The method is also responsible for correctly scaling the displayed scene and inserting letterboxes if
53 * necessary to preserve the aspect ratio.
54 *
55 * @brief Starts the first game scene.
56 */
57 void Run(const std::string &scene_name, std::unique_ptr<game::core::Scene> scene) const;
58
59 private:
60 /// Width of the game scene (unscaled). Fixed for the entire run time of the game.
62 /// Height of the game scene (unscaled). Fixed for the entire run time of the game.
64 /// Defines whether the audio device is initialized.
65 bool audio_;
66 /// Defines if the mouse support should be activated.
67 bool mouse_;
68
69 /// Temporary render target, which will later be output correctly scaled on the screen.
70 RenderTexture2D render_target_ = { };
71
72 /**
73 * @brief Clamp Vector2 value with MIN and MAX and return a new vector2. Required for virtual mouse, to clamp
74 * inside virtual game size
75 */
76 [[nodiscard]] static Vector2 ClampValue(Vector2 value, Vector2 MIN, Vector2 MAX);
77
78 /**
79 * @brief Update virtual mouse position. This becomes necessary because the Raylib function GetMousePosition
80 * can no longer work correctly due to the scaling of the graphics output.
81 */
82 void UpdateMousePosition() const;
83
84 /**
85 * @brief Draws the correctly scaled and, if necessary, letterboxed graphic on the screen
86 */
87 void DrawRenderTexture() const;
88 };
89}
The entry point into the game. The constructor is used to specify the basic settings such as the size...
Definition: game.h:15
Game(const game::core::Game &game)=delete
RenderTexture2D render_target_
Temporary render target, which will later be output correctly scaled on the screen.
Definition: game.h:70
int stage_width_
Width of the game scene (unscaled). Fixed for the entire run time of the game.
Definition: game.h:61
Game & operator=(const Game &)=delete
static Vector2 ClampValue(Vector2 value, Vector2 MIN, Vector2 MAX)
Clamp Vector2 value with MIN and MAX and return a new vector2. Required for virtual mouse,...
Definition: game.cpp:83
void Run(const std::string &scene_name, std::unique_ptr< game::core::Scene > scene) const
Starts the first game scene.
Definition: game.cpp:51
bool audio_
Defines whether the audio device is initialized.
Definition: game.h:65
void UpdateMousePosition() const
Update virtual mouse position. This becomes necessary because the Raylib function GetMousePosition ca...
Definition: game.cpp:94
bool mouse_
Defines if the mouse support should be activated.
Definition: game.h:67
void DrawRenderTexture() const
Draws the correctly scaled and, if necessary, letterboxed graphic on the screen.
Definition: game.cpp:105
int stage_height_
Height of the game scene (unscaled). Fixed for the entire run time of the game.
Definition: game.h:63
#define MIN(a, b)
Definition: game.h:8
#define MAX(a, b)
Definition: game.h:7
Definition: actor.h:5