-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEngine.hpp
More file actions
159 lines (156 loc) · 6.1 KB
/
GameEngine.hpp
File metadata and controls
159 lines (156 loc) · 6.1 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef GAMEENGINE_HPP
#define GAMEENGINE_HPP
#include <allegro5/allegro.h>
#include <string>
#include <unordered_map>
#include "Point.hpp"
/// <summary>
/// All general classes are under this namespace for clarity.
/// </summary>
namespace Engine {
class IScene;
/// <summary>
/// The one and only GameEngine for the entire program. Responsible for low-level initialization and window events.
/// </summary>
class GameEngine final {
private:
// Allegro5 settings, frames per second, screen width, screen height, maximum simultaneous audio samples.
int fps{}, screenW{}, screenH{}, reserveSamples{};
// Determines whether to free memory between scenes.
bool freeMemoryOnSceneChanged{};
// Max delta time for update multiplier. If delta time exceeds this threshold, lag occurs.
float deltaTimeThreshold{};
// All scenes are stored in hash table for easy access.
// Reference: Data Structure - Hash table
std::unordered_map<std::string, IScene*> scenes;
// The active scene that occupies the game's update, draw and various events.
IScene* activeScene{};
// Allegro5 display for window creation.
ALLEGRO_DISPLAY* display{};
// Allegro5 event queue.
ALLEGRO_EVENT_QUEUE* event_queue{};
// Allegro5 timer to inject update & draw event into the event queue.
ALLEGRO_TIMER* update_timer{};
// The window's title text.
const char* title{};
// The window's icon.
const char* icon{};
// The scene to change to at next update.
std::string nextScene{};
/// <summary>
/// Initialize allegro5 library. Creates game window & event queue,
/// install peripherals and initialize add-ons.
/// </summary>
void initAllegro5();
/// <summary>
/// Process events inside the event queue using an infinity loop,
/// exit when close button clicked.
/// </summary>
void startEventLoop();
/// <summary>
/// Delegate the update event to the active scene.
/// </summary>
/// <param name="deltaTime">Time elapsed since last update, can be used to calculate value changes.</param>
void update(float deltaTime);
/// <summary>
/// Delegate the draw event to the active scene.
/// </summary>
void draw() const;
/// <summary>
/// Release Allegro5 resources, and free all allocated scenes.
/// </summary>
void destroy();
/// <summary>
/// Private constructor since this class is a Singleton.
/// Note: Singleton is a class that will only be instantiated once (single instance).
/// Reference: Design Patterns - Singleton.
/// </summary>
explicit GameEngine() = default;
/// <summary>
/// Change to another scene. Must return immediately and stop using anything initialized in
/// the scene. Since this call destroys everything initialized.
/// </summary>
/// <param name="name">The name of the scene you want to change to.</param>
void changeScene(const std::string& name);
public:
// Note: We'll ignore C++11's move constructor, move assignment operator in this project for simplicity.
/// <summary>
/// Copy constructor is deleted, no copying allowed.
/// </summary>
GameEngine(GameEngine const&) = delete;
/// <summary>
/// Copy assignment operator is deleted, no copy assignment allowed.
/// </summary>
GameEngine& operator=(GameEngine const&) = delete;
/// <summary>
/// Start the game loop until exit, scenes should be added before starting.
/// </summary>
/// <param name="firstSceneName">The scene name of the first scene of the game.</param>
/// <param name="fps">The target frame per seconds of the game.</param>
/// <param name="screenW">Window screen width.</param>
/// <param name="screenH">Window screen height.</param>
/// <param name="reserveSamples">Maximum simultaneous audio samples.</param>
/// <param name="title">Window's title text.</param>
/// <param name="icon">Window's icon image path.</param>
/// <param name="freeMemoryOnSceneChanged">Determines whether to free memory between scenes.</param>
void Start(const std::string& firstSceneName, int fps = 60, int screenW = 800, int screenH = 600, int reserveSamples = 1000,
const char* title = "Tower Defense (I2P(II)_2023 Mini Project 2))",
const char* icon = "icon.png", bool freeMemoryOnSceneChanged = false,
float deltaTimeThreshold = 0.05);
/// <summary>
/// Add a new scene to the game. Should only be called once for each scene.
/// Use inline-new when adding scene in order to support polymorphism,
/// The added scenes will be deleted by GameEngine at game end.
/// </summary>
/// <param name="name">The unique name of your scene for later access.</param>
/// <param name="scene">The pointer to the scene you want to add.</param>
void AddNewScene(const std::string& name, IScene* scene);
/// <summary>
/// Change to another scene. The scene will be changed at next update.
/// </summary>
/// <param name="name">The name of the scene you want to change to.</param>
void ChangeScene(const std::string& name);
/// <summary>
/// Get the pointer of the active scene.
/// </summary>
/// <returns>Pointer to active scene.</returns>
IScene* GetActiveScene() const;
/// <summary>
/// Get scene by name.
/// </summary>
/// <param name="name">The scene's name.</param>
/// <returns>Pointer to scene.</returns>
IScene* GetScene(const std::string& name);
/// <summary>
/// Get screen size.
/// </summary>
/// <returns>Screen size.</returns>
Point GetScreenSize() const;
/// <summary>
/// Get screen width.
/// </summary>
/// <returns>Screen width.</returns>
int GetScreenWidth() const;
/// <summary>
/// Get screen height.
/// </summary>
/// <returns>Screen height.</returns>
int GetScreenHeight() const;
/// <summary>
/// Get mouse position.
/// </summary>
/// <returns>Get mouse position.</returns>
Point GetMousePosition() const;
/// <summary>
/// Get key state.
/// </summary>
/// <returns>Returns whether key is down or not.</returns>
bool IsKeyDown(int keyCode) const;
/// <summary>
/// Typical function to retrieve Singleton instance and supports lazy initialization.
/// </summary>
/// <returns>The Singleton instance of GameEngine.</returns>
static GameEngine& GetInstance();
};
}
#endif // GAMEENGINE_HPP