raylibstarter 0.1.0
game.cpp
Go to the documentation of this file.
1#include <raylib.h>
2
3#include "game.h"
4
5#include "store.h"
6#include "stage.h"
7
8using namespace std::string_literals;
9
10game::core::Game::Game(int stage_width, int stage_height, bool full_screen, int target_fps, int window_flags,
11 int exit_key, bool mouse, bool audio, const char *project_name)
12 : stage_width_(stage_width), stage_height_(stage_height), audio_(audio), mouse_(mouse) {
13 SetConfigFlags(window_flags);
14 InitWindow(stage_width, stage_height, project_name);
15 SetWindowMinSize(stage_width / 2, stage_height / 2);
16 SetTargetFPS(target_fps);
17
18 // Render texture initialization, used to hold the rendering result, so we can easily resize it
19 this->render_target_ = LoadRenderTexture(this->stage_width_, this->stage_height_);
20 // Set texture scale filter to use
21 SetTextureFilter(this->render_target_.texture, TEXTURE_FILTER_BILINEAR);
22
23 SetExitKey(exit_key);
24
25 if (full_screen)
26 ToggleFullscreen();
27
28 if (audio)
29 InitAudioDevice();
30
31 if (!mouse)
32 HideCursor();
33}
34
36 TraceLog(LOG_INFO, "game::core::Game destructor called");
37
38 // Release the stage object to trigger potential cleanups that may be dependent on code after the Run() function call.
40
41 if (this->audio_)
42 CloseAudioDevice();
43
44 // Unload render texture
45 UnloadRenderTexture(this->render_target_);
46
47 // Close window and OpenGL context
48 CloseWindow();
49}
50
51void game::core::Game::Run(const std::string& scene_name, std::unique_ptr<game::core::Scene> scene) const {
52 // Create game::Stage instance and assign new scene
53 game::core::Store::stage = std::make_unique<game::core::Stage>(scene_name, std::move(scene));
54
55 // Main game loop
56 while (!WindowShouldClose()) // Detect window close button if defined
57 {
58 if(this->mouse_)
59 this->UpdateMousePosition();
60
61 // Process input and update current active scene
63
64 // Draw
65 BeginDrawing();
66 ClearBackground(BLACK); // Letterbox color
67
68 // Draw everything in the render texture, note this will not be rendered on screen, yet
69 BeginTextureMode(this->render_target_);
70 // Draw the current active scene to render texture
72 EndTextureMode();
73
74 // Draw render texture to window, properly scaled
75 this->DrawRenderTexture();
76 EndDrawing();
77
78 // Increment game tick counter
80 } // Main game loop end
81}
82
83Vector2 game::core::Game::ClampValue(Vector2 value, Vector2 MIN, Vector2 MAX) {
84 Vector2 result = value;
85
86 result.x = (result.x > MAX.x) ? MAX.x : result.x;
87 result.x = (result.x < MIN.x) ? MIN.x : result.x;
88 result.y = (result.y > MAX.y) ? MAX.y : result.y;
89 result.y = (result.y < MIN.y) ? MIN.y : result.y;
90
91 return result;
92}
93
95 // Compute required framebuffer scaling
96 float scale = MIN((float) GetScreenWidth() / this->stage_width_, (float) GetScreenHeight() / this->stage_height_);
97
98 // Update virtual mouse (clamped mouse value behind game screen)
99 Vector2 mouse = GetMousePosition();
100 Store::mouse_position.x = (mouse.x - (static_cast<float>(GetScreenWidth()) - (static_cast<float>(this->stage_width_) * scale)) * 0.5f) / scale;
101 Store::mouse_position.y = (mouse.y - (static_cast<float>(GetScreenHeight()) - (static_cast<float>(this->stage_height_) * scale)) * 0.5f) / scale;
102 Store::mouse_position = ClampValue(Store::mouse_position, {0, 0}, {static_cast<float>(this->stage_width_), static_cast<float>(this->stage_height_)});
103}
104
106 // Compute required framebuffer scaling
107 float scale = MIN((float) GetScreenWidth() / this->stage_width_, (float) GetScreenHeight() / this->stage_height_);
108
109 // Draw RenderTexture2D to window, properly scaled
110 DrawTexturePro(this->render_target_.texture,
111 {0.0f, 0.0f, static_cast<float>(this->render_target_.texture.width), static_cast<float>(-this->render_target_.texture.height)},
112 {(static_cast<float>(GetScreenWidth()) - (static_cast<float>(this->stage_width_) * scale)) * 0.5f,
113 (static_cast<float>(GetScreenHeight()) - (static_cast<float>(this->stage_height_) * scale)) * 0.5f,
114 static_cast<float>(this->stage_width_) * scale, static_cast<float>(this->stage_height_) * scale},
115 {0.0f, 0.0f}, 0.0f, WHITE
116 );
117}
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
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
void UpdateMousePosition() const
Update virtual mouse position. This becomes necessary because the Raylib function GetMousePosition ca...
Definition: game.cpp:94
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
static long long int ticks
Global game counter. Can be used e.g. for the calculation of animation times.
Definition: store.h:23
static std::unique_ptr< game::core::Stage > stage
The Stage object is responsible for the scene change and for updating and drawing the scene contents.
Definition: store.h:16
static Vector2 mouse_position
The virtual mouse position.
Definition: store.h:20