Skip to content

Commit

Permalink
Loading TTF file with stb_truetype. Broke setup API slightly. Font ba…
Browse files Browse the repository at this point in the history
…ked, packed with space for custom data. Embeds compressed ProggyClean.
  • Loading branch information
ocornut committed Jan 8, 2015
1 parent dd8a765 commit b3a2089
Show file tree
Hide file tree
Showing 19 changed files with 3,950 additions and 5,246 deletions.
2 changes: 0 additions & 2 deletions examples/directx11_example/directx11_example.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@
<ItemGroup>
<ClInclude Include="..\..\imconfig.h" />
<ClInclude Include="..\..\imgui.h" />
<ClInclude Include="..\..\stb_textedit.h" />
<ClInclude Include="..\shared\stb_image.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\imgui.cpp" />
Expand Down
6 changes: 0 additions & 6 deletions examples/directx11_example/directx11_example.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
<ClInclude Include="..\..\imgui.h">
<Filter>imgui</Filter>
</ClInclude>
<ClInclude Include="..\..\stb_textedit.h">
<Filter>imgui</Filter>
</ClInclude>
<ClInclude Include="..\shared\stb_image.h">
<Filter>sources</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\imgui.cpp">
Expand Down
37 changes: 17 additions & 20 deletions examples/directx11_example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// ImGui - standalone example application for DirectX 11

#include <windows.h>
#define STB_IMAGE_STATIC
#define STB_IMAGE_IMPLEMENTATION
#include "../shared/stb_image.h" // for .png loading
#include "../../imgui.h"

// DirectX 11
Expand Down Expand Up @@ -290,8 +287,9 @@ HRESULT InitDeviceD3D(HWND hWnd)
\
float4 main(PS_INPUT input) : SV_Target\
{\
float4 out_col = texture0.Sample(sampler0, input.uv);\
return input.col * out_col;\
float4 out_col = input.col; \
out_col.w *= texture0.Sample(sampler0, input.uv).w; \
return out_col; \
}";

D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_5_0", 0, 0, &g_pPixelShaderBlob, NULL);
Expand Down Expand Up @@ -427,39 +425,38 @@ void InitImGui()
}
}

// Load font texture
// Default font (embedded in code)
const void* png_data;
unsigned int png_size;
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
int tex_x, tex_y, tex_comp;
void* tex_data = stbi_load_from_memory((const unsigned char*)png_data, (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
IM_ASSERT(tex_data != NULL);
// Load font
io.Font = new ImFont();
io.Font->LoadDefault();
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
//io.Font->DisplayOffset.y += 0.0f;
IM_ASSERT(io.Font->IsLoaded());

// Copy font texture
{
D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Width = tex_x;
desc.Height = tex_y;
desc.Width = io.Font->TexWidth;
desc.Height = io.Font->TexHeight;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.Format = DXGI_FORMAT_A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;

ID3D11Texture2D *pTexture = NULL;
D3D11_SUBRESOURCE_DATA subResource;
subResource.pSysMem = tex_data;
subResource.SysMemPitch = tex_x * 4;
subResource.pSysMem = io.Font->TexPixels;
subResource.SysMemPitch = desc.Width * 1;
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_R8G8B8A8_UNORM;
srvDesc.Format = DXGI_FORMAT_A8_UNORM;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = desc.MipLevels;
srvDesc.Texture2D.MostDetailedMip = 0;
Expand All @@ -471,7 +468,7 @@ void InitImGui()
{
D3D11_SAMPLER_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
desc.Filter = D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR;
desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
Expand Down
1 change: 0 additions & 1 deletion examples/directx9_example/directx9_example.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
<ItemGroup>
<ClInclude Include="..\..\imconfig.h" />
<ClInclude Include="..\..\imgui.h" />
<ClInclude Include="..\..\stb_textedit.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
3 changes: 0 additions & 3 deletions examples/directx9_example/directx9_example.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@
<ClInclude Include="..\..\imgui.h">
<Filter>imgui</Filter>
</ClInclude>
<ClInclude Include="..\..\stb_textedit.h">
<Filter>imgui</Filter>
</ClInclude>
</ItemGroup>
</Project>
33 changes: 20 additions & 13 deletions examples/directx9_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c

// Setup texture
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE );
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_LINEAR );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );

// Setup orthographic projection matrix
D3DXMATRIXA16 mat;
Expand Down Expand Up @@ -213,15 +214,21 @@ void InitImGui()
return;
}

// Load font texture
const void* png_data;
unsigned int png_size;
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
if (D3DXCreateTextureFromFileInMemory(g_pd3dDevice, png_data, png_size, &g_pTexture) < 0)
{
IM_ASSERT(0);
return;
}
// Load font
io.Font = new ImFont();
io.Font->LoadDefault();
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
io.Font->LoadFromFileTTF("../../extra_fonts/ArialUni.ttf", 20.0f, ImFont::GetGlyphRangesDefault());
//io.Font->DisplayOffset.y += 0.0f;
IM_ASSERT(io.Font->IsLoaded());

// Copy font texture
if (D3DXCreateTexture(g_pd3dDevice, io.Font->TexWidth, io.Font->TexHeight, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8, D3DPOOL_DEFAULT, &g_pTexture) < 0) { IM_ASSERT(0); return; }
D3DLOCKED_RECT tex_locked_rect;
if (g_pTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK) { IM_ASSERT(0); return; }
for (int y = 0; y < io.Font->TexHeight; y++)
memcpy((unsigned char *)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, io.Font->TexPixels + io.Font->TexWidth * y, io.Font->TexWidth);
g_pTexture->UnlockRect(0);
}

INT64 ticks_per_second = 0;
Expand Down Expand Up @@ -330,7 +337,7 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int)
// 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
if (show_test_window)
{
ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCondition_FirstUseEver);
ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCondition_FirstUseEver);
ImGui::ShowTestWindow(&show_test_window);
}

Expand Down
53 changes: 27 additions & 26 deletions examples/opengl3_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
#endif

#include "../../imgui.h"
#define STB_IMAGE_STATIC
#define STB_IMAGE_IMPLEMENTATION
#include "../shared/stb_image.h" // stb_image.h for PNG loading
#include <stdio.h>

// Glfw/Glew
#define GLEW_STATIC
Expand All @@ -24,7 +22,7 @@ static bool mousePressed[2] = { false, false };

// Shader variables
static int shader_handle, vert_handle, frag_handle;
static int texture_location, ortho_location;
static int texture_location, proj_mtx_location;
static int position_location, uv_location, colour_location;
static size_t vbo_max_size = 20000;
static unsigned int vbo_handle, vao_handle;
Expand Down Expand Up @@ -62,7 +60,7 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
};
glUseProgram(shader_handle);
glUniform1i(texture_location, 0);
glUniformMatrix4fv(ortho_location, 1, GL_FALSE, &ortho_projection[0][0]);
glUniformMatrix4fv(proj_mtx_location, 1, GL_FALSE, &ortho_projection[0][0]);

// Grow our buffer according to what we need
size_t total_vtx_count = 0;
Expand Down Expand Up @@ -184,28 +182,29 @@ void InitGL()

const GLchar *vertex_shader =
"#version 330\n"
"uniform mat4 ortho;\n"
"uniform mat4 ProjMtx;\n"
"in vec2 Position;\n"
"in vec2 UV;\n"
"in vec4 Colour;\n"
"in vec4 Color;\n"
"out vec2 Frag_UV;\n"
"out vec4 Frag_Colour;\n"
"out vec4 Frag_Color;\n"
"void main()\n"
"{\n"
" Frag_UV = UV;\n"
" Frag_Colour = Colour;\n"
" gl_Position = ortho*vec4(Position.xy,0,1);\n"
" Frag_Color = Color;\n"
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
"}\n";

const GLchar* fragment_shader =
"#version 330\n"
"uniform sampler2D Texture;\n"
"in vec2 Frag_UV;\n"
"in vec4 Frag_Colour;\n"
"out vec4 FragColor;\n"
"in vec4 Frag_Color;\n"
"out vec4 Out_Color;\n"
"void main()\n"
"{\n"
" FragColor = Frag_Colour * texture( Texture, Frag_UV.st);\n"
" Out_Color = Frag_Color;\n"
" Out_Color.w *= texture( Texture, Frag_UV.st).x;\n"
"}\n";

shader_handle = glCreateProgram();
Expand All @@ -220,10 +219,10 @@ void InitGL()
glLinkProgram(shader_handle);

texture_location = glGetUniformLocation(shader_handle, "Texture");
ortho_location = glGetUniformLocation(shader_handle, "ortho");
proj_mtx_location = glGetUniformLocation(shader_handle, "ProjMtx");
position_location = glGetAttribLocation(shader_handle, "Position");
uv_location = glGetAttribLocation(shader_handle, "UV");
colour_location = glGetAttribLocation(shader_handle, "Colour");
colour_location = glGetAttribLocation(shader_handle, "Color");

glGenBuffers(1, &vbo_handle);
glBindBuffer(GL_ARRAY_BUFFER, vbo_handle);
Expand Down Expand Up @@ -270,18 +269,20 @@ void InitImGui()
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;

// Load font texture
// Load font
io.Font = new ImFont();
io.Font->LoadDefault();
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
//io.Font->DisplayOffset.y += 0.0f;
IM_ASSERT(io.Font->IsLoaded());

// Copy font texture
glGenTextures(1, &fontTex);
glBindTexture(GL_TEXTURE_2D, fontTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
const void* png_data;
unsigned int png_size;
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
int tex_x, tex_y, tex_comp;
void* tex_data = stbi_load_from_memory((const unsigned char*)png_data, (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_x, tex_y, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data);
stbi_image_free(tex_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
IM_ASSERT(io.Font->IsLoaded());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, io.Font->TexWidth, io.Font->TexHeight, 0, GL_RED, GL_UNSIGNED_BYTE, io.Font->TexPixels);
}

void UpdateImGui()
Expand Down Expand Up @@ -364,7 +365,7 @@ int main(int argc, char** argv)
// 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
if (show_test_window)
{
ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCondition_FirstUseEver);
ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCondition_FirstUseEver);
ImGui::ShowTestWindow(&show_test_window);
}

Expand Down
1 change: 0 additions & 1 deletion examples/opengl3_example/opengl3_example.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
<ItemGroup>
<ClInclude Include="..\..\imconfig.h" />
<ClInclude Include="..\..\imgui.h" />
<ClInclude Include="..\..\stb_textedit.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
3 changes: 0 additions & 3 deletions examples/opengl3_example/opengl3_example.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@
<ClInclude Include="..\..\imgui.h">
<Filter>imgui</Filter>
</ClInclude>
<ClInclude Include="..\..\stb_textedit.h">
<Filter>imgui</Filter>
</ClInclude>
</ItemGroup>
</Project>
Binary file added examples/opengl_example/ProggyClean.ttf
Binary file not shown.
48 changes: 12 additions & 36 deletions examples/opengl_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
#endif

#include "../../imgui.h"
#define STB_IMAGE_STATIC
#define STB_IMAGE_IMPLEMENTATION
#include "../shared/stb_image.h" // stb_image.h for PNG loading
#include <stdio.h>

// Glfw/Glew
#define GLEW_STATIC
Expand Down Expand Up @@ -180,42 +178,20 @@ void InitImGui()
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;

// Load font texture
glGenTextures(1, &fontTex);
glBindTexture(GL_TEXTURE_2D, fontTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

#if 1
// Default font (embedded in code)
const void* png_data;
unsigned int png_size;
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
int tex_x, tex_y, tex_comp;
void* tex_data = stbi_load_from_memory((const unsigned char*)png_data, (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
IM_ASSERT(tex_data != NULL);
#else
// Custom font from filesystem
// Load font
io.Font = new ImFont();
io.Font->LoadFromFile("../../extra_fonts/mplus-2m-medium_18.fnt");
io.Font->LoadDefault();
//io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
//io.Font->DisplayOffset.y += 0.0f;
IM_ASSERT(io.Font->IsLoaded());

int tex_x, tex_y, tex_comp;
void* tex_data = stbi_load("../../extra_fonts/mplus-2m-medium_18.png", &tex_x, &tex_y, &tex_comp, 0);
IM_ASSERT(tex_data != NULL);
// Automatically find white pixel from the texture we just loaded
// (io.Font->TexUvForWhite needs to contains UV coordinates pointing to a white pixel in order to render solid objects)
for (int tex_data_off = 0; tex_data_off < tex_x*tex_y; tex_data_off++)
if (((unsigned int*)tex_data)[tex_data_off] == 0xffffffff)
{
io.Font->TexUvForWhite = ImVec2((float)(tex_data_off % tex_x)/(tex_x), (float)(tex_data_off / tex_x)/(tex_y));
break;
}
#endif

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_x, tex_y, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data);
stbi_image_free(tex_data);
// Copy font texture
glGenTextures(1, &fontTex);
glBindTexture(GL_TEXTURE_2D, fontTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
IM_ASSERT(io.Font->IsLoaded());
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, io.Font->TexWidth, io.Font->TexHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, io.Font->TexPixels);
}

void UpdateImGui()
Expand Down
2 changes: 0 additions & 2 deletions examples/opengl_example/opengl_example.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@
<ItemGroup>
<ClInclude Include="..\..\imconfig.h" />
<ClInclude Include="..\..\imgui.h" />
<ClInclude Include="..\..\stb_textedit.h" />
<ClInclude Include="..\shared\stb_image.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
Loading

0 comments on commit b3a2089

Please sign in to comment.