diff --git a/src/Client/Block.c b/src/Client/Block.c index 23b1100b2..262137463 100644 --- a/src/Client/Block.c +++ b/src/Client/Block.c @@ -10,9 +10,9 @@ void Block_Reset(void) { } void Block_Init(void) { - #define DefinedCustomBlocks_Len (Block_Count >> 5) + Int32 count = sizeof(DefinedCustomBlocks) / sizeof(DefinedCustomBlocks[0]); Int32 i; - for (i = 0; i < DefinedCustomBlocks_Len; i++) { + for (i = 0; i < count; i++) { DefinedCustomBlocks[i] = 0; } diff --git a/src/Client/D3D9Api.c b/src/Client/D3D9Api.c index 72dbf5ab8..2a3852ae1 100644 --- a/src/Client/D3D9Api.c +++ b/src/Client/D3D9Api.c @@ -3,6 +3,7 @@ #include "ErrorHandler.h" #include "GraphicsEnums.h" #include "Platform.h" +#include "Window.h" #ifdef USE_DX @@ -10,7 +11,8 @@ IDirect3D9* d3d; IDirect3DDevice9* device; MatrixStack* curStack; MatrixStack viewStack, projStack, texStack; - +DWORD createFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING; +D3DFORMAT viewFormat, depthFormat; #define D3D9_SetRenderState(raw, state, name) \ ReturnCode hresult = IDirect3DDevice9_SetRenderState(device, state, raw); \ @@ -45,10 +47,34 @@ Int32 d3d9_texturesCapacity = d3d9_texturesExpSize; void Gfx_Init(void) { - /* TODO: EVERYTHING ELSE */ + Gfx_MinZNear = 0.05f; + void* winHandle = Window_GetWindowHandle(); + d3d = Direct3DCreate9(D3D_SDK_VERSION); + + D3D9_FindCompatibleFormat(); + D3DPRESENT_PARAMETERS args; + D3D9_GetPresentArgs(640, 480, &args); + ReturnCode res; + + /* Try to create a device with as much hardware usage as possible. */ + res = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winHandle, createFlags, &args, &device); + if (!ErrorHandler_Check(res)) { + createFlags = D3DCREATE_MIXED_VERTEXPROCESSING; + res = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winHandle, createFlags, &args, &device); + } + if (!ErrorHandler_Check(res)) { + createFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING; + res = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winHandle, createFlags, &args, &device); + } + if (!ErrorHandler_Check(res)) { + ErrorHandler_FailWithCode(res, "Failed to create Direct3D9 device"); + } + viewStack.Type = D3DTS_VIEW; projStack.Type = D3DTS_PROJECTION; texStack.Type = D3DTS_TEXTURE0; + SetDefaultRenderStates(); + InitDynamicBuffers(); } void Gfx_Free(void) { @@ -88,6 +114,42 @@ void Gfx_Free(void) { } } +void D3D9_FindCompatibleFormat(void) { + Int32 count = sizeof(d3d9_viewFormats) / sizeof(d3d9_viewFormats[0]); + Int32 i; + for (i = 0; i < count; i++) { + viewFormat = d3d9_viewFormats[i]; + if (IDirect3D9_CheckDeviceType(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, viewFormat, viewFormat, true)) break; + + if (i == count - 1) { + ErrorHandler_Fail("Unable to create a back buffer with sufficient precision."); + } + } + + count = sizeof(d3d9_depthFormats) / sizeof(d3d9_depthFormats[0]); + for (i = 0; i < count; i++) { + depthFormat = d3d9_depthFormats[i]; + if (IDirect3D9_CheckDepthStencilMatch(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, viewFormat, viewFormat, depthFormat)) break; + + if (i == count - 1) { + ErrorHandler_Fail("Unable to create a depth buffer with sufficient precision."); + } + } +} + +D3D9_GetPresentArgs(Int32 width, Int32 height, D3DPRESENT_PARAMETERS* args) { + Platform_MemSet(args, 0, sizeof(D3DPRESENT_PARAMETERS)); + args->AutoDepthStencilFormat = depthFormat; + args->BackBufferWidth = width; + args->BackBufferHeight = height; + args->BackBufferFormat = viewFormat; + args->BackBufferCount = 1; + args->EnableAutoDepthStencil = true; + args->PresentationInterval = vsync ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE; + args->SwapEffect = D3DSWAPEFFECT_DISCARD; + args->Windowed = true; + return args; +} GfxResourceID Gfx_CreateTexture(Bitmap* bmp, bool managedPool) { diff --git a/src/Client/D3D9Api.h b/src/Client/D3D9Api.h index 9648dddd2..108bf3079 100644 --- a/src/Client/D3D9Api.h +++ b/src/Client/D3D9Api.h @@ -32,6 +32,9 @@ D3DCMPFUNC d3d9_compareFuncs[8] = { D3DCMP_ALWAYS, D3DCMP_NOTEQUAL, D3DCMP_NEVER D3DFOGMODE d3d9_modes[3] = { D3DFOG_LINEAR, D3DFOG_EXP, D3DFOG_EXP2 }; Int32 d3d9_formatMappings[2] = { D3DFVF_XYZ | D3DFVF_DIFFUSE, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX2 }; +static void D3D9_FindCompatibleFormat(void); + +static D3D9_GetPresentArgs(Int32 width, Int32 height, D3DPRESENT_PARAMETERS* args); static void D3D9_SetTextureData(IDirect3DTexture9* texture, Bitmap* bmp); diff --git a/src/Client/Program.c b/src/Client/Program.c index ff3bf9d2f..452f68548 100644 --- a/src/Client/Program.c +++ b/src/Client/Program.c @@ -2,6 +2,7 @@ #include "ErrorHandler.h" #include "Platform.h" #include "Window.h" +#include "GraphicsAPI.h" int main(int argc, char* argv[]) { ErrorHandler_Init(); @@ -10,6 +11,7 @@ int main(int argc, char* argv[]) { String str = String_FromConstant("TEST"); Window_Create(320, 320, 320, 320, &str, NULL); Window_SetVisible(true); + Gfx_Init(); while (true) { Window_ProcessEvents(); Platform_ThreadSleep(100); diff --git a/src/Client/WinWindow.c b/src/Client/WinWindow.c index 5b3506ea2..2763f5a82 100644 --- a/src/Client/WinWindow.c +++ b/src/Client/WinWindow.c @@ -570,6 +570,7 @@ API.SendMessage(window.handle, WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero bool Window_GetFocused(void) { return win_Focused; } bool Window_GetExists(void) { return win_Exists; } +void* Window_GetWindowHandle(void) { return win_Handle; } bool Window_GetVisible(void) { return IsWindowVisible(win_Handle); } void Window_SetVisible(bool visible) { diff --git a/src/Client/Window.h b/src/Client/Window.h index 3fb8ec10c..7f79535c4 100644 --- a/src/Client/Window.h +++ b/src/Client/Window.h @@ -69,10 +69,8 @@ void Window_SetVisible(bool visible); /* Gets whether this window has been created and has not been destroyed. */ bool Window_GetExists(void); -/* TODO: IMPLEMENT THIS -/// Gets the for this window. -IWindowInfo WindowInfo{ get; } -*/ +/* TODO: IMPLEMENT THIS */ +void* Window_GetWindowHandle(void); /* Gets the WindowState of this window. */ WindowState Window_GetWindowState(void);