Rise
The Vieneo Province
CustomUI.cpp
Go to the documentation of this file.
1 #ifdef _DEBUG
2 #define DEBUG_VS // Uncomment this line to debug vertex shaders
3 #define DEBUG_PS // Uncomment this line to debug pixel shaders
4 #endif
5 
6 #include <WinSock2.h>
7 #define _WINSOCKAPI_
8 #include <WS2tcpip.h>
9 #include <Windows.h>
10 
11 #include "GameClass.h"
12 
13 //--------------------------------------------------------------------------------------
14 // Entry point to the program. Initializes everything and goes into a message processing
15 // loop. Idle time is used to render the scene.
16 //--------------------------------------------------------------------------------------
17 INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
18 {
19  // Enable run-time memory check for debug builds.
20 #ifdef _DEBUG
21 #define _CRTDBG_MAPALLOC
22  //_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_CHECK_CRT_DF);
23  //_CrtSetBreakAlloc(486);
24 #endif
25 
26  GameClass game_class = GameClass();
27  GameClass* gPtr = &game_class;
28 
29  // Set the callback functions. These functions allow DXUT to notify
30  // the application about device changes, user input, and windows messages. The
31  // callbacks are optional so you need only set callbacks for events you're interested
32  // in. However, if you don't handle the device reset/lost callbacks then the sample
33  // framework won't be able to reset your device since the application must first
34  // release all device resources before resetting. Likewise, if you don't handle the
35  // device created/destroyed callbacks then DXUT won't be able to
36  // recreate your device resources.
37  DXUTSetCallbackD3D9DeviceCreated(GameClass::OnCreateDevice, gPtr);
38  DXUTSetCallbackD3D9DeviceReset(GameClass::OnResetDevice, gPtr);
39  DXUTSetCallbackD3D9DeviceLost(GameClass::OnLostDevice, gPtr);
40  DXUTSetCallbackD3D9DeviceDestroyed(GameClass::OnDestroyDevice, gPtr);
41  DXUTSetCallbackMsgProc(GameClass::MsgProc, gPtr);
42  DXUTSetCallbackKeyboard(GameClass::KeyboardProc, gPtr);
43  DXUTSetCallbackD3D9FrameRender(GameClass::OnFrameRender, gPtr);
44  DXUTSetCallbackFrameMove(GameClass::OnFrameMove, gPtr);
45  DXUTSetCallbackD3D9DeviceAcceptable(GameClass::IsDeviceAcceptable, gPtr);
46  DXUTSetCallbackDeviceChanging(GameClass::ModifyDeviceSettings, gPtr);
47 
48  // Show the cursor and clip it when in full screen
49  DXUTSetCursorSettings(true, true);
50  DXUTSetHotkeyHandling(false, false, false);
51  DXUTSetConstantFrameTime(false);
52 
53  // Initialize DXUT and create the desired Win32 window and Direct3D
54  // device for the application. Calling each of these functions is optional, but they
55  // allow you to set several options which control the behavior of the framework.
56  DXUTInit(false, false, nullptr, false, true); // Parse the command line, bShowMsgBoxOnError, and show msgboxes
57 
58  RECT desktop;
59  if (!GetWindowRect(GetDesktopWindow(), &desktop))
60  {
61  char msg[99];
62  sprintf_s(msg, 99, "Error in GetWindowRect: %d", GetLastError());
63  gPtr->logger->Log(msg, Logger::Fatal);
64  return 0;
65  }
66 
67  const HDC hDC = GetDC(nullptr); // NOLINT
68  const int nLogPixelsY = GetDeviceCaps(hDC, LOGPIXELSY);
69  ReleaseDC(nullptr, hDC);
70  gPtr->dpiscale = static_cast<float>(nLogPixelsY) / 96.0f; // 96 per inch was the original design, canary 2.5, Bill 1.8
71 
72  gPtr->displayWidth = static_cast<float>(desktop.right - desktop.left);
73  gPtr->displayHeight = static_cast<float>(desktop.bottom - desktop.top);
74  const float xScale = gPtr->displayWidth / designedWidthC; // canary 2.3, Bill 2.3
75  const float yScale = gPtr->displayHeight / designedHeightC; // canary 2.1, Bill 2.1
76  if (gPtr->dpiscale > xScale)
77  gPtr->dpiscale = xScale; // dpi now 2.3x
78  if (gPtr->dpiscale > yScale)
79  gPtr->dpiscale = yScale; // dpi now 2.1x
80  if (gPtr->config.scale == 0.0f) // none configured
81  {
82  // 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
83  gPtr->config.scale = max(1, gPtr->dpiscale);
84 
85  char msg[99];
86  sprintf_s(msg, 99, "config initial scale %.1f (dpiscale %.1f)", gPtr->config.scale, gPtr->dpiscale);
87  gPtr->logger->Log(msg, Logger::Level::Debug);
88  }
89  else if (gPtr->config.scale > min(xScale, yScale) * 1.5f) // make sure that previously saved scale doesn't exceed monitor width/height
90  {
91  char msg[99];
92  sprintf_s(msg, 99, "config adjust scale was %.1f xScale %.1f yScale %.1f (min %.1f): %.1f", gPtr->config.scale, xScale, yScale, min(xScale, yScale), min(xScale, yScale) * 1.5f);
93  gPtr->logger->Log(msg, Logger::Level::Debug);
94 
95  gPtr->config.scale = min(xScale, yScale) * 1.5f;
96  }
97  else if (gPtr->config.scale < max(1, 0.5f * gPtr->dpiscale))
98  {
99  char msg[99];
100  sprintf_s(msg, 99, "config adjust scale was %.1f xScale %.1f yScale %.1f (min %.1f): %.1f", gPtr->config.scale, xScale, yScale, min(xScale, yScale), max(1, 0.5f * gPtr->dpiscale));
101  gPtr->logger->Log(msg, Logger::Level::Debug);
102 
103  gPtr->config.scale = max(1, 0.5f * gPtr->dpiscale);
104  }
105  gPtr->displayscale = gPtr->config.scale;
106 
107  // 700 500 of 1080p looks good
108  // Both these use Y because we lock the aspect ratio at 700x500
109  if (FAILED(DXUTCreateDevice(true, static_cast<int>(700 * gPtr->displayscale), static_cast<int>(500 * gPtr->displayscale))))
110  {
111  gPtr->logger->Log("Could not DXUTCreateDevice() instance...", Logger::Level::Fatal);
112  return 0;
113  }
114 
115  if (!SetWindowText(DXUTGetHWND(), L"Rise: The Vieneo Province"))
116  {
117  char msg[99];
118  sprintf_s(msg, 99, "Error in SetWindowText: %d", GetLastError());
119  gPtr->logger->Log(msg, Logger::Fatal);
120  return 0;
121  }
122 
123  // Keyboard and force-feedback
124  if (FAILED(gPtr->InitDirectInput(DXUTGetHWND())))
125  {
126  gPtr->logger->Log("Could not InitDirectInput() instance...", Logger::Level::Fatal);
127  return 0;
128  }
129 
130  try
131  {
132  // Pass control to DXUT for handling the message pump and
133  // dispatching render calls. DXUT will call your FrameMove
134  // and FrameRender callback when there is idle time between handling window messages.
135  DXUTMainLoop();
136 
137  gPtr->logger->AddToCallStack("Exited DXUTMainLoop()");
138 
139  if (DXUTGetExitCode() != 0)
140  gPtr->logger->Log(DXUTGetErrorMessage(), Logger::Level::Fatal);
141  }
142  catch (std::exception& e)
143  {
144  char msg[199];
145  sprintf_s(msg, 199, "std::exception '%s'", e.what());
146  gPtr->logger->Log(msg, Logger::Level::Fatal);
147  }
148  catch (char* e)
149  {
150  char msg[199];
151  sprintf_s(msg, 199, "char* '%s'", e);
152  gPtr->logger->Log(msg, Logger::Level::Fatal);
153  }
154  catch (std::string& e)
155  {
156  char msg[199];
157  sprintf_s(msg, 199, "std::string '%s'", e.c_str());
158  gPtr->logger->Log(msg, Logger::Level::Fatal);
159  }
160  catch (...)
161  {
162  gPtr->logger->Log("Untyped exception occurred", Logger::Level::Fatal);
163  }
164 
165  // Perform any application-level cleanup here. Direct3D device resources are released within the
166  // appropriate callback functions and therefore don't require any cleanup code here.
167 
168  gPtr->Close();
169 
170  DXUTSetCallbackMsgProc(nullptr, nullptr);
171  DXUTSetCallbackD3D9DeviceCreated(nullptr, nullptr);
172  DXUTSetCallbackD3D9DeviceReset(nullptr, nullptr);
173  DXUTSetCallbackD3D9DeviceLost(nullptr, nullptr);
174  DXUTSetCallbackD3D9DeviceDestroyed(nullptr, nullptr);
175  DXUTSetCallbackMsgProc(nullptr, nullptr);
176  DXUTSetCallbackKeyboard(nullptr, nullptr);
177  DXUTSetCallbackD3D9FrameRender(nullptr, nullptr);
178  DXUTSetCallbackFrameMove(nullptr, nullptr);
179  DXUTSetCallbackD3D9DeviceAcceptable(nullptr, nullptr);
180  DXUTSetCallbackDeviceChanging(nullptr, nullptr);
181 
182 // gPtr->OnLostDevice(gPtr);
183  gPtr->OnDestroyDevice(gPtr);
184 
185  return DXUTGetExitCode();
186 }
static void CALLBACK OnLostDevice(void *pUserContext)
Definition: GameClass.cpp:2544
#define designedHeightC
Definition: GameClass.h:23
static void CALLBACK KeyboardProc(UINT nChar, bool bKeyDown, bool bAltDown, void *pUserContext)
Definition: GameClass.cpp:2287
static void CALLBACK OnDestroyDevice(void *pUserContext)
Definition: GameClass.cpp:2603
Config config
Definition: GameClass.h:102
#define designedWidthC
Definition: GameClass.h:22
static LRESULT CALLBACK MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool *pbNoFurtherProcessing, void *pUserContext)
Definition: GameClass.cpp:1694
static HRESULT CALLBACK OnResetDevice(IDirect3DDevice9 *pd3dDevice, const D3DSURFACE_DESC *pBackBufferSurfaceDesc, void *pUserContext)
Definition: GameClass.cpp:654
float displayHeight
Definition: GameClass.h:129
static void CALLBACK OnFrameRender(IDirect3DDevice9 *pd3dDevice, double fTime, float fElapsedTime, void *pUserContext)
Definition: GameClass.cpp:1495
float displayWidth
Definition: GameClass.h:129
static HRESULT CALLBACK OnCreateDevice(IDirect3DDevice9 *pd3dDevice, const D3DSURFACE_DESC *pBackBufferSurfaceDesc, void *pUserContext)
Definition: GameClass.cpp:610
Logger * logger
Definition: GameClass.h:113
INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
Definition: CustomUI.cpp:17
static bool CALLBACK IsDeviceAcceptable(D3DCAPS9 *pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed, void *pUserContext)
Definition: GameClass.cpp:505
void Close()
Definition: GameClass.cpp:72
HRESULT InitDirectInput(HWND hDlg) const
Definition: GameClass.cpp:318
void Log(const char *msg, Level level=Info, int errorCode=0)
Definition: Logger.cpp:11
float dpiscale
Definition: GameClass.h:128
static void CALLBACK OnFrameMove(double fTime, float fElapsedTime, void *pUserContext)
Definition: GameClass.cpp:771
float scale
Definition: config.h:24
float displayscale
Definition: GameClass.h:128
void AddToCallStack(const char *msg)
Definition: Logger.cpp:86
static bool CALLBACK ModifyDeviceSettings(DXUTDeviceSettings *pDeviceSettings, void *pUserContext)
Definition: GameClass.cpp:516