This commit is contained in:
Isuru Samarathunga
2025-10-09 19:28:54 +05:30
parent 1f9d5426b8
commit 96bad30f15
25 changed files with 813 additions and 69 deletions

View File

@ -18,6 +18,7 @@
#include <ResourceManager.hpp>
#include <AudioManager.hpp>
#include <Renderer/Renderer.hpp>
#include <Renderer/GPUResourceManager.hpp>
#define STB_IMAGE_IMPLEMENTATION
@ -27,18 +28,26 @@
namespace ia::iae
{
Vector<SDL_GPUTexture*> ResourceManager::s_imageHandles;
Map<String, Handle> ResourceManager::s_images;
Map<String, Handle> ResourceManager::s_sounds;
Map<Handle, UINT64> ResourceManager::s_imageExtents;
Map<Handle, UINT64> ResourceManager::s_nonScaledImageExtents;
VOID ResourceManager::Initialize()
{
{ // Set Texture Handle 0 to a pure white image
const auto data = new UINT8[100*100*4];
ia_memset(data, 0xFF, 100* 100* 4);
CreateImage(Engine::GetUniqueResourceName(), data, 100, 100);
delete[] data;
}
}
VOID ResourceManager::Terminate()
{
for (const auto &t : s_images)
GPUResourceManager::DestroyTexture((SDL_GPUTexture *) t->Value);
GPUResourceManager::DestroyTexture(s_imageHandles[t->Value]);
for (const auto &t : s_sounds)
AudioManager::DestoryAudio(t->Value);
}
@ -54,10 +63,13 @@ namespace ia::iae
Handle ResourceManager::CreateImage(IN CONST String &name, IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height)
{
const auto handle = (Handle) GPUResourceManager::CreateTexture(SDL_GPU_TEXTUREUSAGE_SAMPLER, width, height,
const auto texture = GPUResourceManager::CreateTexture(SDL_GPU_TEXTUREUSAGE_SAMPLER, width, height,
rgbaData, SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM);
s_imageExtents[handle] = (((UINT64) width) << 32) | height;
s_imageHandles.pushBack(texture);
Handle handle = s_imageHandles.size() - 1;
s_images[name] = handle;
s_imageExtents[handle] = (((UINT64) width) << 32) | height;
s_nonScaledImageExtents[handle] = s_imageExtents[handle];
return handle;
}
@ -102,7 +114,8 @@ namespace ia::iae
VOID ResourceManager::DestroyImage(IN Handle image)
{
GPUResourceManager::DestroyTexture((SDL_GPUTexture *) image);
GPUResourceManager::DestroyTexture(s_imageHandles[image]);
s_imageHandles[image] = nullptr;
}
VOID ResourceManager::DestroySound(IN Handle sound)
@ -120,22 +133,37 @@ namespace ia::iae
{
const auto currentExtent = GetImageExtent(image);
const auto pixelData =
GPUResourceManager::GetTexturePixelData((SDL_GPUTexture *) image, currentExtent.x, currentExtent.y);
GPUResourceManager::DestroyTexture((SDL_GPUTexture*)image);
GPUResourceManager::GetTexturePixelData(s_imageHandles[image], currentExtent.x, currentExtent.y);
const auto newPixelData =
stbir_resize_uint8_linear(pixelData.data(), currentExtent.x, currentExtent.y, currentExtent.x * 4, nullptr,
newWidth, newHeight, newWidth * 4, stbir_pixel_layout::STBIR_RGBA);
const auto result = CreateImage(GetImageName(image), newPixelData, newWidth, newHeight);
const auto texture = GPUResourceManager::CreateTexture(SDL_GPU_TEXTUREUSAGE_SAMPLER, newWidth, newHeight,
newPixelData, SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM);
s_imageExtents[image] = (((UINT64) newWidth) << 32) | newHeight;
GPUResourceManager::DestroyTexture(s_imageHandles[image]);
s_imageHandles[image] = texture;
free(newPixelData);
return result;
return image;
}
Handle ResourceManager::CombineImages(IN CONST Vector<Handle> &images, IN INT32 unitWidth, IN INT32 unitHeight,
IN INT32 unitCountX, IN INT32 unitCountY)
{
return (Handle) GPUResourceManager::CombineTextures((SDL_GPUTexture **) images.data(), unitWidth, unitHeight,
Vector<SDL_GPUTexture*> textures;
for(const auto& t: images)
textures.pushBack(s_imageHandles[t]);
return (Handle) GPUResourceManager::CombineTextures(textures.data(), unitWidth, unitHeight,
unitCountX, unitCountY);
}
VOID ResourceManager::RescaleAllImages(IN FLOAT32 factorX, IN FLOAT32 factorY)
{
for(auto& t: s_images)
{
const auto p = s_nonScaledImageExtents[t->Value];
t->Value = ResizeImage(t->Value, ((INT32) (p >> 32)) * factorX, ((INT32) p) * factorY);
}
}
} // namespace ia::iae
namespace ia::iae
@ -185,6 +213,11 @@ namespace ia::iae
return ResourceManager::ResizeImage(image, newWidth, newHeight);
}
VOID Engine::RescaleAllImages(IN FLOAT32 factorX, IN FLOAT32 factorY)
{
ResourceManager::RescaleAllImages(factorX, factorY);
}
Handle Engine::CombineImages(IN CONST Vector<Handle> &images, IN INT32 unitWidth, IN INT32 unitHeight,
IN INT32 unitCountX, IN INT32 unitCountY)
{