From bc0e90dbec798071e8f8c401f5952fb5a94ef3ef Mon Sep 17 00:00:00 2001 From: IoIxD Date: Thu, 17 Sep 2026 22:16:16 -0700 Subject: [PATCH] error handling for d3d8 and d3d9 --- src/widget/d3d8.c | 32 +++++++++++++++++++++++++++----- src/widget/d3d9.c | 34 ++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/widget/d3d8.c b/src/widget/d3d8.c index 0663321..d569f53 100644 --- a/src/widget/d3d8.c +++ b/src/widget/d3d8.c @@ -17,21 +17,33 @@ static int wcreate_d3d8(MwWidget handle) { gdid3d8_t* o = malloc(sizeof(gdid3d8_t)); D3DPRESENT_PARAMETERS d3dpp; D3DDISPLAYMODE dispMode; + HRESULT hr; + char errbuf[2048] = {0}; o->d3d8dll = LoadLibrary("d3d8.dll"); if(!o->d3d8dll) { - MessageBox(NULL, "d3d8.dll not found! Is DirectX8 installed?", "Error", MB_OK); - ExitProcess(-1); + MwDispatchError(1, "d3d8.dll not found! Is DirectX8 installed?"); + return 1; } o->Direct3DCreate8 = (void*)GetProcAddress(o->d3d8dll, "Direct3DCreate8"); if(!o->Direct3DCreate8) { - MessageBox(NULL, "Direct3DCreate8 not found! Is DirectX8 installed properly?", "Error", MB_OK); - ExitProcess(-1); + MwDispatchError(1, "Direct3DCreate8 not found! Is DirectX8 installed properly?"); + return 1; } o->d3d = o->Direct3DCreate8(D3D_SDK_VERSION); + if(!o->Direct3DCreate8) { + MwStringPrintIntoBuffer(errbuf, sizeof(errbuf) - 1, "Direct3DCreate8 NULL: %s", hr); + MwDispatchError(1, errbuf); + return 1; + } - o->d3d->lpVtbl->GetAdapterDisplayMode(o->d3d, D3DADAPTER_DEFAULT, &dispMode); + hr = o->d3d->lpVtbl->GetAdapterDisplayMode(o->d3d, D3DADAPTER_DEFAULT, &dispMode); + if(hr != D3D_OK) { + MwStringPrintIntoBuffer(errbuf, sizeof(errbuf) - 1, "GetAdapterDisplayMode ERROR: %s", hr); + MwDispatchError(1, errbuf); + return 1; + } ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = TRUE; @@ -49,8 +61,18 @@ static int wcreate_d3d8(MwWidget handle) { D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &o->d3ddev); + if(hr != D3D_OK) { + MwStringPrintIntoBuffer(errbuf, sizeof(errbuf) - 1, "CreateDevice ERROR: %s", hr); + MwDispatchError(1, errbuf); + return 1; + } o->d3ddev->lpVtbl->SetRenderState(o->d3ddev, D3DRS_LIGHTING, FALSE); + if(hr != D3D_OK) { + MwStringPrintIntoBuffer(errbuf, sizeof(errbuf) - 1, "SetRenderState ERROR: %s", hr); + MwDispatchError(1, errbuf); + return 1; + } handle->internal = o; diff --git a/src/widget/d3d9.c b/src/widget/d3d9.c index 3246caf..7219e58 100644 --- a/src/widget/d3d9.c +++ b/src/widget/d3d9.c @@ -12,9 +12,12 @@ typedef struct gdid3d9 { } gdid3d9_t; static int wcreate_d3d9(MwWidget handle) { - void* r = NULL; - MwWidget w = handle; - gdid3d9_t* o = malloc(sizeof(gdid3d9_t)); + void* r = NULL; + MwWidget w = handle; + gdid3d9_t* o = malloc(sizeof(gdid3d9_t)); + HRESULT hr; + char errbuf[2048]; + D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information o->d3d9dll = LoadLibrary("d3d9.dll"); if(!o->d3d9dll) { @@ -24,8 +27,10 @@ static int wcreate_d3d9(MwWidget handle) { o->Direct3DCreate9 = (void*)GetProcAddress(o->d3d9dll, "Direct3DCreate9"); o->d3d = o->Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface - - D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information + if(hr != D3D_OK) { + MwDispatchError(1, "Direct3DCreate9 NULL"); + return 1; + } ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use d3dpp.Windowed = TRUE; // program windowed, not fullscreen @@ -33,13 +38,18 @@ static int wcreate_d3d9(MwWidget handle) { d3dpp.hDeviceWindow = handle->lowlevel->gdi.hWnd; // set the window to be used by Direct3D // create a device class using this information and information from the d3dpp stuct - o->d3d->lpVtbl->CreateDevice(o->d3d, - D3DADAPTER_DEFAULT, - D3DDEVTYPE_HAL, - handle->lowlevel->gdi.hWnd, - D3DCREATE_MIXED_VERTEXPROCESSING, - &d3dpp, - &o->d3ddev); + hr = o->d3d->lpVtbl->CreateDevice(o->d3d, + D3DADAPTER_DEFAULT, + D3DDEVTYPE_HAL, + handle->lowlevel->gdi.hWnd, + D3DCREATE_MIXED_VERTEXPROCESSING, + &d3dpp, + &o->d3ddev); + if(hr != D3D_OK) { + MwStringPrintIntoBuffer(errbuf, sizeof(errbuf) - 1, "CreateDevice ERROR: %s", hr); + MwDispatchError(1, errbuf); + return 1; + } handle->internal = o;