From ab7e91b65eb9e823006ccb16058b8fc5d515b123 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 16 Nov 2015 22:12:18 +0100 Subject: [PATCH] Examples: SDL+OpenGL3: Various fixes to example to make it matches current GL3 example (#356) --- .../imgui_impl_sdl_gl3.cpp | 85 ++++++++++--------- .../sdl_opengl3_example/imgui_impl_sdl_gl3.h | 5 +- examples/sdl_opengl3_example/main.cpp | 2 +- 3 files changed, 48 insertions(+), 44 deletions(-) diff --git a/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp index fdf3ff5a2..3ba31798b 100644 --- a/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp +++ b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp @@ -1,11 +1,16 @@ // ImGui SDL2 binding with OpenGL3 +// You can copy and use unmodified imgui_impl_* files in your project. +// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown(). +// See main.cpp for an example of using this. // https://github.com/ocornut/imgui +#include "imgui.h" +#include "imgui_impl_sdl_gl3.h" + +// SDL,GL3W #include #include #include -#include "imgui.h" -#include "imgui_impl_sdl_gl3.h" // Data static SDL_Window* g_Window = NULL; @@ -33,6 +38,7 @@ void ImGui_ImplSdlGL3_RenderDrawLists(ImDrawData* draw_data) GLint last_blend_dst; glGetIntegerv(GL_BLEND_DST, &last_blend_dst); GLint last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, &last_blend_equation_rgb); GLint last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, &last_blend_equation_alpha); + GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport); GLboolean last_enable_blend = glIsEnabled(GL_BLEND); GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE); GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST); @@ -53,12 +59,13 @@ void ImGui_ImplSdlGL3_RenderDrawLists(ImDrawData* draw_data) draw_data->ScaleClipRects(io.DisplayFramebufferScale); // Setup orthographic projection matrix + glViewport(0, 0, (GLsizei)io.DisplaySize.x, (GLsizei)io.DisplaySize.y); const float ortho_projection[4][4] = { - { 2.0f / io.DisplaySize.x, 0.0f, 0.0f, 0.0f }, - { 0.0f, 2.0f / -io.DisplaySize.y, 0.0f, 0.0f }, + { 2.0f/io.DisplaySize.x, 0.0f, 0.0f, 0.0f }, + { 0.0f, 2.0f/-io.DisplaySize.y, 0.0f, 0.0f }, { 0.0f, 0.0f, -1.0f, 0.0f }, - { -1.0f, 1.0f, 0.0f, 1.0f }, + {-1.0f, 1.0f, 0.0f, 1.0f }, }; glUseProgram(g_ShaderHandle); glUniform1i(g_AttribLocationTex, 0); @@ -86,7 +93,7 @@ void ImGui_ImplSdlGL3_RenderDrawLists(ImDrawData* draw_data) { glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId); glScissor((int)pcmd->ClipRect.x, (int)(fb_height - pcmd->ClipRect.w), (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y)); - glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, GL_UNSIGNED_SHORT, idx_buffer_offset); + glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset); } idx_buffer_offset += pcmd->ElemCount; } @@ -104,6 +111,7 @@ void ImGui_ImplSdlGL3_RenderDrawLists(ImDrawData* draw_data) if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST); + glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]); } static const char* ImGui_ImplSdlGL3_GetClipboardText() @@ -165,7 +173,7 @@ void ImGui_ImplSdlGL3_CreateFontsTexture() int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits for OpenGL3 demo because it is more likely to be compatible with user's existing shader. - // Create OpenGL texture + // Create OpenGL texture glGenTextures(1, &g_FontTexture); glBindTexture(GL_TEXTURE_2D, g_FontTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); @@ -259,12 +267,28 @@ bool ImGui_ImplSdlGL3_CreateDeviceObjects() void ImGui_ImplSdlGL3_InvalidateDeviceObjects() { - if (g_FontTexture) - { - glDeleteTextures(1, &g_FontTexture); - ImGui::GetIO().Fonts->TexID = 0; - g_FontTexture = 0; - } + if (g_VaoHandle) glDeleteVertexArrays(1, &g_VaoHandle); + if (g_VboHandle) glDeleteBuffers(1, &g_VboHandle); + if (g_ElementsHandle) glDeleteBuffers(1, &g_ElementsHandle); + g_VaoHandle = g_VboHandle = g_ElementsHandle = 0; + + glDetachShader(g_ShaderHandle, g_VertHandle); + glDeleteShader(g_VertHandle); + g_VertHandle = 0; + + glDetachShader(g_ShaderHandle, g_FragHandle); + glDeleteShader(g_FragHandle); + g_FragHandle = 0; + + glDeleteProgram(g_ShaderHandle); + g_ShaderHandle = 0; + + if (g_FontTexture) + { + glDeleteTextures(1, &g_FontTexture); + ImGui::GetIO().Fonts->TexID = 0; + g_FontTexture = 0; + } } bool ImGui_ImplSdlGL3_Init(SDL_Window *window) @@ -308,32 +332,11 @@ bool ImGui_ImplSdlGL3_Init(SDL_Window *window) void ImGui_ImplSdlGL3_Shutdown() { - if (g_VaoHandle) glDeleteVertexArrays(1, &g_VaoHandle); - if (g_VboHandle) glDeleteBuffers(1, &g_VboHandle); - if (g_ElementsHandle) glDeleteBuffers(1, &g_ElementsHandle); - g_VaoHandle = g_VboHandle = g_ElementsHandle = 0; - - glDetachShader(g_ShaderHandle, g_VertHandle); - glDeleteShader(g_VertHandle); - g_VertHandle = 0; - - glDetachShader(g_ShaderHandle, g_FragHandle); - glDeleteShader(g_FragHandle); - g_FragHandle = 0; - - glDeleteProgram(g_ShaderHandle); - g_ShaderHandle = 0; - - if (g_FontTexture) - { - glDeleteTextures(1, &g_FontTexture); - ImGui::GetIO().Fonts->TexID = 0; - g_FontTexture = 0; - } + ImGui_ImplSdlGL3_InvalidateDeviceObjects(); ImGui::Shutdown(); } -void ImGui_ImplSdlGL3_NewFrame(SDL_Window *window) +void ImGui_ImplSdlGL3_NewFrame() { if (!g_FontTexture) ImGui_ImplSdlGL3_CreateDeviceObjects(); @@ -342,11 +345,9 @@ void ImGui_ImplSdlGL3_NewFrame(SDL_Window *window) // Setup display size (every frame to accommodate for window resizing) int w, h; - int display_w, display_h; - SDL_GetWindowSize(window, &w, &h); - SDL_GetWindowSize(window, &display_w, &display_h); + SDL_GetWindowSize(g_Window, &w, &h); io.DisplaySize = ImVec2((float)w, (float)h); - io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h); + io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f); // Setup time step Uint32 time = SDL_GetTicks(); @@ -358,7 +359,7 @@ void ImGui_ImplSdlGL3_NewFrame(SDL_Window *window) // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents()) int mx, my; Uint32 mouseMask = SDL_GetMouseState(&mx, &my); - if (SDL_GetWindowFlags(window) & SDL_WINDOW_MOUSE_FOCUS) + if (SDL_GetWindowFlags(g_Window) & SDL_WINDOW_MOUSE_FOCUS) io.MousePos = ImVec2((float)mx, (float)my); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.) else io.MousePos = ImVec2(-1, -1); @@ -376,4 +377,4 @@ void ImGui_ImplSdlGL3_NewFrame(SDL_Window *window) // Start the frame ImGui::NewFrame(); -} \ No newline at end of file +} diff --git a/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h index e324b3da4..ef4757cce 100644 --- a/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h +++ b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h @@ -1,4 +1,7 @@ // ImGui SDL2 binding with OpenGL3 +// You can copy and use unmodified imgui_impl_* files in your project. +// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown(). +// See main.cpp for an example of using this. // https://github.com/ocornut/imgui struct SDL_Window; @@ -6,7 +9,7 @@ typedef union SDL_Event SDL_Event; IMGUI_API bool ImGui_ImplSdlGL3_Init(SDL_Window *window); IMGUI_API void ImGui_ImplSdlGL3_Shutdown(); -IMGUI_API void ImGui_ImplSdlGL3_NewFrame(SDL_Window *window); +IMGUI_API void ImGui_ImplSdlGL3_NewFrame(); IMGUI_API bool ImGui_ImplSdlGL3_ProcessEvent(SDL_Event* event); // Use if you want to reset your rendering device without losing ImGui state. diff --git a/examples/sdl_opengl3_example/main.cpp b/examples/sdl_opengl3_example/main.cpp index 24c37dbe9..ca6406990 100644 --- a/examples/sdl_opengl3_example/main.cpp +++ b/examples/sdl_opengl3_example/main.cpp @@ -61,7 +61,7 @@ int main(int, char**) if (event.type == SDL_QUIT) done = true; } - ImGui_ImplSdlGL3_NewFrame(window); + ImGui_ImplSdlGL3_NewFrame(); // 1. Show a simple window // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"