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 | |
| parent | 27dda70ecec46db9c2e5e3f06cb9323f8a58a5f0 (diff) | |
| download | imgui-impl-raylib-235be66e07d5714007ee976bb60db0a28e9261e5.tar.gz imgui-impl-raylib-235be66e07d5714007ee976bb60db0a28e9261e5.zip | |
Added Compatibility settings and support for UTF-8.
| -rw-r--r-- | imgui_impl_raylib.cpp | 124 | ||||
| -rw-r--r-- | imgui_impl_raylib.h | 26 | ||||
| -rw-r--r-- | imgui_impl_raylib_config.h | 20 |
3 files changed, 159 insertions, 11 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 diff --git a/imgui_impl_raylib.h b/imgui_impl_raylib.h index 7167c9a..8c93897 100644 --- a/imgui_impl_raylib.h +++ b/imgui_impl_raylib.h @@ -1,14 +1,32 @@ #ifndef IMGUI_IMPL_RAYLIB #define IMGUI_IMPL_RAYLIB +/* NOTE: I've added a few macros to deal with compatibility. + Most are turned off by default to keep it as close as possible to the original code, + but ImGui has its quirks when it comes to GL2. I'll be integrating part of WEREMSOFT's rendering code. + + (https://github.com/WEREMSOFT/c99-raylib-cimgui-template/). +*/ + +// The compiler whines about IMGUI_IMPL_API not being defined, so I'm leaving this here. +#include <imgui.h> + +// Config macros +#include "imgui_impl_raylib_config.h" + #if defined(__cplusplus) extern "C" { #endif -IMGUI_IMPL_API bool ImGui_ImplRaylib_Init(); -IMGUI_IMPL_API void ImGui_ImplRaylib_Shutdown(); -IMGUI_IMPL_API void ImGui_ImplRaylib_NewFrame(); -IMGUI_IMPL_API bool ImGui_ImplRaylib_ProcessEvent(); + IMGUI_IMPL_API bool ImGui_ImplRaylib_Init(); + IMGUI_IMPL_API void ImGui_ImplRaylib_Shutdown(); + IMGUI_IMPL_API void ImGui_ImplRaylib_NewFrame(); + IMGUI_IMPL_API bool ImGui_ImplRaylib_ProcessEvent(); + +#ifdef COMPATIBILITY_MODE + IMGUI_IMPL_API void ImGui_ImplRaylib_LoadDefaultFontAtlas(); + IMGUI_IMPL_API void ImGui_ImplRaylib_Render(ImDrawData* draw_data); +#endif #if defined(__cplusplus) } diff --git a/imgui_impl_raylib_config.h b/imgui_impl_raylib_config.h new file mode 100644 index 0000000..75973e0 --- /dev/null +++ b/imgui_impl_raylib_config.h @@ -0,0 +1,20 @@ +#ifndef H_IM_RAYLIB_CONFIG +#define H_IM_RAYLIB_CONFIG + +/* This file contains a few macros for you to play with when using the Raylib ImGui implementation. + To use them, just uncomment the macro under its description, and it should be all set! +*/ + +// COMPATIBILITY_MODE Toggles the integrated rlgl code. +//#define COMPATIBILITY_MODE + +// AUTO_FONTATLAS REQUIRES COMPATIBILITY - Can be done manually, but is pretty useful... +//#define AUTO_FONTATLAS + +/* ENABLE_SCODETOUTF8 Toggled by default, since the code was passing + raylib's scancodes as codepoints, and ImGui uses UTF-8 + on widgets like InputText. +*/ +#define ENABLE_SCODETOUTF8 + +#endif
\ No newline at end of file |
