From 2fe3a24c5c1bcb6c1a357670c4cfb5f2150b7a2e Mon Sep 17 00:00:00 2001 From: zero <103244351+zerohorsepower@users.noreply.github.com> Date: Fri, 18 Jul 2025 16:50:06 -0300 Subject: [PATCH 1/3] Add rlImGuiImageFit into rlImGui.h --- rlImGui.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rlImGui.h b/rlImGui.h index 92d2e2c..f7029d9 100644 --- a/rlImGui.h +++ b/rlImGui.h @@ -159,6 +159,14 @@ RLIMGUIAPI void rlImGuiImageSizeV(const Texture* image, Vector2 size); /// The portion of the texture to draw as an image. Negative values for the width and height will flip the image RLIMGUIAPI void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Rectangle sourceRect); +/// +/// Draws a texture as an image an ImGui Context +/// Fits the texture to the available content area +/// +/// The texture to draw +/// When true the image will be centered in the content area +RLIMGUIAPI void rlImGuiImageFit(const Texture* image, bool center); + /// /// Draws a render texture as an image an ImGui Context, automatically flipping the Y axis so it will show correctly on screen /// From 66f4d8f9a0f6d6330a12246a273be197e683d64b Mon Sep 17 00:00:00 2001 From: zero <103244351+zerohorsepower@users.noreply.github.com> Date: Fri, 18 Jul 2025 16:53:13 -0300 Subject: [PATCH 2/3] Add rlImGuiImageFit into rlImGui.cpp --- rlImGui.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/rlImGui.cpp b/rlImGui.cpp index 0c2f151..a004cdb 100644 --- a/rlImGui.cpp +++ b/rlImGui.cpp @@ -609,6 +609,37 @@ void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Recta ImGui::Image((ImTextureID)image->id, ImVec2(float(destWidth), float(destHeight)), uv0, uv1); } +void rlImGuiImageFit(const Texture* image, bool center) +{ + if (!image) + return; + + if (GlobalContext) + ImGui::SetCurrentContext(GlobalContext); + + ImVec2 area = ImGui::GetContentRegionAvail(); + + float scale = area.x / image->width; + + float y = image->height * scale; + if (y > area.y) + { + scale = area.y / image->height; + } + + int sizeX = int(image->width * scale); + int sizeY = int(image->height * scale); + + if (center) + { + ImGui::SetCursorPosX(0); + ImGui::SetCursorPosX(area.x/2 - sizeX/2); + ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (area.y / 2 - sizeY / 2)); + } + + rlImGuiImageRect(image, sizeX, sizeY, Rectangle{ 0,0, float(image->width), float(image->height) }); +} + void rlImGuiImageRenderTexture(const RenderTexture* image) { if (!image) From e30794aa6ee60f2d85dd484b8dabce597b7b9dc2 Mon Sep 17 00:00:00 2001 From: zero <103244351+zerohorsepower@users.noreply.github.com> Date: Fri, 18 Jul 2025 16:54:20 -0300 Subject: [PATCH 3/3] Add rlImGuiImageFit function into "Images" section --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fc217fd..c3259c0 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ void rlImGuiImage(const Texture *image); void rlImGuiImageSize(const Texture *image, int width, int height); void rlImGuiImageSizeV(const Texture* image, Vector2 size); void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Rectangle sourceRect); +void rlImGuiImageFit(const Texture* image, bool center); void rlImGuiImageRenderTexture(const RenderTexture* image); void rlImGuiImageRenderTextureFit(const RenderTexture* image, bool center);