diff options
| author | Ruwuchi <73458244+Ruwuchi@users.noreply.github.com> | 2021-04-30 05:19:17 -0300 |
|---|---|---|
| committer | Ruwuchi <73458244+Ruwuchi@users.noreply.github.com> | 2021-04-30 05:19:17 -0300 |
| commit | 235be66e07d5714007ee976bb60db0a28e9261e5 (patch) | |
| tree | 3ad986e68e96f8941b4407b033343b8b4ebcd2bd /imgui_impl_raylib.cpp | |
| parent | 27dda70ecec46db9c2e5e3f06cb9323f8a58a5f0 (diff) | |
| download | imgui-impl-raylib-235be66e07d5714007ee976bb60db0a28e9261e5.tar.gz imgui-impl-raylib-235be66e07d5714007ee976bb60db0a28e9261e5.zip | |
Added Compatibility settings and support for UTF-8.
Diffstat (limited to 'imgui_impl_raylib.cpp')
| -rw-r--r-- | imgui_impl_raylib.cpp | 124 |
1 files changed, 117 insertions, 7 deletions
diff --git a/imgui_impl_raylib.cpp b/imgui_impl_raylib.cpp index 308bf84..51b7448 100644 --- a/imgui_impl_raylib.cpp +++ b/imgui_impl_raylib.cpp @@ -1,9 +1,11 @@ -#include <imgui.h> -#include <raylib.h> - #include "imgui_impl_raylib.h" +#include <raylib.h> +#include <rlgl.h> +#include <memory> static double g_Time = 0.0; +static bool g_UnloadAtlas = false; +static int g_AtlasTexID = 0; static const char* ImGui_ImplRaylib_GetClipboardText(void*) { @@ -50,11 +52,19 @@ bool ImGui_ImplRaylib_Init() io.GetClipboardTextFn = ImGui_ImplRaylib_GetClipboardText; io.ClipboardUserData = NULL; +#ifdef AUTO_FONTATLAS + ImGui_ImplRaylib_LoadDefaultFontAtlas(); +#endif + return true; } void ImGui_ImplRaylib_Shutdown() { + if (g_UnloadAtlas) { + ImGuiIO& io = ImGui::GetIO(); + io.Fonts->ClearTexData(); + } g_Time = 0.0; } @@ -97,12 +107,12 @@ static void ImGui_ImplRaylib_UpdateMouseCursor() void ImGui_ImplRaylib_NewFrame() { - ImGuiIO &io = ImGui::GetIO(); + ImGuiIO& io = ImGui::GetIO(); - io.DisplaySize = ImVec2((float) GetScreenWidth(), (float) GetScreenHeight()); + io.DisplaySize = ImVec2((float)GetScreenWidth(), (float)GetScreenHeight()); double current_time = GetTime(); - io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f); + io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f); g_Time = current_time; io.KeyCtrl = IsKeyDown(KEY_RIGHT_CONTROL) || IsKeyDown(KEY_LEFT_CONTROL); @@ -237,8 +247,108 @@ bool ImGui_ImplRaylib_ProcessEvent() // Uncomment the three lines below if using raylib earlier than version 3. //if (GetKeyPressed() != -1) //{ - io.AddInputCharacter(GetKeyPressed()); +#ifdef ENABLE_SCODETOUTF8 + int length; // Length was only ever created to be passed to CodepointToUtf8(), since it doesn't check for nullptrs. + io.AddInputCharactersUTF8(CodepointToUtf8(GetCharPressed(), &length)); + (void)length; // Silencing the compiler warnings. +#else + io.AddInputCharacter(GetKeyPressed()); +#endif //} return true; } + +#ifdef COMPATIBILITY_MODE +void ImGui_ImplRaylib_LoadDefaultFontAtlas() +{ + if (!g_UnloadAtlas) { + ImGuiIO& io = ImGui::GetIO(); + unsigned char* pixels = NULL; + int width, height, bpp; + Image image; + io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, &bpp); + + unsigned int size = GetPixelDataSize(width, height, 7); + image.data = malloc(size); + memcpy(image.data, pixels, size); + image.width = width; + image.height = height; + image.mipmaps = 1; + image.format = UNCOMPRESSED_R8G8B8A8; + Texture2D tex = LoadTextureFromImage(image); + g_AtlasTexID = tex.id; + io.Fonts->TexID = (void*)&g_AtlasTexID; + free(pixels); + free(image.data); + g_UnloadAtlas = true; + } +}; + +// Code originally provided by WEREMSOFT. +void ImGui_ImplRaylib_Render(ImDrawData* draw_data) +{ + + auto DrawTriangleVertex = [](ImDrawVert idx_vert) -> void { + Color* c = (Color*)&idx_vert.col; + rlColor4ub(c->r, c->g, c->b, c->a); + rlTexCoord2f(idx_vert.uv.x, idx_vert.uv.y); + rlVertex2f(idx_vert.pos.x, idx_vert.pos.y); + }; + + rlDisableBackfaceCulling(); + for (int n = 0; n < draw_data->CmdListsCount; n++) + { + const ImDrawList* cmd_list = draw_data->CmdLists[n]; + const ImDrawVert* vtx_buffer = cmd_list->VtxBuffer.Data; // vertex buffer generated by Dear ImGui + const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data; // index buffer generated by Dear ImGui + for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) + { + const ImDrawCmd* pcmd = &(cmd_list->CmdBuffer.Data)[cmd_i]; // cmd_list->CmdBuffer->data[cmd_i]; + if (pcmd->UserCallback) + { + pcmd->UserCallback(cmd_list, pcmd); + } + else + { + ImVec2 pos = draw_data->DisplayPos; + int rectX = (int)(pcmd->ClipRect.x - pos.x); + int rectY = (int)(pcmd->ClipRect.y - pos.y); + int rectW = (int)(pcmd->ClipRect.z - rectX); + int rectH = (int)(pcmd->ClipRect.w - rectY); + BeginScissorMode(rectX, rectY, rectW, rectH); + { + unsigned int* ti = (unsigned int*)pcmd->TextureId; + for (unsigned int i = 0; i <= (pcmd->ElemCount - 3); i += 3) + { + rlPushMatrix(); + rlBegin(RL_TRIANGLES); + rlEnableTexture(*ti); + + ImDrawIdx index; + ImDrawVert vertex; + + index = idx_buffer[i]; + vertex = vtx_buffer[index]; + DrawTriangleVertex(vertex); + + index = idx_buffer[i + 2]; + vertex = vtx_buffer[index]; + DrawTriangleVertex(vertex); + + index = idx_buffer[i + 1]; + vertex = vtx_buffer[index]; + DrawTriangleVertex(vertex); + rlDisableTexture(); + rlEnd(); + rlPopMatrix(); + } + } + } + idx_buffer += pcmd->ElemCount; + } + } + EndScissorMode(); + rlEnableBackfaceCulling(); +} +#endif
\ No newline at end of file |
