150 lines
5.3 KiB
C++
150 lines
5.3 KiB
C++
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
#include <IAEngine/Engine.hpp>
|
|
#include <ResourceManager.hpp>
|
|
|
|
#include <AudioManager.hpp>
|
|
#include <Renderer/GPUResourceManager.hpp>
|
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#define STB_IMAGE_RESIZE_IMPLEMENTATION
|
|
#include <stb_image.h>
|
|
#include <stb_image_resize2.h>
|
|
|
|
namespace ia::iae
|
|
{
|
|
Vector<Handle> ResourceManager::s_images;
|
|
Vector<Handle> ResourceManager::s_sounds;
|
|
Map<Handle, UINT64> ResourceManager::s_imageExtents;
|
|
|
|
VOID ResourceManager::Initialize()
|
|
{
|
|
}
|
|
|
|
VOID ResourceManager::Terminate()
|
|
{
|
|
for (const auto &t : s_images)
|
|
GPUResourceManager::DestroyTexture((SDL_GPUTexture *) t);
|
|
for (const auto &t : s_sounds)
|
|
AudioManager::DestoryAudio(t);
|
|
}
|
|
|
|
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;
|
|
s_images.pushBack(handle);
|
|
return handle;
|
|
}
|
|
|
|
Handle ResourceManager::CreateSound(IN PCUINT8 encodedData, IN SIZE_T encodedDataSize)
|
|
{
|
|
const auto handle = AudioManager::CreateAudio(encodedData, encodedDataSize);
|
|
s_sounds.pushBack(handle);
|
|
return handle;
|
|
}
|
|
|
|
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);
|
|
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<Handle> &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<Handle> &images, IN INT32 unitWidth, IN INT32 unitHeight,
|
|
IN INT32 unitCountX, IN INT32 unitCountY)
|
|
{
|
|
return ResourceManager::CombineImages(images, unitWidth, unitHeight, unitCountX, unitCountY);
|
|
}
|
|
} // namespace ia::iae
|