-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIrrHandling.cpp
264 lines (216 loc) · 6.56 KB
/
IrrHandling.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
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
255
256
257
258
259
260
261
262
263
264
#include "IrrHandling.h"
#include "IrrManagers.h"
#include "LimeReceiver.h"
#include "Sound.h"
#include <filesystem>
namespace fs = std::filesystem;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
struct windowState {
int curWidth;
int curHeight;
};
windowState window = { 640, 480 };
std::string getMainPath(const std::string& searchDirectory) {
try {
for (const auto& entry : fs::recursive_directory_iterator(searchDirectory)) {
if (entry.is_regular_file() && entry.path().filename() == "main.lua") {
return entry.path().string(); // Return the full path to main.lua
}
}
}
catch (const std::exception& e) {
std::cerr << "Error during directory traversal: " << e.what() << std::endl;
}
return "";
}
void IrrHandling::setDriver(irr::video::E_DRIVER_TYPE type) {
driverType = type;
}
void IrrHandling::initScene()
{
driverType = irr::video::EDT_DIRECT3D9;
width = 640;
height = 480;
fullscreen = false;
vSync = false;
stencil = false;
dConsole.enabled = false;
dConsole.doOutput = false;
posX = 0;
posY = 0;
fps = 0;
window.curWidth = width;
window.curHeight = height;
LuaLime l;
l.initLua(smgr, driver);
// Is main.lua safe?
std::string mainPath = getMainPath(".");
if (mainPath == "") {
dConsole.doOutput = true;
std::string err = "main.lua could not be found!";
dConsole.sendMsg(err.c_str(), 1);
end();
return;
}
sol::protected_function_result exec_result = lua->safe_script_file(mainPath);
if (dConsole.enabled)
dConsole.makeConsole();
dConsole.sendMsg("Lime started", 0);
receiver = new LimeReceiver();
sound = irrklang::createIrrKlangDevice();
soundManager = new SoundManager();
device = irr::createDevice(driverType, dimension2d<u32>(width, height), 16, false, stencil, vSync, receiver);
device->setWindowCaption(L"Lime Application");
driver = device->getVideoDriver();
effects = new EffectHandler(device, driver->getScreenSize(), false, false, false);
smgr = device->getSceneManager();
guienv = device->getGUIEnvironment();
appLoop();
}
void IrrHandling::makeNewDevice() {
if (device) {
device->drop();
}
device = irr::createDevice(driverType, dimension2d<u32>(width, height), 16, false, stencil, vSync, receiver);
}
void IrrHandling::doFullscreen() {
if (device) {
device->maximizeWindow();
HWND hwnd = reinterpret_cast<HWND>(device->getVideoDriver()->getExposedVideoData().OpenGLWin32.HWnd);
LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE);
style &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
style |= WS_POPUP;
SetWindowLongPtr(hwnd, GWL_STYLE, style);
ShowWindow(hwnd, SW_MAXIMIZE);
SetWindowPos(hwnd, HWND_TOP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_FRAMECHANGED | SWP_SHOWWINDOW);
}
}
void IrrHandling::doWindowed() {
if (device) {
HWND hwnd = reinterpret_cast<HWND>(device->getVideoDriver()->getExposedVideoData().OpenGLWin32.HWnd);
LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE);
style |= (WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
style &= ~WS_POPUP;
SetWindowLongPtr(hwnd, GWL_STYLE, style);
SetWindowPos(hwnd, HWND_TOP, 100, 100, window.curWidth, window.curHeight, SWP_FRAMECHANGED | SWP_SHOWWINDOW);
ShowWindow(hwnd, SW_RESTORE);
device->restoreWindow();
}
}
int IrrHandling::getMemUsed() {
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
SIZE_T physMemUsedByMe = pmc.WorkingSetSize;
int physMemUsedMB = physMemUsedByMe / (1024.0 * 1024.0);
return physMemUsedMB;
}
void IrrHandling::end() {
if (!didEnd) {
dConsole.sendMsg("Ending application...", 0);
if (dConsole.doOutput) { // write console output to output.txt if enabled
dConsole.writeOutput();
}
if (device) {
device->closeDevice();
}
didEnd = true;
}
}
void IrrHandling::appLoop() {
// Events
sol::protected_function luaOnStart = (*lua)["Lime"]["OnStart"];
sol::protected_function luaOnUpdate = (*lua)["Lime"]["OnUpdate"];
sol::protected_function luaOnEnd = (*lua)["Lime"]["OnEnd"];
// Call start in main
if ((*lua)["Lime"]["OnStart"].get_type() == sol::type::function) {
sol::protected_function_result result = luaOnStart(dt);
if (!result.valid())
{
sol::error err = result;
dConsole.sendMsg(std::string(err.what()).c_str(), 1);
}
}
u32 then = device->getTimer()->getTime();
f32 const frameDur = 1000.f / m_frameLimit;
while (device->run()) {
const u32 now = device->getTimer()->getTime();
dt = (now - then) / 16.667f;
then = now;
// Call update in main
if ((*lua)["Lime"]["OnUpdate"].get_type() == sol::type::function) {
sol::protected_function_result result = luaOnUpdate(dt);
if (!result.valid())
{
sol::error err = result;
dConsole.sendMsg(std::string(err.what()).c_str(), 1);
}
}
if (mainCamera) {
mainCamera->updateAbsolutePosition();
mainCameraForward->updateAbsolutePosition();
mainCamera->setTarget(mainCameraForward->getAbsolutePosition());
}
driver->beginScene(true, true, SColor(0x0));
//smgr->drawAll();
effects->update();
guienv->drawAll();
driver->endScene();
updateFPS();
// Set debug window caption
if (dConsole.enabled) {
stringw tmp(L"Lime Debugger [");
tmp += driver->getName();
tmp += L"] fps: ";
tmp += fps;
tmp += " | mem: ";
tmp += getMemUsed();
tmp += " MB";
SetConsoleTitle(tmp.c_str());
}
f32 frameTime = device->getTimer()->getTime() - now;
if (frameTime < frameDur)
device->sleep((frameDur - frameTime) / 2.0);
}
// Call end in main
if ((*lua)["Lime"]["OnEnd"].get_type() == sol::type::function) {
sol::protected_function_result result = luaOnEnd();
if (!result.valid())
{
sol::error err = result;
dConsole.sendMsg(std::string(err.what()).c_str(), 1);
}
}
if (!didEnd)
end();
}
bool IrrHandling::writeTextureToFile(irr::video::ITexture* texture, const irr::core::stringw& name)
{
if (!texture)
return false;
void* data = texture->lock(irr::video::ETLM_READ_ONLY);
if (!data)
return false;
irr::video::IImage* image = driver->createImageFromData(texture->getColorFormat(), texture->getSize(), data, false);
texture->unlock();
if (!image)
return false;
driver->writeImageToFile(image, name.c_str(), 99);
image->drop();
return true;
}
void IrrHandling::updateFPS() {
u32 currentTime = device->getTimer()->getTime();
// Increment the frame counter
++frameCount;
// Calculate FPS once every second
if (currentTime - lastTime >= 1000) {
fps = frameCount / ((currentTime - lastTime) / 1000.0f);
lastTime = currentTime;
frameCount = 0;
}
}