Skip to content

Commit fb17d4a

Browse files
committed
C Textures are now garbage collected
1 parent 5dea503 commit fb17d4a

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

Framework/C/Rendering/CTexture.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "CTexture.h"
2+
#include <Core/Global.hpp>
23
#include <Renderer/Texture.hpp>
34
#include <stb_image.h>
45

@@ -11,8 +12,7 @@ void UImGui_Texture_defaultFreeFunc(void* data)
1112

1213
UImGui_CTexture* UImGui_Texture_init(const UImGui_String file)
1314
{
14-
auto* texture = new UImGui::Texture(file);
15-
return texture;
15+
return &UImGui::Global::get().deallocationStruct.textures.emplace_back(file);
1616
}
1717

1818
void UImGui_Texture_load(UImGui_CTexture* texture, void* data, const UImGui_FVector2 size, const uint32_t depth,
@@ -46,7 +46,16 @@ void UImGui_Texture_setCustomSaveFunction(UImGui_CTexture* texture, const UImGui
4646
cast(texture)->setCustomSaveFunction(f);
4747
}
4848

49-
void UImGui_Texture_free(UImGui_CTexture* texture)
49+
void UImGui_Texture_release(UImGui_CTexture* texture)
5050
{
51-
delete static_cast<UImGui::Texture*>(texture);
51+
// Could use pointer arithmetic but that would be really unsafe
52+
auto& textures = UImGui::Global::get().deallocationStruct.textures;
53+
for (size_t i = 0; i < textures.size(); i++)
54+
{
55+
if (texture == &textures[i])
56+
{
57+
textures.erase(textures.begin() + static_cast<decltype(UImGui::Global::get().deallocationStruct.textures)::difference_type>(i));
58+
return;
59+
}
60+
}
5261
}

Framework/C/Rendering/CTexture.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ extern "C"
9494

9595
// Frees the Texture handle
9696
// Event safety - Post-begin
97-
UIMGUI_PUBLIC_API void UImGui_Texture_free(UImGui_CTexture* texture);
97+
UIMGUI_PUBLIC_API void UImGui_Texture_release(UImGui_CTexture* texture);
9898
#ifdef __cplusplus
9999
};
100100
#endif

Framework/Core/CDeallocation.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <Types.hpp>
33
#include <C/Internal/CMonitor.h>
4+
#include <Renderer/Texture.hpp>
45

56
namespace UImGui
67
{
@@ -9,5 +10,6 @@ namespace UImGui
910
{
1011
std::vector<FString> keyStrings;
1112
std::vector<UImGui_CMonitorData> monitors;
13+
std::vector<Texture> textures;
1214
};
1315
}

0 commit comments

Comments
 (0)