Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable the OpenGL2 backend to be used with multiple contexts #4141

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions backends/imgui_impl_opengl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,47 @@
#include <GL/gl.h>
#endif

// OpenGL Data
static GLuint g_FontTexture = 0;
struct ImGui_ImplOpenGL2
{
// OpenGL Data
GLuint FontTexture;
};

// Functions
bool ImGui_ImplOpenGL2_Init()
{
// Setup backend capabilities flags
ImGuiIO& io = ImGui::GetIO();

if (io.BackendRendererUserData)
return false;

ImGui_ImplOpenGL2* impl = (ImGui_ImplOpenGL2*)ImGui::MemAlloc(sizeof(ImGui_ImplOpenGL2));
io.BackendRendererName = "imgui_impl_opengl2";
io.BackendRendererUserData = impl;

impl->FontTexture = 0;

return true;
}

void ImGui_ImplOpenGL2_Shutdown()
{
ImGui_ImplOpenGL2_DestroyDeviceObjects();

ImGuiIO& io = ImGui::GetIO();
ImGui_ImplOpenGL2* impl = (ImGui_ImplOpenGL2*)io.BackendRendererUserData;
ImGui::MemFree(impl);
io.BackendPlatformName = NULL;
io.BackendPlatformUserData = NULL;
}

void ImGui_ImplOpenGL2_NewFrame()
{
if (!g_FontTexture)
ImGuiIO& io = ImGui::GetIO();
ImGui_ImplOpenGL2* impl = (ImGui_ImplOpenGL2*)io.BackendRendererUserData;

if (!impl->FontTexture)
ImGui_ImplOpenGL2_CreateDeviceObjects();
}

Expand Down Expand Up @@ -211,24 +232,26 @@ void ImGui_ImplOpenGL2_RenderDrawData(ImDrawData* draw_data)

bool ImGui_ImplOpenGL2_CreateFontsTexture()
{
// Build texture atlas
ImGuiIO& io = ImGui::GetIO();
ImGui_ImplOpenGL2* impl = (ImGui_ImplOpenGL2*)io.BackendRendererUserData;

// Build texture atlas
unsigned char* pixels;
int width, height;
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.

// Upload texture to graphics system
GLint last_texture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
glGenTextures(1, &g_FontTexture);
glBindTexture(GL_TEXTURE_2D, g_FontTexture);
glGenTextures(1, &impl->FontTexture);
glBindTexture(GL_TEXTURE_2D, impl->FontTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

// Store our identifier
io.Fonts->SetTexID((ImTextureID)(intptr_t)g_FontTexture);
io.Fonts->SetTexID((ImTextureID)(intptr_t)impl->FontTexture);

// Restore state
glBindTexture(GL_TEXTURE_2D, last_texture);
Expand All @@ -238,12 +261,14 @@ bool ImGui_ImplOpenGL2_CreateFontsTexture()

void ImGui_ImplOpenGL2_DestroyFontsTexture()
{
if (g_FontTexture)
ImGuiIO& io = ImGui::GetIO();
ImGui_ImplOpenGL2* impl = (ImGui_ImplOpenGL2*)io.BackendRendererUserData;

if (impl->FontTexture)
{
ImGuiIO& io = ImGui::GetIO();
glDeleteTextures(1, &g_FontTexture);
glDeleteTextures(1, &impl->FontTexture);
io.Fonts->SetTexID(0);
g_FontTexture = 0;
impl->FontTexture = 0;
}
}

Expand Down