Apply clang-format across the whole tree
Run clang-format (the repo's .clang-format: LLVM base, 120 cols, tabs, Allman functions) over every source file so the tree is formatter-clean. Whitespace only -- no behavior change; full x64 + x86 suites pass. Also set SortIncludes: false in .clang-format. Windows include order is load-bearing (windows.h must precede tlhelp32.h / mmreg.h / xinput.h / dinput.h; winsock2.h must precede windows.h), and the default alphabetical sort reorders tlhelp32.h ahead of windows.h -- a build break. Leaving order alone keeps the manual, correct grouping.
This commit is contained in:
@@ -9,11 +9,9 @@ extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
constexpr wchar_t kWindowClass[] = L"CoopAllTheThingsWindow";
|
||||
|
||||
// Encode a tightly-packed/row-pitched RGBA8 image to a PNG file via WIC. `src` is the
|
||||
@@ -24,42 +22,34 @@ bool write_rgba8_png(const std::wstring& path, UINT width, UINT height, const BY
|
||||
{
|
||||
ComPtr<IWICImagingFactory> factory;
|
||||
if (FAILED(CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER,
|
||||
IID_PPV_ARGS(factory.GetAddressOf()))))
|
||||
{
|
||||
IID_PPV_ARGS(factory.GetAddressOf())))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IWICBitmap> bitmap; // wrap the back-buffer bytes (RGBA, matches the swap chain)
|
||||
if (FAILED(factory->CreateBitmapFromMemory(width, height, GUID_WICPixelFormat32bppRGBA, row_pitch,
|
||||
row_pitch * height, const_cast<BYTE*>(src),
|
||||
bitmap.GetAddressOf())))
|
||||
{
|
||||
row_pitch * height, const_cast<BYTE*>(src), bitmap.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IWICStream> stream;
|
||||
if (FAILED(factory->CreateStream(stream.GetAddressOf())) ||
|
||||
FAILED(stream->InitializeFromFilename(path.c_str(), GENERIC_WRITE)))
|
||||
{
|
||||
if (FAILED(factory->CreateStream(stream.GetAddressOf()))
|
||||
|| FAILED(stream->InitializeFromFilename(path.c_str(), GENERIC_WRITE))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IWICBitmapEncoder> encoder;
|
||||
if (FAILED(factory->CreateEncoder(GUID_ContainerFormatPng, nullptr, encoder.GetAddressOf())) ||
|
||||
FAILED(encoder->Initialize(stream.Get(), WICBitmapEncoderNoCache)))
|
||||
{
|
||||
if (FAILED(factory->CreateEncoder(GUID_ContainerFormatPng, nullptr, encoder.GetAddressOf()))
|
||||
|| FAILED(encoder->Initialize(stream.Get(), WICBitmapEncoderNoCache))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IWICBitmapFrameEncode> frame;
|
||||
ComPtr<IPropertyBag2> props;
|
||||
if (FAILED(encoder->CreateNewFrame(frame.GetAddressOf(), props.GetAddressOf())) ||
|
||||
FAILED(frame->Initialize(props.Get())) || FAILED(frame->SetSize(width, height)))
|
||||
{
|
||||
if (FAILED(encoder->CreateNewFrame(frame.GetAddressOf(), props.GetAddressOf()))
|
||||
|| FAILED(frame->Initialize(props.Get())) || FAILED(frame->SetSize(width, height))) {
|
||||
return false;
|
||||
}
|
||||
// Let the encoder pick its native pixel format; WriteSource converts our RGBA to it.
|
||||
WICPixelFormatGUID fmt = GUID_WICPixelFormat32bppBGRA;
|
||||
frame->SetPixelFormat(&fmt);
|
||||
if (FAILED(frame->WriteSource(bitmap.Get(), nullptr)) || FAILED(frame->Commit()) ||
|
||||
FAILED(encoder->Commit()))
|
||||
{
|
||||
if (FAILED(frame->WriteSource(bitmap.Get(), nullptr)) || FAILED(frame->Commit()) || FAILED(encoder->Commit())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -69,8 +59,7 @@ bool write_rgba8_png(const std::wstring& path, UINT width, UINT height, const BY
|
||||
D3D11Window::~D3D11Window()
|
||||
{
|
||||
release_render_target();
|
||||
if (hwnd_ != nullptr)
|
||||
{
|
||||
if (hwnd_ != nullptr) {
|
||||
DestroyWindow(hwnd_);
|
||||
hwnd_ = nullptr;
|
||||
}
|
||||
@@ -88,8 +77,7 @@ bool D3D11Window::create(const wchar_t* title)
|
||||
wc.hInstance = instance;
|
||||
wc.hCursor = LoadCursorW(nullptr, IDC_ARROW);
|
||||
wc.lpszClassName = kWindowClass;
|
||||
if (RegisterClassExW(&wc) == 0)
|
||||
{
|
||||
if (RegisterClassExW(&wc) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -100,13 +88,11 @@ bool D3D11Window::create(const wchar_t* title)
|
||||
const int height = GetSystemMetrics(SM_CYSCREEN);
|
||||
|
||||
hwnd_ = CreateWindowExW(0, kWindowClass, title, WS_POPUP, 0, 0, width, height, nullptr, nullptr, instance, this);
|
||||
if (hwnd_ == nullptr)
|
||||
{
|
||||
if (hwnd_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!create_device())
|
||||
{
|
||||
if (!create_device()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -136,30 +122,25 @@ bool D3D11Window::create_device()
|
||||
const D3D_FEATURE_LEVEL levels[] = {D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0};
|
||||
|
||||
if (FAILED(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, levels, _countof(levels),
|
||||
D3D11_SDK_VERSION, device_.GetAddressOf(), nullptr, context_.GetAddressOf())))
|
||||
{
|
||||
D3D11_SDK_VERSION, device_.GetAddressOf(), nullptr, context_.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ComPtr<IDXGIDevice> dxgi_device;
|
||||
if (FAILED(device_.As(&dxgi_device)))
|
||||
{
|
||||
if (FAILED(device_.As(&dxgi_device))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IDXGIAdapter> adapter;
|
||||
if (FAILED(dxgi_device->GetAdapter(adapter.GetAddressOf())))
|
||||
{
|
||||
if (FAILED(dxgi_device->GetAdapter(adapter.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IDXGIFactory2> factory;
|
||||
if (FAILED(adapter->GetParent(IID_PPV_ARGS(factory.GetAddressOf()))))
|
||||
{
|
||||
if (FAILED(adapter->GetParent(IID_PPV_ARGS(factory.GetAddressOf())))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (FAILED(factory->CreateSwapChainForHwnd(device_.Get(), hwnd_, &desc, nullptr, nullptr,
|
||||
swap_chain_.GetAddressOf())))
|
||||
{
|
||||
swap_chain_.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
// Don't let DXGI swallow Alt+Enter into an exclusive-fullscreen transition.
|
||||
@@ -171,8 +152,7 @@ bool D3D11Window::create_device()
|
||||
|
||||
bool D3D11Window::note_device_loss(HRESULT hr)
|
||||
{
|
||||
if (hr != DXGI_ERROR_DEVICE_REMOVED && hr != DXGI_ERROR_DEVICE_RESET)
|
||||
{
|
||||
if (hr != DXGI_ERROR_DEVICE_REMOVED && hr != DXGI_ERROR_DEVICE_RESET) {
|
||||
return false;
|
||||
}
|
||||
// GetDeviceRemovedReason gives the specific cause (HUNG / driver internal / removed); a plain
|
||||
@@ -186,10 +166,8 @@ bool D3D11Window::note_device_loss(HRESULT hr)
|
||||
void D3D11Window::create_render_target()
|
||||
{
|
||||
ComPtr<ID3D11Texture2D> back_buffer;
|
||||
if (SUCCEEDED(swap_chain_->GetBuffer(0, IID_PPV_ARGS(back_buffer.GetAddressOf()))))
|
||||
{
|
||||
const HRESULT hr =
|
||||
device_->CreateRenderTargetView(back_buffer.Get(), nullptr, rtv_.ReleaseAndGetAddressOf());
|
||||
if (SUCCEEDED(swap_chain_->GetBuffer(0, IID_PPV_ARGS(back_buffer.GetAddressOf())))) {
|
||||
const HRESULT hr = device_->CreateRenderTargetView(back_buffer.Get(), nullptr, rtv_.ReleaseAndGetAddressOf());
|
||||
note_device_loss(hr); // a removed device surfaces here too; the render loop checks device_lost()
|
||||
}
|
||||
}
|
||||
@@ -201,14 +179,12 @@ void D3D11Window::release_render_target()
|
||||
|
||||
void D3D11Window::handle_resize(UINT width, UINT height)
|
||||
{
|
||||
if (swap_chain_ == nullptr || width == 0 || height == 0)
|
||||
{
|
||||
if (swap_chain_ == nullptr || width == 0 || height == 0) {
|
||||
return;
|
||||
}
|
||||
release_render_target();
|
||||
const HRESULT hr = swap_chain_->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0);
|
||||
if (note_device_loss(hr))
|
||||
{
|
||||
if (note_device_loss(hr)) {
|
||||
return; // device gone; the render loop will see device_lost() and stop
|
||||
}
|
||||
create_render_target();
|
||||
@@ -217,17 +193,14 @@ void D3D11Window::handle_resize(UINT width, UINT height)
|
||||
bool D3D11Window::pump_messages()
|
||||
{
|
||||
MSG msg;
|
||||
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE))
|
||||
{
|
||||
if (msg.message == WM_QUIT)
|
||||
{
|
||||
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
|
||||
if (msg.message == WM_QUIT) {
|
||||
return false;
|
||||
}
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessageW(&msg);
|
||||
}
|
||||
if (resize_pending_)
|
||||
{
|
||||
if (resize_pending_) {
|
||||
handle_resize(resize_width_, resize_height_);
|
||||
resize_pending_ = false;
|
||||
}
|
||||
@@ -240,17 +213,14 @@ void D3D11Window::render_frame(const RenderCallback& render, UINT sync_interval)
|
||||
context_->OMSetRenderTargets(1, rtv_.GetAddressOf(), nullptr);
|
||||
context_->ClearRenderTargetView(rtv_.Get(), clear);
|
||||
|
||||
if (render)
|
||||
{
|
||||
if (render) {
|
||||
render();
|
||||
}
|
||||
|
||||
// Screenshot (F10): capture after the overlay is drawn but before Present -- the
|
||||
// flip-model back buffer is undefined once presented.
|
||||
if (!pending_screenshot_.empty())
|
||||
{
|
||||
if (save_backbuffer_png(pending_screenshot_))
|
||||
{
|
||||
if (!pending_screenshot_.empty()) {
|
||||
if (save_backbuffer_png(pending_screenshot_)) {
|
||||
saved_screenshot_ = pending_screenshot_;
|
||||
}
|
||||
pending_screenshot_.clear();
|
||||
@@ -277,8 +247,7 @@ std::wstring D3D11Window::take_screenshot_result()
|
||||
bool D3D11Window::save_backbuffer_png(const std::wstring& path)
|
||||
{
|
||||
ComPtr<ID3D11Texture2D> back;
|
||||
if (FAILED(swap_chain_->GetBuffer(0, IID_PPV_ARGS(back.GetAddressOf()))))
|
||||
{
|
||||
if (FAILED(swap_chain_->GetBuffer(0, IID_PPV_ARGS(back.GetAddressOf())))) {
|
||||
return false;
|
||||
}
|
||||
D3D11_TEXTURE2D_DESC desc{};
|
||||
@@ -291,44 +260,37 @@ bool D3D11Window::save_backbuffer_png(const std::wstring& path)
|
||||
staging.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
staging.MiscFlags = 0;
|
||||
ComPtr<ID3D11Texture2D> cpu;
|
||||
if (FAILED(device_->CreateTexture2D(&staging, nullptr, cpu.GetAddressOf())))
|
||||
{
|
||||
if (FAILED(device_->CreateTexture2D(&staging, nullptr, cpu.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
context_->CopyResource(cpu.Get(), back.Get());
|
||||
|
||||
D3D11_MAPPED_SUBRESOURCE map{};
|
||||
if (FAILED(context_->Map(cpu.Get(), 0, D3D11_MAP_READ, 0, &map)))
|
||||
{
|
||||
if (FAILED(context_->Map(cpu.Get(), 0, D3D11_MAP_READ, 0, &map))) {
|
||||
return false;
|
||||
}
|
||||
// The swap chain is DXGI_FORMAT_R8G8B8A8_UNORM (see create_device), i.e. RGBA bytes.
|
||||
const bool ok =
|
||||
write_rgba8_png(path, desc.Width, desc.Height, static_cast<const BYTE*>(map.pData), map.RowPitch);
|
||||
const bool ok = write_rgba8_png(path, desc.Width, desc.Height, static_cast<const BYTE*>(map.pData), map.RowPitch);
|
||||
context_->Unmap(cpu.Get(), 0);
|
||||
return ok;
|
||||
}
|
||||
|
||||
LRESULT CALLBACK D3D11Window::wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
||||
{
|
||||
if (msg == WM_NCCREATE)
|
||||
{
|
||||
if (msg == WM_NCCREATE) {
|
||||
auto* create = reinterpret_cast<CREATESTRUCTW*>(lparam);
|
||||
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(create->lpCreateParams));
|
||||
}
|
||||
|
||||
if (ImGui_ImplWin32_WndProcHandler(hwnd, msg, wparam, lparam))
|
||||
{
|
||||
if (ImGui_ImplWin32_WndProcHandler(hwnd, msg, wparam, lparam)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto* self = reinterpret_cast<D3D11Window*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
|
||||
|
||||
switch (msg)
|
||||
{
|
||||
switch (msg) {
|
||||
case WM_SIZE:
|
||||
if (self != nullptr && wparam != SIZE_MINIMIZED)
|
||||
{
|
||||
if (self != nullptr && wparam != SIZE_MINIMIZED) {
|
||||
self->resize_pending_ = true;
|
||||
self->resize_width_ = LOWORD(lparam);
|
||||
self->resize_height_ = HIWORD(lparam);
|
||||
@@ -338,13 +300,10 @@ LRESULT CALLBACK D3D11Window::wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARA
|
||||
// Per-monitor-v2: the DPI of the display we're on changed. Resize to the rect Windows suggests
|
||||
// in lparam (its recommended handling; the resulting WM_SIZE repaints the swap chain via the
|
||||
// deferred-resize path above), then latch the new DPI for the overlay to rescale its font/style.
|
||||
if (self != nullptr)
|
||||
{
|
||||
if (const auto* suggested = reinterpret_cast<const RECT*>(lparam); suggested != nullptr)
|
||||
{
|
||||
SetWindowPos(hwnd, nullptr, suggested->left, suggested->top,
|
||||
suggested->right - suggested->left, suggested->bottom - suggested->top,
|
||||
SWP_NOZORDER | SWP_NOACTIVATE);
|
||||
if (self != nullptr) {
|
||||
if (const auto* suggested = reinterpret_cast<const RECT*>(lparam); suggested != nullptr) {
|
||||
SetWindowPos(hwnd, nullptr, suggested->left, suggested->top, suggested->right - suggested->left,
|
||||
suggested->bottom - suggested->top, SWP_NOZORDER | SWP_NOACTIVATE);
|
||||
}
|
||||
self->dpi_pending_ = true;
|
||||
self->pending_dpi_ = HIWORD(wparam); // X and Y DPI are equal; HIWORD is the Y value
|
||||
@@ -354,8 +313,7 @@ LRESULT CALLBACK D3D11Window::wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARA
|
||||
// F10 is our screenshot key; ImGui already saw this message (handler runs above),
|
||||
// so swallow it here to stop DefWindowProc from flicking into Win32 menu mode.
|
||||
// Alt+F4 (VK_F4) falls through to DefWindowProc so it still closes the window.
|
||||
if (wparam == VK_F10)
|
||||
{
|
||||
if (wparam == VK_F10) {
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user