improve imgui integration

This commit is contained in:
Daniel Gibson 2024-03-29 01:16:30 +01:00
parent c0c82e763a
commit 8fce796680
2 changed files with 86 additions and 38 deletions

View file

@ -1125,7 +1125,10 @@ sysEvent_t Sys_GetEvent() {
// loop until there is an event we care about (will return then) or no more events // loop until there is an event we care about (will return then) or no more events
while(SDL_PollEvent(&ev)) { while(SDL_PollEvent(&ev)) {
D3::ImGuiHooks::ProcessEvent(&ev); if(D3::ImGuiHooks::ProcessEvent(&ev)) {
// ImGui has used the event, so it shouldn't also be handled by the game
continue;
}
switch (ev.type) { switch (ev.type) {
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)

View file

@ -10,6 +10,7 @@
#include "framework/Common.h" #include "framework/Common.h"
#include "renderer/qgl.h" #include "renderer/qgl.h"
#include "renderer/tr_local.h" // glconfig
namespace D3 { namespace D3 {
@ -21,11 +22,6 @@ static bool haveNewFrame = false;
// using void* instead of SDL_Window and SDL_GLContext to avoid dragging SDL headers into sys_imgui.h // using void* instead of SDL_Window and SDL_GLContext to avoid dragging SDL headers into sys_imgui.h
bool Init(void* sdlWindow, void* sdlGlContext) bool Init(void* sdlWindow, void* sdlGlContext)
{ {
SDL_Window* win = (SDL_Window*)sdlWindow;
SDL_GL_MakeCurrent(win, (SDL_GLContext)sdlGlContext); // XXX: really?
//SDL_GLContext glctx = (SDL_GLContext)sdlGlContext;
SDL_GL_SetSwapInterval(1); // XXX: really?
common->Printf( "Initializing ImGui\n" ); common->Printf( "Initializing ImGui\n" );
// Setup Dear ImGui context // Setup Dear ImGui context
@ -42,9 +38,22 @@ bool Init(void* sdlWindow, void* sdlGlContext)
// Setup Dear ImGui style // Setup Dear ImGui style
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
//ImGui::StyleColorsLight(); //ImGui::StyleColorsLight();
//ImGui::StyleColorsClassic();
// make it a bit prettier with rounded edges
ImGuiStyle& style = ImGui::GetStyle();
style.WindowRounding = 2.0f;
style.FrameRounding = 3.0f;
//style.ChildRounding = 6.0f;
style.ScrollbarRounding = 8.0f;
style.GrabRounding = 1.0f;
style.PopupRounding = 2.0f;
ImVec4* colors = style.Colors;
colors[ImGuiCol_TitleBg] = ImVec4(0.28f, 0.36f, 0.48f, 0.88f);
// Setup Platform/Renderer backends // Setup Platform/Renderer backends
if (!ImGui_ImplSDL2_InitForOpenGL(win, sdlGlContext)) { if ( ! ImGui_ImplSDL2_InitForOpenGL( (SDL_Window*)sdlWindow, sdlGlContext ) ) {
ImGui::DestroyContext( imguiCtx ); ImGui::DestroyContext( imguiCtx );
imguiCtx = NULL; imguiCtx = NULL;
common->Warning( "Failed to initialize ImGui SDL platform backend!\n" ); common->Warning( "Failed to initialize ImGui SDL platform backend!\n" );
@ -136,9 +145,37 @@ void NewFrame()
} }
} }
// returns true if ImGui has handled the event and it shouldn't be passed to the game
bool ProcessEvent(const void* sdlEvent) bool ProcessEvent(const void* sdlEvent)
{ {
return ImGui_ImplSDL2_ProcessEvent( (const SDL_Event*)sdlEvent ); const SDL_Event* ev = (const SDL_Event*)sdlEvent;
// ImGui_ImplSDL2_ProcessEvent() doc says:
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
if( ImGui_ImplSDL2_ProcessEvent( ev ) ) {
ImGuiIO& io = ImGui::GetIO();
if ( io.WantCaptureMouse ) {
switch( ev->type ) {
case SDL_MOUSEMOTION:
case SDL_MOUSEWHEEL:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
return true;
}
}
if ( io.WantCaptureKeyboard ) {
switch( ev->type ) {
case SDL_TEXTINPUT:
case SDL_KEYDOWN:
case SDL_KEYUP:
return true;
}
}
}
return false;
} }
void EndFrame() void EndFrame()
@ -149,32 +186,40 @@ void EndFrame()
} }
haveNewFrame = false; haveNewFrame = false;
ImGui::Render(); ImGui::Render();
ImGuiIO& io = ImGui::GetIO();
//qglViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
//ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
//qglClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
//qglClear(GL_COLOR_BUFFER_BIT);
// Doom3 uses the OpenGL ARB shader extensions, for most things it renders.
// disable those shaders, the OpenGL classic integration of ImGui doesn't use shaders
qglDisable( GL_VERTEX_PROGRAM_ARB ); qglDisable( GL_VERTEX_PROGRAM_ARB );
qglDisable( GL_FRAGMENT_PROGRAM_ARB ); qglDisable( GL_FRAGMENT_PROGRAM_ARB );
GLint curArrayBuffer = 0, curElemBuffer = 0; // Doom3 uses OpenGL's ARB_vertex_buffer_object extension to use VBOs on the GPU
// as buffers for glDrawElements() (instead of passing userspace buffers to that function)
// ImGui however uses userspace buffers, so remember the currently bound VBO
// and unbind it (after drawing, bind it again)
GLint curArrayBuffer = 0;
if ( glConfig.ARBVertexBufferObjectAvailable ) {
qglGetIntegerv( GL_ARRAY_BUFFER_BINDING_ARB, &curArrayBuffer ); qglGetIntegerv( GL_ARRAY_BUFFER_BINDING_ARB, &curArrayBuffer );
qglGetIntegerv( GL_ELEMENT_ARRAY_BUFFER_BINDING_ARB, &curElemBuffer );
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); qglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 ); }
// disable all texture units, ImGui_ImplOpenGL2_RenderDrawData() will enable texture 0
// and bind its own textures to it as needed
for ( int i = glConfig.maxTextureUnits - 1 ; i >= 0 ; i-- ) {
GL_SelectTexture( i );
qglDisable( GL_TEXTURE_2D );
if ( glConfig.texture3DAvailable ) {
qglDisable( GL_TEXTURE_3D );
}
if ( glConfig.cubeMapAvailable ) {
qglDisable( GL_TEXTURE_CUBE_MAP_EXT );
}
}
ImDrawData* drawData = ImGui::GetDrawData(); ImGui_ImplOpenGL2_RenderDrawData( ImGui::GetDrawData() );
ImGui_ImplOpenGL2_RenderDrawData( drawData );
if ( curArrayBuffer != 0 ) { if ( curArrayBuffer != 0 ) {
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, curArrayBuffer ); qglBindBufferARB( GL_ARRAY_BUFFER_ARB, curArrayBuffer );
} }
if( curElemBuffer != 0 ) {
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, curElemBuffer );
}
} }
}} //namespace D3::ImGuiHooks }} //namespace D3::ImGuiHooks