@ -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,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_IN T, 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 ( w indow) & SDL_WINDOW_MOUSE_FOCUS )
if ( SDL_GetWindowFlags ( g_W indow) & 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 ( ) ;
}
}