Rise
The Vieneo Province
Loading...
Searching...
No Matches
CustomUI.cpp
Go to the documentation of this file.
1#include <WinSock2.h>
2#include <WS2tcpip.h>
3#include <Windows.h>
4#include "GameClass.h"
6
7// Keep native exception context before C++ unwinding and Windows callbacks.
9{
10 __try { return window.Run(); }
11 __except (Logger::HandleNativeException(GetExceptionInformation()))
12 {
13 TerminateProcess(GetCurrentProcess(), GetExceptionCode()); return EXIT_FAILURE;
14 }
15}
16static LRESULT MsgProcWithCrashDiagnostics(HWND window, UINT message, WPARAM wParam, LPARAM lParam, bool* handled, void* context)
17{
18 __try { return GameClass::MsgProc(window, message, wParam, lParam, handled, context); }
19 __except (Logger::HandleNativeException(GetExceptionInformation()))
20 {
21 TerminateProcess(GetCurrentProcess(), GetExceptionCode()); return 0;
22 }
23}
24static int RunClient()
25{
26 const HRESULT comResult = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
27 if (FAILED(comResult) && comResult != RPC_E_CHANGED_MODE)return EXIT_FAILURE;
28 struct ComLifetime { bool initialized; ~ComLifetime() { if (initialized)CoUninitialize(); } } com{ SUCCEEDED(comResult) };
32 struct RuntimeLifetime
33 {
34 ~RuntimeLifetime() { Rendering::AttachClientRuntime(nullptr, nullptr); }
35 } runtime;
36 GameClass game;
37 // Stop callbacks before the game dies; retain D3D until its remaining
38 // resources have finished destruction. Window11 owns the final device reset.
39 struct ClientLifetime
40 {
41 Rendering::Window11& window;
42 ~ClientLifetime() { window.StopCallbacksAndWindow(); }
43 } lifetime{ window };
44 GameClass* gPtr = &game;
45 RECT desktop;
46 if (!GetWindowRect(GetDesktopWindow(), &desktop))
47 {
48 char msg[99];
49 sprintf_s(msg, sizeof msg, "Error in GetWindowRect: %d", GetLastError());
50 gPtr->logger->Log(msg, Logger::Fatal);
51 return 0;
52 }
53
54 const HDC hDC = GetDC(nullptr); // NOLINT
55 const int nLogPixelsY = GetDeviceCaps(hDC, LOGPIXELSY);
56 ReleaseDC(nullptr, hDC);
57 gPtr->dpiscale = static_cast<float>(nLogPixelsY) / 96.0f; // 96 per inch was the original design, canary 2.5, Bill 1.8
58
59 gPtr->displayWidth = static_cast<float>(desktop.right - desktop.left);
60 gPtr->displayHeight = static_cast<float>(desktop.bottom - desktop.top);
61 const float xScale = gPtr->displayWidth / designedWidthC; // canary 2.3, Bill 2.3
62 const float yScale = gPtr->displayHeight / designedHeightC; // canary 2.1, Bill 2.1
63 if (gPtr->dpiscale > xScale)
64 gPtr->dpiscale = xScale; // dpi now 2.3x
65 if (gPtr->dpiscale > yScale)
66 gPtr->dpiscale = yScale; // dpi now 2.1x
67 if (gPtr->config.scale == 0.0f) // none configured
68 {
69 // default to DPI scale but no lower than 1.0 ... they will be able to later adjust to 0.5x the dpiscale but never lower than 1.0
70 gPtr->config.scale = RiseMath::Max(1, gPtr->dpiscale);
71
72 char msg[99];
73 sprintf_s(msg, sizeof msg, "config initial scale %.1f (dpiscale %.1f)", gPtr->config.scale, gPtr->dpiscale);
74 gPtr->logger->Log(msg, Logger::Level::Debug);
75 }
76 else if (gPtr->config.scale > RiseMath::Min(xScale, yScale) * 1.5f) // make sure that previously saved scale doesn't exceed monitor width/height
77 {
78 char msg[99];
79 sprintf_s(msg, sizeof msg, "config adjust scale was %.1f xScale %.1f yScale %.1f (min %.1f): %.1f", gPtr->config.scale, xScale, yScale, RiseMath::Min(xScale, yScale), RiseMath::Min(xScale, yScale) * 1.5f);
80 gPtr->logger->Log(msg, Logger::Level::Debug);
81
82 gPtr->config.scale = RiseMath::Min(xScale, yScale) * 1.5f;
83 }
84 else if (gPtr->config.scale < RiseMath::Max(1, 0.5f * gPtr->dpiscale))
85 {
86 char msg[99];
87 sprintf_s(msg, sizeof msg, "config adjust scale was %.1f xScale %.1f yScale %.1f (min %.1f): %.1f", gPtr->config.scale, xScale, yScale, RiseMath::Min(xScale, yScale), RiseMath::Max(1, 0.5f * gPtr->dpiscale));
88 gPtr->logger->Log(msg, Logger::Level::Debug);
89
90 gPtr->config.scale = RiseMath::Max(1, 0.5f * gPtr->dpiscale);
91 }
92 gPtr->displayscale = gPtr->config.scale;
93
94
96 options.width = static_cast<UINT>(700 * gPtr->displayscale);
97 options.height = static_cast<UINT>(500 * gPtr->displayscale);
98 options.samples = gPtr->config.videoSamples;
99 options.vsync = gPtr->config.videoVSync;
100 // Splash/login have no native frame. STATUS_GAME adds the normal game
101 // window frame before applying the saved fullscreen/maximized setting.
102 options.borderless = true;
103 options.centered = true;
104 // The graphics debug layer is opt-in; the development executable can also
105 // run on machines where that optional Windows component is not installed.
106 options.debugLayer = wcsstr(GetCommandLineW(), L"--d3d11-debug") != nullptr;
107 options.warp = wcsstr(GetCommandLineW(), L"--warp") != nullptr;
109 callbacks.create = [&]() {renderer.Reset(); return GameClass::OnCreateDevice(&renderer, Rendering::ClientBackBufferDescription(), gPtr); };
110 callbacks.resize = [&](UINT, UINT) {return GameClass::OnResetDevice(&renderer, Rendering::ClientBackBufferDescription(), gPtr); };
111 callbacks.releaseSizeDependent = [&]() {GameClass::OnLostDevice(gPtr); };
112 callbacks.destroy = [&]() {GameClass::OnDestroyDevice(gPtr); renderer.Reset(); };
113 callbacks.move = [&](double time, float elapsed) {GameClass::OnFrameMove(time, elapsed, gPtr); };
114 callbacks.draw = [&](double time, float elapsed) {GameClass::OnFrameRender(&renderer, time, elapsed, gPtr); return S_OK; };
115 callbacks.presented = [&]() {return gPtr->OnFramePresented(); };
116 callbacks.keyboard = [&](UINT key, bool down, bool alt) {GameClass::KeyboardProc(key, down, alt, gPtr); };
117 callbacks.message = [&](HWND handle, UINT message, WPARAM wParam, LPARAM lParam, bool& handled)
118 {return MsgProcWithCrashDiagnostics(handle, message, wParam, lParam, &handled, gPtr); };
119 callbacks.report = [&](const std::string& status, HRESULT result)
120 {gPtr->logger->Log(status.c_str(), FAILED(result) ? Logger::Level::Error : Logger::Level::Debug, result); };
121 HRESULT hr = window.Create(options, std::move(callbacks));
122 if (FAILED(hr)) { gPtr->logger->Log("Could not initialize the native Direct3D 11 client", Logger::Level::Fatal, hr); return EXIT_FAILURE; }
123 if (FAILED(hr = gPtr->InitDirectInput(window.Handle())))
124 {
125 gPtr->logger->Log("Could not initialize game input", Logger::Level::Fatal, hr); window.StopCallbacksAndWindow(); return EXIT_FAILURE;
126 }
127 int result = EXIT_FAILURE;
128 try { result = RunMainLoopWithCrashDiagnostics(window); }
129 catch (const std::exception& error) { gPtr->logger->Log(error.what(), Logger::Level::Fatal); }
130 catch (...) { gPtr->logger->Log("Unhandled client exception", Logger::Level::Fatal); }
131 gPtr->Close(); window.StopCallbacksAndWindow(); return result;
132}
133INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
134{
135 __try { return RunClient(); }
136 __except (Logger::HandleNativeException(GetExceptionInformation()))
137 {
138 TerminateProcess(GetCurrentProcess(), GetExceptionCode()); return EXIT_FAILURE;
139 }
140}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
static int RunClient()
Definition CustomUI.cpp:24
static int RunMainLoopWithCrashDiagnostics(Rendering::Window11 &window)
Definition CustomUI.cpp:8
INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
Definition CustomUI.cpp:133
static LRESULT MsgProcWithCrashDiagnostics(HWND window, UINT message, WPARAM wParam, LPARAM lParam, bool *handled, void *context)
Definition CustomUI.cpp:16
#define designedHeightC
Definition GameClass.h:24
#define designedWidthC
Definition GameClass.h:23
bool videoVSync
Definition config.h:28
unsigned char videoSamples
Definition config.h:27
float scale
Definition config.h:29
static HRESULT CALLBACK OnResetDevice(Rendering::RenderDevice *pd3dDevice, const Rendering::SurfaceDescription *pBackBufferSurfaceDesc, void *pUserContext)
Config config
Definition GameClass.h:100
static void CALLBACK OnDestroyDevice(void *pUserContext)
HRESULT InitDirectInput(HWND hDlg) const
float dpiscale
Definition GameClass.h:130
static void CALLBACK KeyboardProc(UINT nChar, bool bKeyDown, bool bAltDown, void *pUserContext)
void Close()
static void CALLBACK OnLostDevice(void *pUserContext)
static void CALLBACK OnFrameMove(double fTime, float fElapsedTime, void *pUserContext)
static void CALLBACK OnFrameRender(Rendering::RenderDevice *pd3dDevice, double fTime, float fElapsedTime, void *pUserContext)
HRESULT OnFramePresented()
static LRESULT CALLBACK MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool *pbNoFurtherProcessing, void *pUserContext)
float displayscale
Definition GameClass.h:130
float displayWidth
Definition GameClass.h:131
std::unique_ptr< Logger > logger
Definition GameClass.h:109
static HRESULT CALLBACK OnCreateDevice(Rendering::RenderDevice *pd3dDevice, const Rendering::SurfaceDescription *pBackBufferSurfaceDesc, void *pUserContext)
float displayHeight
Definition GameClass.h:131
void Log(const char *msg, Level level=Info, int errorCode=0)
Definition Logger.cpp:283
static long __stdcall HandleNativeException(struct _EXCEPTION_POINTERS *exception)
Definition Logger.cpp:88
@ Debug
Definition Logger.h:27
@ Fatal
Definition Logger.h:29
@ Error
Definition Logger.h:28
DrawContext11 & Draws()
Definition Window11.h:52
void StopCallbacksAndWindow()
Definition Window11.cpp:220
HRESULT Create(const WindowOptions11 &options, Callbacks callbacks)
Definition Window11.cpp:24
HWND Handle() const
Definition Window11.h:50
renderer(Viewscreen *ptr)
Definition renderer.cpp:21
void AttachClientRuntime(Window11 *window, RenderDevice *device)
const SurfaceDescription * ClientBackBufferDescription()
constexpr auto Min(A first, B second)
Definition ClientMath.h:15
constexpr auto Max(A first, B second)
Definition ClientMath.h:16
std::function< HRESULT(UINT, UINT)> resize
Definition Window11.h:24
std::function< void()> destroy
Definition Window11.h:26
std::function< void(double, float)> move
Definition Window11.h:29
std::function< LRESULT(HWND, UINT, WPARAM, LPARAM, bool &)> message
Definition Window11.h:31
std::function< HRESULT()> presented
Definition Window11.h:28
std::function< void(const std::string &, HRESULT)> report
Definition Window11.h:33
std::function< HRESULT()> create
Definition Window11.h:23
std::function< void(UINT, bool, bool)> keyboard
Definition Window11.h:32
std::function< void()> releaseSizeDependent
Definition Window11.h:25
std::function< HRESULT(double, float)> draw
Definition Window11.h:30