-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Siv3DMain.cpp
381 lines (319 loc) · 8.84 KB
/
Siv3DMain.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//-----------------------------------------------
//
// This file is part of the Siv3D Engine.
//
// Copyright (c) 2008-2023 Ryo Suzuki
// Copyright (c) 2016-2023 OpenSiv3D Project
//
// Licensed under the MIT License.
//
//-----------------------------------------------
# include <crtdbg.h>
# include <future>
# include <Siv3D/Error.hpp>
# include <Siv3D/Unicode.hpp>
# include <Siv3D/Format.hpp>
# include <Siv3D/System.hpp>
# include <Siv3D/FileSystem.hpp>
# include <Siv3D/EngineLog.hpp>
# include <Siv3D/FreestandingMessageBox/FreestandingMessageBox.hpp>
# include <Siv3D/EngineOptions.hpp>
# include <Siv3D/Windows/Windows.hpp>
# include <Siv3D/Common/Siv3DEngine.hpp>
# include <Siv3D/System/CSystem.hpp>
# include <Siv3D/Window/CWindow.hpp>
# include <Siv3D/Script/IScript.hpp>
void Main();
// [THREAD #0] [THREAD #1]
//
// WinMain() {
// f = std::async(MainThread) || MainThread() {
// **LOCK** || Siv3DEngine engine;
// || engine->System->init();
// <= **LOCK**
// engine->Window->init(); ||
// **LOCK** =>
// || engine->System->init2();
// <=
// while (!f._Is_ready()) { || Main() { while(System::Update()) {} } // User code
// PumMessages(); ||
// || engine::~Siv3DEngine() {
// if (g_callWindowDestroy) { || ...
// engine->Window->destroy(); || g_callWindowDestroy = true
// g_callWindowDestroy = false; || |
// } || wait until (g_callWindowDestroy == false)
// || ...
// Sleep(1); || }
// } || }
// }
namespace s3d
{
namespace
{
std::condition_variable g_cv;
std::mutex g_mutex;
int g_initStep = 0;
bool g_hasError = false;
// デバッグ実行時には、Visual Studio に例外の情報を渡す
const bool g_isDebuggerPresent = IsDebuggerPresent();
}
std::atomic<bool> g_callWindowDestroy{ false };
static void PumpMessages()
{
for (int32 i = 0; i < 100; ++i)
{
MSG message = {};
if (::PeekMessageW(&message, nullptr, 0, 0, PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessageW(&message);
}
else
{
return;
}
}
}
static void TryMain()
{
String errorMessage;
try
{
LOG_TRACE(U"Main() ---");
Main();
LOG_TRACE(U"--- Main()");
}
catch (const Error& error)
{
errorMessage = Format(error);
}
catch (const fmt::format_error& error)
{
errorMessage = U"Error occured in _fmt : {}"_fmt(Unicode::Widen(error.what()));
}
catch (const std::runtime_error& error)
{
errorMessage = U"[std::runtime_error] {}"_fmt(Unicode::Widen(error.what()));
}
catch (const std::out_of_range& error)
{
errorMessage = U"[std::out_of_range] {}"_fmt(Unicode::Widen(error.what()));
}
catch (const std::exception& error)
{
errorMessage = U"[std::exception] {}"_fmt(Unicode::Widen(error.what()));
}
if (errorMessage)
{
//static_cast<void>(Window::SetFullscreen(false)); // メッセージボックスを表示するためにフルスクリーンモードを解除
errorMessage += U"\n\nFor more information, [Debug] -> [Windows] -> [Exception Settings] -> Tick the C++ Exceptions checkbox under the [Break When Thrown] heading.";
FreestandingMessageBox::ShowError(errorMessage);
}
SIV3D_ENGINE(Script)->shutdown();
}
[[nodiscard]]
constexpr StringView ExceptionToString(const DWORD code) noexcept
{
switch (code)
{
case EXCEPTION_ACCESS_VIOLATION:
return U"EXCEPTION_ACCESS_VIOLATION"_sv;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
return U"EXCEPTION_ARRAY_BOUNDS_EXCEEDED"_sv;
case EXCEPTION_BREAKPOINT:
return U"EXCEPTION_BREAKPOINT"_sv;
case EXCEPTION_DATATYPE_MISALIGNMENT:
return U"EXCEPTION_DATATYPE_MISALIGNMENT"_sv;
case EXCEPTION_FLT_DENORMAL_OPERAND:
return U"EXCEPTION_FLT_DENORMAL_OPERAND"_sv;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
return U"EXCEPTION_FLT_DIVIDE_BY_ZERO"_sv;
case EXCEPTION_FLT_INEXACT_RESULT:
return U"EXCEPTION_FLT_INEXACT_RESULT"_sv;
case EXCEPTION_FLT_INVALID_OPERATION:
return U"EXCEPTION_FLT_INVALID_OPERATION"_sv;
case EXCEPTION_FLT_OVERFLOW:
return U"EXCEPTION_FLT_OVERFLOW"_sv;
case EXCEPTION_FLT_STACK_CHECK:
return U"EXCEPTION_FLT_STACK_CHECK"_sv;
case EXCEPTION_FLT_UNDERFLOW:
return U"EXCEPTION_FLT_UNDERFLOW"_sv;
case EXCEPTION_ILLEGAL_INSTRUCTION:
return U"EXCEPTION_ILLEGAL_INSTRUCTION"_sv;
case EXCEPTION_IN_PAGE_ERROR:
return U"EXCEPTION_IN_PAGE_ERROR"_sv;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
return U"EXCEPTION_INT_DIVIDE_BY_ZERO"_sv;
case EXCEPTION_INT_OVERFLOW:
return U"EXCEPTION_INT_OVERFLOW"_sv;
case EXCEPTION_INVALID_DISPOSITION:
return U"EXCEPTION_INVALID_DISPOSITION"_sv;
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
return U"EXCEPTION_NONCONTINUABLE_EXCEPTION"_sv;
case EXCEPTION_PRIV_INSTRUCTION:
return U"EXCEPTION_PRIV_INSTRUCTION"_sv;
case EXCEPTION_SINGLE_STEP:
return U"EXCEPTION_SINGLE_STEP"_sv;
case EXCEPTION_STACK_OVERFLOW:
return U"EXCEPTION_STACK_OVERFLOW"_sv;
default:
return U"Unknown Exception"_sv;
}
}
static int ShowException(const DWORD code, struct _EXCEPTION_POINTERS*)
{
LOG_ERROR(U"🛑 Application terminated due to an exception `{}`"_fmt(ExceptionToString(code)));
Window::Minimize();
FreestandingMessageBox::ShowError(U"Application terminated due to an exception `{}`"_fmt(ExceptionToString(code)));
return EXCEPTION_EXECUTE_HANDLER;
}
static void MainSEH()
{
__try
{
TryMain();
}
__except (ShowException(GetExceptionCode(), GetExceptionInformation()))
{
}
}
static void MainThread()
{
Siv3DEngine engine;
std::unique_lock ul(g_mutex); // (1)--
try
{
SIV3D_ENGINE(System)->init();
}
catch (const Error& error)
{
::OutputDebugStringW(Format(error, U'\n').toWstr().c_str());
FreestandingMessageBox::ShowError(Format(error));
g_hasError = true;
ul.unlock(); // --(1)
g_cv.notify_one();
return;
}
g_initStep = 1;
ul.unlock(); // --(1)
g_cv.notify_one();
ul.lock();
g_cv.wait(ul, []() { return ((g_initStep == 2) || g_hasError); }); // (3)--
if (g_hasError)
{
ul.unlock();
g_cv.notify_one();
return;
}
try
{
static_cast<CSystem*>(SIV3D_ENGINE(System))->init2();
}
catch (const Error& error)
{
::OutputDebugStringW(Format(error, U'\n').toWstr().c_str());
FreestandingMessageBox::ShowError(Format(error));
g_hasError = true;
ul.unlock(); // --(3)
g_cv.notify_one();
return;
}
g_initStep = 3;
ul.unlock(); // --(3)
g_cv.notify_one();
// (4b)--
MainSEH();
}
static void SetWorkingDirectory()
{
if (System::IsRunningInVisualStudio())
{
return;
}
if (const FilePath workingDirectory = FileSystem::ParentPath(FileSystem::ModulePath()))
{
FileSystem::ChangeCurrentDirectory(workingDirectory);
}
}
namespace detail::init
{
void InitCommandLines(int argc, char** argv);
}
}
int WINAPI WinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR, _In_ int)
{
using namespace s3d;
if constexpr (SIV3D_BUILD(DEBUG))
{
if (g_isDebuggerPresent &&
(g_engineOptions.debugHeap == EngineOption::DebugHeap::Yes))
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
}
}
detail::init::InitCommandLines(__argc, __argv);
if (::OleInitialize(nullptr) >= 0)
{
::CoUninitialize();
::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
}
SetWorkingDirectory();
std::unique_lock ul(g_mutex); // (0)--
const std::future<void> f = std::async(std::launch::async, MainThread);
g_cv.wait(ul, [&]() { return ((g_initStep == 1) || g_hasError); }); // --(0) (2)--
if (g_hasError)
{
return -1;
}
try
{
SIV3D_ENGINE(Window)->init();
}
catch (const Error& error)
{
::OutputDebugStringW(Format(error, U'\n').toWstr().c_str());
g_hasError = true;
return -1;
}
g_initStep = 2;
ul.unlock(); // --(2)
g_cv.notify_one();
ul.lock();
g_cv.wait(ul, []() { return ((g_initStep == 3) || g_hasError); }); // (4a)--
if (g_hasError)
{
while (!f._Is_ready())
{
if (g_callWindowDestroy)
{
if (auto pCWindow = dynamic_cast<CWindow*>(SIV3D_ENGINE(Window)))
{
pCWindow->destroy();
}
g_callWindowDestroy = false;
}
::Sleep(1);
}
return -1;
}
while (f.wait_for(std::chrono::seconds(0)) != std::future_status::ready)
{
PumpMessages();
if (g_callWindowDestroy)
{
if (auto pCWindow = dynamic_cast<CWindow*>(SIV3D_ENGINE(Window)))
{
pCWindow->destroy();
}
g_callWindowDestroy = false;
}
::timeBeginPeriod(1);
{
::Sleep(1);
}
::timeEndPeriod(1);
}
::CoUninitialize();
::OleUninitialize();
return 0;
}