untested texture loader

main
Brett 2023-12-30 17:05:41 -05:00
parent 35273334a3
commit 8c86faf1ce
2 changed files with 72 additions and 5 deletions

View File

@ -31,17 +31,29 @@ namespace blt::gfx
{
std::string path;
std::string name;
loadable_texture() = default;
loadable_texture(std::string path, std::string name): path(std::move(path)), name(std::move(name))
{}
};
class texture_resource_manager
{
private:
blt::thread_pool<false> pool;
std::mutex load_lock;
std::mutex save_lock;
std::vector<loadable_texture> textures_to_load;
std::vector<texture_file*> loaded_textures;
HASHMAP<std::string, texture_gl2D*> textures_2d;
public:
texture_resource_manager() = default;
~texture_resource_manager();
void enqueue(std::string path, std::string name = "");
void load_resources(std::size_t threads = 8);
void delete_resources();
};
}

View File

@ -18,8 +18,63 @@
#include <blt/gfx/renderer/texture_manager.h>
#include <blt/std/logging.h>
blt::gfx::texture_resource_manager::~texture_resource_manager()
namespace blt::gfx
{
void texture_resource_manager::delete_resources()
{
for (const auto& p : textures_2d)
delete p.second;
}
void texture_resource_manager::enqueue(std::string path, std::string name)
{
textures_to_load.emplace_back(std::move(path), std::move(name));
}
void texture_resource_manager::load_resources(std::size_t threads)
{
blt::thread_pool<false> pool;
pool.execute([&]() {
loadable_texture texture;
{
std::scoped_lock lock(load_lock);
texture = textures_to_load.back();
textures_to_load.pop_back();
}
auto* file = new texture_file(texture.path, texture.name);
{
std::scoped_lock lock(save_lock);
loaded_textures.push_back(file);
}
});
do
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
{
std::scoped_lock lock(load_lock);
if (textures_to_load.empty())
break;
}
{
std::scoped_lock lock(save_lock);
while (!loaded_textures.empty())
{
auto* back = loaded_textures.back();
textures_2d.insert({back->getName(), new texture_gl2D(back->texture())});
BLT_DEBUG("Loaded Texture %s", back->getName().c_str());
delete back;
loaded_textures.pop_back();
}
}
} while (true);
pool.stop();
while (!pool.complete());
while (!loaded_textures.empty())
{
auto* back = loaded_textures.back();
textures_2d.insert({back->getName(), new texture_gl2D(back->texture())});
loaded_textures.pop_back();
}
}
}