Sfoglia il codice sorgente

Examples: OpenGL 2/3 examples cleanup to match DirectX9/11 structure.

ocornut 10 anni fa
parent
commit
5879f3f5ac

+ 15 - 14
examples/opengl3_example/imgui_impl_glfw_gl3.cpp

@@ -19,7 +19,7 @@ static GLFWwindow*  g_Window = NULL;
 static double       g_Time = 0.0f;
 static bool         g_MousePressed[3] = { false, false, false };
 static float        g_MouseWheel = 0.0f;
-static bool         g_FontTextureLoaded = false;
+static GLuint       g_FontTexture = 0;
 static int          g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
 static int          g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0;
 static int          g_AttribLocationPosition = 0, g_AttribLocationUV = 0, g_AttribLocationColor = 0;
@@ -145,7 +145,7 @@ void ImGui_ImplGlfwGL3_CharCallback(GLFWwindow* window, unsigned int c)
         io.AddInputCharacter((unsigned short)c);
 }
 
-void ImGui_ImplGlfwGL3_InitFontsTexture()
+void ImGui_ImplGlfwGL3_CreateFontsTexture()
 {
     ImGuiIO& io = ImGui::GetIO();
 
@@ -153,20 +153,17 @@ void ImGui_ImplGlfwGL3_InitFontsTexture()
     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.
 
-    GLuint tex_id;
-    glGenTextures(1, &tex_id);
-    glBindTexture(GL_TEXTURE_2D, tex_id);
+    glGenTextures(1, &g_FontTexture);
+    glBindTexture(GL_TEXTURE_2D, g_FontTexture);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
 
     // Store our identifier
-    io.Fonts->TexID = (void *)(intptr_t)tex_id;
-
-    g_FontTextureLoaded = true;
+    io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
 }
 
-static void InitGL()
+bool ImGui_ImplGlfwGL3_CreateDeviceObjects()
 {
     const GLchar *vertex_shader =
         "#version 330\n"
@@ -229,12 +226,15 @@ static void InitGL()
 #undef OFFSETOF
     glBindVertexArray(0);
     glBindBuffer(GL_ARRAY_BUFFER, 0);
+
+    ImGui_ImplGlfwGL3_CreateFontsTexture();
+
+    return true;
 }
 
 bool    ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks)
 {
     g_Window = window;
-    InitGL();
 
     ImGuiIO& io = ImGui::GetIO();
     io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;                 // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
@@ -291,18 +291,19 @@ void ImGui_ImplGlfwGL3_Shutdown()
     glDeleteProgram(g_ShaderHandle);
     g_ShaderHandle = 0;
 
-    if (GLuint tex_id = (GLuint)(intptr_t)ImGui::GetIO().Fonts->TexID)
+    if (g_FontTexture)
     {
-        glDeleteTextures(1, &tex_id);
+        glDeleteTextures(1, &g_FontTexture);
         ImGui::GetIO().Fonts->TexID = 0;
+        g_FontTexture = 0;
     }
     ImGui::Shutdown();
 }
 
 void ImGui_ImplGlfwGL3_NewFrame()
 {
-    if (!g_FontTextureLoaded)
-        ImGui_ImplGlfwGL3_InitFontsTexture();
+    if (!g_FontTexture)
+        ImGui_ImplGlfwGL3_CreateDeviceObjects();
 
     ImGuiIO& io = ImGui::GetIO();
 

+ 4 - 1
examples/opengl3_example/imgui_impl_glfw_gl3.h

@@ -4,10 +4,13 @@
 struct GLFWwindow;
 
 bool        ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks);
-void        ImGui_ImplGlfwGL3_InitFontsTexture();
 void        ImGui_ImplGlfwGL3_Shutdown();
 void        ImGui_ImplGlfwGL3_NewFrame();
 
+// Use if you want to reset your rendering device without losing ImGui state.
+void        ImGui_ImplGlfwGL3_InvalidateDeviceObjects();
+bool        ImGui_ImplGlfwGL3_CreateDeviceObjects();
+
 // GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization)
 // Provided here if you want to chain callbacks.
 // You can also handle inputs yourself and use those as a reference.

+ 0 - 1
examples/opengl3_example/main.cpp

@@ -32,7 +32,6 @@ int main(int argc, char** argv)
     //ImFont* my_font3 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyClean.ttf", 13.0f); my_font3->DisplayOffset.y += 1;
     //ImFont* my_font4 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyTiny.ttf", 10.0f); my_font4->DisplayOffset.y += 1;
     //ImFont* my_font5 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, io.Fonts->GetGlyphRangesJapanese());
-    ImGui_ImplGlfwGL3_InitFontsTexture();
 
     bool show_test_window = true;
     bool show_another_window = false;

+ 21 - 14
examples/opengl_example/imgui_impl_glfw.cpp

@@ -18,7 +18,7 @@ static GLFWwindow*  g_Window = NULL;
 static double       g_Time = 0.0f;
 static bool         g_MousePressed[3] = { false, false, false };
 static float        g_MouseWheel = 0.0f;
-static bool         g_FontTextureLoaded = false;
+static GLuint       g_FontTexture = 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:
@@ -125,25 +125,36 @@ void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c)
         io.AddInputCharacter((unsigned short)c);
 }
 
-void ImGui_ImplGlfw_InitFontsTexture()
+bool ImGui_ImplGlfw_CreateDeviceObjects()
 {
     ImGuiIO& io = ImGui::GetIO();
 
+    // Build texture
     unsigned char* pixels;
     int width, height;
     io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
 
-    GLuint tex_id;
-    glGenTextures(1, &tex_id);
-    glBindTexture(GL_TEXTURE_2D, tex_id);
+    // Create texture
+    glGenTextures(1, &g_FontTexture);
+    glBindTexture(GL_TEXTURE_2D, g_FontTexture);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
 
     // Store our identifier
-    io.Fonts->TexID = (void *)(intptr_t)tex_id;
+    io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
 
-    g_FontTextureLoaded = true;
+    return true;
+}
+
+void    ImGui_ImplGlfw_InvalidateDeviceObjects()
+{
+    if (g_FontTexture)
+    {
+        glDeleteTextures(1, &g_FontTexture);
+        ImGui::GetIO().Fonts->TexID = 0;
+        g_FontTexture = 0;
+    }
 }
 
 bool    ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks)
@@ -189,18 +200,14 @@ bool    ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks)
 
 void ImGui_ImplGlfw_Shutdown()
 {
-    if (GLuint tex_id = (GLuint)(intptr_t)ImGui::GetIO().Fonts->TexID)
-    {
-        glDeleteTextures(1, &tex_id);
-        ImGui::GetIO().Fonts->TexID = 0;
-    }
+    ImGui_ImplGlfw_InvalidateDeviceObjects();
     ImGui::Shutdown();
 }
 
 void ImGui_ImplGlfw_NewFrame()
 {
-    if (!g_FontTextureLoaded)
-        ImGui_ImplGlfw_InitFontsTexture();
+    if (!g_FontTexture)
+        ImGui_ImplGlfw_CreateDeviceObjects();
 
     ImGuiIO& io = ImGui::GetIO();
 

+ 4 - 1
examples/opengl_example/imgui_impl_glfw.h

@@ -4,10 +4,13 @@
 struct GLFWwindow;
 
 bool        ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks);
-void        ImGui_ImplGlfw_InitFontsTexture();
 void        ImGui_ImplGlfw_Shutdown();
 void        ImGui_ImplGlfw_NewFrame();
 
+// Use if you want to reset your rendering device without losing ImGui state.
+void        ImGui_ImplGlfw_InvalidateDeviceObjects();
+bool        ImGui_ImplGlfw_CreateDeviceObjects();
+
 // GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization)
 // Provided here if you want to chain callbacks.
 // You can also handle inputs yourself and use those as a reference.

+ 0 - 1
examples/opengl_example/main.cpp

@@ -27,7 +27,6 @@ int main(int argc, char** argv)
     //ImFont* my_font3 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyClean.ttf", 13.0f); my_font3->DisplayOffset.y += 1;
     //ImFont* my_font4 = io.Fonts->AddFontFromFileTTF("extra_fonts/ProggyTiny.ttf", 10.0f); my_font4->DisplayOffset.y += 1;
     //ImFont* my_font5 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, io.Fonts->GetGlyphRangesJapanese());
-    ImGui_ImplGlfw_InitFontsTexture();
 
     bool show_test_window = true;
     bool show_another_window = false;