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

(WIP) Dynamic glyph rasterization and unlimited number of font textures #3471

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8942a8d
Added dynamic glyph rasterization and unlimited number of font textures.
georgegerdin Sep 14, 2020
af65082
Some small fixes
georgegerdin Sep 14, 2020
6076638
Fixing some warnings from unused debug code.
georgegerdin Sep 14, 2020
2e247f7
Update README.md
georgegerdin Sep 14, 2020
97f197f
Added FIXME-DYNAMICFONT comments in places where things needs to be f…
georgegerdin Sep 18, 2020
9f0cdc5
Added support for CustomRects and software mouse cursor.
georgegerdin Sep 18, 2020
634745d
Added textured lines and FindGlyphNoFallback.
georgegerdin Sep 19, 2020
b8b5c71
Added support for SDL backend.
georgegerdin Sep 25, 2020
58b51b1
Added support for freetype (link with freetype libraries and add a gl…
georgegerdin Sep 25, 2020
fe1fbb9
Some minor fixes...
georgegerdin Sep 25, 2020
986738c
Fixed flicker issues. FindGlyph can now provide a backup glyph if the…
georgegerdin Sep 27, 2020
4133a5e
In this update only one font texture is used for the majority of the
georgegerdin Jul 14, 2021
a049652
Merge remote-tracking branch 'upstream/master'
georgegerdin Jul 18, 2021
9a38be3
Merge branch 'master' of https://github.com/ocornut/imgui
georgegerdin Jul 20, 2021
ba975a2
Added DirectX9 support.
georgegerdin Jul 20, 2021
068df22
Added support for DirectX 10.
georgegerdin Jul 20, 2021
76b8d85
Added DirectX11 support.
georgegerdin Jul 21, 2021
2bf352f
Small changes and bug fixes.
georgegerdin Jul 23, 2021
a844ce9
Merge branch 'master' of https://github.com/ocornut/imgui into ocornu…
georgegerdin Jul 27, 2021
728979f
Merge branch 'master' of https://github.com/ocornut/imgui into ocorn…
georgegerdin Dec 17, 2021
d4d67ea
Merge branch 'master' of https://github.com/georgegerdin/imgui
georgegerdin Dec 17, 2021
137ec70
Merge branch 'master' of git://github.com/ocornut/imgui into ocornut-…
georgegerdin Dec 17, 2021
fa0b88f
Merge branch 'ocornut-master'
georgegerdin Dec 17, 2021
2d4d3e5
Merge v1.90.6
georgegerdin May 18, 2024
a6ebfef
Fix a bug that caused the columns to be generated erroneously.
georgegerdin May 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/README.md
@@ -1,3 +1,5 @@
Demonstration video: https://i.imgur.com/4Es7pUg.mp4

Dear ImGui
=====
[![Build Status](https://github.com/ocornut/imgui/workflows/build/badge.svg)](https://github.com/ocornut/imgui/actions?workflow=build) [![Static Analysis Status](https://github.com/ocornut/imgui/workflows/static-analysis/badge.svg)](https://github.com/ocornut/imgui/actions?workflow=static-analysis)
Expand Down
12 changes: 6 additions & 6 deletions examples/example_glfw_opengl2/main.cpp
Expand Up @@ -61,14 +61,14 @@ int main(int, char**)
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
//io.Fonts->AddFontDefault();
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
io.Fonts->AddFontDefault();
io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf");
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != NULL);

// Our state
bool show_demo_window = true;
bool show_another_window = false;
Expand Down
12 changes: 6 additions & 6 deletions examples/example_glfw_opengl3/main.cpp
Expand Up @@ -125,14 +125,14 @@ int main(int, char**)
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
//io.Fonts->AddFontDefault();
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
io.Fonts->AddFontDefault();
io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf"/*, 16.0f*/);
io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf"/*, 15.0f*/);
io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf"/*, 16.0f*/);
io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf"/*, 10.0f*/);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != NULL);

// Our state
bool show_demo_window = true;
bool show_another_window = false;
Expand Down
9 changes: 5 additions & 4 deletions examples/example_null/main.cpp
Expand Up @@ -11,10 +11,11 @@ int main(int, char**)
ImGuiIO& io = ImGui::GetIO();

// Build atlas
unsigned char* tex_pixels = NULL;
int tex_w, tex_h;
io.Fonts->GetTexDataAsRGBA32(&tex_pixels, &tex_w, &tex_h);

//unsigned char* tex_pixels = NULL;
//int tex_w, tex_h;
//io.Fonts->GetTexDataAsRGBA32(&tex_pixels, &tex_w, &tex_h);
io.Fonts->AddFontDefault();

for (int n = 0; n < 20; n++)
{
printf("NewFrame() %d\n", n);
Expand Down
10 changes: 5 additions & 5 deletions examples/example_sdl_opengl2/main.cpp
Expand Up @@ -59,11 +59,11 @@ int main(int, char**)
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
//io.Fonts->AddFontDefault();
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
io.Fonts->AddFontDefault();
io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf"/*, 16.0f*/);
io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf"/*, 15.0f*/);
io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf"/*, 16.0f*/);
io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf"/*, 10.0f*/);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != NULL);

Expand Down
10 changes: 5 additions & 5 deletions examples/example_sdl_opengl3/main.cpp
Expand Up @@ -120,11 +120,11 @@ int main(int, char**)
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
//io.Fonts->AddFontDefault();
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
io.Fonts->AddFontDefault();
io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf"/*, 16.0f*/);
io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf"/*, 15.0f*/);
io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf"/*, 16.0f*/);
io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf"/*, 10.0f*/);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != NULL);

Expand Down
9 changes: 7 additions & 2 deletions examples/imgui_impl_dx10.cpp
Expand Up @@ -265,9 +265,11 @@ void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data)
ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
}

//FIXME-DYNAMICFONT: Should be renamed ImGui_ImplDX10_UpdateFontsTextures
static void ImGui_ImplDX10_CreateFontsTexture()
{
// Build texture atlas
// FIXME-DYNAMICFONT: Dynamic font texture update code similar to imgui_impl_opengl3.cpp should be implemented for DirectX 10
/* // Build texture atlas
ImGuiIO& io = ImGui::GetIO();
unsigned char* pixels;
int width, height;
Expand Down Expand Up @@ -322,6 +324,7 @@ static void ImGui_ImplDX10_CreateFontsTexture()
desc.MaxLOD = 0.f;
g_pd3dDevice->CreateSamplerState(&desc, &g_pFontSampler);
}
*/
}

bool ImGui_ImplDX10_CreateDeviceObjects()
Expand Down Expand Up @@ -472,6 +475,7 @@ bool ImGui_ImplDX10_CreateDeviceObjects()
g_pd3dDevice->CreateDepthStencilState(&desc, &g_pDepthStencilState);
}

// FIXME-DYNAMICFONT: ImGui_ImplDX10_UpdateFontsTexture should replace this function and be called every frame
ImGui_ImplDX10_CreateFontsTexture();

return true;
Expand All @@ -483,7 +487,8 @@ void ImGui_ImplDX10_InvalidateDeviceObjects()
return;

if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; }
if (g_pFontTextureView) { g_pFontTextureView->Release(); g_pFontTextureView = NULL; ImGui::GetIO().Fonts->TexID = NULL; } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
// FIXME-DYNAMICFONT: After destroying a font texture the appropriate ImFontTexture->TexID should be updated
if (g_pFontTextureView) { g_pFontTextureView->Release(); g_pFontTextureView = NULL; /*ImGui::GetIO().Fonts->TexID = NULL;*/ } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }

Expand Down
8 changes: 5 additions & 3 deletions examples/imgui_impl_dx11.cpp
Expand Up @@ -277,9 +277,10 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
}

//FIXME-DYNAMICFONT: Should be replaced by ImGui_ImplDX11_UpdateFontTextures called every frame similiar to ImGui_OpenGL3_UpdateFontTextures
static void ImGui_ImplDX11_CreateFontsTexture()
{
// Build texture atlas
/* // Build texture atlas
ImGuiIO& io = ImGui::GetIO();
unsigned char* pixels;
int width, height;
Expand Down Expand Up @@ -333,7 +334,7 @@ static void ImGui_ImplDX11_CreateFontsTexture()
desc.MinLOD = 0.f;
desc.MaxLOD = 0.f;
g_pd3dDevice->CreateSamplerState(&desc, &g_pFontSampler);
}
}*/
}

bool ImGui_ImplDX11_CreateDeviceObjects()
Expand Down Expand Up @@ -495,7 +496,8 @@ void ImGui_ImplDX11_InvalidateDeviceObjects()
return;

if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; }
if (g_pFontTextureView) { g_pFontTextureView->Release(); g_pFontTextureView = NULL; ImGui::GetIO().Fonts->TexID = NULL; } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
// FIXME-DYNAMICFONT: When destroying the font textures the corresponding ImFontTexture->TexID should be set to NULL
if (g_pFontTextureView) { g_pFontTextureView->Release(); g_pFontTextureView = NULL; /*ImGui::GetIO().Fonts->TexID = NULL;*/ } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }

Expand Down
10 changes: 6 additions & 4 deletions examples/imgui_impl_dx12.cpp
Expand Up @@ -245,9 +245,10 @@ void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandL
}
}

// FIXME-DYNAMICFONT: Should be replaced by ImGui_ImplDX12_UpdateFontTextures and called every frame
static void ImGui_ImplDX12_CreateFontsTexture()
{
// Build texture atlas
/* // Build texture atlas
ImGuiIO& io = ImGui::GetIO();
unsigned char* pixels;
int width, height;
Expand Down Expand Up @@ -391,7 +392,7 @@ static void ImGui_ImplDX12_CreateFontsTexture()

// Store our identifier
static_assert(sizeof(ImTextureID) >= sizeof(g_hFontSrvGpuDescHandle.ptr), "Can't pack descriptor handle into TexID, 32-bit not supported yet.");
io.Fonts->TexID = (ImTextureID)g_hFontSrvGpuDescHandle.ptr;
io.Fonts->TexID = (ImTextureID)g_hFontSrvGpuDescHandle.ptr;*/
}

bool ImGui_ImplDX12_CreateDeviceObjects()
Expand Down Expand Up @@ -609,8 +610,9 @@ void ImGui_ImplDX12_InvalidateDeviceObjects()
SafeRelease(g_pPipelineState);
SafeRelease(g_pFontTextureResource);

ImGuiIO& io = ImGui::GetIO();
io.Fonts->TexID = NULL; // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
// FIXME-DYNAMICFONT: When destroying the font textures the corresponding ImFontTexture->TexID should be updated
// ImGuiIO& io = ImGui::GetIO();
// io.Fonts->TexID = NULL; // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.

for (UINT i = 0; i < g_numFramesInFlight; i++)
{
Expand Down
8 changes: 5 additions & 3 deletions examples/imgui_impl_dx9.cpp
Expand Up @@ -234,9 +234,10 @@ void ImGui_ImplDX9_Shutdown()
if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
}

// FIXME-DYNAMICFONT: Should be renamed ImGui_ImplDX9_UpdateFontTextures and called every frame
static bool ImGui_ImplDX9_CreateFontsTexture()
{
// Build texture atlas
/* // Build texture atlas
ImGuiIO& io = ImGui::GetIO();
unsigned char* pixels;
int width, height, bytes_per_pixel;
Expand All @@ -255,7 +256,7 @@ static bool ImGui_ImplDX9_CreateFontsTexture()

// Store our identifier
io.Fonts->TexID = (ImTextureID)g_FontTexture;

*/
return true;
}

Expand All @@ -274,7 +275,8 @@ void ImGui_ImplDX9_InvalidateDeviceObjects()
return;
if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
if (g_FontTexture) { g_FontTexture->Release(); g_FontTexture = NULL; ImGui::GetIO().Fonts->TexID = NULL; } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
// FIXME-DYNAMICFONT: After destroying a font the appropriate ImFontTexture->TexID should be updated
if (g_FontTexture) { g_FontTexture->Release(); g_FontTexture = NULL; /*ImGui::GetIO().Fonts->TexID = NULL;*/ } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
}

void ImGui_ImplDX9_NewFrame()
Expand Down
2 changes: 1 addition & 1 deletion examples/imgui_impl_glfw.cpp
Expand Up @@ -345,7 +345,7 @@ static void ImGui_ImplGlfw_UpdateGamepads()
void ImGui_ImplGlfw_NewFrame()
{
ImGuiIO& io = ImGui::GetIO();
IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");
//IM_ASSERT(io.Fonts->FontTextures.Size && "A font texture has not been created.");

// Setup display size (every frame to accommodate for window resizing)
int w, h;
Expand Down
83 changes: 57 additions & 26 deletions examples/imgui_impl_opengl2.cpp
Expand Up @@ -52,8 +52,6 @@
#include <GL/gl.h>
#endif

// OpenGL Data
static GLuint g_FontTexture = 0;

// Functions
bool ImGui_ImplOpenGL2_Init()
Expand All @@ -71,8 +69,7 @@ void ImGui_ImplOpenGL2_Shutdown()

void ImGui_ImplOpenGL2_NewFrame()
{
if (!g_FontTexture)
ImGui_ImplOpenGL2_CreateDeviceObjects();
ImGui_ImplOpenGL2_UpdateFontsTexture();
}

static void ImGui_ImplOpenGL2_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height)
Expand Down Expand Up @@ -199,50 +196,84 @@ void ImGui_ImplOpenGL2_RenderDrawData(ImDrawData* draw_data)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, last_tex_env_mode);
}

bool ImGui_ImplOpenGL2_CreateFontsTexture()
{
// Build texture atlas
ImGuiIO& io = ImGui::GetIO();
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.
static ImVector<ImTextureID> g_FontTextures;

// Upload texture to graphics system
static ImTextureID CreateOpenGL2Texture(int width, int height, unsigned char* pixels) {
GLint last_texture;
GLuint new_texture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
glGenTextures(1, &g_FontTexture);
glBindTexture(GL_TEXTURE_2D, g_FontTexture);
glGenTextures(1, &new_texture);
glBindTexture(GL_TEXTURE_2D, new_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#ifdef GL_UNPACK_ROW_LENGTH
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glBindTexture(GL_TEXTURE_2D, last_texture);
return (ImTextureID)(intptr_t)new_texture;
}

static void UpdateOpenGL2Texture(ImTextureID tex, int x, int y, int width, int height, unsigned char* pixels) {
GLint last_texture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);

// Store our identifier
io.Fonts->TexID = (ImTextureID)(intptr_t)g_FontTexture;
glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)tex);
#ifdef GL_UNPACK_ROW_LENGTH
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

// Restore state
glBindTexture(GL_TEXTURE_2D, last_texture);
}

bool ImGui_ImplOpenGL2_UpdateFontsTexture()
{
ImGuiIO& io = ImGui::GetIO();

if (io.Fonts->Fonts.Size == 0) //Load a font if there is none
io.Fonts->AddFontDefault();

// FIXME-DYNAMICFONT: Add support for resizing textures
for (ImFontTexture** font_texture = io.Fonts->FontTextures.begin(); font_texture != io.Fonts->FontTextures.end(); ++font_texture) {
ImFontTexture* font_texture_ptr = *font_texture;
if (font_texture_ptr->TexID == NULL) {
//This OpenGL texure has not yet been created. Create it.
ImTextureID new_texture = CreateOpenGL2Texture(font_texture_ptr->TexWidth, font_texture_ptr->TexHeight, font_texture_ptr->TexData.Data);
font_texture_ptr->TexID = new_texture;
font_texture_ptr->IsDirty = false;
g_FontTextures.push_back(new_texture);
}

if (font_texture_ptr->IsDirty) {
//Update whole affected rows
int x = 0, y = static_cast<int>(font_texture_ptr->DirtyTopLeft.y);
int width = font_texture_ptr->TexWidth, height = static_cast<int>(font_texture_ptr->DirtyBotRight.y) - static_cast<int>(font_texture_ptr->DirtyTopLeft.y) + 1;
UpdateOpenGL2Texture(font_texture_ptr->TexID, x, y, width, height, &font_texture_ptr->TexData.Data[y*width * 4]);
font_texture_ptr->IsDirty = false;
}
}

return true;
}

void ImGui_ImplOpenGL2_DestroyFontsTexture()
void ImGui_ImplOpenGL2_DestroyFontTextures()
{
if (g_FontTexture)
if (g_FontTextures.Size)
{
ImGuiIO& io = ImGui::GetIO();
glDeleteTextures(1, &g_FontTexture);
io.Fonts->TexID = 0;
g_FontTexture = 0;
for (ImFontTexture** font_texture = io.Fonts->FontTextures.begin(); font_texture != io.Fonts->FontTextures.end(); ++font_texture) {
ImFontTexture* font_texture_ptr = *font_texture;
g_FontTextures.find_erase(font_texture_ptr->TexID);
glDeleteTextures(1, (const GLuint*)(intptr_t*)&font_texture_ptr->TexID);
font_texture_ptr->TexID = 0;
}
}
}

bool ImGui_ImplOpenGL2_CreateDeviceObjects()
{
return ImGui_ImplOpenGL2_CreateFontsTexture();
}

void ImGui_ImplOpenGL2_DestroyDeviceObjects()
{
ImGui_ImplOpenGL2_DestroyFontsTexture();
ImGui_ImplOpenGL2_DestroyFontTextures();
}
7 changes: 4 additions & 3 deletions examples/imgui_impl_opengl2.h
Expand Up @@ -25,7 +25,8 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL2_NewFrame();
IMGUI_IMPL_API void ImGui_ImplOpenGL2_RenderDrawData(ImDrawData* draw_data);

// Called by Init/NewFrame/Shutdown
IMGUI_IMPL_API bool ImGui_ImplOpenGL2_CreateFontsTexture();
IMGUI_IMPL_API void ImGui_ImplOpenGL2_DestroyFontsTexture();
IMGUI_IMPL_API bool ImGui_ImplOpenGL2_CreateDeviceObjects();
IMGUI_IMPL_API bool ImGui_ImplOpenGL2_UpdateFontsTexture();
//IMGUI_IMPL_API bool ImGui_ImplOpenGL2_CreateFontsTexture();
IMGUI_IMPL_API void ImGui_ImplOpenGL2_DestroyFontTextures();
//IMGUI_IMPL_API bool ImGui_ImplOpenGL2_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplOpenGL2_DestroyDeviceObjects();