Examples: SDL+OpenGL3: Using glew like existing example + renaming (#356)

This commit is contained in:
ocornut 2015-11-16 22:05:22 +01:00
parent 624adb1b23
commit 9d0caa2e66
5 changed files with 58 additions and 43 deletions

View File

@ -1,8 +1,22 @@
# How to Build # How to Build
Link - On Windows with Visual Studio's CLI
===
OpenGL ```
SDL2 set SDL2DIR=path_to_your_sdl2_folder
GLEW 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
```

View File

@ -3,9 +3,9 @@
#include <SDL.h> #include <SDL.h>
#include <SDL_syswm.h> #include <SDL_syswm.h>
#include <GL/glew.h> #include <GL/gl3w.h>
#include "imgui.h" #include "imgui.h"
#include "imgui_impl_sdlogl3.h" #include "imgui_impl_sdl_gl3.h"
// Data // Data
static SDL_Window* g_Window = NULL; 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) // 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: // 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) // - 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 // Backup GL state
GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program); 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); 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(); return SDL_GetClipboardText();
} }
static void ImGui_ImplSdlOgl3_SetClipboardText(const char* text) static void ImGui_ImplSdlGL3_SetClipboardText(const char* text)
{ {
SDL_SetClipboardText(text); SDL_SetClipboardText(text);
} }
bool ImGui_ImplSdlOgl3_ProcessEvent(SDL_Event* event) bool ImGui_ImplSdlGL3_ProcessEvent(SDL_Event* event)
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
switch (event->type) switch (event->type)
@ -156,7 +156,7 @@ bool ImGui_ImplSdlOgl3_ProcessEvent(SDL_Event* event)
return false; return false;
} }
void ImGui_ImplSdlOgl3_CreateFontsTexture() void ImGui_ImplSdlGL3_CreateFontsTexture()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
@ -180,7 +180,7 @@ void ImGui_ImplSdlOgl3_CreateFontsTexture()
io.Fonts->ClearTexData(); io.Fonts->ClearTexData();
} }
bool ImGui_ImplSdlOgl3_CreateDeviceObjects() bool ImGui_ImplSdlGL3_CreateDeviceObjects()
{ {
// Backup GL state // Backup GL state
GLint last_texture, last_array_buffer, last_vertex_array; 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)); glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, col));
#undef OFFSETOF #undef OFFSETOF
ImGui_ImplSdlOgl3_CreateFontsTexture(); ImGui_ImplSdlGL3_CreateFontsTexture();
// Restore modified GL state // Restore modified GL state
glBindTexture(GL_TEXTURE_2D, last_texture); glBindTexture(GL_TEXTURE_2D, last_texture);
@ -257,7 +257,7 @@ bool ImGui_ImplSdlOgl3_CreateDeviceObjects()
return true; return true;
} }
void ImGui_ImplSdlOgl3_InvalidateDeviceObjects() void ImGui_ImplSdlGL3_InvalidateDeviceObjects()
{ {
if (g_FontTexture) 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; g_Window = window;
@ -292,9 +292,9 @@ bool ImGui_ImplSdlOgl3_Init(SDL_Window *window)
io.KeyMap[ImGuiKey_Y] = SDLK_y; io.KeyMap[ImGuiKey_Y] = SDLK_y;
io.KeyMap[ImGuiKey_Z] = SDLK_z; 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.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_ImplSdlOgl3_SetClipboardText; io.SetClipboardTextFn = ImGui_ImplSdlGL3_SetClipboardText;
io.GetClipboardTextFn = ImGui_ImplSdlOgl3_GetClipboardText; io.GetClipboardTextFn = ImGui_ImplSdlGL3_GetClipboardText;
#ifdef _WIN32 #ifdef _WIN32
SDL_SysWMinfo wmInfo; SDL_SysWMinfo wmInfo;
@ -306,7 +306,7 @@ bool ImGui_ImplSdlOgl3_Init(SDL_Window *window)
return true; return true;
} }
void ImGui_ImplSdlOgl3_Shutdown() void ImGui_ImplSdlGL3_Shutdown()
{ {
if (g_VaoHandle) glDeleteVertexArrays(1, &g_VaoHandle); if (g_VaoHandle) glDeleteVertexArrays(1, &g_VaoHandle);
if (g_VboHandle) glDeleteBuffers(1, &g_VboHandle); if (g_VboHandle) glDeleteBuffers(1, &g_VboHandle);
@ -333,10 +333,10 @@ void ImGui_ImplSdlOgl3_Shutdown()
ImGui::Shutdown(); ImGui::Shutdown();
} }
void ImGui_ImplSdlOgl3_NewFrame(SDL_Window *window) void ImGui_ImplSdlGL3_NewFrame(SDL_Window *window)
{ {
if (!g_FontTexture) if (!g_FontTexture)
ImGui_ImplSdlOgl3_CreateDeviceObjects(); ImGui_ImplSdlGL3_CreateDeviceObjects();
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();

View File

@ -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();

View File

@ -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();

View File

@ -1,10 +1,10 @@
// ImGui - standalone example application for SDL2 + OpenGL // ImGui - standalone example application for SDL2 + OpenGL
#include <imgui.h> #include <imgui.h>
#include "imgui_impl_sdlogl3.h" #include "imgui_impl_sdl_gl3.h"
#include <stdio.h> #include <stdio.h>
#include <GL/gl3w.h>
#include <SDL.h> #include <SDL.h>
#include <GL/glew.h>
int main(int, char**) int main(int, char**)
{ {
@ -23,11 +23,12 @@ int main(int, char**)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_DisplayMode current; SDL_DisplayMode current;
SDL_GetCurrentDisplayMode(0, &current); SDL_GetCurrentDisplayMode(0, &current);
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); SDL_GLContext glcontext = SDL_GL_CreateContext(window);
gl3wInit();
// Setup ImGui binding // Setup ImGui binding
ImGui_ImplSdlOgl3_Init(window); ImGui_ImplSdlGL3_Init(window);
// Load Fonts // Load Fonts
// (see extra_fonts/README.txt for more details) // (see extra_fonts/README.txt for more details)
@ -56,11 +57,11 @@ int main(int, char**)
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) while (SDL_PollEvent(&event))
{ {
ImGui_ImplSdlOgl3_ProcessEvent(&event); ImGui_ImplSdlGL3_ProcessEvent(&event);
if (event.type == SDL_QUIT) if (event.type == SDL_QUIT)
done = true; done = true;
} }
ImGui_ImplSdlOgl3_NewFrame(window); ImGui_ImplSdlGL3_NewFrame(window);
// 1. Show a simple 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" // 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 // Cleanup
ImGui_ImplSdlOgl3_Shutdown(); ImGui_ImplSdlGL3_Shutdown();
SDL_GL_DeleteContext(glcontext); SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();