272 lines
8.0 KiB
C++
272 lines
8.0 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/AssetManager.hpp>
|
|
#include <IAEngine/IAEngine.hpp>
|
|
|
|
|
|
#include <SDL3/SDL_iostream.h>
|
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#include <Vendor/stb/stb_image.h>
|
|
|
|
#include <RenderCore/RenderCore.hpp>
|
|
|
|
#include <IACore/DynamicLib.hpp>
|
|
|
|
namespace ia::iae
|
|
{
|
|
Handle CreateTextureFromFile(IN PCCHAR path, IN INT32 tileWidth = -1, IN INT32 tileHeight = -1)
|
|
{
|
|
const auto data = IAEngine::GetAssetManager()->ReadBinaryAsset(path);
|
|
|
|
INT32 w, h, n;
|
|
auto pixels = stbi_load_from_memory(data.data(), data.size(), &w, &h, &n, STBI_rgb_alpha);
|
|
if (!pixels)
|
|
THROW_INVALID_DATA(path);
|
|
const auto t =
|
|
RDC::CreateImage(pixels, w, h, tileWidth == -1 ? 1 : w / tileWidth, tileHeight == -1 ? 1 : h / tileHeight);
|
|
stbi_image_free(pixels);
|
|
return t;
|
|
}
|
|
|
|
VOID AssetManager::Initialize()
|
|
{
|
|
}
|
|
|
|
VOID AssetManager::Terminate()
|
|
{
|
|
for (SIZE_T i = 0; i < m_assets.size(); i++)
|
|
DestroyAsset(m_assets[i]);
|
|
}
|
|
|
|
VOID AssetManager::SetAssetDirectory(IN CONST String &path)
|
|
{
|
|
m_assetDirectory = path;
|
|
}
|
|
|
|
String AssetManager::ReadTextAsset(IN CONST String &path)
|
|
{
|
|
const auto t = BuildString(m_assetDirectory, "/", path);
|
|
SDL_IOStream *f = SDL_IOFromFile(t.c_str(), "r");
|
|
if (!f)
|
|
THROW_FILE_OPEN_READ(t);
|
|
Vector<CHAR> result;
|
|
SDL_SeekIO(f, 0, SDL_IO_SEEK_END);
|
|
result.resize(SDL_TellIO(f) + 1);
|
|
SDL_SeekIO(f, 0, SDL_IO_SEEK_SET);
|
|
SDL_ReadIO(f, result.data(), result.size());
|
|
result.back() = '\0';
|
|
SDL_CloseIO(f);
|
|
return result.data();
|
|
}
|
|
|
|
Vector<UINT8> AssetManager::ReadBinaryAsset(IN CONST String &path)
|
|
{
|
|
const auto t = BuildString(m_assetDirectory, "/", path);
|
|
SDL_IOStream *f = SDL_IOFromFile(t.c_str(), "rb");
|
|
if (!f)
|
|
THROW_FILE_OPEN_READ(t);
|
|
Vector<UINT8> result;
|
|
SDL_SeekIO(f, 0, SDL_IO_SEEK_END);
|
|
result.resize(SDL_TellIO(f));
|
|
SDL_SeekIO(f, 0, SDL_IO_SEEK_SET);
|
|
SDL_ReadIO(f, result.data(), result.size());
|
|
SDL_CloseIO(f);
|
|
return result;
|
|
}
|
|
|
|
Asset_Plugin *AssetManager::LoadPlugin(IN CONST String &path)
|
|
{
|
|
auto lib = DynamicLib::Load(BuildString(m_assetDirectory, "/Plugins"), path);
|
|
auto t = new Asset_Plugin();
|
|
t->OnInitialize = lib.GetFunction<VOID (*)()>("Plugin_OnInitialize");
|
|
t->OnTerminate = lib.GetFunction<VOID (*)()>("Plugin_OnTerminate");
|
|
t->OnDebugDraw = lib.GetFunction<VOID (*)()>("Plugin_OnDebugDraw");
|
|
t->OnFixedUpdate = lib.GetFunction<VOID (*)()>("Plugin_OnFixedUpdate");
|
|
t->OnUpdate = lib.GetFunction<VOID (*)(IN FLOAT32 deltaTime)>("Plugin_OnUpdate");
|
|
return t;
|
|
}
|
|
|
|
Asset_Sprite *AssetManager::CreateSprite(IN CONST String &path)
|
|
{
|
|
const auto t = new Asset_Sprite();
|
|
t->m_texture = CreateTextureFromFile(path.c_str());
|
|
m_assets.pushBack(t);
|
|
return t;
|
|
}
|
|
|
|
Asset_Sprite *AssetManager::CreateSprite(IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height)
|
|
{
|
|
const auto t = new Asset_Sprite();
|
|
t->m_texture = RDC::CreateImage(rgbaData, width, height);
|
|
m_assets.pushBack(t);
|
|
return t;
|
|
}
|
|
|
|
Asset_SpriteSheet *AssetManager::CreateSpriteSheet(IN CONST Vector<Vector<Handle>> &animations)
|
|
{
|
|
THROW_NOT_IMPLEMENTED();
|
|
return nullptr;
|
|
}
|
|
|
|
Asset_SpriteSheet *AssetManager::CreateSpriteSheet(IN CONST String &path, IN INT32 spriteWidth,
|
|
IN INT32 spriteHeight, IN CONST Vector<INT32> &frameCounts)
|
|
{
|
|
const auto t = new Asset_SpriteSheet();
|
|
t->m_frameCounts = frameCounts;
|
|
t->m_texture = CreateTextureFromFile(path.c_str(), spriteWidth, spriteHeight);
|
|
m_assets.pushBack(t);
|
|
return t;
|
|
}
|
|
|
|
Asset_TileSheet *AssetManager::CreateTileSheet(IN CONST String &path, IN INT32 tileWidth, IN INT32 tileHeight)
|
|
{
|
|
const auto t = new Asset_TileSheet();
|
|
t->m_texture = CreateTextureFromFile(path.c_str(), tileWidth, tileHeight);
|
|
m_assets.pushBack(t);
|
|
return t;
|
|
}
|
|
|
|
Asset_TileSheet *AssetManager::CreateTileSheet(IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height,
|
|
IN INT32 tileWidth, IN INT32 tileHeight)
|
|
{
|
|
const auto t = new Asset_TileSheet();
|
|
t->m_texture = RDC::CreateImage(rgbaData, width, height, width / tileWidth, height / tileHeight);
|
|
m_assets.pushBack(t);
|
|
return t;
|
|
}
|
|
|
|
VOID AssetManager::DestroyAsset(IN IAsset *asset)
|
|
{
|
|
asset->Destroy();
|
|
delete asset;
|
|
|
|
for (auto &t : m_assets)
|
|
if (t == asset)
|
|
t = nullptr;
|
|
|
|
for (auto &t : m_assetNames)
|
|
if (t->Value == asset)
|
|
t->Value = nullptr;
|
|
}
|
|
|
|
IAsset *AssetManager::GetAssetByName(IN CONST String &name)
|
|
{
|
|
return m_assetNames[name];
|
|
}
|
|
|
|
VOID AssetManager::AssignAssetName(IN IAsset *asset, IN CONST String &name)
|
|
{
|
|
m_assetNames[name] = asset;
|
|
}
|
|
|
|
Asset_Scene *AssetManager::CreateScene(IN IVec2 extent)
|
|
{
|
|
const auto t = new Asset_Scene();
|
|
t->m_scene.SetExtent(extent);
|
|
m_assets.pushBack(t);
|
|
return t;
|
|
}
|
|
} // namespace ia::iae
|
|
|
|
namespace ia::iae
|
|
{
|
|
IAsset::IAsset(IN EAssetType type) : m_type(type)
|
|
{
|
|
}
|
|
|
|
IAsset::~IAsset()
|
|
{
|
|
}
|
|
|
|
VOID Asset_Sprite::Compile()
|
|
{
|
|
}
|
|
|
|
VOID Asset_Sprite::Destroy()
|
|
{
|
|
}
|
|
|
|
VOID Asset_SpriteSheet::Compile()
|
|
{
|
|
}
|
|
|
|
VOID Asset_SpriteSheet::Destroy()
|
|
{
|
|
}
|
|
|
|
VOID Asset_Scene::Compile()
|
|
{
|
|
}
|
|
|
|
VOID Asset_Scene::Destroy()
|
|
{
|
|
}
|
|
|
|
VOID Asset_Plugin::Compile()
|
|
{
|
|
}
|
|
|
|
VOID Asset_Plugin::Destroy()
|
|
{
|
|
}
|
|
} // namespace ia::iae
|
|
|
|
#include <zlib.h>
|
|
|
|
namespace ia::iae
|
|
{
|
|
Vector<UINT8> AssetManager::Inflate(IN PCUINT8 data, IN SIZE_T dataSize)
|
|
{
|
|
STATIC UINT8 TMP_BUFFER[16384];
|
|
Vector<UINT8> result;
|
|
result.reserve(dataSize * 3);
|
|
z_stream stream;
|
|
stream.zalloc = Z_NULL;
|
|
stream.zfree = Z_NULL;
|
|
stream.opaque = Z_NULL;
|
|
stream.avail_in = 0;
|
|
stream.next_in = Z_NULL;
|
|
if (inflateInit(&stream) != Z_OK)
|
|
THROW_UNKNOWN("Inflate failed: init zlib inflate");
|
|
stream.avail_in = dataSize;
|
|
stream.next_in = (Bytef *) data;
|
|
while (true)
|
|
{
|
|
stream.avail_out = sizeof(TMP_BUFFER);
|
|
stream.next_out = TMP_BUFFER;
|
|
const auto r = inflate(&stream, Z_SYNC_FLUSH);
|
|
result.insert(result.end(), sizeof(TMP_BUFFER) - stream.avail_out, TMP_BUFFER);
|
|
if (r == Z_STREAM_END)
|
|
break;
|
|
else if (r != Z_OK)
|
|
THROW_INVALID_DATA("Inflate failed: zlib inflation");
|
|
}
|
|
inflateEnd(&stream);
|
|
return result;
|
|
}
|
|
|
|
Vector<UINT8> AssetManager::Deflate(IN PCUINT8 data, IN SIZE_T dataSize)
|
|
{
|
|
Vector<UINT8> result;
|
|
auto deflateBound = compressBound(dataSize);
|
|
result.resize(deflateBound);
|
|
compress(result.data(), &deflateBound, data, dataSize);
|
|
result.resize(deflateBound);
|
|
return result;
|
|
}
|
|
} // namespace ia::iae
|