Move some functions into helpers.cpp, move directx9 variables into header.

Cleanup directx9_test.cpp a bit, remove _TEST2 preprocessor.
This commit is contained in:
kelson8 2025-03-13 02:35:21 -04:00
parent ee288b0540
commit a6015f1e21
4 changed files with 93 additions and 173 deletions

View File

@ -59,9 +59,6 @@
#include "http_lib_test.h" #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 Create src folder for project, move source files into it
// TODO Possibly create headers folder for project. // TODO Possibly create headers folder for project.
@ -77,23 +74,13 @@
#endif #endif
// Data // Data
// TODO Move into helpers.cpp or helpers.h
#ifndef _TEST2 //!_TEST2
// TODO Make these public, add them into the header. // TODO Test these public variables in helpers.cpp, I should be able to call this stuff like this.
// That should fix this to work in the helpers.cpp LPDIRECT3D9 DirectX9Test::g_pD3D = nullptr;
static LPDIRECT3D9 g_pD3D = nullptr; LPDIRECT3DDEVICE9 DirectX9Test::g_pd3dDevice = nullptr;
static LPDIRECT3DDEVICE9 g_pd3dDevice = nullptr; UINT DirectX9Test::g_ResizeWidth = 0, DirectX9Test::g_ResizeHeight = 0;
static UINT g_ResizeWidth = 0, g_ResizeHeight = 0; D3DPRESENT_PARAMETERS DirectX9Test::g_d3dpp = {};
static D3DPRESENT_PARAMETERS g_d3dpp = {};
#endif //_!_TEST2
// 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); LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
static void HelpMarker(const char* desc) static void HelpMarker(const char* desc)
@ -183,23 +170,12 @@ static void CreateApplicationWindow()
static void InitializeD3D(HWND hwnd, WNDCLASSEXW wc) static void InitializeD3D(HWND hwnd, WNDCLASSEXW wc)
{ {
// Initialize Direct3D // Initialize Direct3D
#ifndef _TEST2 //!_TEST2
if (!CreateDeviceD3D(hwnd))
#else
if (!Helpers::CreateDeviceD3D(hwnd)) if (!Helpers::CreateDeviceD3D(hwnd))
#endif //!_TEST2
{ {
#ifndef _TEST2 //!_TEST2
CleanupDeviceD3D();
#else
Helpers::CleanupDeviceD3D(); Helpers::CleanupDeviceD3D();
#endif //!_TEST2
::UnregisterClassW(wc.lpszClassName, wc.hInstance); ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
//return 1; //return 1;
} }
} }
static void ShowWindow(HWND hwnd) static void ShowWindow(HWND hwnd)
@ -211,46 +187,24 @@ static void ShowWindow(HWND hwnd)
static void Render(ImVec4 clear_color) static void Render(ImVec4 clear_color)
{ {
#ifndef _TEST2 //!_TEST2 DirectX9Test::g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE); DirectX9Test::g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); DirectX9Test::g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
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)); 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); DirectX9Test::g_pd3dDevice->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0);
if (g_pd3dDevice->BeginScene() >= 0) if (DirectX9Test::g_pd3dDevice->BeginScene() >= 0)
{ {
ImGui::Render(); ImGui::Render();
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); 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 // 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 // Moved into helpers.cpp
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
Helpers::ResetDevice(); Helpers::ResetDevice();
#endif //_TEST2
} }
void setupContext() void setupContext()
@ -322,11 +276,9 @@ void DirectX9Test::directX9Test()
// Setup Platform/Renderer backends // Setup Platform/Renderer backends
ImGui_ImplWin32_Init(hwnd); ImGui_ImplWin32_Init(hwnd);
#ifndef _TEST2
ImGui_ImplDX9_Init(g_pd3dDevice); ImGui_ImplDX9_Init(g_pd3dDevice);
#else
ImGui_ImplDX9_Init(Helpers::g_pd3dDevice); #endif //!_TEST
#endif //!_TEST2
// Our state // Our state
@ -359,26 +311,17 @@ void DirectX9Test::directX9Test()
if (done) if (done)
break; break;
#ifndef _TEST2
// Handle window resize (we don't resize directly in the WM_SIZE handler) // 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) if (g_ResizeWidth != 0 && g_ResizeHeight != 0)
{ {
g_d3dpp.BackBufferWidth = g_ResizeWidth; g_d3dpp.BackBufferWidth = g_ResizeWidth;
g_d3dpp.BackBufferHeight = g_ResizeHeight; g_d3dpp.BackBufferHeight = g_ResizeHeight;
g_ResizeWidth = g_ResizeHeight = 0; 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(); // Moved into helpers.cpp
#else
Helpers::ResetDevice(); Helpers::ResetDevice();
#endif //!_TEST2
} }
// Start the Dear ImGui frame // Start the Dear ImGui frame
@ -403,8 +346,6 @@ void DirectX9Test::directX9Test()
// Show the main menu // Show the main menu
MainMenu::MainMenuTest(); MainMenu::MainMenuTest();
// Text file functions test menu // Text file functions test menu
if (ImGui::CollapsingHeader("Text File Functions")) if (ImGui::CollapsingHeader("Text File Functions"))
{ {
@ -464,11 +405,8 @@ void DirectX9Test::directX9Test()
// Cleanup D3D Device // Cleanup D3D Device
#ifndef _TEST2 // Moved into helpers.cpp
CleanupDeviceD3D();
#else
Helpers::CleanupDeviceD3D(); Helpers::CleanupDeviceD3D();
#endif
::DestroyWindow(hwnd); ::DestroyWindow(hwnd);
::UnregisterClassW(wc.lpszClassName, wc.hInstance); ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
@ -476,46 +414,6 @@ void DirectX9Test::directX9Test()
// End Main class // 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 // 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); 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: case WM_SIZE:
if (wParam == SIZE_MINIMIZED) if (wParam == SIZE_MINIMIZED)
return 0; return 0;
#ifndef _TEST2 //g_ResizeWidth = (UINT)LOWORD(lParam); // Queue resize
g_ResizeWidth = (UINT)LOWORD(lParam); // Queue resize //g_ResizeHeight = (UINT)HIWORD(lParam);
g_ResizeHeight = (UINT)HIWORD(lParam); DirectX9Test::g_ResizeWidth = (UINT)LOWORD(lParam); // Queue resize
#else DirectX9Test::g_ResizeHeight = (UINT)HIWORD(lParam);
Helpers::g_ResizeWidth = (UINT)LOWORD(lParam); // Queue resize
Helpers::g_ResizeHeight = (UINT)HIWORD(lParam);
#endif //!_TEST2
return 0; return 0;
case WM_SYSCOMMAND: case WM_SYSCOMMAND:
if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu 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 0;
} }
return ::DefWindowProcW(hWnd, msg, wParam, lParam); return ::DefWindowProcW(hWnd, msg, wParam, lParam);
}
#endif //_TEST }
#endif //_DIRECTX9 #endif //!_DIRECTX9

View File

@ -1,4 +1,13 @@
#pragma once #pragma once
#ifdef _WIN32
#include <Windows.h>
#ifdef _DIRECTX9
#include <d3d9.h>
#endif
#endif
class DirectX9Test { class DirectX9Test {
public: public:
static void directX9Test(); static void directX9Test();
@ -10,4 +19,15 @@ public:
static bool show_demo_window; static bool show_demo_window;
static bool define_test; static bool define_test;
static bool toggle_text; 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;
}; };

View File

@ -1,38 +1,36 @@
#include "helpers.h" #include "helpers.h"
#include <d3d9.h>
#include "imgui.h" #include "imgui.h"
#ifdef _DIRECTX9
#include <d3d9.h>
#include "imgui_impl_dx9.h" #include "imgui_impl_dx9.h"
#include "directx9_test.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
//-------------- DirectX9 Functions --------/
bool Helpers::CreateDeviceD3D(HWND hWnd) bool Helpers::CreateDeviceD3D(HWND hWnd)
{ {
if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == nullptr) if ((DirectX9Test::g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == nullptr)
return false; return false;
// Create the D3DDevice // Create the D3DDevice
ZeroMemory(&g_d3dpp, sizeof(g_d3dpp)); ZeroMemory(&DirectX9Test::g_d3dpp, sizeof(DirectX9Test::g_d3dpp));
g_d3dpp.Windowed = TRUE; DirectX9Test::g_d3dpp.Windowed = TRUE;
g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; DirectX9Test::g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Need to use an explicit format with alpha if needing per-pixel alpha composition. DirectX9Test::g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Need to use an explicit format with alpha if needing per-pixel alpha composition.
g_d3dpp.EnableAutoDepthStencil = TRUE; DirectX9Test::g_d3dpp.EnableAutoDepthStencil = TRUE;
g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16; DirectX9Test::g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // Present with vsync DirectX9Test::g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
//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)
//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 false;
return true; return true;
@ -40,17 +38,25 @@ bool Helpers::CreateDeviceD3D(HWND hWnd)
void Helpers::CleanupDeviceD3D() void Helpers::CleanupDeviceD3D()
{ {
if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; } if (DirectX9Test::g_pd3dDevice)
if (g_pD3D) { g_pD3D->Release(); g_pD3D = nullptr; } {
DirectX9Test::g_pd3dDevice->Release();
DirectX9Test::g_pd3dDevice = nullptr;
}
if (DirectX9Test::g_pD3D)
{
DirectX9Test::g_pD3D->Release();
DirectX9Test::g_pD3D = nullptr;
}
} }
void Helpers::ResetDevice() void Helpers::ResetDevice()
{ {
ImGui_ImplDX9_InvalidateDeviceObjects(); ImGui_ImplDX9_InvalidateDeviceObjects();
HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp); HRESULT hr = DirectX9Test::g_pd3dDevice->Reset(&DirectX9Test::g_d3dpp);
if (hr == D3DERR_INVALIDCALL) if (hr == D3DERR_INVALIDCALL)
IM_ASSERT(0); IM_ASSERT(0);
ImGui_ImplDX9_CreateDeviceObjects(); ImGui_ImplDX9_CreateDeviceObjects();
} }
#endif //_TEST2 #endif //_DIRECTX9

View File

@ -1,34 +1,33 @@
#pragma once #pragma once
#ifdef _WIN32 #ifdef _WIN32
#include <Windows.h> #include <Windows.h>
#ifdef _DIRECTX9
#include <d3d9.h>
#endif
#include "defines.h"
#endif //_WIN32 #endif //_WIN32
// Oops I named the source file with a .c extension instead of .cpp // Oops I named the source file with a .c extension instead of .cpp
// No wonder this didn't work. // No wonder this didn't work.
// https://stackoverflow.com/questions/12524168/resolving-found-at-file-scope-missing-function-header-in-vs2010-c // https://stackoverflow.com/questions/12524168/resolving-found-at-file-scope-missing-function-header-in-vs2010-c
#ifdef _TEST2 #ifdef _DIRECTX9
class Helpers { class Helpers {
public: 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 bool CreateDeviceD3D(HWND hWnd);
static void CleanupDeviceD3D(); static void CleanupDeviceD3D();
static void ResetDevice(); 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 #else
// Will this work on Linux or Mac? I don't think DirectX does. // Will this work on Linux or Mac? I don't think DirectX does.
static bool CreateDeviceD3D(); //static bool CreateDeviceD3D();
#endif #endif
}; };
#endif //_TEST2
#endif //_DIRECTX9