-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.cpp
More file actions
215 lines (186 loc) · 6.18 KB
/
Copy pathApplication.cpp
File metadata and controls
215 lines (186 loc) · 6.18 KB
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
#include "Application.h"
#include "Util.h"
#include <vector>
namespace FD2D
{
Application& Application::Instance()
{
static Application instance;
return instance;
}
HRESULT Application::Initialize(const InitContext& context)
{
if (m_initialized)
{
return S_FALSE;
}
m_context = context;
HRESULT hr = Core::Initialize(m_context);
if (FAILED(hr))
{
return hr;
}
m_initialized = true;
return S_OK;
}
void Application::Shutdown()
{
m_backplates.clear();
Core::Shutdown();
m_initialized = false;
}
std::shared_ptr<Backplate> Application::CreateBackplate(const std::wstring& name)
{
if (name.empty() || m_backplates.find(name) != m_backplates.end())
{
return nullptr;
}
auto backplate = std::make_shared<Backplate>(name);
// Use insert instead of emplace (prevents optimization issues in Release mode)
m_backplates[name] = backplate;
return backplate;
}
std::shared_ptr<Backplate> Application::CreateWindowedBackplate(const std::wstring& name, const WindowOptions& options)
{
if (name.empty() || m_backplates.find(name) != m_backplates.end())
{
return nullptr;
}
auto backplate = std::make_shared<Backplate>(name);
if (FAILED(backplate->CreateWindowed(options)))
{
return nullptr;
}
// Use insert instead of emplace (prevents optimization issues in Release mode)
m_backplates[name] = backplate;
return backplate;
}
bool Application::RegisterBackplate(const std::shared_ptr<Backplate>& backplate)
{
if (!backplate)
{
return false;
}
const std::wstring name = backplate->Name();
if (name.empty() || m_backplates.find(name) != m_backplates.end())
{
return false;
}
m_backplates[name] = backplate;
return true;
}
std::shared_ptr<Backplate> Application::GetBackplate(const std::wstring& name) const
{
auto it = m_backplates.find(name);
if (it != m_backplates.end())
{
return it->second;
}
return nullptr;
}
int Application::RunMessageLoop()
{
for (;;)
{
// Build the set of async redraw events (one per backplate).
std::vector<HANDLE> events;
events.reserve(m_backplates.size());
for (const auto& kv : m_backplates)
{
if (kv.second)
{
HANDLE ev = kv.second->AsyncRedrawEvent();
if (ev)
{
events.push_back(ev);
}
}
}
const DWORD waitCount = static_cast<DWORD>(events.size());
// If any backplate has an active animation, wake at ~60fps to paint smoothly.
// Otherwise, keep a safety heartbeat (prevents "stuck forever" if a wakeup is missed).
DWORD timeoutMs = 1000;
const unsigned long long now = Util::NowMs();
for (const auto& kv : m_backplates)
{
if (kv.second && kv.second->HasActiveAnimation(now))
{
timeoutMs = 16;
break;
}
}
DWORD waitRes = WAIT_TIMEOUT;
if (waitCount > 0)
{
waitRes = MsgWaitForMultipleObjectsEx(
waitCount,
waitCount > 0 ? events.data() : nullptr,
timeoutMs,
QS_ALLINPUT,
MWMO_INPUTAVAILABLE);
if (waitRes == WAIT_FAILED)
{
return -1;
}
// One of our async redraw events fired.
if (waitRes >= WAIT_OBJECT_0 && waitRes < WAIT_OBJECT_0 + waitCount)
{
for (const auto& kv : m_backplates)
{
if (kv.second)
{
kv.second->ProcessAsyncRedraw();
}
}
// Do NOT continue here:
// When worker completions arrive frequently (e.g., heavy I/O), we can starve the animation tick
// and make spinners/fades appear to "pause". We'll fall through to drain messages and run a
// throttled animation tick each loop.
}
}
// Animation tick timeout (no messages/events, but animations are active).
if (waitRes == WAIT_TIMEOUT)
{
const unsigned long long t = Util::NowMs();
for (const auto& kv : m_backplates)
{
if (kv.second)
{
kv.second->ProcessAnimationTick(t);
}
}
continue;
}
// Process all pending Windows messages.
MSG msg {};
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
return static_cast<int>(msg.wParam);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Important: animations must advance even when the message queue is busy and we never hit WAIT_TIMEOUT.
// So after draining messages, run a throttled animation tick.
const unsigned long long t = Util::NowMs();
for (const auto& kv : m_backplates)
{
if (kv.second)
{
kv.second->ProcessAnimationTick(t);
}
}
Sleep(0);
}
}
HINSTANCE Application::HInstance() const
{
return m_context.instance != nullptr ? m_context.instance : Core::Instance();
}
bool Application::Initialized() const
{
return m_initialized;
}
}