forked from tapetums/include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.hpp
132 lines (102 loc) · 3.38 KB
/
Application.hpp
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
#pragma once
//---------------------------------------------------------------------------//
//
// Application.hpp
// メッセージループをカプセル化するクラス
// Copyright (C) 2013-2016 tapetums
//
//---------------------------------------------------------------------------//
#include <windows.h>
//---------------------------------------------------------------------------//
namespace tapetums
{
class Application;
}
//---------------------------------------------------------------------------//
// Class
//---------------------------------------------------------------------------//
// メッセージループをカプセル化するクラス
class tapetums::Application
{
public:
Application() = delete;
~Application() = delete;
Application(const Application&) = delete;
Application& operator= (const Application&) = delete;
Application(Application&&) noexcept = delete;
Application& operator= (Application&&) noexcept = delete;
public:
static DWORD ThreadId() noexcept { return threadId(); }
static bool IsLoop() noexcept { return loop(); }
public:
static INT32 Run();
static bool Exit();
static void Pause();
static void Resume();
template<typename Updater>
static INT32 Run(Updater& update);
private:
static DWORD& threadId() noexcept { static DWORD threadId { 0 }; return threadId; }
static bool& loop() noexcept { static bool loop { true }; return loop; }
};
//---------------------------------------------------------------------------//
// Methods
//---------------------------------------------------------------------------//
// メッセージループ
inline INT32 tapetums::Application::Run()
{
threadId() = ::GetCurrentThreadId();
MSG msg { };
while ( ::GetMessage(&msg, nullptr, 0, 0) > 0 )
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return static_cast<INT32>(msg.wParam);
}
//---------------------------------------------------------------------------//
// ゲームループ
template<typename Updater>
inline INT32 tapetums::Application::Run(Updater& update)
{
threadId() = ::GetCurrentThreadId();
MSG msg { };
while ( true )
{
if ( ::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) )
{
if ( msg.message == WM_QUIT ) { break; }
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else if ( loop() )
{
update(); // ファンクタの呼び出し
}
else
{
::MsgWaitForMultipleObjects(0, nullptr, FALSE, INFINITE, QS_ALLINPUT);
}
}
return static_cast<INT32>(msg.wParam);
}
//---------------------------------------------------------------------------//
// メッセージループの終了
inline bool tapetums::Application::Exit()
{
return ::PostThreadMessage(threadId(), WM_QUIT, 0, 0) ? true : false;
}
//---------------------------------------------------------------------------//
// ゲームループを停止
inline void tapetums::Application::Pause()
{
loop() = false;
}
//---------------------------------------------------------------------------//
// ゲームループを再開
inline void tapetums::Application::Resume()
{
loop() = true;
}
//---------------------------------------------------------------------------//
// Application.hpp