Files
Seele/src/Engine/Asset/TextureLoader.cpp
T

60 lines
1.9 KiB
C++
Raw Normal View History

2020-06-02 11:46:18 +02:00
#include "TextureLoader.h"
#include "TextureAsset.h"
#include "Graphics/Graphics.h"
#include "AssetRegistry.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
2020-06-08 01:44:47 +02:00
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
2020-06-02 11:46:18 +02:00
using namespace Seele;
TextureLoader::TextureLoader(Gfx::PGraphics graphics)
: graphics(graphics)
{
2020-10-03 11:00:10 +02:00
placeholderAsset = new TextureAsset();
2020-09-19 14:36:50 +02:00
placeholderTexture = import("./textures/placeholder.png");
2020-10-03 11:00:10 +02:00
placeholderAsset->setTexture(placeholderTexture);
placeholderAsset->setStatus(Asset::Status::Ready);
2020-06-02 11:46:18 +02:00
}
TextureLoader::~TextureLoader()
{
}
2020-06-08 01:44:47 +02:00
void TextureLoader::importAsset(const std::filesystem::path& filePath)
2020-06-02 11:46:18 +02:00
{
2020-09-19 14:36:50 +02:00
auto assetFileName = filePath;
PTextureAsset asset = new TextureAsset(assetFileName.replace_extension("asset").filename().generic_string());
asset->setStatus(Asset::Status::Loading);
asset->setTexture(placeholderTexture);
2021-01-19 15:30:00 +01:00
AssetRegistry::get().textures[asset->getFileName()] = asset;
2020-10-03 11:00:10 +02:00
//futures.add(std::async(std::launch::async, [this, filePath, asset] () mutable {
2020-09-19 14:36:50 +02:00
Gfx::PTexture2D texture = import(filePath);
asset->setTexture(texture);
asset->setStatus(Asset::Status::Ready);
2020-10-03 11:00:10 +02:00
//}));
}
PTextureAsset TextureLoader::getPlaceholderTexture()
{
return placeholderAsset;
2020-06-02 11:46:18 +02:00
}
2020-09-19 14:36:50 +02:00
Gfx::PTexture2D TextureLoader::import(const std::filesystem::path& path)
2020-06-02 11:46:18 +02:00
{
int x, y, n;
2020-06-08 01:44:47 +02:00
unsigned char* data = stbi_load(path.string().c_str(), &x, &y, &n, 4);
2021-01-19 15:30:00 +01:00
2020-06-02 11:46:18 +02:00
TextureCreateInfo createInfo;
2021-01-19 15:30:00 +01:00
createInfo.format = Gfx::SE_FORMAT_R8G8B8A8_SRGB;
2020-06-02 11:46:18 +02:00
createInfo.resourceData.data = data;
2020-06-08 01:44:47 +02:00
createInfo.resourceData.size = x * y * 4 * sizeof(unsigned char);
createInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
2020-06-02 11:46:18 +02:00
createInfo.width = x;
createInfo.height = y;
Gfx::PTexture2D texture = graphics->createTexture2D(createInfo);
2020-06-08 01:44:47 +02:00
stbi_image_free(data);
2020-06-02 11:46:18 +02:00
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
2020-09-19 14:36:50 +02:00
return texture;
2020-06-02 11:46:18 +02:00
}