From 8ab2942716138c087a9e5b4621d93fc38ea31c11 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 11 Aug 2014 14:51:22 +0100 Subject: [PATCH] Added PixelCenterOffset for OpenGL/DirectX compatibility. --- examples/directx9_example/main.cpp | 1 + examples/opengl_example/main.cpp | 1 + imgui.cpp | 11 +++++++---- imgui.h | 1 + 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/examples/directx9_example/main.cpp b/examples/directx9_example/main.cpp index fd25f010..017a3a91 100644 --- a/examples/directx9_example/main.cpp +++ b/examples/directx9_example/main.cpp @@ -293,6 +293,7 @@ void InitImGui() io.KeyMap[ImGuiKey_X] = 'X'; io.KeyMap[ImGuiKey_Y] = 'Y'; io.KeyMap[ImGuiKey_Z] = 'Z'; + io.PixelCenterOffset = 0.0f; io.RenderDrawListsFn = ImImpl_RenderDrawLists; io.SetClipboardTextFn = ImImpl_SetClipboardTextFn; diff --git a/examples/opengl_example/main.cpp b/examples/opengl_example/main.cpp index 909ffa98..2d23a746 100644 --- a/examples/opengl_example/main.cpp +++ b/examples/opengl_example/main.cpp @@ -297,6 +297,7 @@ void InitImGui() io.KeyMap[ImGuiKey_X] = GLFW_KEY_X; io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y; io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z; + io.PixelCenterOffset = 0.5f; io.RenderDrawListsFn = ImImpl_RenderDrawLists; io.SetClipboardTextFn = ImImpl_SetClipboardTextFn; diff --git a/imgui.cpp b/imgui.cpp index 1ce8cce1..44901779 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -262,6 +262,7 @@ ImGuiIO::ImGuiIO() LogFilename = "imgui_log.txt"; Font = NULL; FontAllowScaling = false; + PixelCenterOffset = 0.5f; MousePos = ImVec2(-1,-1); MousePosPrev = ImVec2(-1,-1); MouseDoubleClickTime = 0.30f; @@ -5003,6 +5004,8 @@ void ImBitmapFont::RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& c float line_width = 0.0f; const ImVec4 clip_rect = clip_rect_ref; + const float uv_offset = GImGui.IO.PixelCenterOffset; + float x = pos.x; float y = pos.y; for (const char* s = text_begin; s < text_end; s++) @@ -5039,10 +5042,10 @@ void ImBitmapFont::RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& c continue; } - const float s1 = (0.0f + glyph->X) * tex_scale_x; - const float t1 = (0.0f + glyph->Y) * tex_scale_y; - const float s2 = (0.0f + glyph->X + glyph->Width) * tex_scale_x; - const float t2 = (0.0f + glyph->Y + glyph->Height) * tex_scale_y; + const float s1 = (uv_offset + glyph->X) * tex_scale_x; + const float t1 = (uv_offset + glyph->Y) * tex_scale_y; + const float s2 = (uv_offset + glyph->X + glyph->Width) * tex_scale_x; + const float t2 = (uv_offset + glyph->Y + glyph->Height) * tex_scale_y; out_vertices[0].pos = ImVec2(x1, y1); out_vertices[0].uv = ImVec2(s1, t1); diff --git a/imgui.h b/imgui.h index 3c750577..d2e90417 100644 --- a/imgui.h +++ b/imgui.h @@ -373,6 +373,7 @@ struct ImGuiIO ImFont Font; // // Gets passed to text functions. Typedef ImFont to the type you want (ImBitmapFont* or your own font). float FontHeight; // // Default font height, must be the vertical distance between two lines of text, aka == CalcTextSize(" ").y bool FontAllowScaling; // = false // Set to allow scaling text with CTRL+Wheel. + float PixelCenterOffset; // = 0.5f // Set to 0.0f for DirectX <= 9, 0.5f for Direct3D >= 10 and OpenGL. // Settings - Functions (fill once) void (*RenderDrawListsFn)(ImDrawList** const draw_lists, int count); // Required