diff --git a/src/test/directx9_test.cpp b/src/test/directx9_test.cpp index 69116fd..bc4af55 100644 --- a/src/test/directx9_test.cpp +++ b/src/test/directx9_test.cpp @@ -59,9 +59,6 @@ #include "http_lib_test.h" // -// Switch to using helpers.cpp for helper functions. -//#define _TEST2 - // TODO Create src folder for project, move source files into it // TODO Possibly create headers folder for project. @@ -77,23 +74,13 @@ #endif // Data -// TODO Move into helpers.cpp or helpers.h -#ifndef _TEST2 //!_TEST2 -// TODO Make these public, add them into the header. -// That should fix this to work in the helpers.cpp -static LPDIRECT3D9 g_pD3D = nullptr; -static LPDIRECT3DDEVICE9 g_pd3dDevice = nullptr; -static UINT g_ResizeWidth = 0, g_ResizeHeight = 0; -static D3DPRESENT_PARAMETERS g_d3dpp = {}; -#endif //_!_TEST2 +// TODO Test these public variables in helpers.cpp, I should be able to call this stuff like this. +LPDIRECT3D9 DirectX9Test::g_pD3D = nullptr; +LPDIRECT3DDEVICE9 DirectX9Test::g_pd3dDevice = nullptr; +UINT DirectX9Test::g_ResizeWidth = 0, DirectX9Test::g_ResizeHeight = 0; +D3DPRESENT_PARAMETERS DirectX9Test::g_d3dpp = {}; -// Forward declarations of helper functions -#ifndef _TEST2 //!_TEST2 -bool CreateDeviceD3D(HWND hWnd); -void CleanupDeviceD3D(); -void ResetDevice(); -#endif //!_TEST2 LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); static void HelpMarker(const char* desc) @@ -183,23 +170,12 @@ static void CreateApplicationWindow() static void InitializeD3D(HWND hwnd, WNDCLASSEXW wc) { // Initialize Direct3D - -#ifndef _TEST2 //!_TEST2 - if (!CreateDeviceD3D(hwnd)) -#else if (!Helpers::CreateDeviceD3D(hwnd)) -#endif //!_TEST2 { -#ifndef _TEST2 //!_TEST2 - CleanupDeviceD3D(); -#else Helpers::CleanupDeviceD3D(); -#endif //!_TEST2 ::UnregisterClassW(wc.lpszClassName, wc.hInstance); //return 1; } - - } static void ShowWindow(HWND hwnd) @@ -211,46 +187,24 @@ static void ShowWindow(HWND hwnd) static void Render(ImVec4 clear_color) { -#ifndef _TEST2 //!_TEST2 - g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE); - g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); - g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); + DirectX9Test::g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE); + DirectX9Test::g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); + DirectX9Test::g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_color.x * clear_color.w * 255.0f), (int)(clear_color.y * clear_color.w * 255.0f), (int)(clear_color.z * clear_color.w * 255.0f), (int)(clear_color.w * 255.0f)); - g_pd3dDevice->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0); - if (g_pd3dDevice->BeginScene() >= 0) + DirectX9Test::g_pd3dDevice->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0); + if (DirectX9Test::g_pd3dDevice->BeginScene() >= 0) { ImGui::Render(); ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); - g_pd3dDevice->EndScene(); + DirectX9Test::g_pd3dDevice->EndScene(); } - HRESULT result = g_pd3dDevice->Present(nullptr, nullptr, nullptr, nullptr); + HRESULT result = DirectX9Test::g_pd3dDevice->Present(nullptr, nullptr, nullptr, nullptr); // Handle loss of D3D9 device - if (result == D3DERR_DEVICELOST && g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET) + if (result == D3DERR_DEVICELOST && DirectX9Test::g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET) -#else - Helpers::g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE); - Helpers::g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); - Helpers::g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); - D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_color.x * clear_color.w * 255.0f), (int)(clear_color.y * clear_color.w * 255.0f), (int)(clear_color.z * clear_color.w * 255.0f), (int)(clear_color.w * 255.0f)); - Helpers::g_pd3dDevice->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0); - if (Helpers::g_pd3dDevice->BeginScene() >= 0) - { - ImGui::Render(); - ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); - Helpers::g_pd3dDevice->EndScene(); - } - HRESULT result = Helpers::g_pd3dDevice->Present(nullptr, nullptr, nullptr, nullptr); - - // Handle loss of D3D9 device - if (result == D3DERR_DEVICELOST && Helpers::g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET) -#endif //!_TEST2 - -#ifndef _TEST2 //!_TEST2 - ResetDevice(); -#else + // Moved into helpers.cpp Helpers::ResetDevice(); -#endif //_TEST2 } void setupContext() @@ -322,11 +276,9 @@ void DirectX9Test::directX9Test() // Setup Platform/Renderer backends ImGui_ImplWin32_Init(hwnd); -#ifndef _TEST2 ImGui_ImplDX9_Init(g_pd3dDevice); -#else - ImGui_ImplDX9_Init(Helpers::g_pd3dDevice); -#endif //!_TEST2 + +#endif //!_TEST // Our state @@ -359,26 +311,17 @@ void DirectX9Test::directX9Test() if (done) break; -#ifndef _TEST2 // Handle window resize (we don't resize directly in the WM_SIZE handler) + // TODO Move this into helpers.cpp if needed. if (g_ResizeWidth != 0 && g_ResizeHeight != 0) { g_d3dpp.BackBufferWidth = g_ResizeWidth; g_d3dpp.BackBufferHeight = g_ResizeHeight; g_ResizeWidth = g_ResizeHeight = 0; -#else - if (Helpers::g_ResizeWidth != 0 && Helpers::g_ResizeHeight != 0) - { - Helpers::g_d3dpp.BackBufferWidth = Helpers::g_ResizeWidth; - Helpers::g_d3dpp.BackBufferHeight = Helpers::g_ResizeHeight; - Helpers::g_ResizeWidth = Helpers::g_ResizeHeight = 0; -#endif //!_TEST2 -#ifndef _TEST2 - ResetDevice(); -#else + + // Moved into helpers.cpp Helpers::ResetDevice(); -#endif //!_TEST2 } // Start the Dear ImGui frame @@ -403,8 +346,6 @@ void DirectX9Test::directX9Test() // Show the main menu MainMenu::MainMenuTest(); - - // Text file functions test menu if (ImGui::CollapsingHeader("Text File Functions")) { @@ -464,11 +405,8 @@ void DirectX9Test::directX9Test() // Cleanup D3D Device -#ifndef _TEST2 - CleanupDeviceD3D(); -#else + // Moved into helpers.cpp Helpers::CleanupDeviceD3D(); -#endif ::DestroyWindow(hwnd); ::UnregisterClassW(wc.lpszClassName, wc.hInstance); @@ -476,46 +414,6 @@ void DirectX9Test::directX9Test() // End Main class -// Helper functions - -#ifndef _TEST2 -bool CreateDeviceD3D(HWND hWnd) -{ - if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == nullptr) - return false; - - // Create the D3DDevice - ZeroMemory(&g_d3dpp, sizeof(g_d3dpp)); - g_d3dpp.Windowed = TRUE; - g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; - g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Need to use an explicit format with alpha if needing per-pixel alpha composition. - g_d3dpp.EnableAutoDepthStencil = TRUE; - g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16; - g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // Present with vsync - //g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync, maximum unthrottled framerate - if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0) - return false; - - return true; -} - -void CleanupDeviceD3D() -{ - if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; } - if (g_pD3D) { g_pD3D->Release(); g_pD3D = nullptr; } -} - -void ResetDevice() -{ - ImGui_ImplDX9_InvalidateDeviceObjects(); - HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp); - if (hr == D3DERR_INVALIDCALL) - IM_ASSERT(0); - ImGui_ImplDX9_CreateDeviceObjects(); -} - -#endif //!_TEST2 - // Forward declare message handler from imgui_impl_win32.cpp extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); @@ -534,14 +432,12 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) case WM_SIZE: if (wParam == SIZE_MINIMIZED) return 0; -#ifndef _TEST2 - g_ResizeWidth = (UINT)LOWORD(lParam); // Queue resize - g_ResizeHeight = (UINT)HIWORD(lParam); -#else - Helpers::g_ResizeWidth = (UINT)LOWORD(lParam); // Queue resize - Helpers::g_ResizeHeight = (UINT)HIWORD(lParam); - -#endif //!_TEST2 + //g_ResizeWidth = (UINT)LOWORD(lParam); // Queue resize + //g_ResizeHeight = (UINT)HIWORD(lParam); + DirectX9Test::g_ResizeWidth = (UINT)LOWORD(lParam); // Queue resize + DirectX9Test::g_ResizeHeight = (UINT)HIWORD(lParam); + + return 0; case WM_SYSCOMMAND: if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu @@ -552,7 +448,6 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) return 0; } return ::DefWindowProcW(hWnd, msg, wParam, lParam); -} -#endif //_TEST -#endif //_DIRECTX9 \ No newline at end of file +} +#endif //!_DIRECTX9 diff --git a/src/test/directx9_test.h b/src/test/directx9_test.h index 077c205..1f6e320 100644 --- a/src/test/directx9_test.h +++ b/src/test/directx9_test.h @@ -1,4 +1,13 @@ #pragma once +#ifdef _WIN32 +#include + +#ifdef _DIRECTX9 +#include +#endif + +#endif + class DirectX9Test { public: static void directX9Test(); @@ -10,4 +19,15 @@ public: static bool show_demo_window; static bool define_test; static bool toggle_text; + + // Data + +// TODO Make these public, add them into the header. +// That should fix this to work in the helpers.cpp + static LPDIRECT3D9 g_pD3D; + // Oh, having these undefined in the directx9_test.cpp + // causes unresolved external symbol errors. + static LPDIRECT3DDEVICE9 g_pd3dDevice; + static UINT g_ResizeWidth, g_ResizeHeight; + static D3DPRESENT_PARAMETERS g_d3dpp; }; \ No newline at end of file diff --git a/src/util/helpers.cpp b/src/util/helpers.cpp index 0a1b45d..40abc46 100644 --- a/src/util/helpers.cpp +++ b/src/util/helpers.cpp @@ -1,38 +1,36 @@ #include "helpers.h" -#include #include "imgui.h" +#ifdef _DIRECTX9 +#include + + #include "imgui_impl_dx9.h" - -#ifdef _TEST2 -// Data -static LPDIRECT3D9 g_pD3D = nullptr; -static LPDIRECT3DDEVICE9 g_pd3dDevice = nullptr; -static UINT g_ResizeWidth = 0, g_ResizeHeight = 0; -static D3DPRESENT_PARAMETERS g_d3dpp = {}; - -// TODO Figure out if I can move these DirectX helpers into here. -// If I define _TEST2 into directx9_test.cpp it'll switch everything to using this file -// It crashes because g_d3dpp is a nullptr though +#include "directx9_test.h" +//-------------- DirectX9 Functions --------/ + bool Helpers::CreateDeviceD3D(HWND hWnd) { - if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == nullptr) + if ((DirectX9Test::g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == nullptr) return false; // Create the D3DDevice - ZeroMemory(&g_d3dpp, sizeof(g_d3dpp)); - g_d3dpp.Windowed = TRUE; - g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; - g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Need to use an explicit format with alpha if needing per-pixel alpha composition. - g_d3dpp.EnableAutoDepthStencil = TRUE; - g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16; - g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // Present with vsync - //g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync, maximum unthrottled framerate - if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0) + ZeroMemory(&DirectX9Test::g_d3dpp, sizeof(DirectX9Test::g_d3dpp)); + DirectX9Test::g_d3dpp.Windowed = TRUE; + DirectX9Test::g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; + DirectX9Test::g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Need to use an explicit format with alpha if needing per-pixel alpha composition. + DirectX9Test::g_d3dpp.EnableAutoDepthStencil = TRUE; + DirectX9Test::g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16; + DirectX9Test::g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; + + + + //DirectX9Test::g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync, maximum unthrottled framerate + if (DirectX9Test::g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &DirectX9Test::g_d3dpp, &DirectX9Test::g_pd3dDevice) < 0) return false; return true; @@ -40,17 +38,25 @@ bool Helpers::CreateDeviceD3D(HWND hWnd) void Helpers::CleanupDeviceD3D() { - if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; } - if (g_pD3D) { g_pD3D->Release(); g_pD3D = nullptr; } + if (DirectX9Test::g_pd3dDevice) + { + DirectX9Test::g_pd3dDevice->Release(); + DirectX9Test::g_pd3dDevice = nullptr; + } + if (DirectX9Test::g_pD3D) + { + DirectX9Test::g_pD3D->Release(); + DirectX9Test::g_pD3D = nullptr; + } } void Helpers::ResetDevice() { ImGui_ImplDX9_InvalidateDeviceObjects(); - HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp); + HRESULT hr = DirectX9Test::g_pd3dDevice->Reset(&DirectX9Test::g_d3dpp); if (hr == D3DERR_INVALIDCALL) IM_ASSERT(0); ImGui_ImplDX9_CreateDeviceObjects(); } -#endif //_TEST2 \ No newline at end of file +#endif //_DIRECTX9 \ No newline at end of file diff --git a/src/util/helpers.h b/src/util/helpers.h index d9b9b70..862a621 100644 --- a/src/util/helpers.h +++ b/src/util/helpers.h @@ -1,34 +1,33 @@ #pragma once #ifdef _WIN32 #include + +#ifdef _DIRECTX9 +#include +#endif + +#include "defines.h" + #endif //_WIN32 // Oops I named the source file with a .c extension instead of .cpp // No wonder this didn't work. // https://stackoverflow.com/questions/12524168/resolving-found-at-file-scope-missing-function-header-in-vs2010-c -#ifdef _TEST2 +#ifdef _DIRECTX9 class Helpers { public: -#ifdef _WIN32 + // Never really used this preprocessor format before, but these should only be used in DirectX9 and on Windows. + // https://stackoverflow.com/questions/17237545/preprocessor-check-if-multiple-defines-are-not-defined +#if defined(_WIN32) && defined(_DIRECTX9) static bool CreateDeviceD3D(HWND hWnd); static void CleanupDeviceD3D(); static void ResetDevice(); - -#ifdef _TEST2 - static LPDIRECT3D9 g_pD3D; - static LPDIRECT3DDEVICE9 g_pd3dDevice; - static UINT g_ResizeWidth; - static UINT g_ResizeHeight; - static D3DPRESENT_PARAMETERS g_d3dpp; - -#endif //_TEST2 - #else // Will this work on Linux or Mac? I don't think DirectX does. - static bool CreateDeviceD3D(); + //static bool CreateDeviceD3D(); #endif }; -#endif //_TEST2 +#endif //_DIRECTX9