BLT-With-Graphics-Template/include/blt/gfx/renderer/resource_manager.h

81 lines
2.5 KiB
C
Raw Normal View History

2023-12-30 03:26:06 -05:00
/*
* <Short Description>
* Copyright (C) 2023 Brett Terpstra
*
* 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/>.
*/
2024-01-01 22:15:30 -05:00
#ifndef BLT_WITH_GRAPHICS_RESOURCE_MANAGER_H
#define BLT_WITH_GRAPHICS_RESOURCE_MANAGER_H
2023-12-30 03:26:06 -05:00
#include <blt/std/hashmap.h>
#include <blt/gfx/texture.h>
#include <mutex>
#include <vector>
#include <optional>
2023-12-30 03:26:06 -05:00
#include <string>
#include "blt/compatibility.h"
2023-12-30 03:26:06 -05:00
namespace blt::gfx
{
struct loadable_texture
{
std::string path;
std::string name;
2023-12-30 17:05:41 -05:00
loadable_texture() = default;
loadable_texture(std::string path, std::string name): path(std::move(path)), name(std::move(name))
{}
2023-12-30 03:26:06 -05:00
};
2024-01-01 22:15:30 -05:00
class resource_manager
2023-12-30 03:26:06 -05:00
{
private:
2023-12-30 17:05:41 -05:00
std::mutex load_lock;
std::mutex save_lock;
2023-12-30 03:26:06 -05:00
std::vector<loadable_texture> textures_to_load;
2023-12-30 17:05:41 -05:00
std::vector<texture_file*> loaded_textures;
2024-03-23 19:52:57 -04:00
blt::hashmap_t<std::string, texture_gl2D*> textures_2d;
2023-12-30 03:26:06 -05:00
public:
2024-01-01 22:15:30 -05:00
resource_manager() = default;
2023-12-30 17:05:41 -05:00
void enqueue(std::string path, std::string name = "");
void load_resources(std::size_t threads = 8);
inline std::optional<texture_gl2D*> get(const std::string& name)
2024-01-01 22:15:30 -05:00
{
if (name.empty() || textures_2d.find(name) == textures_2d.end())
return {};
2024-01-01 22:15:30 -05:00
return textures_2d[name];
}
/**
* Directly sets the internal representation of the texture. Can be used to set custom, non file system loaded textures.
* Note: this function takes ownership of the ptr.
*/
inline void set(std::string_view name, texture_gl2D* texture)
{
textures_2d[name] = texture;
}
2024-01-01 22:15:30 -05:00
void cleanup();
2023-12-30 03:26:06 -05:00
};
}
2024-01-01 22:15:30 -05:00
#endif //BLT_WITH_GRAPHICS_RESOURCE_MANAGER_H