From 9d0caa2e6649cc7f058f5d684251109b8d9a4f59 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 16 Nov 2015 22:05:22 +0100 Subject: [PATCH] Examples: SDL+OpenGL3: Using glew like existing example + renaming (#356) --- examples/sdl_opengl3_example/README.md | 24 ++++++++++--- ...mpl_sdlogl3.cpp => imgui_impl_sdl_gl3.cpp} | 34 +++++++++---------- .../sdl_opengl3_example/imgui_impl_sdl_gl3.h | 14 ++++++++ .../sdl_opengl3_example/imgui_impl_sdlogl3.h | 14 -------- examples/sdl_opengl3_example/main.cpp | 15 ++++---- 5 files changed, 58 insertions(+), 43 deletions(-) rename examples/sdl_opengl3_example/{imgui_impl_sdlogl3.cpp => imgui_impl_sdl_gl3.cpp} (93%) create mode 100644 examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h delete mode 100644 examples/sdl_opengl3_example/imgui_impl_sdlogl3.h diff --git a/examples/sdl_opengl3_example/README.md b/examples/sdl_opengl3_example/README.md index f0b83b242..a0d94cfee 100644 --- a/examples/sdl_opengl3_example/README.md +++ b/examples/sdl_opengl3_example/README.md @@ -1,8 +1,22 @@ # How to Build -Link -=== -OpenGL -SDL2 -GLEW \ No newline at end of file +- On Windows with Visual Studio's CLI + +``` +set SDL2DIR=path_to_your_sdl2_folder +cl /Zi /MD /I ..\.. /I ..\libs\gl3w /I %SDL2DIR%\include main.cpp imgui_impl_sdl_gl3.cpp ..\..\imgui*.cpp ..\libs\gl3w\GL\gl3w.c /link /libpath:%SDL2DIR%\lib\x86 SDL2.lib SDL2main.lib opengl32.lib /subsystem:console +``` + +- On Linux and similar Unixes + +``` +c++ `sdl2-config --cflags` -I ../.. -I ../libs/gl3w main.cpp imgui_impl_sdl.cpp ../../imgui*.cpp ../libs/gl3w/GL/gl3w.c `sdl2-config --libs` -lGL -o sdl2example +``` + +- On Mac OS X + +``` +brew install sdl2 +c++ `sdl2-config --cflags` -I ../.. -I ../libs/gl3w main.cpp imgui_impl_sdl.cpp ../../imgui*.cpp ../libs/gl3w/GL/gl3w.c `sdl2-config --libs` -framework OpenGl -o sdl2example +``` diff --git a/examples/sdl_opengl3_example/imgui_impl_sdlogl3.cpp b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp similarity index 93% rename from examples/sdl_opengl3_example/imgui_impl_sdlogl3.cpp rename to examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp index 16a773f9e..fdf3ff5a2 100644 --- a/examples/sdl_opengl3_example/imgui_impl_sdlogl3.cpp +++ b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp @@ -3,9 +3,9 @@ #include #include -#include +#include #include "imgui.h" -#include "imgui_impl_sdlogl3.h" +#include "imgui_impl_sdl_gl3.h" // Data static SDL_Window* g_Window = NULL; @@ -21,7 +21,7 @@ static unsigned int g_VboHandle = 0, g_VaoHandle = 0, g_ElementsHandle = 0; // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure) // If text or lines are blurry when integrating ImGui in your engine: // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f) -void ImGui_ImplSdlOgl3_RenderDrawLists(ImDrawData* draw_data) +void ImGui_ImplSdlGL3_RenderDrawLists(ImDrawData* draw_data) { // Backup GL state GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program); @@ -106,17 +106,17 @@ void ImGui_ImplSdlOgl3_RenderDrawLists(ImDrawData* draw_data) if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST); } -static const char* ImGui_ImplSdlOgl3_GetClipboardText() +static const char* ImGui_ImplSdlGL3_GetClipboardText() { return SDL_GetClipboardText(); } -static void ImGui_ImplSdlOgl3_SetClipboardText(const char* text) +static void ImGui_ImplSdlGL3_SetClipboardText(const char* text) { SDL_SetClipboardText(text); } -bool ImGui_ImplSdlOgl3_ProcessEvent(SDL_Event* event) +bool ImGui_ImplSdlGL3_ProcessEvent(SDL_Event* event) { ImGuiIO& io = ImGui::GetIO(); switch (event->type) @@ -156,7 +156,7 @@ bool ImGui_ImplSdlOgl3_ProcessEvent(SDL_Event* event) return false; } -void ImGui_ImplSdlOgl3_CreateFontsTexture() +void ImGui_ImplSdlGL3_CreateFontsTexture() { ImGuiIO& io = ImGui::GetIO(); @@ -180,7 +180,7 @@ void ImGui_ImplSdlOgl3_CreateFontsTexture() io.Fonts->ClearTexData(); } -bool ImGui_ImplSdlOgl3_CreateDeviceObjects() +bool ImGui_ImplSdlGL3_CreateDeviceObjects() { // Backup GL state GLint last_texture, last_array_buffer, last_vertex_array; @@ -247,7 +247,7 @@ bool ImGui_ImplSdlOgl3_CreateDeviceObjects() glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, col)); #undef OFFSETOF - ImGui_ImplSdlOgl3_CreateFontsTexture(); + ImGui_ImplSdlGL3_CreateFontsTexture(); // Restore modified GL state glBindTexture(GL_TEXTURE_2D, last_texture); @@ -257,7 +257,7 @@ bool ImGui_ImplSdlOgl3_CreateDeviceObjects() return true; } -void ImGui_ImplSdlOgl3_InvalidateDeviceObjects() +void ImGui_ImplSdlGL3_InvalidateDeviceObjects() { if (g_FontTexture) { @@ -267,7 +267,7 @@ void ImGui_ImplSdlOgl3_InvalidateDeviceObjects() } } -bool ImGui_ImplSdlOgl3_Init(SDL_Window *window) +bool ImGui_ImplSdlGL3_Init(SDL_Window *window) { g_Window = window; @@ -292,9 +292,9 @@ bool ImGui_ImplSdlOgl3_Init(SDL_Window *window) io.KeyMap[ImGuiKey_Y] = SDLK_y; io.KeyMap[ImGuiKey_Z] = SDLK_z; - io.RenderDrawListsFn = ImGui_ImplSdlOgl3_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer. - io.SetClipboardTextFn = ImGui_ImplSdlOgl3_SetClipboardText; - io.GetClipboardTextFn = ImGui_ImplSdlOgl3_GetClipboardText; + io.RenderDrawListsFn = ImGui_ImplSdlGL3_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer. + io.SetClipboardTextFn = ImGui_ImplSdlGL3_SetClipboardText; + io.GetClipboardTextFn = ImGui_ImplSdlGL3_GetClipboardText; #ifdef _WIN32 SDL_SysWMinfo wmInfo; @@ -306,7 +306,7 @@ bool ImGui_ImplSdlOgl3_Init(SDL_Window *window) return true; } -void ImGui_ImplSdlOgl3_Shutdown() +void ImGui_ImplSdlGL3_Shutdown() { if (g_VaoHandle) glDeleteVertexArrays(1, &g_VaoHandle); if (g_VboHandle) glDeleteBuffers(1, &g_VboHandle); @@ -333,10 +333,10 @@ void ImGui_ImplSdlOgl3_Shutdown() ImGui::Shutdown(); } -void ImGui_ImplSdlOgl3_NewFrame(SDL_Window *window) +void ImGui_ImplSdlGL3_NewFrame(SDL_Window *window) { if (!g_FontTexture) - ImGui_ImplSdlOgl3_CreateDeviceObjects(); + ImGui_ImplSdlGL3_CreateDeviceObjects(); ImGuiIO& io = ImGui::GetIO(); diff --git a/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h new file mode 100644 index 000000000..e324b3da4 --- /dev/null +++ b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.h @@ -0,0 +1,14 @@ +// ImGui SDL2 binding with OpenGL3 +// https://github.com/ocornut/imgui + +struct SDL_Window; +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 bool ImGui_ImplSdlGL3_ProcessEvent(SDL_Event* event); + +// Use if you want to reset your rendering device without losing ImGui state. +IMGUI_API void ImGui_ImplSdlGL3_InvalidateDeviceObjects(); +IMGUI_API bool ImGui_ImplSdlGL3_CreateDeviceObjects(); diff --git a/examples/sdl_opengl3_example/imgui_impl_sdlogl3.h b/examples/sdl_opengl3_example/imgui_impl_sdlogl3.h deleted file mode 100644 index 812efce21..000000000 --- a/examples/sdl_opengl3_example/imgui_impl_sdlogl3.h +++ /dev/null @@ -1,14 +0,0 @@ -// ImGui SDL2 binding with OpenGL3 -// https://github.com/ocornut/imgui - -struct SDL_Window; -typedef union SDL_Event SDL_Event; - -IMGUI_API bool ImGui_ImplSdlOgl3_Init(SDL_Window *window); -IMGUI_API void ImGui_ImplSdlOgl3_Shutdown(); -IMGUI_API void ImGui_ImplSdlOgl3_NewFrame(SDL_Window *window); -IMGUI_API bool ImGui_ImplSdlOgl3_ProcessEvent(SDL_Event* event); - -// Use if you want to reset your rendering device without losing ImGui state. -IMGUI_API void ImGui_ImplSdlOgl3_InvalidateDeviceObjects(); -IMGUI_API bool ImGui_ImplSdlOgl3_CreateDeviceObjects(); diff --git a/examples/sdl_opengl3_example/main.cpp b/examples/sdl_opengl3_example/main.cpp index 0c5438f35..24c37dbe9 100644 --- a/examples/sdl_opengl3_example/main.cpp +++ b/examples/sdl_opengl3_example/main.cpp @@ -1,10 +1,10 @@ // ImGui - standalone example application for SDL2 + OpenGL #include -#include "imgui_impl_sdlogl3.h" +#include "imgui_impl_sdl_gl3.h" #include +#include #include -#include int main(int, char**) { @@ -23,11 +23,12 @@ int main(int, char**) SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_DisplayMode current; SDL_GetCurrentDisplayMode(0, ¤t); - SDL_Window *window = SDL_CreateWindow("ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); + SDL_Window *window = SDL_CreateWindow("ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); SDL_GLContext glcontext = SDL_GL_CreateContext(window); + gl3wInit(); // Setup ImGui binding - ImGui_ImplSdlOgl3_Init(window); + ImGui_ImplSdlGL3_Init(window); // Load Fonts // (see extra_fonts/README.txt for more details) @@ -56,11 +57,11 @@ int main(int, char**) SDL_Event event; while (SDL_PollEvent(&event)) { - ImGui_ImplSdlOgl3_ProcessEvent(&event); + ImGui_ImplSdlGL3_ProcessEvent(&event); if (event.type == SDL_QUIT) done = true; } - ImGui_ImplSdlOgl3_NewFrame(window); + ImGui_ImplSdlGL3_NewFrame(window); // 1. Show a simple window // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" @@ -99,7 +100,7 @@ int main(int, char**) } // Cleanup - ImGui_ImplSdlOgl3_Shutdown(); + ImGui_ImplSdlGL3_Shutdown(); SDL_GL_DeleteContext(glcontext); SDL_DestroyWindow(window); SDL_Quit();