This commit is contained in:
Asuro
2025-02-20 23:46:10 +01:00
parent e568363ca7
commit 43d7d2d012
7 changed files with 67 additions and 53 deletions

View File

@@ -21,9 +21,9 @@ file(GLOB imgui_backend_sdl
# Engine # Engine
file(GLOB_RECURSE sources_engine engine/*.cpp engine/*.h) 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) 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) #file(COPY ${data} DESTINATION resources)
#target_compile_options(PuzGameEngine PUBLIC xyz) #target_compile_options(PuzGameEngine PUBLIC xyz)
@@ -32,7 +32,7 @@ target_include_directories(PuzGameEngine PUBLIC dependency/imgui)
# Game # Game
file(GLOB_RECURSE sources_game game/*.cpp game/*.h) 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) 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) set_property(TARGET PuzGame PROPERTY CXX_STANDARD 17)
target_include_directories(PuzGame PUBLIC dependency/imgui) target_include_directories(PuzGame PUBLIC dependency/imgui)
@@ -45,6 +45,6 @@ SET(BGFX_BUILD_EXAMPLES OFF)
add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/bgfx.cmake") add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/bgfx.cmake")
# Link # Link
target_link_libraries(PuzGame bx bimg bgfx) target_link_libraries(PuzGame bx bimg bgfx SDL3::SDL3)
target_link_libraries(PuzGameEngine bx SDL3::SDL3) target_link_libraries(PuzGameEngine bx SDL3::SDL3)
set_target_properties(PuzGame PROPERTIES OUTPUT_NAME "PuzGame2") set_target_properties(PuzGame PROPERTIES OUTPUT_NAME "PuzGame2")

View File

@@ -63,8 +63,14 @@ struct PerfCounter
} }
}; };
class SDL_Window;
union SDL_Event;
struct SharedWindowData struct SharedWindowData
{ {
SDL_Window* SDLWindow = nullptr;
SDL_Event* SDLEvents{nullptr};
uint16_t SDLEventCount = 0;
void* Handle = nullptr; void* Handle = nullptr;
int32_t WindowWidth = 1920; int32_t WindowWidth = 1920;
int32_t WindowHeight = 1080; int32_t WindowHeight = 1080;

View File

@@ -3,9 +3,7 @@
#include "Shared.h" #include "Shared.h"
#include "Window.h" #include "Window.h"
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <backends/imgui_impl_sdl3.h>
#include <cstdio> #include <cstdio>
#include <imgui.h>
void EngineWindow::Startup(SharedWindowData& shared) void EngineWindow::Startup(SharedWindowData& shared)
{ {
@@ -29,29 +27,28 @@ void EngineWindow::Startup(SharedWindowData& shared)
printf("Failed to get window pointer!\n"); printf("Failed to get window pointer!\n");
return; return;
} }
shared.SDLWindow = Window;
// SDL_SetWindowRelativeMouseMode(Window, true); // 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) void EngineWindow::Update(SharedWindowData& shared)
{ {
START_PERF(); START_PERF();
shared.SDLEvents = &EventBuffer[0];
SDL_Event evt; SDL_Event evt;
while (SDL_PollEvent(&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) switch (evt.type)
{ {
case SDL_EVENT_WINDOW_RESIZED: case SDL_EVENT_WINDOW_RESIZED:

View File

@@ -4,9 +4,12 @@
class EngineWindow class EngineWindow
{ {
static constexpr uint32_t MaxEventBufferSize = 16;
public: public:
SDL_Window* Window; SDL_Window* Window;
bool CloseRequested = false; bool CloseRequested = false;
SDL_Event EventBuffer[MaxEventBufferSize];
public: public:
void Startup(SharedWindowData& Shared); void Startup(SharedWindowData& Shared);

View File

@@ -5,6 +5,8 @@
#include "../Mesh.h" #include "../Mesh.h"
#include "Rendering.h" #include "Rendering.h"
#include "SDL3/SDL_events.h"
#include "backends/imgui_impl_sdl3.h"
#include "bgfx/defines.h" #include "bgfx/defines.h"
#include "bx/filepath.h" #include "bx/filepath.h"
#include "bx/math.h" #include "bx/math.h"
@@ -228,6 +230,21 @@ namespace Game
Materials[0] = Material::LoadFromShader("vert", "frag", Textures[0].Handle, Textures[0].SamplerHandle); Materials[0] = Material::LoadFromShader("vert", "frag", Textures[0].Handle, Textures[0].SamplerHandle);
imguiCreate(); 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) if (!GetInstance().IsInitialized)
{ {
GetInstance().Time.StartTime = bx::getHPCounter(); GetInstance().Time.StartTime = bx::getHPCounter();
@@ -236,10 +253,19 @@ namespace Game
void GameRendering::Update() void GameRendering::Update()
{ {
imguiBeginFrame(0, 0, 0, 0, 100, 100);
ImGui::ShowDemoWindow();
SharedData& shared = GetShared(); 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 // Reload shaders if necessary
FileChangeNotification* shaderChange = nullptr; FileChangeNotification* shaderChange = nullptr;
if (shared.Dev.ChangedShaderCount > 0) if (shared.Dev.ChangedShaderCount > 0)
@@ -305,6 +331,12 @@ namespace Game
} }
imguiEndFrame(); imguiEndFrame();
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}
START_PERF(); START_PERF();
bgfx::frame(); bgfx::frame();
END_PERF(shared.Window.PerfCounters, PerfCounterType::Submit, shared.Window.FrameCounter); END_PERF(shared.Window.PerfCounters, PerfCounterType::Submit, shared.Window.FrameCounter);
@@ -312,6 +344,7 @@ namespace Game
void GameRendering::Shutdown() void GameRendering::Shutdown()
{ {
ImGui_ImplSDL3_Shutdown();
imguiDestroy(); imguiDestroy();
bgfx::shutdown(); bgfx::shutdown();
} }

View File

@@ -3,6 +3,8 @@
#include <bx/string.h> #include <bx/string.h>
#include <cstdint> #include <cstdint>
union SDL_Event;
namespace Game namespace Game
{ {
struct PosColorVertex struct PosColorVertex

View File

@@ -322,43 +322,16 @@ struct OcornutImguiContext
{ {
m_viewId = _viewId; m_viewId = _viewId;
ImGuiIO& io = ImGui::GetIO(); // ImGuiIO& io = ImGui::GetIO();
if (_inputChar >= 0) // io.DisplaySize = ImVec2((float)_width, (float)_height);
{
io.AddInputCharacter(_inputChar);
}
io.DisplaySize = ImVec2((float)_width, (float)_height);
const int64_t now = bx::getHPCounter(); const int64_t now = bx::getHPCounter();
const int64_t frameTime = now - m_last; const int64_t frameTime = now - m_last;
m_last = now; m_last = now;
const double freq = double(bx::getHPFrequency()); // const double freq = double(bx::getHPFrequency());
io.DeltaTime = float(frameTime / freq); // 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
ImGui::NewFrame(); ImGui::NewFrame();
// ImGuizmo::BeginFrame();
} }
void endFrame() void endFrame()