-
Notifications
You must be signed in to change notification settings - Fork 1
/
Application.hpp
146 lines (116 loc) · 3.79 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once
//---------------------------------------------------------------------------//
//
// Application.hpp
// メッセージループをカプセル化するクラス
// Copyright (C) 2013-2017 tapetums
//
//---------------------------------------------------------------------------//
#include <windows.h>
//---------------------------------------------------------------------------//
// Forward Declarations
//---------------------------------------------------------------------------//
namespace tapetums
{
class Application;
}
//---------------------------------------------------------------------------//
// Classes
//---------------------------------------------------------------------------//
// メッセージループをカプセル化するクラス
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 thread_id() noexcept { return m_thread_id(); }
static bool is_loop() noexcept { return m_loop(); }
public:
static bool Exit (INT32 nExitCode = 0);
static void Pause ();
static void Resume();
static INT32 Run();
template<typename Updater, typename... Args>
static INT32 Run(Updater& update, Args&... args);
private:
static DWORD& m_thread_id() noexcept { static DWORD id { 0 }; return id; }
static bool& m_loop() noexcept { static bool loop { true }; return loop; }
};
//---------------------------------------------------------------------------//
// Methods
//---------------------------------------------------------------------------//
// メッセージループの終了
inline bool tapetums::Application::Exit(INT32 nExitCode)
{
return ::PostThreadMessage(thread_id(), WM_QUIT, nExitCode, 0) ? true : false;
}
//---------------------------------------------------------------------------//
// ゲームループを停止
inline void tapetums::Application::Pause()
{
m_loop() = false;
}
//---------------------------------------------------------------------------//
// ゲームループを再開
inline void tapetums::Application::Resume()
{
m_loop() = true;
}
//---------------------------------------------------------------------------//
// メッセージループ
inline INT32 tapetums::Application::Run()
{
// 静的変数を初期化
m_thread_id() = ::GetCurrentThreadId();
// メインループ
MSG msg;
while ( ::GetMessage(&msg, nullptr, 0, 0) > 0 )
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return static_cast<INT32>(msg.wParam);
}
//---------------------------------------------------------------------------//
// ゲームループ
template<typename Updater, typename... Args>
inline INT32 tapetums::Application::Run
(
Updater& update
Args&... args
)
{
// 静的変数を初期化
m_thread_id() = ::GetCurrentThreadId();
m_loop() = true;
// メインループ
MSG msg;
for ( ; ; )
{
if ( ::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) )
{
if ( msg.message == WM_QUIT ) { break; }
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else if ( is_loop() )
{
update(args...); // ファンクタの呼び出し
}
else
{
::MsgWaitForMultipleObjects
(
0, nullptr, FALSE, INFINITE, QS_ALLINPUT
);
}
}
return static_cast<INT32>(msg.wParam);
}
//---------------------------------------------------------------------------//
// Application.hpp