mirror of
https://github.com/Drezil/imgui.git
synced 2025-07-04 20:18:47 +02:00
ImFont::GetTextureData API allow to retrieve 8/32 bits data + lazily load defaults font
Examples: OpenGL3 and DirectX11 back to using 32-bits texture solely for ease of integration.
This commit is contained in:
@ -286,8 +286,7 @@ HRESULT InitDeviceD3D(HWND hWnd)
|
||||
\
|
||||
float4 main(PS_INPUT input) : SV_Target\
|
||||
{\
|
||||
float4 out_col = input.col; \
|
||||
out_col.w *= texture0.Sample(sampler0, input.uv).w; \
|
||||
float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
|
||||
return out_col; \
|
||||
}";
|
||||
|
||||
@ -381,16 +380,18 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
||||
|
||||
void LoadFontTexture(ImFont* font)
|
||||
{
|
||||
IM_ASSERT(font && font->IsLoaded());
|
||||
unsigned char* pixels;
|
||||
int width, height;
|
||||
font->GetTextureDataRGBA32(&pixels, &width, &height);
|
||||
|
||||
// Create texture
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
ZeroMemory(&desc, sizeof(desc));
|
||||
desc.Width = font->TexWidth;
|
||||
desc.Height = font->TexHeight;
|
||||
desc.Width = width;
|
||||
desc.Height = height;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = DXGI_FORMAT_A8_UNORM;
|
||||
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Usage = D3D11_USAGE_DEFAULT;
|
||||
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
||||
@ -398,15 +399,15 @@ void LoadFontTexture(ImFont* font)
|
||||
|
||||
ID3D11Texture2D *pTexture = NULL;
|
||||
D3D11_SUBRESOURCE_DATA subResource;
|
||||
subResource.pSysMem = font->TexPixels;
|
||||
subResource.SysMemPitch = desc.Width * 1;
|
||||
subResource.pSysMem = pixels;
|
||||
subResource.SysMemPitch = desc.Width * 4;
|
||||
subResource.SysMemSlicePitch = 0;
|
||||
g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
|
||||
|
||||
// Create texture view
|
||||
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
|
||||
ZeroMemory(&srvDesc, sizeof(srvDesc));
|
||||
srvDesc.Format = DXGI_FORMAT_A8_UNORM;
|
||||
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
||||
srvDesc.Texture2D.MipLevels = desc.MipLevels;
|
||||
srvDesc.Texture2D.MostDetailedMip = 0;
|
||||
@ -464,10 +465,9 @@ void InitImGui()
|
||||
}
|
||||
}
|
||||
|
||||
// Load font
|
||||
io.Font->LoadDefault();
|
||||
// Load font (optionally load a custom TTF font)
|
||||
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
|
||||
//io.Font->DisplayOffset.y += 0.0f;
|
||||
//io.Font->DisplayOffset.y += 1.0f;
|
||||
LoadFontTexture(io.Font);
|
||||
|
||||
// Create texture sampler
|
||||
@ -569,11 +569,6 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int)
|
||||
show_test_window ^= ImGui::Button("Test Window");
|
||||
show_another_window ^= ImGui::Button("Another Window");
|
||||
|
||||
static ImFont* font2 = NULL;
|
||||
if (!font2) { font2 = new ImFont(); font2->LoadFromFileTTF("../../extra_fonts/ArialUni.ttf", 30.0f); LoadFontTexture(font2); }
|
||||
ImGui::Image(font2->TexID, ImVec2((float)font2->TexWidth, (FLOAT)font2->TexHeight));
|
||||
//ImGui::GetWindowDrawList()->AddText(font2, 30.0f, ImGui::GetCursorScreenPos(), 0xFFFFFFFF, "Another font");
|
||||
|
||||
// Calculate and show frame rate
|
||||
static float ms_per_frame[120] = { 0 };
|
||||
static int ms_per_frame_idx = 0;
|
||||
|
@ -176,10 +176,13 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
||||
|
||||
void LoadFontTexture(ImFont* font)
|
||||
{
|
||||
IM_ASSERT(font && font->IsLoaded());
|
||||
unsigned char* pixels;
|
||||
int width, height;
|
||||
int bytes_per_pixel;
|
||||
font->GetTextureDataAlpha8(&pixels, &width, &height, &bytes_per_pixel);
|
||||
|
||||
LPDIRECT3DTEXTURE9 pTexture = NULL;
|
||||
if (D3DXCreateTexture(g_pd3dDevice, font->TexWidth, font->TexHeight, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8, D3DPOOL_DEFAULT, &pTexture) < 0)
|
||||
if (D3DXCreateTexture(g_pd3dDevice, width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8, D3DPOOL_DEFAULT, &pTexture) < 0)
|
||||
{
|
||||
IM_ASSERT(0);
|
||||
return;
|
||||
@ -192,8 +195,8 @@ void LoadFontTexture(ImFont* font)
|
||||
IM_ASSERT(0);
|
||||
return;
|
||||
}
|
||||
for (int y = 0; y < font->TexHeight; y++)
|
||||
memcpy((unsigned char *)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, font->TexPixels + font->TexWidth * y, font->TexWidth);
|
||||
for (int y = 0; y < height; y++)
|
||||
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;
|
||||
@ -237,10 +240,9 @@ void InitImGui()
|
||||
return;
|
||||
}
|
||||
|
||||
// Load font
|
||||
io.Font->LoadDefault();
|
||||
// Load font (optionally load a custom TTF font)
|
||||
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
|
||||
//io.Font->DisplayOffset.y += 0.0f;
|
||||
//io.Font->DisplayOffset.y += 1.0f;
|
||||
LoadFontTexture(io.Font);
|
||||
}
|
||||
|
||||
@ -327,11 +329,6 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int)
|
||||
show_test_window ^= ImGui::Button("Test Window");
|
||||
show_another_window ^= ImGui::Button("Another Window");
|
||||
|
||||
static ImFont* font2 = NULL;
|
||||
if (!font2) { font2 = new ImFont(); font2->LoadFromFileTTF("../../extra_fonts/ArialUni.ttf", 30.0f); LoadFontTexture(font2); }
|
||||
ImGui::Image(font2->TexID, ImVec2((float)font2->TexWidth, (FLOAT)font2->TexHeight));
|
||||
//ImGui::GetWindowDrawList()->AddText(font2, 30.0f, ImGui::GetCursorScreenPos(), 0xFFFFFFFF, "Another font");
|
||||
|
||||
// Calculate and show frame rate
|
||||
static float ms_per_frame[120] = { 0 };
|
||||
static int ms_per_frame_idx = 0;
|
||||
|
@ -198,8 +198,7 @@ void InitGL()
|
||||
"out vec4 Out_Color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" Out_Color = Frag_Color;\n"
|
||||
" Out_Color.w *= texture( Texture, Frag_UV.st).x;\n"
|
||||
" Out_Color = Frag_Color * texture( Texture, Frag_UV.st);\n"
|
||||
"}\n";
|
||||
|
||||
shader_handle = glCreateProgram();
|
||||
@ -239,14 +238,17 @@ void InitGL()
|
||||
|
||||
void LoadFontTexture(ImFont* font)
|
||||
{
|
||||
IM_ASSERT(font && font->IsLoaded());
|
||||
unsigned char* pixels;
|
||||
int width, height;
|
||||
font->GetTextureDataRGBA32(&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);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, font->TexWidth, font->TexHeight, 0, GL_RED, GL_UNSIGNED_BYTE, font->TexPixels);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
||||
|
||||
font->TexID = (void *)(intptr_t)tex_id;
|
||||
}
|
||||
|
||||
@ -277,10 +279,9 @@ void InitImGui()
|
||||
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
|
||||
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;
|
||||
|
||||
// Load font
|
||||
io.Font->LoadDefault();
|
||||
// Load font (optionally load a custom TTF font)
|
||||
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
|
||||
//io.Font->DisplayOffset.y += 0.0f;
|
||||
//io.Font->DisplayOffset.y += 1.0f;
|
||||
LoadFontTexture(io.Font);
|
||||
}
|
||||
|
||||
@ -341,11 +342,6 @@ int main(int argc, char** argv)
|
||||
show_test_window ^= ImGui::Button("Test Window");
|
||||
show_another_window ^= ImGui::Button("Another Window");
|
||||
|
||||
static ImFont* font2 = NULL;
|
||||
if (!font2) { font2 = new ImFont(); font2->LoadFromFileTTF("../../extra_fonts/ArialUni.ttf", 30.0f); LoadFontTexture(font2); }
|
||||
ImGui::Image(font2->TexID, ImVec2((float)font2->TexWidth, (float)font2->TexHeight));
|
||||
//ImGui::GetWindowDrawList()->AddText(font2, 30.0f, ImGui::GetCursorScreenPos(), 0xFFFFFFFF, "Another font");
|
||||
|
||||
// Calculate and show frame rate
|
||||
static float ms_per_frame[120] = { 0 };
|
||||
static int ms_per_frame_idx = 0;
|
||||
|
@ -148,14 +148,17 @@ void InitGL()
|
||||
|
||||
void LoadFontTexture(ImFont* font)
|
||||
{
|
||||
IM_ASSERT(font && font->IsLoaded());
|
||||
unsigned char* pixels;
|
||||
int width, height;
|
||||
font->GetTextureDataAlpha8(&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);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, font->TexWidth, font->TexHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, font->TexPixels);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
|
||||
|
||||
font->TexID = (void *)(intptr_t)tex_id;
|
||||
}
|
||||
|
||||
@ -186,10 +189,9 @@ void InitImGui()
|
||||
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
|
||||
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;
|
||||
|
||||
// Load font
|
||||
io.Font->LoadDefault();
|
||||
// Load font (optionally load a custom TTF font)
|
||||
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
|
||||
//io.Font->DisplayOffset.y += 0.0f;
|
||||
//io.Font->DisplayOffset.y += 1.0f;
|
||||
LoadFontTexture(io.Font);
|
||||
}
|
||||
|
||||
@ -249,11 +251,6 @@ int main(int argc, char** argv)
|
||||
show_test_window ^= ImGui::Button("Test Window");
|
||||
show_another_window ^= ImGui::Button("Another Window");
|
||||
|
||||
static ImFont* font2 = NULL;
|
||||
if (!font2) { font2 = new ImFont(); font2->LoadFromFileTTF("../../extra_fonts/ArialUni.ttf", 30.0f); LoadFontTexture(font2); }
|
||||
ImGui::Image(font2->TexID, ImVec2((float)font2->TexWidth, (float)font2->TexHeight));
|
||||
//ImGui::GetWindowDrawList()->AddText(font2, 30.0f, ImGui::GetCursorScreenPos(), 0xFFFFFFFF, "Another font");
|
||||
|
||||
// Calculate and show frame rate
|
||||
static float ms_per_frame[120] = { 0 };
|
||||
static int ms_per_frame_idx = 0;
|
||||
|
Reference in New Issue
Block a user