diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c0d64ee..37a8903 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,9 +21,9 @@ file(GLOB imgui_backend_sdl # Engine file(GLOB_RECURSE sources_engine engine/*.cpp engine/*.h) -add_executable(PuzGameEngine ${sources_engine} ${imgui_sources} ${imgui_backend_sdl}) +add_executable(PuzGameEngine ${sources_engine}) set_property(TARGET PuzGameEngine PROPERTY CXX_STANDARD 17) -target_include_directories(PuzGameEngine PUBLIC dependency/imgui) +target_include_directories(PuzGameEngine PUBLIC) #file(COPY ${data} DESTINATION resources) #target_compile_options(PuzGameEngine PUBLIC xyz) @@ -32,7 +32,7 @@ target_include_directories(PuzGameEngine PUBLIC dependency/imgui) # Game file(GLOB_RECURSE sources_game game/*.cpp game/*.h) file(GLOB source_singleheader dependency/tinygltf/stb_image.h dependency/tinygltf/stb_image_write.h dependency/tinygltf/json.hpp dependency/tinygltf/tiny_gltf.h) -add_library(PuzGame SHARED ${sources_game} ${source_singleheader} ${imgui_sources}) +add_library(PuzGame SHARED ${sources_game} ${source_singleheader} ${imgui_sources} ${imgui_backend_sdl}) set_property(TARGET PuzGame PROPERTY CXX_STANDARD 17) target_include_directories(PuzGame PUBLIC dependency/imgui) @@ -45,6 +45,6 @@ SET(BGFX_BUILD_EXAMPLES OFF) add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/bgfx.cmake") # Link -target_link_libraries(PuzGame bx bimg bgfx) +target_link_libraries(PuzGame bx bimg bgfx SDL3::SDL3) target_link_libraries(PuzGameEngine bx SDL3::SDL3) set_target_properties(PuzGame PROPERTIES OUTPUT_NAME "PuzGame2") diff --git a/src/engine/Shared.h b/src/engine/Shared.h index e0cd99a..eeef812 100644 --- a/src/engine/Shared.h +++ b/src/engine/Shared.h @@ -63,8 +63,14 @@ struct PerfCounter } }; +class SDL_Window; +union SDL_Event; + struct SharedWindowData { + SDL_Window* SDLWindow = nullptr; + SDL_Event* SDLEvents{nullptr}; + uint16_t SDLEventCount = 0; void* Handle = nullptr; int32_t WindowWidth = 1920; int32_t WindowHeight = 1080; diff --git a/src/engine/Window.cpp b/src/engine/Window.cpp index 6cb5f41..8ec65d4 100644 --- a/src/engine/Window.cpp +++ b/src/engine/Window.cpp @@ -3,9 +3,7 @@ #include "Shared.h" #include "Window.h" #include -#include #include -#include void EngineWindow::Startup(SharedWindowData& shared) { @@ -29,29 +27,28 @@ void EngineWindow::Startup(SharedWindowData& shared) printf("Failed to get window pointer!\n"); return; } + shared.SDLWindow = Window; // SDL_SetWindowRelativeMouseMode(Window, true); - - IMGUI_CHECKVERSION(); - auto* imguiCtx = ImGui::CreateContext(); - if (imguiCtx == nullptr) - { - printf("Failed to set up imgui context!\n"); - return; - } - if (!ImGui_ImplSDL3_InitForVulkan(Window)) - { - printf("Failed to set up imgui implementation!\n"); - return; - } } void EngineWindow::Update(SharedWindowData& shared) { START_PERF(); + shared.SDLEvents = &EventBuffer[0]; + SDL_Event evt; while (SDL_PollEvent(&evt)) { - ImGui_ImplSDL3_ProcessEvent(&evt); + if (shared.SDLEventCount < MaxEventBufferSize) + { + EventBuffer[shared.SDLEventCount] = evt; + ++shared.SDLEventCount; + } + else + { + printf("Warning: Skipping SDL event!\n"); + } + switch (evt.type) { case SDL_EVENT_WINDOW_RESIZED: diff --git a/src/engine/Window.h b/src/engine/Window.h index 7924316..61e0cef 100644 --- a/src/engine/Window.h +++ b/src/engine/Window.h @@ -4,9 +4,12 @@ class EngineWindow { + static constexpr uint32_t MaxEventBufferSize = 16; + public: SDL_Window* Window; bool CloseRequested = false; + SDL_Event EventBuffer[MaxEventBufferSize]; public: void Startup(SharedWindowData& Shared); diff --git a/src/game/rendering/Rendering.cpp b/src/game/rendering/Rendering.cpp index fa86936..3c3fed9 100644 --- a/src/game/rendering/Rendering.cpp +++ b/src/game/rendering/Rendering.cpp @@ -5,6 +5,8 @@ #include "../Mesh.h" #include "Rendering.h" +#include "SDL3/SDL_events.h" +#include "backends/imgui_impl_sdl3.h" #include "bgfx/defines.h" #include "bx/filepath.h" #include "bx/math.h" @@ -228,6 +230,21 @@ namespace Game Materials[0] = Material::LoadFromShader("vert", "frag", Textures[0].Handle, Textures[0].SamplerHandle); imguiCreate(); + if (!ImGui_ImplSDL3_InitForOther(shared.Window.SDLWindow)) + { + Log("Failed to set up imgui implementation!"); + return; + } + + // ImGui::GetIO().BackendFlags |= ImGuiBackendFlags_RendererHasViewports; + ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable; + // ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; + // auto& platIO = ImGui::GetPlatformIO(); + // platIO.Renderer_CreateWindow = TODO; + // platIO.Platform_DestroyWindow = TODO; + // platIO.Platform_SetWindowSize = TODO; + // platIO.Platform_RenderWindow = TODO; + if (!GetInstance().IsInitialized) { GetInstance().Time.StartTime = bx::getHPCounter(); @@ -236,10 +253,19 @@ namespace Game void GameRendering::Update() { - imguiBeginFrame(0, 0, 0, 0, 100, 100); - ImGui::ShowDemoWindow(); SharedData& shared = GetShared(); + for (uint16_t i = 0; i < shared.Window.SDLEventCount; ++i) + { + ImGui_ImplSDL3_ProcessEvent(&shared.Window.SDLEvents[i]); + } + shared.Window.SDLEventCount = 0; + + ImGui_ImplSDL3_NewFrame(); + imguiBeginFrame(0, 0, 0, 0, shared.Window.WindowWidth, shared.Window.WindowHeight); + ImGui::DockSpaceOverViewport(); + ImGui::ShowDemoWindow(); + // Reload shaders if necessary FileChangeNotification* shaderChange = nullptr; if (shared.Dev.ChangedShaderCount > 0) @@ -305,6 +331,12 @@ namespace Game } imguiEndFrame(); + if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + ImGui::UpdatePlatformWindows(); + ImGui::RenderPlatformWindowsDefault(); + } + START_PERF(); bgfx::frame(); END_PERF(shared.Window.PerfCounters, PerfCounterType::Submit, shared.Window.FrameCounter); @@ -312,6 +344,7 @@ namespace Game void GameRendering::Shutdown() { + ImGui_ImplSDL3_Shutdown(); imguiDestroy(); bgfx::shutdown(); } diff --git a/src/game/rendering/Rendering.h b/src/game/rendering/Rendering.h index e0988a0..4237ad1 100644 --- a/src/game/rendering/Rendering.h +++ b/src/game/rendering/Rendering.h @@ -3,6 +3,8 @@ #include #include +union SDL_Event; + namespace Game { struct PosColorVertex diff --git a/src/game/rendering/imgui-helper.cpp b/src/game/rendering/imgui-helper.cpp index 84d4ffd..ba57594 100644 --- a/src/game/rendering/imgui-helper.cpp +++ b/src/game/rendering/imgui-helper.cpp @@ -322,43 +322,16 @@ struct OcornutImguiContext { m_viewId = _viewId; - ImGuiIO& io = ImGui::GetIO(); - if (_inputChar >= 0) - { - io.AddInputCharacter(_inputChar); - } - - io.DisplaySize = ImVec2((float)_width, (float)_height); + // 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); - - io.AddMousePosEvent((float)_mx, (float)_my); - io.AddMouseButtonEvent(ImGuiMouseButton_Left, 0 != (_button & IMGUI_MBUT_LEFT)); - io.AddMouseButtonEvent(ImGuiMouseButton_Right, 0 != (_button & IMGUI_MBUT_RIGHT)); - io.AddMouseButtonEvent(ImGuiMouseButton_Middle, 0 != (_button & IMGUI_MBUT_MIDDLE)); - io.AddMouseWheelEvent(0.0f, (float)(_scroll - m_lastScroll)); - m_lastScroll = _scroll; - -#if USE_ENTRY - uint8_t modifiers = inputGetModifiersState(); - io.AddKeyEvent(ImGuiMod_Shift, 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift))); - io.AddKeyEvent(ImGuiMod_Ctrl, 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl))); - io.AddKeyEvent(ImGuiMod_Alt, 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt))); - io.AddKeyEvent(ImGuiMod_Super, 0 != (modifiers & (entry::Modifier::LeftMeta | entry::Modifier::RightMeta))); - for (int32_t ii = 0; ii < (int32_t)entry::Key::Count; ++ii) - { - io.AddKeyEvent(m_keyMap[ii], inputGetKeyState(entry::Key::Enum(ii))); - io.SetKeyEventNativeData(m_keyMap[ii], 0, 0, ii); - } -#endif // USE_ENTRY + // const double freq = double(bx::getHPFrequency()); + // io.DeltaTime = float(frameTime / freq); ImGui::NewFrame(); - - // ImGuizmo::BeginFrame(); } void endFrame()