/
VGlobal.h
255 lines (235 loc) · 11.2 KB
/
VGlobal.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* @file VGlobal.h
* @author Tim Stoddard <tim@gamepopper.co.uk>
*
* @section LICENSE
*
* MIT License
*
* Copyright (c) 2020 Tim Stoddard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @section DESCRIPTION
*
* A Singleton that contains the global properties for the game.
*/
#pragma once
#include <SFML/System.hpp>
#include <SFML/Config.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include "VState.h"
#include "VContent.h"
#include "VMusic.h"
#include "VSoundManager.h"
#include "VRandom.h"
#include "VInputHandler.h"
#include "VAsync.h"
#include <vector>
#include <functional>
#include <memory>
#if _DEBUG
#include <iostream>
#include <sstream>
#endif
///Currently defined build version of the Vigilante Game Framework
#define VFRAME_VERSION "1.0.0.10"
class VBase;
class VObject;
class VPostEffectBase;
class VCollision;
///A singleton class that holds a lot of global properties other object require.
class VGlobal
{
private:
static VGlobal* Instance; ///Singleton Instance of VGlobal
VGlobal();
~VGlobal();
public:
///Access and creation of VGlobal
static VGlobal* p();
///How to orientate the screen
enum OrientationOption : unsigned char
{
///No rotation
ANGLE_NONE,
///Rotate the screen 90 degrees, so the top of the screen faces right.
ANGLE_90,
///Rotate the screen 180 degrees, so the top of the screen faces right.
ANGLE_180,
///Rotate the screen 270 degrees, so the top of the screen faces right.
ANGLE_270
};
///Window the game is rendered on.
std::unique_ptr<sf::RenderWindow> App;
///The last frame rendered.
const sf::RenderTexture* Frame;
///sf::RenderStates object used for rendering the target to the scene with additional features like transform and blend mode.
sf::RenderStates RenderState;
///Offset for the main game's viewport, important for the game window being resized and the screen needing to be repositioned in the window.
sf::Vector2f ViewportOffset;
///The OpenGL Context Settings
sf::ContextSettings ContextSettings;
///Should pixels be smooth or pixellated when the screen is resized?
bool Antialiasing = false;
///Should the game use Verticle Syncing (where the framerate is based on the monitor refresh rate) instead of the variable framerate?
bool VSync = false;
#ifdef _DEBUG
///Show the rectangle collision area of each collideable object.
bool DrawDebug = false;
#endif
///Colour of the Window's background prior to rendering.
sf::Color BackgroundColor = sf::Color::Black;
///Speed of GameTime by manipulating the Delta Time. (e.g. 1 is default speed, 2 is 2x speed)
float TimeScale = 1.0f;
///Set the game to pause when going out of focus.
bool FocusPause = true;
///VGame Area Width (Read Only)
unsigned int Width = 0;
///VGame Area Height (Read Only)
unsigned int Height = 0;
///Width of the game's Window
unsigned int WindowWidth = 0;
///Height of the game's Window
unsigned int WindowHeight = 0;
///Bounderies of the game world regardless of state. Especially important for collision handling.
sf::FloatRect WorldBounds;
///Allows streamed audio files to be played between states (typically for music).
std::unique_ptr<VMusic> Music;
///Allows loaded audio files to be played between states (typically for sound effects).
std::unique_ptr<VSoundManager> Sound;
///Content Management System for handling asset files (i.e. Textures, Fonts ect)
std::unique_ptr<VContent> Content;
///PostEffect used for the entire game, applied after the state is drawn.
std::unique_ptr<VPostEffectBase> PostProcess;
///Frames Per Second - used to calculate average framerate
float FPS = 0;
///Orientation of the whole in-game screen, default is ANGLE_NONE or no rotation of the in-game screen.
OrientationOption Orientation = ANGLE_NONE;
///Random Number Generator
std::unique_ptr<VRandom> Random;
///Input Handling for Keyboard, Mouse and Controllers.
std::unique_ptr<VInputHandler> Input;
///Asynchronous Function Handler for Multithreaded Functionality.
std::unique_ptr<VAsync> Async;
///Current Window Style being used (set with sf::Style).
int WindowStyle;
///@param newTitle The new text the window should display in the title bar.
void SetTitle(const char* newTitle);
///@param newTitle The new text the window should display in the title bar.
void SetTitle(std::string newTitle);
///@return The current text on the window's title bar.
const char* GetTitle();
///@param set Set the game to fullscreen mode or not.
void SetFullscreen(bool set);
///Toggles from fullscreen to window mode.
void ToggleFullscreen();
///@return If the game is in fullscreen mode, function returns true.
bool IsFullscreen();
///@return True if the game is running.
bool IsRunning();
///@return True if the current VState is being changed in the state manager.
bool IfChangedState();
///@return True if the a new VState is being pushed onto the stack in the state manager.
bool IfPushedState();
///@return Position of the mouse relative to the game screen (even when resized).
sf::Vector2f GetMousePosition();
/// @param set Sets mouse cursor to be visible or not, depending of if you want an in-game cursor.
void SetMouseCursorVisible(bool set);
/// @return The Current Gameplay State.
VState* CurrentState();
/// @param state The new VState to change to.
void ChangeState(VState* state);
/// @param state A new VState to push onto the stack to run without changing the current one.
void PushState(VState* state);
/// Pop the current state from the stack.
void PopState(bool preUpdate = false);
/// Clear all states from the stack.
void ClearStates();
/// Close window and close out game.
void Exit();
/**
* Test if an object is overlapping at a specified point.
* @param point The position in the game world to test with.
* @param a The base object to test overlap with.
* @return True if there is an overlap.
*/
bool OverlapAtPoint(const sf::Vector2f& point, VBase* a);
/**
* Test if two objects are overlapping.
* @param a The base object to test overlap with.
* @param b The other base object to test overlap with. If you only want to test overlap with a itself (like in a VGroup), set this to null.
* @param responseCall An optional response callback if an overlap is found. Only gets called if an overlap is true.
* @param processCall An optional process callback if an overlap is found and there is a need to handle further tests and changes to the objects. Only gets called if an overlap is true, and should return true further collision tests are true.
* @return Function returns true if an overlap has been found to be true.
*/
bool Overlaps(VBase* a, VBase* b = nullptr, std::function<void(VObject*, VObject*)>const& responseCall = nullptr, std::function<bool(VObject*, VObject*)>const& processCall = nullptr);
/**
* Test if two objects are colliding by testing an overlap and applying the VObject::separate function as a process call to separate the objects.
* @param a The base object to test overlap with.
* @param b The other base object to test overlap with. If you only want to test overlap with a itself (like in a VGroup), set this to null.
* @param responseCall An optional response callback if an overlap is found. Only gets called if an overlap is true.
* @return Function returns true if an overlap has been found to be true.
*/
bool Collides(VBase* a, VBase* b = nullptr, std::function<void(VObject*, VObject*)>const& responseCall = nullptr);
/**
* Test if an object is overlapping at a specified point using circle overlap testing.
* @param point The position in the game world to test with.
* @param a The base object to test overlap with.
* @return True if there is an overlap.
*/
bool OverlapCircleAtPoint(const sf::Vector2f& point, VBase* a);
/**
* Test if two objects are overlapping using circle overlap testing.
* @param a The base object to test overlap with.
* @param b The other base object to test overlap with. If you only want to test overlap with a itself (like in a VGroup), set this to null.
* @param responseCall An optional response callback if an overlap is found. Only gets called if an overlap is true.
* @param processCall An optional process callback if an overlap is found and there is a need to handle further tests and changes to the objects. Only gets called if an overlap is true, and should return true further collision tests are true.
* @return Function returns true if an overlap has been found to be true.
*/
bool OverlapsCircle(VBase* a, VBase* b = nullptr, std::function<void(VObject*, VObject*)>const& responseCall = nullptr, std::function<bool(VObject*, VObject*)>const& processCall = nullptr);
/**
* Test if two objects are colliding by testing an overlap with circle overlap testing and applying the VObject::separateCircle function as a process call to separate the objects.
* @param a The base object to test overlap with.
* @param b The other base object to test overlap with. If you only want to test overlap with a itself (like in a VGroup), set this to null.
* @param responseCall An optional response callback if an overlap is found. Only gets called if an overlap is true.
* @return Function returns true if an overlap has been found to be true.
*/
bool CollidesCircle(VBase* a, VBase* b = nullptr, std::function<void(VObject*, VObject*)>const& responseCall = nullptr);
///Cleans up all data in VGlobal to NULL.
static void Cleanup();
private:
std::unique_ptr<VStateManager> gsm; ///The game's state manager.
VState* nextState = nullptr; ///Store the next state to apply until the current state has finished processing.
bool fullscreen = false; ///Sets for when the game is in fullscreen mode or not.
bool mouseCursorVisible = true; ///Sets for when the system mouse cursor is visible.
bool running = true;
const char* title = "";
///Value is true if the current VState is being changed in the state manager.
bool ifChangedState = false;
///Value is true if the a new VState is being pushed onto the stack in the state manager.
bool ifPushedState = false;
///Value is true if the a new VState is being popped off the stack in the state manager.
bool ifPoppedState = false;
std::unique_ptr<VCollision> collision; ///The game's collision handling system.
std::function<bool(VObject*, VObject*)> rectCollision; ///Function for handling rectangle collisions.
std::function<bool(VObject*, VObject*)> circleCollision; ///Function for handling circle collisions.
};