-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.hpp
110 lines (79 loc) · 2 KB
/
Game.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @file Game.hpp
* @brief Manages the game loop and the game state.
* @author Josh Kennedy
*
* Kablam!
* Copyright (C) 2023 Josh Kennedy.
*/
#pragma once
#include "GameState.hpp"
#include "Constants.hpp"
#include "Menu.hpp"
#include "Gameplay.hpp"
#include "Assets.hpp"
#include <chrono>
struct SDL_Window;
struct SDL_Renderer;
union SDL_Event;
class Game
{
friend class Assets;
private:
static Game* __instance;
Game();
SDL_Window* window;
SDL_Renderer* renderer;
bool isRunning;
GameState currentState;
int init();
void quit();
void thunk();
void handleInput();
void update(double deltaTime);
void render();
Menu* menu;
Gameplay* gameplay;
bool __isTouchAvailable;
std::chrono::high_resolution_clock::time_point timePrev;
inline double calculateDeltaTime() noexcept
{
// Get current time as a std::chrono::time_point
// which basically contains info about the current point in time.
auto timeCurrent = std::chrono::high_resolution_clock::now();
// Compare the two to create time_point containing delta time in nanoseconds.
auto timeDiff = std::chrono::duration_cast<std::chrono::nanoseconds>(timeCurrent - timePrev);
// Get the ticks as a variable.
double delta = static_cast<double>(timeDiff.count());
// Turn nanoseconds into seconds.
delta /= 1'000'000'000.00;
// Swap the times.
this->timePrev = timeCurrent;
// Clamp the delta
if (delta > TARGET_FRAME_TIME)
{
delta = TARGET_FRAME_TIME;
}
return delta;
}
public:
inline static Game* get()
{
if (__instance == nullptr)
{
__instance = new Game();
}
return __instance;
}
int run();
inline GameState getState() const noexcept { return this->currentState; }
inline void setState(GameState state) noexcept { this->currentState = state; }
inline bool isTouchAvailable() const noexcept { return this->__isTouchAvailable; }
inline SDL_Renderer* getRenderer() const noexcept { return this->renderer; }
#if __EMSCRIPTEN__
inline void __loopThunk()
{
this->thunk();
}
#endif
};