imgui fixed!

This commit is contained in:
Asuro
2025-02-21 22:12:12 +01:00
parent dc12510fcb
commit edde743542
5 changed files with 40 additions and 86 deletions

View File

@@ -32,7 +32,7 @@ namespace Game
bgfx::setUniform(currentMaterial.Uniforms[Material::UTime], TimeValues); bgfx::setUniform(currentMaterial.Uniforms[Material::UTime], TimeValues);
bgfx::setUniform(currentMaterial.Uniforms[Material::UDotColor], TestColor); bgfx::setUniform(currentMaterial.Uniforms[Material::UDotColor], TestColor);
bgfx::submit(0, currentMaterial.Shader); bgfx::submit(currentMaterial.ViewID, currentMaterial.Shader);
} }
void Level::Setup(GameData& data) void Level::Setup(GameData& data)

View File

@@ -194,11 +194,11 @@ namespace Game
void GameRendering::Setup() void GameRendering::Setup()
{ {
Log("Game rendering setup..."); Log("--- RENDERING STARTUP ---");
SharedData& shared = GetShared(); SharedData& shared = GetShared();
bgfx::Init init; bgfx::Init init;
init.type = bgfx::RendererType::Vulkan; init.type = bgfx::RendererType::Direct3D12;
init.debug = true; init.debug = true;
init.platformData.nwh = shared.Window.Handle; init.platformData.nwh = shared.Window.Handle;
init.platformData.ndt = nullptr; init.platformData.ndt = nullptr;
@@ -209,6 +209,7 @@ namespace Game
LastWidth = shared.Window.WindowWidth; LastWidth = shared.Window.WindowWidth;
LastHeight = shared.Window.WindowHeight; LastHeight = shared.Window.WindowHeight;
Log("%i by %i", init.resolution.width, init.resolution.height); Log("%i by %i", init.resolution.width, init.resolution.height);
if (!bgfx::init(init)) if (!bgfx::init(init))
@@ -219,8 +220,9 @@ namespace Game
{ {
Log("BGFX setup succeded!"); Log("BGFX setup succeded!");
} }
// bgfx::setDebug(BGFX_DEBUG_TEXT); bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0); bgfx::setViewClear(MainViewID, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x3399FFff, 1.0f, 0);
bgfx::setViewRect(MainViewID, 0, 0, shared.Window.WindowWidth, shared.Window.WindowHeight);
DefaultSampler = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler); DefaultSampler = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
Textures[0].Handle = Textures[0].Handle =
@@ -229,7 +231,8 @@ namespace Game
LoadMesh(Models[0], "models/cube.gltf"); LoadMesh(Models[0], "models/cube.gltf");
LoadMesh(Models[1], "models/zurg.gltf"); LoadMesh(Models[1], "models/zurg.gltf");
Materials[0] = Material::LoadFromShader("vert", "frag", Textures[0].Handle, Textures[0].SamplerHandle); Materials[0] =
Material::LoadFromShader("vert", "frag", MainViewID, Textures[0].Handle, Textures[0].SamplerHandle);
imguiCreate(); imguiCreate();
if (!ImGui_ImplSDL3_InitForOther(shared.Window.SDLWindow)) if (!ImGui_ImplSDL3_InitForOther(shared.Window.SDLWindow))
@@ -257,17 +260,22 @@ namespace Game
{ {
SharedData& shared = GetShared(); SharedData& shared = GetShared();
// Handle events
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]); ImGui_ImplSDL3_ProcessEvent(&shared.Window.SDLEvents[i]);
} }
shared.Window.SDLEventCount = 0; shared.Window.SDLEventCount = 0;
ImGui_ImplSDL3_NewFrame(); // Resize if necessary
imguiBeginFrame(0, 0, 0, 0, shared.Window.WindowWidth, shared.Window.WindowHeight); if (shared.Window.WindowWidth != LastWidth || shared.Window.WindowHeight != LastHeight)
// TODO: why does this break stuff?? {
// ImGui::DockSpaceOverViewport(); bgfx::reset(shared.Window.WindowWidth, shared.Window.WindowHeight, ResetFlags);
ImGui::ShowDemoWindow(); bgfx::setViewRect(MainViewID, 0, 0, shared.Window.WindowWidth, shared.Window.WindowHeight);
LastWidth = shared.Window.WindowWidth;
LastHeight = shared.Window.WindowHeight;
}
// Reload shaders if necessary // Reload shaders if necessary
FileChangeNotification* shaderChange = nullptr; FileChangeNotification* shaderChange = nullptr;
@@ -292,19 +300,18 @@ namespace Game
} }
} }
} }
if (shared.Window.WindowWidth != LastWidth || shared.Window.WindowHeight != LastHeight)
{
bgfx::reset(shared.Window.WindowWidth, shared.Window.WindowHeight, ResetFlags);
LastWidth = shared.Window.WindowWidth; imguiBeginFrame(20);
LastHeight = shared.Window.WindowHeight; ImGui_ImplSDL3_NewFrame();
} // TODO: why does this break stuff??
ImGui::DockSpaceOverViewport(0, 0, ImGuiDockNodeFlags_PassthruCentralNode);
ImGui::ShowDemoWindow();
auto& IO = ImGui::GetIO();
{
GetInstance().GameLevel.Update(); GetInstance().GameLevel.Update();
}
// Set view and projection matrix for view 0. // TODO: Move player stuff to level
{ {
float proj[16]; float proj[16];
bx::mtxProj(proj, bx::mtxProj(proj,
@@ -318,16 +325,13 @@ namespace Game
if (player.Mode == PlayerMode::Freefly) if (player.Mode == PlayerMode::Freefly)
{ {
player.FreeflyCamTransform.UpdateMatrix(); player.FreeflyCamTransform.UpdateMatrix();
bgfx::setViewTransform(0, player.FreeflyCamTransform.M.M, proj); bgfx::setViewTransform(MainViewID, player.FreeflyCamTransform.M.M, proj);
} }
else else
{ {
player.PlayerCamTransform.UpdateMatrix(); player.PlayerCamTransform.UpdateMatrix();
bgfx::setViewTransform(0, player.PlayerCamTransform.M.M, proj); bgfx::setViewTransform(MainViewID, player.PlayerCamTransform.M.M, proj);
} }
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, shared.Window.WindowWidth, shared.Window.WindowHeight);
} }
GetInstance().GameLevel.Cubes.Render(Models, Materials); GetInstance().GameLevel.Cubes.Render(Models, Materials);
@@ -359,10 +363,8 @@ namespace Game
bgfx::shutdown(); bgfx::shutdown();
} }
Material Material::LoadFromShader(const char* vertPath, Material Material::LoadFromShader(
const char* fragPath, const char* vertPath, const char* fragPath, uint32_t view, bgfx::TextureHandle tex, bgfx::UniformHandle sampler)
bgfx::TextureHandle tex,
bgfx::UniformHandle sampler)
{ {
BX_ASSERT(vertPath != nullptr && fragPath != nullptr, "Invalid shader path!"); BX_ASSERT(vertPath != nullptr && fragPath != nullptr, "Invalid shader path!");
bgfx::ShaderHandle vertexShader = loadShader(vertPath); bgfx::ShaderHandle vertexShader = loadShader(vertPath);
@@ -376,6 +378,7 @@ namespace Game
mat.Uniforms[Material::UDotColor] = bgfx::createUniform("u_testColor", bgfx::UniformType::Vec4); mat.Uniforms[Material::UDotColor] = bgfx::createUniform("u_testColor", bgfx::UniformType::Vec4);
mat.Textures[0].Handle = tex; mat.Textures[0].Handle = tex;
mat.Textures[0].SamplerHandle = sampler; mat.Textures[0].SamplerHandle = sampler;
mat.ViewID = view;
return mat; return mat;
} }
} // namespace Game } // namespace Game

View File

@@ -46,8 +46,10 @@ namespace Game
bgfx::UniformHandle Uniforms[8]; bgfx::UniformHandle Uniforms[8];
Texture Textures[4]; Texture Textures[4];
uint64_t State = 0; uint64_t State = 0;
uint32_t ViewID = 0;
static Material LoadFromShader(const char* vertPath, static Material LoadFromShader(const char* vertPath,
const char* fragPath, const char* fragPath,
uint32_t view = 0,
bgfx::TextureHandle = BGFX_INVALID_HANDLE, bgfx::TextureHandle = BGFX_INVALID_HANDLE,
bgfx::UniformHandle sampler = BGFX_INVALID_HANDLE); bgfx::UniformHandle sampler = BGFX_INVALID_HANDLE);
}; };
@@ -62,6 +64,7 @@ namespace Game
int32_t LastWidth = 0; int32_t LastWidth = 0;
int32_t LastHeight = 0; int32_t LastHeight = 0;
uint32_t ResetFlags = BGFX_RESET_VSYNC; uint32_t ResetFlags = BGFX_RESET_VSYNC;
uint32_t MainViewID = 10;
public: public:
void Setup(); void Setup();

View File

@@ -11,22 +11,12 @@
#include <imgui.h> #include <imgui.h>
#include <imgui_internal.h> #include <imgui_internal.h>
// #include "../bgfx_utils.h"
#include "imgui-helper.h" #include "imgui-helper.h"
#ifndef USE_ENTRY
#define USE_ENTRY 0
#endif // USE_ENTRY
#ifndef USE_LOCAL_STB #ifndef USE_LOCAL_STB
#define USE_LOCAL_STB 1 #define USE_LOCAL_STB 1
#endif // USE_LOCAL_STB #endif // USE_LOCAL_STB
#if USE_ENTRY
#include "../entry/entry.h"
#include "../entry/input.h"
#endif // USE_ENTRY
#include "fs_imgui_image.bin.h" #include "fs_imgui_image.bin.h"
#include "fs_ocornut_imgui.bin.h" #include "fs_ocornut_imgui.bin.h"
#include "vs_imgui_image.bin.h" #include "vs_imgui_image.bin.h"
@@ -208,9 +198,7 @@ struct OcornutImguiContext
m_last = bx::getHPCounter(); m_last = bx::getHPCounter();
ImGui::SetAllocatorFunctions(memAlloc, memFree, NULL); ImGui::SetAllocatorFunctions(memAlloc, memFree, NULL);
m_imgui = ImGui::CreateContext(); m_imgui = ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(1280.0f, 720.0f); io.DisplaySize = ImVec2(1280.0f, 720.0f);
@@ -274,13 +262,10 @@ struct OcornutImguiContext
bgfx::TextureFormat::BGRA8, bgfx::TextureFormat::BGRA8,
0, 0,
bgfx::copy(data, width * height * 4)); bgfx::copy(data, width * height * 4));
// ImGui::InitDockContext();
} }
void destroy() void destroy()
{ {
// ImGui::ShutdownDockContext();
ImGui::DestroyContext(m_imgui); ImGui::DestroyContext(m_imgui);
bgfx::destroy(s_tex); bgfx::destroy(s_tex);
@@ -311,26 +296,9 @@ struct OcornutImguiContext
style.WindowBorderSize = 0.0f; style.WindowBorderSize = 0.0f;
} }
void beginFrame(int32_t _mx, void beginFrame(bgfx::ViewId _viewId)
int32_t _my,
uint8_t _button,
int32_t _scroll,
int _width,
int _height,
int _inputChar,
bgfx::ViewId _viewId)
{ {
m_viewId = _viewId; m_viewId = _viewId;
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)_width, (float)_height);
const int64_t now = bx::getHPCounter();
const int64_t frameTime = now - m_last;
m_last = now;
// const double freq = double(bx::getHPFrequency());
// io.DeltaTime = float(frameTime / freq);
ImGui::NewFrame(); ImGui::NewFrame();
} }
@@ -381,16 +349,9 @@ void imguiDestroy()
s_ctx.destroy(); s_ctx.destroy();
} }
void imguiBeginFrame(int32_t _mx, void imguiBeginFrame(bgfx::ViewId _viewId)
int32_t _my,
uint8_t _button,
int32_t _scroll,
uint16_t _width,
uint16_t _height,
int _inputChar,
bgfx::ViewId _viewId)
{ {
s_ctx.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _viewId); s_ctx.beginFrame(_viewId);
} }
void imguiEndFrame() void imguiEndFrame()

View File

@@ -28,22 +28,9 @@ namespace bx
void imguiCreate(float _fontSize = 18.0f, bx::AllocatorI* _allocator = NULL); void imguiCreate(float _fontSize = 18.0f, bx::AllocatorI* _allocator = NULL);
void imguiDestroy(); void imguiDestroy();
void imguiBeginFrame(int32_t _mx, void imguiBeginFrame(bgfx::ViewId _view = 255);
int32_t _my,
uint8_t _button,
int32_t _scroll,
uint16_t _width,
uint16_t _height,
int _inputChar = -1,
bgfx::ViewId _view = 255);
void imguiEndFrame(); void imguiEndFrame();
namespace entry
{
class AppI;
}
void showExampleDialog(entry::AppI* _app, const char* _errorText = NULL);
namespace ImGui namespace ImGui
{ {
#define IMGUI_FLAGS_NONE UINT8_C(0x00) #define IMGUI_FLAGS_NONE UINT8_C(0x00)