ImFontAtlas allows loading multiple fonts into same texture. Revamped new init API for 1.30

This commit is contained in:
ocornut
2015-01-17 22:15:29 +00:00
parent cb9a3235be
commit 2c31599bcc
6 changed files with 488 additions and 335 deletions

View File

@ -321,7 +321,7 @@ void CleanupDevice()
// InitImGui
if (g_pFontSampler) g_pFontSampler->Release();
if (ID3D11ShaderResourceView* font_texture_view = (ID3D11ShaderResourceView*)ImGui::GetIO().Font->TexID)
if (ID3D11ShaderResourceView* font_texture_view = (ID3D11ShaderResourceView*)ImGui::GetIO().FontAtlas->TexID)
font_texture_view->Release();
if (g_pVB) g_pVB->Release();
@ -377,11 +377,17 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
return DefWindowProc(hWnd, msg, wParam, lParam);
}
void LoadFontTexture(ImFont* font)
void LoadFontTexture()
{
// Load one or more font
ImGuiIO& io = ImGui::GetIO();
//ImFont* my_font = io.FontAtlas->AddFontDefault();
//ImFont* my_font2 = io.FontAtlas->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, ImFontAtlas::GetGlyphRangesJapanese());
// Build
unsigned char* pixels;
int width, height;
font->GetTextureDataRGBA32(&pixels, &width, &height);
io.FontAtlas->GetTexDataAsRGBA32(&pixels, &width, &height);
// Create texture
D3D11_TEXTURE2D_DESC desc;
@ -414,8 +420,8 @@ void LoadFontTexture(ImFont* font)
g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &font_texture_view);
pTexture->Release();
// Store ID
font->TexID = (void *)font_texture_view;
// Store our identifier
io.FontAtlas->TexID = (void *)font_texture_view;
}
void InitImGui()
@ -463,10 +469,8 @@ void InitImGui()
}
}
// Load font (optionally load a custom TTF font)
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
//io.Font->DisplayOffset.y += 1.0f;
LoadFontTexture(io.Font);
// Load fonts
LoadFontTexture();
// Create texture sampler
{

View File

@ -73,8 +73,8 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
// Setup orthographic projection matrix
D3DXMATRIXA16 mat;
@ -129,7 +129,7 @@ void CleanupDevice()
if (g_pVB) g_pVB->Release();
// InitDeviceD3D
if (LPDIRECT3DTEXTURE9 tex = (LPDIRECT3DTEXTURE9)ImGui::GetIO().Font->TexID)
if (LPDIRECT3DTEXTURE9 tex = (LPDIRECT3DTEXTURE9)ImGui::GetIO().FontAtlas->TexID)
tex->Release();
if (g_pd3dDevice) g_pd3dDevice->Release();
if (g_pD3D) g_pD3D->Release();
@ -173,13 +173,19 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
return DefWindowProc(hWnd, msg, wParam, lParam);
}
void LoadFontTexture(ImFont* font)
void LoadFontTexture()
{
unsigned char* pixels;
int width, height;
int bytes_per_pixel;
font->GetTextureDataAlpha8(&pixels, &width, &height, &bytes_per_pixel);
// Load one or more font
ImGuiIO& io = ImGui::GetIO();
//ImFont* my_font = io.FontAtlas->AddFontDefault();
//ImFont* my_font2 = io.FontAtlas->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, ImFontAtlas::GetGlyphRangesJapanese());
// Build
unsigned char* pixels;
int width, height, bytes_per_pixel;
io.FontAtlas->GetTexDataAsAlpha8(&pixels, &width, &height, &bytes_per_pixel);
// Create texture
LPDIRECT3DTEXTURE9 pTexture = NULL;
if (D3DXCreateTexture(g_pd3dDevice, width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8, D3DPOOL_DEFAULT, &pTexture) < 0)
{
@ -198,7 +204,8 @@ void LoadFontTexture(ImFont* font)
memcpy((unsigned char *)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, pixels + (width * bytes_per_pixel) * y, (width * bytes_per_pixel));
pTexture->UnlockRect(0);
font->TexID = (void *)pTexture;
// Store our identifier
io.FontAtlas->TexID = (void *)pTexture;
}
void InitImGui()
@ -238,10 +245,7 @@ void InitImGui()
return;
}
// Load font (optionally load a custom TTF font)
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
//io.Font->DisplayOffset.y += 1.0f;
LoadFontTexture(io.Font);
LoadFontTexture();
}
INT64 ticks_per_second = 0;

View File

@ -235,20 +235,25 @@ void InitGL()
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void LoadFontTexture(ImFont* font)
void LoadFontTexture()
{
ImGuiIO& io = ImGui::GetIO();
//ImFont* my_font = io.FontAtlas->AddFontDefault();
//ImFont* my_font2 = io.FontAtlas->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, ImFontAtlas::GetGlyphRangesJapanese());
unsigned char* pixels;
int width, height;
font->GetTextureDataRGBA32(&pixels, &width, &height);
io.FontAtlas->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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
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);
font->TexID = (void *)(intptr_t)tex_id;
// Store our identifier
io.FontAtlas->TexID = (void *)(intptr_t)tex_id;
}
void InitImGui()
@ -277,10 +282,7 @@ void InitImGui()
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;
// Load font (optionally load a custom TTF font)
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
//io.Font->DisplayOffset.y += 1.0f;
LoadFontTexture(io.Font);
LoadFontTexture();
}
void UpdateImGui()

View File

@ -145,20 +145,25 @@ void InitGL()
glewInit();
}
void LoadFontTexture(ImFont* font)
void LoadFontTexture()
{
ImGuiIO& io = ImGui::GetIO();
//ImFont* my_font = io.FontAtlas->AddFontDefault();
//ImFont* my_font2 = io.FontAtlas->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, ImFontAtlas::GetGlyphRangesJapanese());
unsigned char* pixels;
int width, height;
font->GetTextureDataAlpha8(&pixels, &width, &height);
io.FontAtlas->GetTexDataAsAlpha8(&pixels, &width, &height);
GLuint tex_id;
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
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);
font->TexID = (void *)(intptr_t)tex_id;
// Store our identifier
io.FontAtlas->TexID = (void *)(intptr_t)tex_id;
}
void InitImGui()
@ -186,11 +191,6 @@ void InitImGui()
io.RenderDrawListsFn = ImImpl_RenderDrawLists;
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;
// Load font (optionally load a custom TTF font)
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
//io.Font->DisplayOffset.y += 1.0f;
LoadFontTexture(io.Font);
}
void UpdateImGui()