Move most ImGui functions into imgui_functions.cpp.

Remove ImGui functions out of directx9_test.cpp.
Move InitializeD3D into helpers.cpp
This commit is contained in:
kelson8 2025-03-13 03:04:28 -04:00
parent a6015f1e21
commit afd62ebc5f
7 changed files with 183 additions and 113 deletions

View File

@ -210,7 +210,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug-Directx9|x64'">
<ClCompile>
<AdditionalIncludeDirectories>lib\imgui\;lib\imgui\backends;.\src;.\src\test;.\src\util\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>lib\imgui\;lib\imgui\backends;.\src;.\src\test;.\src\util\;.\src\functions;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_DIRECTX9</PreprocessorDefinitions>
</ClCompile>
<Link>
@ -227,6 +227,7 @@
<ItemGroup>
<ClCompile Include="lib\ImGui\backends\imgui_impl_dx9.cpp" />
<ClCompile Include="lib\ImGui\backends\imgui_impl_opengl3.cpp" />
<ClCompile Include="src\functions\imgui_functions.cpp" />
<ClCompile Include="src\Main.cpp" />
<ClCompile Include="lib\ImGui\backends\imgui_impl_win32.cpp" />
<ClCompile Include="lib\ImGui\imgui.cpp" />
@ -248,6 +249,7 @@
<ClCompile Include="src\util\text_functions.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\functions\imgui_functions.h" />
<ClInclude Include="src\menus\main_menu.h" />
<ClInclude Include="src\menus\pch.h" />
<ClInclude Include="src\menus\text_menu.h" />

View File

@ -64,6 +64,9 @@
<ClCompile Include="src\menus\vice_city_menu.cpp">
<Filter>src\menus</Filter>
</ClCompile>
<ClCompile Include="src\functions\imgui_functions.cpp">
<Filter>src\functions</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="lib\ImGui\imconfig.h">
@ -138,6 +141,9 @@
<ClInclude Include="src\menus\vice_city_menu.h">
<Filter>src\menus</Filter>
</ClInclude>
<ClInclude Include="src\functions\imgui_functions.h">
<Filter>src\functions</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="src">
@ -161,6 +167,9 @@
<Filter Include="src\lib\ImGui">
<UniqueIdentifier>{1804e534-3e6f-47ec-bf39-30070e52ce27}</UniqueIdentifier>
</Filter>
<Filter Include="src\functions">
<UniqueIdentifier>{95eb670c-9521-4121-8c32-2817ecc4e7fd}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="lib\ImGui\backends\imgui_impl_metal.mm">

View File

@ -0,0 +1,110 @@
#include "imgui_functions.h"
// ImGui
#include "imgui.h"
#include "imgui_impl_dx9.h"
#include "imgui_impl_win32.h"
// TODO Move ImGui functions for use in here.
/// <summary>
/// Destory the ImGui window
/// </summary>
/// <param name="hwnd"></param>
/// <param name="wc"></param>
void ImGuiFunctions::Main::DestroyWindow(HWND hwnd, WNDCLASSEXW wc)
{
::DestroyWindow(hwnd);
::UnregisterClassW(wc.lpszClassName, wc.hInstance);
}
/// <summary>
/// Shutdown ImGui
/// </summary>
void ImGuiFunctions::Main::ShutDown()
{
// Shutdown ImGui
ImGui_ImplDX9_Shutdown();
ImGui_ImplWin32_Shutdown();
// Destroy ImGui Context
ImGui::DestroyContext();
}
/// <summary>
/// Show the ImGui Window
/// </summary>
/// <param name="hwnd"></param>
void ImGuiFunctions::Main::ShowWindow(HWND hwnd)
{
// Show the window
::ShowWindow(hwnd, SW_SHOWDEFAULT);
::UpdateWindow(hwnd);
}
// imgui_demo line 256
// TODO What's this for? Seems like it's not needed
//static void ImGuiFunctions::Main::ShowWindow(bool* p_open)
//{
// static bool show_main_menu = false;
//
// if (show_main_menu)
// {
// if (ImGui::Begin("Test"))
// {
// ImGui::Text("Hello World!");
// }
// }
//
// if (!ImGui::Begin("Test", p_open))
// {
//
// ImGui::End();
// return;
// }
//}
/// <summary>
/// Setup ImGui
/// </summary>
void ImGuiFunctions::Main::SetupContext()
{
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
io.Fonts->AddFontDefault();
io.Fonts->AddFontFromFileTTF("./lib/ImGui/misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != nullptr);
}
/// <summary>
/// Add a help marker
/// </summary>
/// <param name="desc"></param>
static void ImGuiFunctions::Main::HelpMarker(const char* desc)
{
ImGui::TextDisabled("(?)");
if (ImGui::BeginItemTooltip())
{
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted(desc);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}

View File

@ -0,0 +1,22 @@
#pragma once
#ifdef _WIN32
#include <Windows.h>
#endif
namespace ImGuiFunctions {
namespace Main {
static void HelpMarker(const char* desc);
// TODO What's this for? Seems like it's not needed
//static void ShowWindow(bool* p_open);
void SetupContext();
void ShowWindow(HWND hwnd);
void ShutDown();
void DestroyWindow(HWND hwnd, WNDCLASSEXW wc);
}
}
//class ImGuiFunctions
//{
//};

View File

@ -14,6 +14,8 @@
#endif
#ifdef _DIRECTX9
// TODO Why is this here?
#ifdef _TEST
// Dear ImGui: standalone example application for DirectX 9
@ -59,6 +61,10 @@
#include "http_lib_test.h"
//
// Imgui functions
#include "imgui_functions.h"
// TODO Create src folder for project, move source files into it
// TODO Possibly create headers folder for project.
@ -83,18 +89,6 @@ D3DPRESENT_PARAMETERS DirectX9Test::g_d3dpp = {};
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
static void HelpMarker(const char* desc)
{
ImGui::TextDisabled("(?)");
if (ImGui::BeginItemTooltip())
{
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted(desc);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}
// Windows specific features
// https://stackoverflow.com/questions/41600981/how-do-i-check-if-a-key-is-pressed-on-c
#ifdef _WIN32
@ -138,53 +132,11 @@ std::string testString1()
return "The value of " + num1_string + " + " + num2_string + " = " + sum_string;
}
// imgui_demo line 256
static void ShowWindow(bool* p_open)
{
static bool show_main_menu = false;
if (show_main_menu)
{
if (ImGui::Begin("Test"))
{
ImGui::Text("Hello World!");
}
}
if (!ImGui::Begin("Test", p_open))
{
ImGui::End();
return;
}
}
static void CreateApplicationWindow()
{
}
static void InitializeD3D(HWND hwnd, WNDCLASSEXW wc)
{
// Initialize Direct3D
if (!Helpers::CreateDeviceD3D(hwnd))
{
Helpers::CleanupDeviceD3D();
::UnregisterClassW(wc.lpszClassName, wc.hInstance);
//return 1;
}
}
static void ShowWindow(HWND hwnd)
{
// Show the window
::ShowWindow(hwnd, SW_SHOWDEFAULT);
::UpdateWindow(hwnd);
}
static void Render(ImVec4 clear_color)
{
DirectX9Test::g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
@ -207,35 +159,6 @@ static void Render(ImVec4 clear_color)
Helpers::ResetDevice();
}
void setupContext()
{
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
io.Fonts->AddFontDefault();
io.Fonts->AddFontFromFileTTF("./lib/ImGui/misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != nullptr);
}
// Main code
// TODO Fix this not to break when imgui_impl_glfw.cpp and imgui_impl_glfw.h is included.
@ -262,13 +185,13 @@ void DirectX9Test::directX9Test()
HWND hwnd = ::CreateWindowW(wc.lpszClassName, defines->window_title, WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
// Initialize Direct3D
InitializeD3D(hwnd, wc);
Helpers::InitializeD3D(hwnd, wc);
// Show the window
ShowWindow(hwnd);
ImGuiFunctions::Main::ShowWindow(hwnd);
// Setup Dear ImGui context
setupContext();
ImGuiFunctions::Main::SetupContext();
// Setup Dear ImGui style
ImGui::StyleColorsDark();
@ -340,7 +263,9 @@ void DirectX9Test::directX9Test()
//***************
// Start of ImGui code
//***************
//if (ImGui::Begin("KCNet ImGui", nullptr, ImGuiWindowFlags_MenuBar))
// TODO Possibly move this into it's own file, so it can be used
// in my DirectX9 test and OpenGL test with preprocessors.
if (ImGui::Begin(defines->imgui_window_name, nullptr, ImGuiWindowFlags_MenuBar))
{
// Show the main menu
@ -372,44 +297,33 @@ void DirectX9Test::directX9Test()
}
#endif //_TEST1
// End http test menu
//if (ImGui::BeginMenu("My menu"))
//{
// //ImGui::BulletText("You should see this");
// ImGui::MenuItem("test", NULL);
// ImGui::EndMenu();
//}
}
// End ImGui
ImGui::End();
ImGui::EndFrame();
//***************
// End of ImGui code
//***************
// Rendering
Render(clear_color);
//***************
// End of ImGui code
//***************
}
// Shutdown ImGui
ImGui_ImplDX9_Shutdown();
ImGui_ImplWin32_Shutdown();
// Destroy ImGui Context
ImGui::DestroyContext();
// Shutdown ImGui, and destory context.
ImGuiFunctions::Main::ShutDown();
// Cleanup D3D Device
// Moved into helpers.cpp
Helpers::CleanupDeviceD3D();
::DestroyWindow(hwnd);
::UnregisterClassW(wc.lpszClassName, wc.hInstance);
// Destory ImGui window
ImGuiFunctions::Main::DestroyWindow(hwnd, wc);
}
// End Main class
@ -424,6 +338,7 @@ extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// This is required in all instances of ImGui, for the mouse and keyboard input I'm quite sure.
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
return true;
@ -432,8 +347,6 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_SIZE:
if (wParam == SIZE_MINIMIZED)
return 0;
//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);
@ -450,4 +363,6 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
return ::DefWindowProcW(hWnd, msg, wParam, lParam);
}
#endif //!_DIRECTX9
#endif //_DIRECTX9

View File

@ -11,7 +11,17 @@
//-------------- DirectX9 Functions --------/
//-------------- DirectX9 Functions --------------/
void Helpers::InitializeD3D(HWND hwnd, WNDCLASSEXW wc)
{
// Initialize Direct3D
if (!Helpers::CreateDeviceD3D(hwnd))
{
Helpers::CleanupDeviceD3D();
::UnregisterClassW(wc.lpszClassName, wc.hInstance);
}
}
bool Helpers::CreateDeviceD3D(HWND hWnd)
{

View File

@ -23,6 +23,8 @@ public:
static bool CreateDeviceD3D(HWND hWnd);
static void CleanupDeviceD3D();
static void ResetDevice();
static void InitializeD3D(HWND hwnd, WNDCLASSEXW wc);
#else
// Will this work on Linux or Mac? I don't think DirectX does.
//static bool CreateDeviceD3D();