-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppWindow.cpp
More file actions
219 lines (178 loc) · 6.15 KB
/
AppWindow.cpp
File metadata and controls
219 lines (178 loc) · 6.15 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
215
216
217
218
219
#include "AppWindow_Internal.h"
#include "core/Log.h"
#include <algorithm>
#include <string>
AppWindow::AppWindow() = default;
AppWindow::~AppWindow()
{
#if defined(COLONY_WITH_IMGUI)
if (m_impl && m_impl->imguiInitialized)
{
m_impl->imgui.shutdown();
m_impl->imguiInitialized = false;
}
#endif
}
bool AppWindow::Create(HINSTANCE hInst, int nCmdShow, int width, int height)
{
m_impl = std::make_unique<Impl>();
// Default window size.
m_width = (width > 0) ? width : 1280;
m_height = (height > 0) ? height : 720;
// Load persisted settings (if any).
colony::appwin::UserSettings loaded{};
if (colony::appwin::LoadUserSettings(loaded))
{
m_impl->settings = loaded;
m_impl->settingsLoaded = true;
// If caller didn't supply an explicit size, use the saved size.
if (width <= 0 && loaded.windowWidth > 0)
m_width = loaded.windowWidth;
if (height <= 0 && loaded.windowHeight > 0)
m_height = loaded.windowHeight;
}
else
{
// Ensure we persist something sensible on first run.
m_impl->settings.windowWidth = m_width;
m_impl->settings.windowHeight = m_height;
}
// Apply settings → runtime.
m_vsync = m_impl->settings.vsync;
m_impl->pacer.SetMaxFpsWhenVsyncOff(m_impl->settings.maxFpsWhenVsyncOff);
m_impl->pacer.SetMaxFpsWhenUnfocused(m_impl->settings.maxFpsWhenUnfocused);
// Debug overlay preference.
m_impl->overlayVisible = m_impl->settings.overlayVisible;
// Fixed-step simulation settings (clamped to sane ranges).
m_impl->simTickHz = std::clamp(m_impl->settings.simTickHz, 1.0, 1000.0);
m_impl->simFixedDt = 1.0 / m_impl->simTickHz;
m_impl->simMaxStepsPerFrame = std::clamp(m_impl->settings.simMaxStepsPerFrame, 1, 240);
m_impl->simMaxFrameDt = std::clamp(m_impl->settings.simMaxFrameDt, 0.001, 1.0);
m_impl->simTimeScale = std::clamp(m_impl->settings.simTimeScale, 0.0f, 16.0f);
// -------------------------------------------------------------------------
// Register window class.
// -------------------------------------------------------------------------
const wchar_t* const CLASS_NAME = L"ColonyGameWindow";
WNDCLASSW wc{};
wc.lpfnWndProc = AppWindow::WndProc;
wc.hInstance = hInst;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursorW(nullptr, IDC_ARROW);
RegisterClassW(&wc);
// -------------------------------------------------------------------------
// Create window.
// -------------------------------------------------------------------------
RECT wr{ 0, 0, m_width, m_height };
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
m_hwnd = CreateWindowExW(
0,
CLASS_NAME,
L"Colony Game Prototype",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
wr.right - wr.left,
wr.bottom - wr.top,
nullptr,
nullptr,
hInst,
this);
if (!m_hwnd)
return false;
// Initialize helper systems.
m_impl->mouse.Initialize(m_hwnd);
m_impl->fullscreen.InitFromCurrent(m_hwnd);
// Initialize D3D.
if (!m_gfx.Init(m_hwnd, static_cast<UINT>(m_width), static_cast<UINT>(m_height)))
return false;
#if defined(COLONY_WITH_IMGUI)
// Minimal overlay by default (no docking/viewports).
m_impl->imgui.enabled = true;
m_impl->imgui.enableDocking = false;
m_impl->imgui.enableViewports = false;
m_impl->imgui.drawDockspaceAndMenu = false;
m_impl->imgui.drawImGuiDebugWindows = false;
m_impl->imguiInitialized = m_impl->imgui.initialize(m_hwnd, m_gfx.Device(), m_gfx.Context());
if (!m_impl->imguiInitialized)
colony::LogLine(L"[ImGui] Failed to initialize");
#endif
// Show window (respect saved maximize state).
const int showCmd = m_impl->settings.maximize ? SW_MAXIMIZE : nCmdShow;
ShowWindow(m_hwnd, showCmd);
UpdateWindow(m_hwnd);
// Apply saved fullscreen preference.
if (m_impl->settings.fullscreen)
{
// Apply without marking settings dirty.
m_impl->fullscreen.Toggle(m_hwnd);
RECT cr{};
GetClientRect(m_hwnd, &cr);
m_width = static_cast<int>(cr.right - cr.left);
m_height = static_cast<int>(cr.bottom - cr.top);
m_gfx.Resize(static_cast<UINT>(m_width), static_cast<UINT>(m_height));
}
UpdateTitle();
return true;
}
void AppWindow::MarkSettingsDirty()
{
if (!m_impl)
return;
m_impl->settingsDirty = true;
m_impl->settingsDirtySince = std::chrono::steady_clock::now();
}
void AppWindow::ToggleVsync()
{
m_vsync = !m_vsync;
if (m_impl)
{
m_impl->settings.vsync = m_vsync;
MarkSettingsDirty();
}
UpdateTitle();
}
void AppWindow::ToggleFullscreen()
{
if (!m_impl)
return;
m_impl->fullscreen.Toggle(m_hwnd);
m_impl->settings.fullscreen = m_impl->fullscreen.IsFullscreen();
// Ensure swapchain matches the new client size.
RECT cr{};
GetClientRect(m_hwnd, &cr);
m_width = static_cast<int>(cr.right - cr.left);
m_height = static_cast<int>(cr.bottom - cr.top);
m_gfx.Resize(static_cast<UINT>(m_width), static_cast<UINT>(m_height));
MarkSettingsDirty();
UpdateTitle();
}
void AppWindow::ToggleOverlay()
{
if (!m_impl)
return;
m_impl->overlayVisible = !m_impl->overlayVisible;
m_impl->settings.overlayVisible = m_impl->overlayVisible;
MarkSettingsDirty();
}
void AppWindow::UpdateTitle()
{
if (!m_hwnd || !m_impl)
return;
const auto cam = m_impl->game.GetDebugCameraInfo();
const bool isFullscreen = m_impl->fullscreen.IsFullscreen();
const bool isActive = m_impl->active;
wchar_t title[512]{};
swprintf_s(
title,
L"Colony Game | %s%s | %s | FPS: %.1f | Sim: %.1f Hz%s | Yaw: %.0f Pitch: %.0f Dist: %.1f",
m_vsync ? L"VSync" : L"NoVSync",
isFullscreen ? L" | Fullscreen" : L"",
isActive ? L"Active" : L"Background",
m_impl->pacer.Fps(),
m_impl->simTickHz,
m_impl->simPaused ? L" (Paused)" : L"",
cam.yawDeg,
cam.pitchDeg,
cam.distance);
SetWindowTextW(m_hwnd, title);
}