mirror of
https://github.com/Drezil/imgui.git
synced 2025-07-04 12:08:47 +02:00
Examples: all supports TextureID in renderer, added LoadFontTexture() function.
This commit is contained in:
@ -17,7 +17,6 @@
|
||||
#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
|
||||
|
||||
static GLFWwindow* window;
|
||||
static GLuint fontTex;
|
||||
static bool mousePressed[2] = { false, false };
|
||||
|
||||
// Shader variables
|
||||
@ -43,10 +42,7 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
|
||||
// Setup texture
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, fontTex);
|
||||
|
||||
// Setup orthographic projection matrix
|
||||
const float width = ImGui::GetIO().DisplaySize.x;
|
||||
@ -96,6 +92,7 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
|
||||
const ImDrawCmd* pcmd_end = cmd_list->commands.end();
|
||||
for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, (GLuint)pcmd->texture_id);
|
||||
glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
|
||||
glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
|
||||
vtx_offset += pcmd->vtx_count;
|
||||
@ -242,6 +239,19 @@ void InitGL()
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
void LoadFontTexture(ImFont* font)
|
||||
{
|
||||
IM_ASSERT(font && font->IsLoaded());
|
||||
|
||||
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);
|
||||
font->TexID = (void *)tex_id;
|
||||
}
|
||||
|
||||
void InitImGui()
|
||||
{
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
@ -274,15 +284,7 @@ void InitImGui()
|
||||
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_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);
|
||||
LoadFontTexture(io.Font);
|
||||
}
|
||||
|
||||
void UpdateImGui()
|
||||
@ -342,6 +344,11 @@ 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