make imgui optional-ish

This commit is contained in:
Till Wübbers
2025-04-29 08:46:05 +02:00
parent 02c40aeea6
commit 91e9566747
6 changed files with 74 additions and 35 deletions

View File

@@ -2,6 +2,7 @@
#include "Global.h" #include "Global.h"
#include "Input.h" #include "Input.h"
#include "imgui.h" #include "imgui.h"
#include "imgui_internal.h"
namespace Game namespace Game
{ {
@@ -9,12 +10,14 @@ namespace Game
bool IsKeyboardAllowed() bool IsKeyboardAllowed()
{ {
if (GImGui == nullptr) return true;
auto& IO = ImGui::GetIO(); auto& IO = ImGui::GetIO();
return !IO.WantCaptureKeyboard || GetFlag(IO.ConfigFlags, ImGuiConfigFlags_NoKeyboard); return !IO.WantCaptureKeyboard || GetFlag(IO.ConfigFlags, ImGuiConfigFlags_NoKeyboard);
} }
bool IsMouseAllowed() bool IsMouseAllowed()
{ {
if (GImGui == nullptr) return true;
auto& IO = ImGui::GetIO(); auto& IO = ImGui::GetIO();
return !IO.WantCaptureMouse || GetFlag(IO.ConfigFlags, ImGuiConfigFlags_NoMouse); return !IO.WantCaptureMouse || GetFlag(IO.ConfigFlags, ImGuiConfigFlags_NoMouse);
} }
@@ -60,6 +63,8 @@ namespace Game
} }
Vec2 GetMousePos() Vec2 GetMousePos()
{ {
// TODO: fix this!!
if (GImGui == nullptr) return {};
ImVec2 pos = ImGui::GetMousePos(); ImVec2 pos = ImGui::GetMousePos();
return {pos.x, pos.y}; return {pos.x, pos.y};
} }

View File

@@ -93,9 +93,15 @@ namespace Game
bool IsGaming = GetInstance().Player.InputM == InputMode::Game; bool IsGaming = GetInstance().Player.InputM == InputMode::Game;
bool captureMouse = IsGaming && GetInstance().Player.InteractionM == InteractionMode::Walk; bool captureMouse = IsGaming && GetInstance().Player.InteractionM == InteractionMode::Walk;
SDL_SetWindowRelativeMouseMode(GetShared().Window.SDLWindow, captureMouse); SDL_SetWindowRelativeMouseMode(GetShared().Window.SDLWindow, captureMouse);
auto& IO = ImGui::GetIO();
IO.ConfigFlags = FlagBool(IO.ConfigFlags, ImGuiConfigFlags_NoMouse | ImGuiConfigFlags_NoKeyboard, IsGaming); auto& rendering = GameRendering::Get();
GameRendering::Get().UIVisible = IsGaming ? UIVisibilityState::Game : UIVisibilityState::Debug; if (rendering.SetupData.UseImgui)
{
auto& IO = ImGui::GetIO();
IO.ConfigFlags =
FlagBool(IO.ConfigFlags, ImGuiConfigFlags_NoMouse | ImGuiConfigFlags_NoKeyboard, IsGaming);
}
rendering.UIVisible = IsGaming ? UIVisibilityState::Game : UIVisibilityState::Debug;
} }
} // namespace } // namespace

View File

@@ -58,7 +58,7 @@ namespace Game
SetInstance(instance); SetInstance(instance);
ResetScratch(); ResetScratch();
Puzzle::LoadStaticPuzzleData(); Puzzle::LoadStaticPuzzleData();
SetupInstance.Rendering.Setup(); SetupInstance.Rendering.Setup({false});
instance.GameLevel.Setup(shared.Game); instance.GameLevel.Setup(shared.Game);
instance.IsInitialized = true; instance.IsInitialized = true;
} }

View File

@@ -182,6 +182,8 @@ namespace Tools
void RenderDebugUI(Game::GameRendering& rendering) void RenderDebugUI(Game::GameRendering& rendering)
{ {
if (!rendering.SetupData.UseImgui) return;
auto& time = Game::GetInstance().Time; auto& time = Game::GetInstance().Time;
auto& shared = Game::GetShared(); auto& shared = Game::GetShared();
auto& debug = Game::GetInstance().DebugData; auto& debug = Game::GetInstance().DebugData;

View File

@@ -312,11 +312,13 @@ namespace Game
return *Instance; return *Instance;
} }
void GameRendering::Setup() void GameRendering::Setup(const RenderingSetup& setup)
{ {
LOG("--- RENDERING STARTUP ---"); LOG("--- RENDERING STARTUP ---");
ZoneScopedN("Setup"); ZoneScopedN("Setup");
SetupData = setup;
if (Instance != nullptr) LOG_WARN("old rendering wasn't destroyed!"); if (Instance != nullptr) LOG_WARN("old rendering wasn't destroyed!");
Instance = this; Instance = this;
SharedData& shared = GetShared(); SharedData& shared = GetShared();
@@ -325,6 +327,8 @@ namespace Game
init.type = bgfx::RendererType::Direct3D12; init.type = bgfx::RendererType::Direct3D12;
#ifdef _DEBUG #ifdef _DEBUG
// init.debug = true; // init.debug = true;
#else
// init.debug = false;
#endif #endif
init.platformData.nwh = shared.Window.Handle; init.platformData.nwh = shared.Window.Handle;
init.platformData.ndt = nullptr; init.platformData.ndt = nullptr;
@@ -354,23 +358,26 @@ namespace Game
LoadModels(Models, ModelCount); LoadModels(Models, ModelCount);
ReloadShaders(); ReloadShaders();
imguiCreate();
SetImguiStyle();
if (!ImGui_ImplSDL3_InitForOther(shared.Window.SDLWindow)) if (SetupData.UseImgui)
{ {
LOG_ERROR("Failed to set up imgui implementation!"); imguiCreate();
return; SetImguiStyle();
} if (!ImGui_ImplSDL3_InitForOther(shared.Window.SDLWindow))
{
LOG_ERROR("Failed to set up imgui implementation!");
return;
}
// ImGui::GetIO().BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // ImGui::GetIO().BackendFlags |= ImGuiBackendFlags_RendererHasViewports;
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable; ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable;
// ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
// auto& platIO = ImGui::GetPlatformIO(); // auto& platIO = ImGui::GetPlatformIO();
// platIO.Renderer_CreateWindow = TODO; // platIO.Renderer_CreateWindow = TODO;
// platIO.Platform_DestroyWindow = TODO; // platIO.Platform_DestroyWindow = TODO;
// platIO.Platform_SetWindowSize = TODO; // platIO.Platform_SetWindowSize = TODO;
// platIO.Platform_RenderWindow = TODO; // platIO.Platform_RenderWindow = TODO;
}
GameInstance& inst = GetInstance(); GameInstance& inst = GetInstance();
if (!inst.IsInitialized) if (!inst.IsInitialized)
@@ -378,13 +385,16 @@ namespace Game
inst.Time.StartTime = bx::getHPCounter(); inst.Time.StartTime = bx::getHPCounter();
} }
if (inst.DebugData.ImguiIniSize > 0) if (SetupData.UseImgui)
{ {
ImGui::LoadIniSettingsFromMemory(inst.DebugData.ImguiIni, inst.DebugData.ImguiIniSize); if (inst.DebugData.ImguiIniSize > 0)
} {
else ImGui::LoadIniSettingsFromMemory(inst.DebugData.ImguiIni, inst.DebugData.ImguiIniSize);
{ }
ImGui::LoadIniSettingsFromDisk("imgui.ini"); else
{
ImGui::LoadIniSettingsFromDisk("imgui.ini");
}
} }
DitherGen(DitherTextures, DitherRecursion); DitherGen(DitherTextures, DitherRecursion);
} }
@@ -396,7 +406,10 @@ namespace Game
for (uint16_t i = 0; i < shared.Window.SDLEventCount; ++i) for (uint16_t i = 0; i < shared.Window.SDLEventCount; ++i)
{ {
ImGui_ImplSDL3_ProcessEvent(&shared.Window.SDLEvents[i]); if (SetupData.UseImgui)
{
ImGui_ImplSDL3_ProcessEvent(&shared.Window.SDLEvents[i]);
}
} }
shared.Window.SDLEventCount = 0; shared.Window.SDLEventCount = 0;
@@ -505,6 +518,7 @@ namespace Game
HandleEvents(); HandleEvents();
// Start Rendering // Start Rendering
if (SetupData.UseImgui)
{ {
ZoneScopedN("Imgui Start Frame"); ZoneScopedN("Imgui Start Frame");
imguiBeginFrame(20); imguiBeginFrame(20);
@@ -518,12 +532,13 @@ namespace Game
GetInstance().GameLevel.Render(MainViewID, Models, Materials, Textures); GetInstance().GameLevel.Render(MainViewID, Models, Materials, Textures);
// Finish Frame // Finish Frame
if (SetupData.UseImgui)
{ {
ZoneScopedN("Imgui End Frame"); ZoneScopedN("Imgui End Frame");
imguiEndFrame(); imguiEndFrame();
} }
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) if (SetupData.UseImgui && ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{ {
ZoneScopedN("Imgui Platform Update"); ZoneScopedN("Imgui Platform Update");
ImGui::UpdatePlatformWindows(); ImGui::UpdatePlatformWindows();
@@ -542,13 +557,18 @@ namespace Game
{ {
ZoneScopedN("Shutdown"); ZoneScopedN("Shutdown");
LOG("--- RENDERING_SHUTDOWN ---"); LOG("--- RENDERING_SHUTDOWN ---");
ImGui::SaveIniSettingsToDisk("imgui.ini");
auto& debug = GetInstance().DebugData; if (SetupData.UseImgui)
const char* iniData = ImGui::SaveIniSettingsToMemory(reinterpret_cast<uint64_t*>(&debug.ImguiIniSize)); {
assert(debug.ImguiIniSize <= BX_COUNTOF(InstanceDebugData::ImguiIni)); ImGui::SaveIniSettingsToDisk("imgui.ini");
bx::memCopy(debug.ImguiIni, iniData, bx::min(debug.ImguiIniSize, BX_COUNTOF(InstanceDebugData::ImguiIni))); auto& debug = GetInstance().DebugData;
ImGui_ImplSDL3_Shutdown(); const char* iniData = ImGui::SaveIniSettingsToMemory(reinterpret_cast<uint64_t*>(&debug.ImguiIniSize));
imguiDestroy(); assert(debug.ImguiIniSize <= BX_COUNTOF(InstanceDebugData::ImguiIni));
bx::memCopy(debug.ImguiIni, iniData, bx::min(debug.ImguiIniSize, BX_COUNTOF(InstanceDebugData::ImguiIni)));
ImGui_ImplSDL3_Shutdown();
imguiDestroy();
}
bgfx::shutdown(); bgfx::shutdown();
Instance = nullptr; Instance = nullptr;
} }

View File

@@ -64,6 +64,11 @@ namespace Game
Debug, Debug,
}; };
struct RenderingSetup
{
bool UseImgui = true;
};
class GameRendering class GameRendering
{ {
public: public:
@@ -76,6 +81,7 @@ namespace Game
DitherData DitherTextures; DitherData DitherTextures;
public: public:
RenderingSetup SetupData;
bgfx::UniformHandle DefaultSampler; bgfx::UniformHandle DefaultSampler;
Texture Textures[MaxTextures]; Texture Textures[MaxTextures];
Material Materials[8]; Material Materials[8];
@@ -89,7 +95,7 @@ namespace Game
int32_t DitherRecursion = 1; int32_t DitherRecursion = 1;
public: public:
void Setup(); void Setup(const RenderingSetup& setup);
void Update(); void Update();
void HandleEvents(); void HandleEvents();
void LoadTextures(); void LoadTextures();