Browse Source

Revert "Use texture ID with ImGui so that the calling texture pointer can go out of scope."

This reverts commit 1dc076c2e8edf13bbf6860ba1029cc6286b5e384.
Jeffery Myers 11 months ago
parent
commit
57efef06f6
1 changed files with 21 additions and 36 deletions
  1. 21 36
      rlImGui.cpp

+ 21 - 36
rlImGui.cpp

@@ -63,8 +63,6 @@ bool rlImGuiIsShiftDown() { return IsKeyDown(KEY_RIGHT_SHIFT) || IsKeyDown(KEY_L
 bool rlImGuiIsAltDown() { return IsKeyDown(KEY_RIGHT_ALT) || IsKeyDown(KEY_LEFT_ALT); }
 bool rlImGuiIsAltDown() { return IsKeyDown(KEY_RIGHT_ALT) || IsKeyDown(KEY_LEFT_ALT); }
 bool rlImGuiIsSuperDown() { return IsKeyDown(KEY_RIGHT_SUPER) || IsKeyDown(KEY_LEFT_SUPER); }
 bool rlImGuiIsSuperDown() { return IsKeyDown(KEY_RIGHT_SUPER) || IsKeyDown(KEY_LEFT_SUPER); }
 
 
-std::map<size_t, Texture2D*> FontTextures;
-
 void ReloadFonts(void)
 void ReloadFonts(void)
 {
 {
     ImGuiIO& io = ImGui::GetIO();
     ImGuiIO& io = ImGui::GetIO();
@@ -76,20 +74,17 @@ void ReloadFonts(void)
     Image image = GenImageColor(width, height, BLANK);
     Image image = GenImageColor(width, height, BLANK);
     memcpy(image.data, pixels, width * height * 4);
     memcpy(image.data, pixels, width * height * 4);
 
 
-    size_t textureId = reinterpret_cast<size_t>(io.Fonts->TexID);
-    if (FontTextures.find(textureId) != FontTextures.end())
+    Texture2D* fontTexture = (Texture2D*)io.Fonts->TexID;
+    if (fontTexture && fontTexture->id != 0)
     {
     {
-        UnloadTexture(*FontTextures[textureId]);
-        MemFree(FontTextures[textureId]);
-        FontTextures.erase(FontTextures.find(textureId));
+        UnloadTexture(*fontTexture);
+        MemFree(fontTexture);
     }
     }
 
 
-    Texture2D* fontTexture = (Texture2D*)MemAlloc(sizeof(Texture2D));
+    fontTexture = (Texture2D*)MemAlloc(sizeof(Texture2D));
     *fontTexture = LoadTextureFromImage(image);
     *fontTexture = LoadTextureFromImage(image);
     UnloadImage(image);
     UnloadImage(image);
-    textureId = size_t(fontTexture->id);
-    FontTextures.insert_or_assign(textureId, fontTexture);
-    io.Fonts->TexID = (ImTextureID)textureId;
+    io.Fonts->TexID = fontTexture;
 }
 }
 
 
 static const char* GetClipTextCallback(void*) 
 static const char* GetClipTextCallback(void*) 
@@ -203,11 +198,12 @@ static void ImGuiRenderTriangles(unsigned int count, int indexStart, const ImVec
     if (count < 3)
     if (count < 3)
         return;
         return;
 
 
+    Texture* texture = (Texture*)texturePtr;
 
 
-    size_t textureId = reinterpret_cast<size_t>(texturePtr);
+    unsigned int textureId = (texture == nullptr) ? 0 : texture->id;
 
 
     rlBegin(RL_TRIANGLES);
     rlBegin(RL_TRIANGLES);
-    rlSetTexture(uint32_t(textureId));
+    rlSetTexture(textureId);
 
 
     for (unsigned int i = 0; i <= (count - 3); i += 3)
     for (unsigned int i = 0; i <= (count - 3); i += 3)
     {
     {
@@ -506,8 +502,7 @@ void rlImGuiImage(const Texture* image)
     if (GlobalContext)
     if (GlobalContext)
         ImGui::SetCurrentContext(GlobalContext);
         ImGui::SetCurrentContext(GlobalContext);
     
     
-    size_t id = image->id;
-    ImGui::Image((ImTextureID)id, ImVec2(float(image->width), float(image->height)));
+    ImGui::Image((ImTextureID)image, ImVec2(float(image->width), float(image->height)));
 }
 }
 
 
 bool rlImGuiImageButton(const char* name, const Texture* image)
 bool rlImGuiImageButton(const char* name, const Texture* image)
@@ -517,9 +512,8 @@ bool rlImGuiImageButton(const char* name, const Texture* image)
     
     
     if (GlobalContext)
     if (GlobalContext)
         ImGui::SetCurrentContext(GlobalContext);
         ImGui::SetCurrentContext(GlobalContext);
-
-    size_t id = image->id;
-    return ImGui::ImageButton(name, (ImTextureID)id, ImVec2(float(image->width), float(image->height)));
+    
+    return ImGui::ImageButton(name, (ImTextureID)image, ImVec2(float(image->width), float(image->height)));
 }
 }
 
 
 bool rlImGuiImageButtonSize(const char* name, const Texture* image, ImVec2 size)
 bool rlImGuiImageButtonSize(const char* name, const Texture* image, ImVec2 size)
@@ -530,9 +524,7 @@ bool rlImGuiImageButtonSize(const char* name, const Texture* image, ImVec2 size)
     if (GlobalContext)
     if (GlobalContext)
         ImGui::SetCurrentContext(GlobalContext);
         ImGui::SetCurrentContext(GlobalContext);
    
    
-    size_t id = image->id;
-
-    return ImGui::ImageButton(name, (ImTextureID)id, size);
+    return ImGui::ImageButton(name, (ImTextureID)image, size);
 }
 }
 
 
 void rlImGuiImageSize(const Texture* image, int width, int height)
 void rlImGuiImageSize(const Texture* image, int width, int height)
@@ -542,10 +534,8 @@ void rlImGuiImageSize(const Texture* image, int width, int height)
     
     
     if (GlobalContext)
     if (GlobalContext)
         ImGui::SetCurrentContext(GlobalContext);
         ImGui::SetCurrentContext(GlobalContext);
-
-    size_t id = image->id;
     
     
-    ImGui::Image((ImTextureID)id, ImVec2(float(width), float(height)));
+    ImGui::Image((ImTextureID)image, ImVec2(float(width), float(height)));
 }
 }
 
 
 void rlImGuiImageSizeV(const Texture* image, Vector2 size)
 void rlImGuiImageSizeV(const Texture* image, Vector2 size)
@@ -556,9 +546,7 @@ void rlImGuiImageSizeV(const Texture* image, Vector2 size)
     if (GlobalContext)
     if (GlobalContext)
         ImGui::SetCurrentContext(GlobalContext);
         ImGui::SetCurrentContext(GlobalContext);
     
     
-    size_t id = image->id;
-
-    ImGui::Image((ImTextureID)id, ImVec2(size.x, size.y));
+    ImGui::Image((ImTextureID)image, ImVec2(size.x, size.y));
 }
 }
 
 
 void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Rectangle sourceRect)
 void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Rectangle sourceRect)
@@ -594,9 +582,7 @@ void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Recta
         uv1.y = uv0.y + (float)(sourceRect.height / image->height);
         uv1.y = uv0.y + (float)(sourceRect.height / image->height);
     }
     }
 
 
-    size_t id = image->id;
-
-    ImGui::Image((ImTextureID)id, ImVec2(float(destWidth), float(destHeight)), uv0, uv1);
+    ImGui::Image((ImTextureID)image, ImVec2(float(destWidth), float(destHeight)), uv0, uv1);
 }
 }
 
 
 void rlImGuiImageRenderTexture(const RenderTexture* image)
 void rlImGuiImageRenderTexture(const RenderTexture* image)
@@ -663,15 +649,14 @@ void Imgui_ImplRaylib_BuildFontAtlas(void)
 void ImGui_ImplRaylib_Shutdown()
 void ImGui_ImplRaylib_Shutdown()
 {
 {
     ImGuiIO& io =ImGui::GetIO();
     ImGuiIO& io =ImGui::GetIO();
+    Texture2D* fontTexture = (Texture2D*)io.Fonts->TexID;
 
 
-    size_t fontTextureId = reinterpret_cast<size_t>(io.Fonts->TexID);
-    if (FontTextures.find(fontTextureId) != FontTextures.end())
+    if (fontTexture)
     {
     {
-        UnloadTexture(*FontTextures[fontTextureId]);
-        MemFree(FontTextures[fontTextureId]);
-
-        FontTextures.erase(FontTextures.find(fontTextureId));
+        UnloadTexture(*fontTexture);
+        MemFree(fontTexture);
     }
     }
+
     io.Fonts->TexID = 0;
     io.Fonts->TexID = 0;
 }
 }