Loading TTF file with stb_truetype. Broke setup API slightly. Font baked, packed with space for custom data. Embeds compressed ProggyClean.

This commit is contained in:
ocornut
2015-01-08 23:35:01 +00:00
parent dd8a7655f5
commit b3a208901a
19 changed files with 3953 additions and 5249 deletions
@@ -68,8 +68,6 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\imconfig.h" /> <ClInclude Include="..\..\imconfig.h" />
<ClInclude Include="..\..\imgui.h" /> <ClInclude Include="..\..\imgui.h" />
<ClInclude Include="..\..\stb_textedit.h" />
<ClInclude Include="..\shared\stb_image.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\imgui.cpp" /> <ClCompile Include="..\..\imgui.cpp" />
@@ -15,12 +15,6 @@
<ClInclude Include="..\..\imgui.h"> <ClInclude Include="..\..\imgui.h">
<Filter>imgui</Filter> <Filter>imgui</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\stb_textedit.h">
<Filter>imgui</Filter>
</ClInclude>
<ClInclude Include="..\shared\stb_image.h">
<Filter>sources</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\imgui.cpp"> <ClCompile Include="..\..\imgui.cpp">
+17 -20
View File
@@ -1,9 +1,6 @@
// ImGui - standalone example application for DirectX 11 // ImGui - standalone example application for DirectX 11
#include <windows.h> #include <windows.h>
#define STB_IMAGE_STATIC
#define STB_IMAGE_IMPLEMENTATION
#include "../shared/stb_image.h" // for .png loading
#include "../../imgui.h" #include "../../imgui.h"
// DirectX 11 // DirectX 11
@@ -290,8 +287,9 @@ HRESULT InitDeviceD3D(HWND hWnd)
\ \
float4 main(PS_INPUT input) : SV_Target\ float4 main(PS_INPUT input) : SV_Target\
{\ {\
float4 out_col = texture0.Sample(sampler0, input.uv);\ float4 out_col = input.col; \
return input.col * out_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); D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_5_0", 0, 0, &g_pPixelShaderBlob, NULL);
@@ -427,23 +425,22 @@ void InitImGui()
} }
} }
// Load font texture // Load font
// Default font (embedded in code) io.Font = new ImFont();
const void* png_data; io.Font->LoadDefault();
unsigned int png_size; //io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); //io.Font->DisplayOffset.y += 0.0f;
int tex_x, tex_y, tex_comp; IM_ASSERT(io.Font->IsLoaded());
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);
// Copy font texture
{ {
D3D11_TEXTURE2D_DESC desc; D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc)); ZeroMemory(&desc, sizeof(desc));
desc.Width = tex_x; desc.Width = io.Font->TexWidth;
desc.Height = tex_y; desc.Height = io.Font->TexHeight;
desc.MipLevels = 1; desc.MipLevels = 1;
desc.ArraySize = 1; desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; desc.Format = DXGI_FORMAT_A8_UNORM;
desc.SampleDesc.Count = 1; desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT; desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
@@ -451,15 +448,15 @@ void InitImGui()
ID3D11Texture2D *pTexture = NULL; ID3D11Texture2D *pTexture = NULL;
D3D11_SUBRESOURCE_DATA subResource; D3D11_SUBRESOURCE_DATA subResource;
subResource.pSysMem = tex_data; subResource.pSysMem = io.Font->TexPixels;
subResource.SysMemPitch = tex_x * 4; subResource.SysMemPitch = desc.Width * 1;
subResource.SysMemSlicePitch = 0; subResource.SysMemSlicePitch = 0;
g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture); g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
// Create texture view // Create texture view
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
ZeroMemory(&srvDesc, sizeof(srvDesc)); ZeroMemory(&srvDesc, sizeof(srvDesc));
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; srvDesc.Format = DXGI_FORMAT_A8_UNORM;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = desc.MipLevels; srvDesc.Texture2D.MipLevels = desc.MipLevels;
srvDesc.Texture2D.MostDetailedMip = 0; srvDesc.Texture2D.MostDetailedMip = 0;
@@ -471,7 +468,7 @@ void InitImGui()
{ {
D3D11_SAMPLER_DESC desc; D3D11_SAMPLER_DESC desc;
ZeroMemory(&desc, sizeof(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.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
@@ -76,7 +76,6 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\imconfig.h" /> <ClInclude Include="..\..\imconfig.h" />
<ClInclude Include="..\..\imgui.h" /> <ClInclude Include="..\..\imgui.h" />
<ClInclude Include="..\..\stb_textedit.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
@@ -24,8 +24,5 @@
<ClInclude Include="..\..\imgui.h"> <ClInclude Include="..\..\imgui.h">
<Filter>imgui</Filter> <Filter>imgui</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\stb_textedit.h">
<Filter>imgui</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
+19 -12
View File
@@ -73,12 +73,13 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
// Setup texture // Setup texture
g_pd3dDevice->SetTexture( 0, g_pTexture ); g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); 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 // Setup orthographic projection matrix
D3DXMATRIXA16 mat; D3DXMATRIXA16 mat;
@@ -213,15 +214,21 @@ void InitImGui()
return; return;
} }
// Load font texture // Load font
const void* png_data; io.Font = new ImFont();
unsigned int png_size; io.Font->LoadDefault();
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); //io.Font->LoadFromFileTTF("myfont.ttf", font_size_px, ImFont::GetGlyphRangesDefault());
if (D3DXCreateTextureFromFileInMemory(g_pd3dDevice, png_data, png_size, &g_pTexture) < 0) io.Font->LoadFromFileTTF("../../extra_fonts/ArialUni.ttf", 20.0f, ImFont::GetGlyphRangesDefault());
{ //io.Font->DisplayOffset.y += 0.0f;
IM_ASSERT(0); IM_ASSERT(io.Font->IsLoaded());
return;
} // 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; INT64 ticks_per_second = 0;
+26 -25
View File
@@ -8,9 +8,7 @@
#endif #endif
#include "../../imgui.h" #include "../../imgui.h"
#define STB_IMAGE_STATIC #include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
#include "../shared/stb_image.h" // stb_image.h for PNG loading
// Glfw/Glew // Glfw/Glew
#define GLEW_STATIC #define GLEW_STATIC
@@ -24,7 +22,7 @@ static bool mousePressed[2] = { false, false };
// Shader variables // Shader variables
static int shader_handle, vert_handle, frag_handle; 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 int position_location, uv_location, colour_location;
static size_t vbo_max_size = 20000; static size_t vbo_max_size = 20000;
static unsigned int vbo_handle, vao_handle; static unsigned int vbo_handle, vao_handle;
@@ -62,7 +60,7 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
}; };
glUseProgram(shader_handle); glUseProgram(shader_handle);
glUniform1i(texture_location, 0); 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 // Grow our buffer according to what we need
size_t total_vtx_count = 0; size_t total_vtx_count = 0;
@@ -184,28 +182,29 @@ void InitGL()
const GLchar *vertex_shader = const GLchar *vertex_shader =
"#version 330\n" "#version 330\n"
"uniform mat4 ortho;\n" "uniform mat4 ProjMtx;\n"
"in vec2 Position;\n" "in vec2 Position;\n"
"in vec2 UV;\n" "in vec2 UV;\n"
"in vec4 Colour;\n" "in vec4 Color;\n"
"out vec2 Frag_UV;\n" "out vec2 Frag_UV;\n"
"out vec4 Frag_Colour;\n" "out vec4 Frag_Color;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" Frag_UV = UV;\n" " Frag_UV = UV;\n"
" Frag_Colour = Colour;\n" " Frag_Color = Color;\n"
" gl_Position = ortho*vec4(Position.xy,0,1);\n" " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
"}\n"; "}\n";
const GLchar* fragment_shader = const GLchar* fragment_shader =
"#version 330\n" "#version 330\n"
"uniform sampler2D Texture;\n" "uniform sampler2D Texture;\n"
"in vec2 Frag_UV;\n" "in vec2 Frag_UV;\n"
"in vec4 Frag_Colour;\n" "in vec4 Frag_Color;\n"
"out vec4 FragColor;\n" "out vec4 Out_Color;\n"
"void main()\n" "void main()\n"
"{\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"; "}\n";
shader_handle = glCreateProgram(); shader_handle = glCreateProgram();
@@ -220,10 +219,10 @@ void InitGL()
glLinkProgram(shader_handle); glLinkProgram(shader_handle);
texture_location = glGetUniformLocation(shader_handle, "Texture"); 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"); position_location = glGetAttribLocation(shader_handle, "Position");
uv_location = glGetAttribLocation(shader_handle, "UV"); uv_location = glGetAttribLocation(shader_handle, "UV");
colour_location = glGetAttribLocation(shader_handle, "Colour"); colour_location = glGetAttribLocation(shader_handle, "Color");
glGenBuffers(1, &vbo_handle); glGenBuffers(1, &vbo_handle);
glBindBuffer(GL_ARRAY_BUFFER, vbo_handle); glBindBuffer(GL_ARRAY_BUFFER, vbo_handle);
@@ -270,18 +269,20 @@ void InitImGui()
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn; io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn; 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); glGenTextures(1, &fontTex);
glBindTexture(GL_TEXTURE_2D, fontTex); glBindTexture(GL_TEXTURE_2D, fontTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
const void* png_data; IM_ASSERT(io.Font->IsLoaded());
unsigned int png_size; glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, io.Font->TexWidth, io.Font->TexHeight, 0, GL_RED, GL_UNSIGNED_BYTE, io.Font->TexPixels);
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);
} }
void UpdateImGui() void UpdateImGui()
@@ -74,7 +74,6 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\imconfig.h" /> <ClInclude Include="..\..\imconfig.h" />
<ClInclude Include="..\..\imgui.h" /> <ClInclude Include="..\..\imgui.h" />
<ClInclude Include="..\..\stb_textedit.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
@@ -24,8 +24,5 @@
<ClInclude Include="..\..\imgui.h"> <ClInclude Include="..\..\imgui.h">
<Filter>imgui</Filter> <Filter>imgui</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\stb_textedit.h">
<Filter>imgui</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
Binary file not shown.
+12 -36
View File
@@ -8,9 +8,7 @@
#endif #endif
#include "../../imgui.h" #include "../../imgui.h"
#define STB_IMAGE_STATIC #include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
#include "../shared/stb_image.h" // stb_image.h for PNG loading
// Glfw/Glew // Glfw/Glew
#define GLEW_STATIC #define GLEW_STATIC
@@ -180,42 +178,20 @@ void InitImGui()
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn; io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn; io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;
// Load font texture // Load font
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
io.Font = new ImFont(); 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()); IM_ASSERT(io.Font->IsLoaded());
int tex_x, tex_y, tex_comp; // Copy font texture
void* tex_data = stbi_load("../../extra_fonts/mplus-2m-medium_18.png", &tex_x, &tex_y, &tex_comp, 0); glGenTextures(1, &fontTex);
IM_ASSERT(tex_data != NULL); glBindTexture(GL_TEXTURE_2D, fontTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Automatically find white pixel from the texture we just loaded glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// (io.Font->TexUvForWhite needs to contains UV coordinates pointing to a white pixel in order to render solid objects) IM_ASSERT(io.Font->IsLoaded());
for (int tex_data_off = 0; tex_data_off < tex_x*tex_y; tex_data_off++) glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, io.Font->TexWidth, io.Font->TexHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, io.Font->TexPixels);
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);
} }
void UpdateImGui() void UpdateImGui()
@@ -74,8 +74,6 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\imconfig.h" /> <ClInclude Include="..\..\imconfig.h" />
<ClInclude Include="..\..\imgui.h" /> <ClInclude Include="..\..\imgui.h" />
<ClInclude Include="..\..\stb_textedit.h" />
<ClInclude Include="..\shared\stb_image.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
@@ -24,11 +24,5 @@
<ClInclude Include="..\..\imgui.h"> <ClInclude Include="..\..\imgui.h">
<Filter>imgui</Filter> <Filter>imgui</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\stb_textedit.h">
<Filter>imgui</Filter>
</ClInclude>
<ClInclude Include="..\shared\stb_image.h">
<Filter>sources</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
-3
View File
@@ -1,3 +0,0 @@
stb_image.h is used to load the PNG texture data by
opengl_example
directx11_example
File diff suppressed because it is too large Load Diff
+633 -292
View File
File diff suppressed because it is too large Load Diff
+38 -77
View File
@@ -1,4 +1,4 @@
// ImGui library v1.20 // ImGui library v1.30 wip
// See .cpp file for commentary. // See .cpp file for commentary.
// See ImGui::ShowTestWindow() for sample code. // See ImGui::ShowTestWindow() for sample code.
// Read 'Programmer guide' in .cpp for notes on how to setup ImGui in your codebase. // Read 'Programmer guide' in .cpp for notes on how to setup ImGui in your codebase.
@@ -129,7 +129,7 @@ public:
// - struct ImGuiTextBuffer // Text buffer for logging/accumulating text // - struct ImGuiTextBuffer // Text buffer for logging/accumulating text
// - struct ImGuiStorage // Custom key value storage (if you need to alter open/close states manually) // - struct ImGuiStorage // Custom key value storage (if you need to alter open/close states manually)
// - struct ImDrawList // Draw command list // - struct ImDrawList // Draw command list
// - struct ImFont // Bitmap font loader // - struct ImFont // TTF font loader, bake glyphs into bitmap
// ImGui End-user API // ImGui End-user API
// In a namespace so that user can add extra functions (e.g. Value() helpers for your vector or common types) // In a namespace so that user can add extra functions (e.g. Value() helpers for your vector or common types)
@@ -309,7 +309,6 @@ namespace ImGui
IMGUI_API float GetTime(); IMGUI_API float GetTime();
IMGUI_API int GetFrameCount(); IMGUI_API int GetFrameCount();
IMGUI_API const char* GetStyleColName(ImGuiCol idx); IMGUI_API const char* GetStyleColName(ImGuiCol idx);
IMGUI_API void GetDefaultFontData(const void** fnt_data, unsigned int* fnt_size, const void** png_data, unsigned int* png_size);
IMGUI_API ImVec2 CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f); IMGUI_API ImVec2 CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f);
} // namespace ImGui } // namespace ImGui
@@ -729,97 +728,59 @@ struct ImDrawList
IMGUI_API void AddText(ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f); IMGUI_API void AddText(ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f);
}; };
// Bitmap font data loader & renderer into vertices // TTF font loading and rendering
// Using the .fnt format exported by BMFont // NB: kerning pair are not supported (because some ImGui code does per-character CalcTextSize calls, need to turn it into something more state-ful to allow for kerning)
// - tool: http://www.angelcode.com/products/bmfont
// - file-format: http://www.angelcode.com/products/bmfont/doc/file_format.html
// Assume valid file data (won't handle invalid/malicious data)
// Handle a subset of the options, namely:
// - kerning pair are not supported (because some ImGui code does per-character CalcTextSize calls, need to turn it into something more state-ful to allow for kerning)
struct ImFont struct ImFont
{ {
struct FntInfo;
struct FntCommon;
struct FntGlyph;
struct FntKerning;
// Settings // Settings
float Scale; // = 1.0f // Base font scale, multiplied by the per-window font scale which you can adjust with SetFontScale() float Scale; // = 1.0f // Base font scale, multiplied by the per-window font scale which you can adjust with SetFontScale()
ImVec2 DisplayOffset; // = (0.0f,0.0f) // Offset font rendering by xx pixels ImVec2 DisplayOffset; // = (0.0f,0.0f) // Offset font rendering by xx pixels
ImVec2 TexUvForWhite; // = (0.0f,0.0f) // Font texture must have a white pixel at this UV coordinate. Adjust if you are using custom texture. ImWchar FallbackChar; // = '?' // Replacement glyph if one isn't found.
ImWchar FallbackChar; // = '?' // Replacement glyph is one isn't found.
// Data // Texture data
unsigned char* Data; // Raw data, content of .fnt file unsigned char* TexPixels; // 1 byte, 1 component per pixel. Total byte size of TexWidth * TexHeight
size_t DataSize; // int TexWidth;
bool DataOwned; // int TexHeight;
const FntInfo* Info; // (point into raw data) ImVec2 TexExtraDataPos; // Position of our rectangle where we draw non-font graphics
const FntCommon* Common; // (point into raw data) ImVec2 TexUvWhitePixel; // Texture coordinates to a white pixel (part of the TexExtraData block)
const FntGlyph* Glyphs; // (point into raw data)
size_t GlyphsCount; //
const FntKerning* Kerning; // (point into raw data) - NB: kerning is unsupported
size_t KerningCount; //
ImVector<const char*> Filenames; // (point into raw data)
ImVector<int> IndexLookup; // (built)
const FntGlyph* FallbackGlyph; // == FindGlyph(FontFallbackChar)
struct Glyph
{
ImWchar Codepoint;
signed short XAdvance;
signed short Width, Height;
signed short XOffset, YOffset;
float U0, V0, U1, V1; // Texture coordinates
};
// Runtime data
float FontSize; // Height of characters
ImVector<Glyph> Glyphs;
ImVector<int> IndexLookup;
const Glyph* FallbackGlyph; // == FindGlyph(FontFallbackChar)
// Methods
IMGUI_API ImFont(); IMGUI_API ImFont();
IMGUI_API ~ImFont() { Clear(); } IMGUI_API ~ImFont();
IMGUI_API bool LoadDefault();
IMGUI_API bool LoadFromMemory(const void* data, size_t data_size); IMGUI_API bool LoadFromFileTTF(const char* filename, float size_pixels, const ImWchar* glyph_ranges = NULL, int font_no = 0);
IMGUI_API bool LoadFromFile(const char* filename); IMGUI_API bool LoadFromMemoryTTF(const void* data, size_t data_size, float size_pixels, const ImWchar* glyph_ranges = NULL, int font_no = 0);
IMGUI_API void Clear(); IMGUI_API void Clear();
IMGUI_API void BuildLookupTable(); IMGUI_API void BuildLookupTable();
IMGUI_API const FntGlyph* FindGlyph(unsigned short c) const; IMGUI_API const Glyph* FindGlyph(unsigned short c) const;
IMGUI_API bool IsLoaded() const { return Info != NULL && Common != NULL && Glyphs != NULL; } IMGUI_API bool IsLoaded() const { return TexPixels != NULL && !Glyphs.empty(); }
// Retrieve list of common Unicode ranges (2 value per range, values are inclusive, zero-terminated list)
static IMGUI_API const ImWchar* GetGlyphRangesDefault(); // Basic Latin, Extended Latin + a few more
static IMGUI_API const ImWchar* GetGlyphRangesJapanese(); // Default + Hiragana, Katakana, Half-Width, Selection of 1946 Ideographs
static IMGUI_API const ImWchar* GetGlyphRangesChinese(); // Japanese + full set of about 21000 CJK Unified Ideographs
// 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable. // 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.
// 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable. // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.
IMGUI_API ImVec2 CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8 IMGUI_API ImVec2 CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8
IMGUI_API ImVec2 CalcTextSizeW(float size, float max_width, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL) const; // wchar IMGUI_API ImVec2 CalcTextSizeW(float size, float max_width, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL) const; // wchar
IMGUI_API void RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, ImDrawVert*& out_vertices, float wrap_width = 0.0f) const; IMGUI_API void RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, ImDrawVert*& out_vertices, float wrap_width = 0.0f) const;
IMGUI_API const char* CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const; IMGUI_API const char* CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const;
#pragma pack(push, 1)
struct FntInfo
{
signed short FontSize;
unsigned char BitField; // bit 0: smooth, bit 1: unicode, bit 2: italic, bit 3: bold, bit 4: fixedHeight, bits 5-7: reserved
unsigned char CharSet;
unsigned short StretchH;
unsigned char AA;
unsigned char PaddingUp, PaddingRight, PaddingDown, PaddingLeft;
unsigned char SpacingHoriz, SpacingVert, Outline;
//char FontName[];
};
struct FntCommon
{
unsigned short LineHeight, Base;
unsigned short ScaleW, ScaleH;
unsigned short Pages;
unsigned char BitField;
unsigned char Channels[4];
};
struct FntGlyph
{
unsigned int Id;
unsigned short X, Y, Width, Height;
signed short XOffset, YOffset;
signed short XAdvance;
unsigned char Page;
unsigned char Channel;
};
struct FntKerning
{
unsigned int IdFirst;
unsigned int IdSecond;
signed short Amount;
};
#pragma pack(pop)
}; };
//---- Include imgui_user.h at the end of imgui.h //---- Include imgui_user.h at the end of imgui.h
+546
View File
File diff suppressed because it is too large Load Diff
+2646
View File
File diff suppressed because it is too large Load Diff