[Materials] Correctly cleanup images

This commit is contained in:
momo5502 2017-06-05 14:31:45 +02:00
parent 78e7d6375d
commit 38b64628ef
3 changed files with 42 additions and 14 deletions

View File

@ -5,6 +5,7 @@ namespace Components
int Materials::ImageNameLength; int Materials::ImageNameLength;
Utils::Hook Materials::ImageVersionCheckHook; Utils::Hook Materials::ImageVersionCheckHook;
std::vector<Game::GfxImage*> Materials::ImageTable;
std::vector<Game::Material*> Materials::MaterialTable; std::vector<Game::Material*> Materials::MaterialTable;
Game::Material* Materials::Create(std::string name, Game::GfxImage* image) Game::Material* Materials::Create(std::string name, Game::GfxImage* image)
@ -51,12 +52,6 @@ namespace Components
{ {
if (!material) return; if (!material) return;
auto mat = std::find(Materials::MaterialTable.begin(), Materials::MaterialTable.end(), material);
if(mat != Materials::MaterialTable.end())
{
Materials::MaterialTable.erase(mat);
}
if (deleteImage) if (deleteImage)
{ {
for (char i = 0; i < material->textureCount; ++i) for (char i = 0; i < material->textureCount; ++i)
@ -68,6 +63,12 @@ namespace Components
Utils::Memory::GetAllocator()->free(material->textureTable); Utils::Memory::GetAllocator()->free(material->textureTable);
Utils::Memory::GetAllocator()->free(material->name); Utils::Memory::GetAllocator()->free(material->name);
Utils::Memory::GetAllocator()->free(material); Utils::Memory::GetAllocator()->free(material);
auto mat = std::find(Materials::MaterialTable.begin(), Materials::MaterialTable.end(), material);
if (mat != Materials::MaterialTable.end())
{
Materials::MaterialTable.erase(mat);
}
} }
Game::GfxImage* Materials::CreateImage(std::string name, unsigned int width, unsigned int height, unsigned int depth, unsigned int flags, _D3DFORMAT format) Game::GfxImage* Materials::CreateImage(std::string name, unsigned int width, unsigned int height, unsigned int depth, unsigned int flags, _D3DFORMAT format)
@ -77,6 +78,8 @@ namespace Components
Game::Image_Setup(image, width, height, depth, flags, format); Game::Image_Setup(image, width, height, depth, flags, format);
Materials::ImageTable.push_back(image);
return image; return image;
} }
@ -88,6 +91,12 @@ namespace Components
Utils::Memory::GetAllocator()->free(image->name); Utils::Memory::GetAllocator()->free(image->name);
Utils::Memory::GetAllocator()->free(image); Utils::Memory::GetAllocator()->free(image);
auto img = std::find(Materials::ImageTable.begin(), Materials::ImageTable.end(), image);
if (img != Materials::ImageTable.end())
{
Materials::ImageTable.erase(img);
}
} }
void Materials::DeleteAll() void Materials::DeleteAll()
@ -100,6 +109,15 @@ namespace Components
{ {
Materials::Delete(material); Materials::Delete(material);
} }
std::vector<Game::GfxImage*> images;
Utils::Merge(&images, Materials::ImageTable);
Materials::ImageTable.clear();
for (auto& image : images)
{
Materials::DeleteImage(image);
}
} }
__declspec(naked) void Materials::ImageVersionCheck() __declspec(naked) void Materials::ImageVersionCheck()
@ -284,13 +302,22 @@ namespace Components
} }
#endif #endif
// Scheduler::OnFrame([] () Renderer::OnDeviceRecoveryBegin([]()
// { {
// Game::Font* font = Game::R_RegisterFont("fonts/normalFont"); for (auto& image : Materials::ImageTable)
// float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; {
// Game::Image_Release(image);
// Game::R_AddCmdDrawText("test^==preview_mp_rustzob", 0x7FFFFFFF, font, 500.0f, 150.0f, 1.0f, 1.0f, 0.0f, color, Game::ITEM_TEXTSTYLE_SHADOWED); image->map = nullptr;
// }, true); }
});
Renderer::OnDeviceRecoveryEnd([]()
{
for (auto& image : Materials::ImageTable)
{
Utils::Hook::Call<void(void*)>(0x51F7B0)(image);
}
});
} }
Materials::~Materials() Materials::~Materials()

View File

@ -17,6 +17,7 @@ namespace Components
static void DeleteImage(Game::GfxImage* image); static void DeleteImage(Game::GfxImage* image);
private: private:
static std::vector<Game::GfxImage*> ImageTable;
static std::vector<Game::Material*> MaterialTable; static std::vector<Game::Material*> MaterialTable;
static int ImageNameLength; static int ImageNameLength;