소스 검색

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

This reverts commit 1dc076c2e8edf13bbf6860ba1029cc6286b5e384.
Jeffery Myers 11 달 전
부모
커밋
57efef06f6
1개의 변경된 파일21개의 추가작업 그리고 36개의 파일을 삭제
  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 rlImGuiIsSuperDown() { return IsKeyDown(KEY_RIGHT_SUPER) || IsKeyDown(KEY_LEFT_SUPER); }
 
-std::map<size_t, Texture2D*> FontTextures;
-
 void ReloadFonts(void)
 {
     ImGuiIO& io = ImGui::GetIO();
@@ -76,20 +74,17 @@ void ReloadFonts(void)
     Image image = GenImageColor(width, height, BLANK);
     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);
     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*) 
@@ -203,11 +198,12 @@ static void ImGuiRenderTriangles(unsigned int count, int indexStart, const ImVec
     if (count < 3)
         return;
 
+    Texture* texture = (Texture*)texturePtr;
 
-    size_t textureId = reinterpret_cast<size_t>(texturePtr);
+    unsigned int textureId = (texture == nullptr) ? 0 : texture->id;
 
     rlBegin(RL_TRIANGLES);
-    rlSetTexture(uint32_t(textureId));
+    rlSetTexture(textureId);
 
     for (unsigned int i = 0; i <= (count - 3); i += 3)
     {
@@ -506,8 +502,7 @@ void rlImGuiImage(const Texture* image)
     if (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)
@@ -517,9 +512,8 @@ bool rlImGuiImageButton(const char* name, const Texture* image)
     
     if (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)
@@ -530,9 +524,7 @@ bool rlImGuiImageButtonSize(const char* name, const Texture* image, ImVec2 size)
     if (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)
@@ -542,10 +534,8 @@ void rlImGuiImageSize(const Texture* image, int width, int height)
     
     if (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)
@@ -556,9 +546,7 @@ void rlImGuiImageSizeV(const Texture* image, Vector2 size)
     if (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)
@@ -594,9 +582,7 @@ void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Recta
         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)
@@ -663,15 +649,14 @@ void Imgui_ImplRaylib_BuildFontAtlas(void)
 void ImGui_ImplRaylib_Shutdown()
 {
     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;
 }