forked from hitsz-ldjam/MixEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMixEngine.cpp
211 lines (175 loc) · 6.87 KB
/
MixEngine.cpp
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
#include "MixEngine.h"
#include "Mx/Window/MxWindow.h"
#include "Mx/Time/MxTime.h"
#include "Mx/Input/MxInput.h"
#include "Mx/Audio/MxAudio.hpp"
#include "Mx/Physics/MxPhysicsWorld.h"
#include "Mx/Vulkan/Shader/MxVkStandardShader.h"
#include "Mx/GUI/MxGUi.h"
#include "Mx/Resource/MxResourceLoader.h"
#include "Mx/Graphics/MxGraphics.h"
#include "Mx/Scene/MxSceneManager.h"
#include "Mx/Engine/MxPlatform.h"
#include "MxApplicationBase.h"
namespace Mix {
MixEngine::MixEngine(int _argc, char** _argv) {
for (int i = 0; i < _argc; ++i)
mCommandLines.emplace_back(_argv[i]);
}
void MixEngine::requestQuit() {
Platform::PushQuitEvent();
}
void MixEngine::setFPSLimit(uint32_t _limit) {
mFPSLimit = _limit;
if (mFPSLimit > 0)
mFrameStep = 1 / mFPSLimit;
}
MixEngine::~MixEngine() {
//mModuleHolder.get<Audio::Core>()->release();
mModuleHolder.clear();
Platform::ShutDown();
}
int MixEngine::execute(std::shared_ptr<ApplicationBase> _app) {
if (_app == nullptr) {
throw std::runtime_error("No application specified.");
}
mApp = std::move(_app);
Time::Awake();
Platform::Initialize();
Platform::QuitEvent.connect(std::bind(&MixEngine::onQuitRequested, this));
mApp->startUp(getCommandLines());
try {
loadModule();
initModule();
loadMainScene();
while (!mQuit) {
/*awake();
init();*/
Platform::Update();
Time::Tick();
if (mRunning) {
// Limit FPS if limit was set.
if (mFPSLimit > 0) {
float currentTime = Time::TotalTime();
float nextFrameTime = mLastFrameTime + mFrameStep;
while (nextFrameTime > currentTime) {
uint32_t waitTime = (nextFrameTime - currentTime) * 1000;
// If waiting for longer, sleep
if (waitTime >= 2000) {
Platform::Sleep(waitTime);
currentTime = Time::TotalTime();
}
}
mLastFrameTime = currentTime;
}
// Calculate fps
static auto startTp = Time::RealTime();
if (++mFrameCount > mFrameSampleRate) {
mFramePerSecond = mFrameCount / (Time::RealTime() - startTp);
startTp = Time::RealTime();
mFrameCount = 0u;
}
//Window::Get()->setTitle(std::to_string(mFramePerSecond));
//auto start = Time::RealTime();
update();
//Log::Info("Update %1%", Time::RealTime() - start);
//start = Time::RealTime();
lateUpdate();
//Log::Info("lateUpdate %1%", Time::RealTime() - start);
//start = Time::RealTime();
render();
//Log::Info("render %1%", Time::RealTime() - start);
//start = Time::RealTime();
postRender();
//Log::Info("postRender %1%", Time::RealTime() - start);
//Log::Info("");
}
}
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
void MixEngine::loadMainScene() {
auto sceneManager = mModuleHolder.get<SceneManager>();
auto scene = sceneManager->createScene("MainScene");
auto filler = std::make_shared<DefaultSceneFiller>();
scene->setFiller(filler);
sceneManager->loadScene(scene->getIndex());
sceneManager->setActiveScene(scene);
mApp->onMainSceneCreated();
}
void MixEngine::update() {
mApp->onUpdate();
mModuleHolder.get<Physics::World>()->sync(Time::FixedDeltaTime(), Time::SmoothingFactor());
mModuleHolder.get<SceneManager>()->sceneUpdate();
for (auto i = 0u; i < Time::sFixedClampedSteps; ++i) {
mApp->onFixedUpdate();
fixedUpdate();
}
}
void MixEngine::fixedUpdate() {
mApp->onFixedUpdate();
mModuleHolder.get<Physics::World>()->step(Time::FixedDeltaTime());
mModuleHolder.get<SceneManager>()->sceneFixedUpdate();
#ifdef MX_ENABLE_PHYSICS_DEBUG_DRAW_
mModuleHolder.get<Physics::World>()->pushDrawData();
#endif
}
void MixEngine::lateUpdate() {
mApp->onLateUpdate();
mModuleHolder.get<SceneManager>()->sceneLateUpdate();
mModuleHolder.get<SceneObjectManager>()->lateUpdate();
mModuleHolder.get<Audio::Core>()->update();
}
void MixEngine::loadModule() {
SDL_Rect rect;
SDL_GetDisplayBounds(0, &rect);
mModuleHolder.add<Window>("Mix Engine Demo", Vector2i{ rect.w * 0.4f, rect.h * 0.8f }, WindowFlag::Vulkan | WindowFlag::Shown)->load();
mModuleHolder.add<Input>()->load();
mModuleHolder.add<Audio::Core>()->load();
mModuleHolder.add<Physics::World>()->load();
mModuleHolder.add<Graphics>()->load();
mModuleHolder.add<GUI>()->load();
mModuleHolder.add<ResourceLoader>()->load();
mModuleHolder.add<SceneObjectManager>()->load();
mModuleHolder.add<SceneManager>()->load();
std::string n = mApp->getAppName();
Version v = mApp->getAppVersion();
std::string title = Utils::StringFormat("%1% V %2%.%3%.%4%",
mApp->getAppName(),
v.getMajor(), v.getMinor(), v.getPatch());
Window::Get()->setTitle(title);
mApp->onModuleLoaded();
}
void MixEngine::initModule() {
auto modules = mModuleHolder.getAllOrdered();
for (auto m : modules)
m->init();
mApp->onModuleInitialized();
}
void MixEngine::onQuitRequested() {
if (mApp->onQuitRequested())
quit();
}
void MixEngine::render() {
mApp->onRender();
#ifdef MX_ENABLE_PHYSICS_DEBUG_DRAW_
mModuleHolder.get<Physics::World>()->render();
#endif
mModuleHolder.get<GUI>()->beginGUI();
mApp->onGUI();
mModuleHolder.get<GUI>()->endGUI();
mModuleHolder.get<GUI>()->update();
mModuleHolder.get<Graphics>()->update();
mModuleHolder.get<Graphics>()->render();
}
void MixEngine::postRender() {
mApp->onPostRender();
mModuleHolder.get<SceneManager>()->scenePostRender();
mModuleHolder.get<SceneObjectManager>()->postRender();
mModuleHolder.get<Input>()->nextFrame();
}
}