From aec759153f74f40cd2d9f8ba6a4f29a276ea5d57 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 6 Jun 2020 10:28:02 +1000 Subject: [PATCH] Split up D3D9 device creation from Gfx_Init --- src/Game.c | 2 +- src/Graphics.c | 27 +++++++++++++++++---------- src/Program.c | 4 ++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Game.c b/src/Game.c index c38294047..15a34d1de 100644 --- a/src/Game.c +++ b/src/Game.c @@ -389,8 +389,8 @@ static void Game_Load(void) { Game_UserViewDistance = 512; Game_Fov = 70; - Gfx_Init(); Game_UpdateDimensions(); + Gfx_Init(); LoadOptions(); Event_RegisterVoid(&WorldEvents.NewMap, NULL, HandleOnNewMap); diff --git a/src/Graphics.c b/src/Graphics.c index 9cf0910b6..1f131f9d4 100644 --- a/src/Graphics.c +++ b/src/Graphics.c @@ -365,21 +365,19 @@ static void D3D9_FillPresentArgs(int width, int height, D3DPRESENT_PARAMETERS* a args->Windowed = true; } -void Gfx_Init(void) { +static void TryCreateDevice(void) { cc_result res; D3DCAPS9 caps; - - Gfx.MinZNear = 0.05f; HWND winHandle = (HWND)WindowInfo.Handle; - d3d = Direct3DCreate9(D3D_SDK_VERSION); - - FindCompatibleViewFormat(); - FindCompatibleDepthFormat(); D3DPRESENT_PARAMETERS args = { 0 }; D3D9_FillPresentArgs(640, 480, &args); /* Try to create a device with as much hardware usage as possible. */ res = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winHandle, createFlags, &args, &device); + /* Another running fullscreen application might prevent creating device */ + /*if (res == D3DERR_DEVICELOST) { Gfx.LostContext = true; return; } */ + + /* Fallback with using CPU for some parts of rendering */ if (res) { createFlags = D3DCREATE_MIXED_VERTEXPROCESSING; res = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winHandle, createFlags, &args, &device); @@ -388,20 +386,29 @@ void Gfx_Init(void) { createFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING; res = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winHandle, createFlags, &args, &device); } - if (res) Logger_Abort2(res, "Creating Direct3D9 device"); + if (res) Logger_Abort2(res, "Creating Direct3D9 device"); res = IDirect3DDevice9_GetDeviceCaps(device, &caps); if (res) Logger_Abort2(res, "Getting Direct3D9 capabilities"); Gfx.MaxTexWidth = caps.MaxTextureWidth; Gfx.MaxTexHeight = caps.MaxTextureHeight; - Gfx.CustomMipmapsLevels = true; - CommonInit(); Gfx_RestoreState(); totalMem = IDirect3DDevice9_GetAvailableTextureMem(device) / (1024.0f * 1024.0f); } +void Gfx_Init(void) { + d3d = Direct3DCreate9(D3D_SDK_VERSION); + FindCompatibleViewFormat(); + FindCompatibleDepthFormat(); + + Gfx.MinZNear = 0.05f; + Gfx.CustomMipmapsLevels = true; + CommonInit(); + TryCreateDevice(); +} + cc_bool Gfx_TryRestoreContext(void) { D3DPRESENT_PARAMETERS args = { 0 }; cc_result res; diff --git a/src/Program.c b/src/Program.c index 10c8273e9..9b0a199b8 100644 --- a/src/Program.c +++ b/src/Program.c @@ -108,8 +108,8 @@ static int Program_Run(int argc, char** argv) { #ifdef _MSC_VER /* NOTE: Make sure to comment this out before pushing a commit */ //String rawArgs = String_FromConst("UnknownShadow200 fffff 127.0.0.1 25565"); - //String rawArgs = String_FromConst("UnknownShadow200"); - //argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4); + String rawArgs = String_FromConst("UnknownShadow200"); + argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4); #endif if (argsCount == 0) {