Examples: SDL+OpenGL3: Various fixes to example to make it matches current GL3 example (#356)

This commit is contained in:
ocornut 2015-11-16 22:12:18 +01:00
parent 9d0caa2e66
commit ab7e91b65e
3 changed files with 48 additions and 44 deletions

View File

@ -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 <SDL.h>
#include <SDL_syswm.h>
#include <GL/gl3w.h>
#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,6 +59,7 @@ 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 },
@ -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()
@ -259,6 +267,22 @@ bool ImGui_ImplSdlGL3_CreateDeviceObjects()
void ImGui_ImplSdlGL3_InvalidateDeviceObjects()
{
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);
@ -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);

View File

@ -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.

View File

@ -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"