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& rendering = GameRendering::Get();
if (rendering.SetupData.UseImgui)
{
auto& IO = ImGui::GetIO(); auto& IO = ImGui::GetIO();
IO.ConfigFlags = FlagBool(IO.ConfigFlags, ImGuiConfigFlags_NoMouse | ImGuiConfigFlags_NoKeyboard, IsGaming); IO.ConfigFlags =
GameRendering::Get().UIVisible = IsGaming ? UIVisibilityState::Game : UIVisibilityState::Debug; 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,9 +358,11 @@ namespace Game
LoadModels(Models, ModelCount); LoadModels(Models, ModelCount);
ReloadShaders(); ReloadShaders();
if (SetupData.UseImgui)
{
imguiCreate(); imguiCreate();
SetImguiStyle(); SetImguiStyle();
if (!ImGui_ImplSDL3_InitForOther(shared.Window.SDLWindow)) if (!ImGui_ImplSDL3_InitForOther(shared.Window.SDLWindow))
{ {
LOG_ERROR("Failed to set up imgui implementation!"); LOG_ERROR("Failed to set up imgui implementation!");
@@ -371,6 +377,7 @@ namespace Game
// 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,6 +385,8 @@ namespace Game
inst.Time.StartTime = bx::getHPCounter(); inst.Time.StartTime = bx::getHPCounter();
} }
if (SetupData.UseImgui)
{
if (inst.DebugData.ImguiIniSize > 0) if (inst.DebugData.ImguiIniSize > 0)
{ {
ImGui::LoadIniSettingsFromMemory(inst.DebugData.ImguiIni, inst.DebugData.ImguiIniSize); ImGui::LoadIniSettingsFromMemory(inst.DebugData.ImguiIni, inst.DebugData.ImguiIniSize);
@@ -386,6 +395,7 @@ namespace Game
{ {
ImGui::LoadIniSettingsFromDisk("imgui.ini"); ImGui::LoadIniSettingsFromDisk("imgui.ini");
} }
}
DitherGen(DitherTextures, DitherRecursion); DitherGen(DitherTextures, DitherRecursion);
} }
@@ -395,9 +405,12 @@ namespace Game
SharedData& shared = GetShared(); SharedData& shared = GetShared();
for (uint16_t i = 0; i < shared.Window.SDLEventCount; ++i) for (uint16_t i = 0; i < shared.Window.SDLEventCount; ++i)
{
if (SetupData.UseImgui)
{ {
ImGui_ImplSDL3_ProcessEvent(&shared.Window.SDLEvents[i]); ImGui_ImplSDL3_ProcessEvent(&shared.Window.SDLEvents[i]);
} }
}
shared.Window.SDLEventCount = 0; shared.Window.SDLEventCount = 0;
// Resize if necessary // Resize if necessary
@@ -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,6 +557,9 @@ namespace Game
{ {
ZoneScopedN("Shutdown"); ZoneScopedN("Shutdown");
LOG("--- RENDERING_SHUTDOWN ---"); LOG("--- RENDERING_SHUTDOWN ---");
if (SetupData.UseImgui)
{
ImGui::SaveIniSettingsToDisk("imgui.ini"); ImGui::SaveIniSettingsToDisk("imgui.ini");
auto& debug = GetInstance().DebugData; auto& debug = GetInstance().DebugData;
const char* iniData = ImGui::SaveIniSettingsToMemory(reinterpret_cast<uint64_t*>(&debug.ImguiIniSize)); const char* iniData = ImGui::SaveIniSettingsToMemory(reinterpret_cast<uint64_t*>(&debug.ImguiIniSize));
@@ -549,6 +567,8 @@ namespace Game
bx::memCopy(debug.ImguiIni, iniData, bx::min(debug.ImguiIniSize, BX_COUNTOF(InstanceDebugData::ImguiIni))); bx::memCopy(debug.ImguiIni, iniData, bx::min(debug.ImguiIniSize, BX_COUNTOF(InstanceDebugData::ImguiIni)));
ImGui_ImplSDL3_Shutdown(); ImGui_ImplSDL3_Shutdown();
imguiDestroy(); 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();