-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEngine.h
124 lines (108 loc) · 3.79 KB
/
Engine.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
#pragma once
#include "core/Window.h"
#include "core/Settings.h"
#include "core/Utility.h"
#include "core/GameObject.h"
#include "Physics/ICollider.h"
#include <memory>
#include <vector>
#include <future>
class Engine {
private:
inline static Engine* instance;
std::vector<std::shared_ptr<engine::core::GameObject>> OBJECTS;
std::shared_ptr<engine::core::Window> window;
double framesPerSecond{};
double curTime{};
double lastTime{};
inline static const double maxUpdates = 60.0;
inline static const double limitFPS = 1.0 / maxUpdates;
private:
Engine() = default;
public:
static Engine* GetInstance() {
if (instance) return instance;
return new Engine();
}
Engine(Engine &) = delete;
Engine(Engine &&) = delete;
public:
~Engine() {
glfwTerminate();
}
public:
Engine &operator=(Engine &) = delete;
Engine &operator=(Engine &&) = delete;
private:
void CreateWindow(int width, int height, const char *title) {
window = std::make_shared<engine::core::Window>(width, height, title);
}
public:
void Draw(std::vector<std::future<void>> &threads) {
for (const auto &obj : OBJECTS) {
engine::core::Vector2f position = obj->GetPosition();
if (position.x <= 1 && position.x >= -1 && position.y <= 1 && position.y >= -1) {
threads.push_back(std::async(std::launch::async, &engine::core::GameObject::Draw, obj.get()));
obj->Draw();
}
}
threads.clear();
}
void Loop() {
std::vector<std::future<void>> threads;
lastTime = glfwGetTime();
double time = lastTime;
double delta = 0;
curTime = 0;
int updates = 0;
int frames = 0;
while (!window->ShouldClose()) {
curTime = glfwGetTime();
glClear(GL_COLOR_BUFFER_BIT);
delta += (curTime - lastTime) / limitFPS;
lastTime = curTime;
if (delta >= 1.0) {
Update();
for (auto &obj : OBJECTS) {
threads.push_back(std::async(
std::launch::async,
&engine::core::GameObject::Update,
obj.get()));
}
threads.clear();
Draw(threads);
window->SwapBuffers();
--delta;
++updates;
}
frames++;
if (glfwGetTime() - time > 1) {
++time;
std::string str = "Updates: " + std::to_string(updates) + ", " + "Frames: " + std::to_string(frames);
window->SetTitle(str);
updates = frames = 0;
}
glfwPollEvents();
}
}
public:
template<typename T, typename = typename std::enable_if<std::is_base_of<engine::core::GameObject, T>::value>::type>
std::shared_ptr<engine::core::GameObject> CreateObject(T &&tmp) {
std::shared_ptr<T> obj = std::make_shared<T>(std::forward<T>(tmp));
OBJECTS.push_back(obj);
return obj;
}
template<typename T, typename G,
typename = typename std::enable_if<std::is_base_of<engine::components::ICollider, T>::value>::type,
typename = typename std::enable_if<std::is_base_of<engine::core::GameObject, G>::value>::type>
std::shared_ptr<T> CreateCollider(std::shared_ptr<G> &object) {
std::shared_ptr<T> collider = std::make_shared<T>(object);
object->AddComponent(collider);
engine::components::ALL_COLLIDERS.push_back(collider);
return collider;
}
public:
void Awake();
void Start();
void Update();
};