// IAEngine: 2D Game Engine by IA // Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev) // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . #include #include #include #include #define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_RESIZE_IMPLEMENTATION #include #include namespace ia::iae { Map ResourceManager::s_imageExtents; VOID ResourceManager::Initialize() { } VOID ResourceManager::Terminate() { } Handle ResourceManager::CreateImage(IN PCUINT8 encodedData, IN SIZE_T encodedDataSize) { INT32 w, h, n; const auto rgbaData = stbi_load_from_memory(encodedData, encodedDataSize, &w, &h, &n, STBI_rgb_alpha); const auto result = CreateImage(rgbaData, w, h); STBI_FREE(rgbaData); return result; } Handle ResourceManager::CreateImage(IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height) { const auto handle = (Handle) GPUResourceManager::CreateTexture(SDL_GPU_TEXTUREUSAGE_SAMPLER, width, height, rgbaData, SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM); s_imageExtents[handle] = (((UINT64) width) << 32) | height; return handle; } Handle ResourceManager::CreateSound(IN PCUINT8 encodedData, IN SIZE_T encodedDataSize) { return AudioManager::CreateAudio(encodedData, encodedDataSize); } VOID ResourceManager::DestroyImage(IN Handle image) { GPUResourceManager::DestroyTexture((SDL_GPUTexture *) image); } VOID ResourceManager::DestroySound(IN Handle sound) { AudioManager::DestoryAudio(sound); } IVec2 ResourceManager::GetImageExtent(IN Handle image) { const auto t = s_imageExtents[image]; return {(INT32) (t >> 32), (INT32) t}; } Handle ResourceManager::RescaleImage(IN Handle image, IN INT32 newWidth, IN INT32 newHeight) { const auto currentExtent = GetImageExtent(image); const auto pixelData = GPUResourceManager::GetTexturePixelData((SDL_GPUTexture *) image, currentExtent.x, currentExtent.y); GPUResourceManager::DestroyTexture((SDL_GPUTexture *) image); 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(newPixelData, newWidth, newHeight); free(newPixelData); return result; } Handle ResourceManager::CombineImages(IN CONST Vector &images, IN INT32 unitWidth, IN INT32 unitHeight, IN INT32 unitCountX, IN INT32 unitCountY) { return (Handle)GPUResourceManager::CombineTextures((SDL_GPUTexture**)images.data(), unitWidth, unitHeight, unitCountX, unitCountY); } } // namespace ia::iae namespace ia::iae { Handle Engine::CreateImage(IN PCUINT8 encodedData, IN SIZE_T encodedDataSize) { return ResourceManager::CreateImage(encodedData, encodedDataSize); } Handle Engine::CreateImage(IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height) { return ResourceManager::CreateImage(rgbaData, width, height); } Handle Engine::CreateSound(IN PCUINT8 encodedData, IN SIZE_T encodedDataSize) { return ResourceManager::CreateSound(encodedData, encodedDataSize); } VOID Engine::DestroyImage(IN Handle image) { ResourceManager::DestroyImage(image); } VOID Engine::DestroySound(IN Handle sound) { ResourceManager::DestroySound(sound); } IVec2 Engine::GetImageExtent(IN Handle image) { return ResourceManager::GetImageExtent(image); } Handle Engine::RescaleImage(IN Handle image, IN INT32 newWidth, IN INT32 newHeight) { return ResourceManager::RescaleImage(image, newWidth, newHeight); } Handle Engine::CombineImages(IN CONST Vector &images, IN INT32 unitWidth, IN INT32 unitHeight, IN INT32 unitCountX, IN INT32 unitCountY) { return ResourceManager::CombineImages(images, unitWidth, unitHeight, unitCountX, unitCountY); } } // namespace ia::iae