2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Neirokan on 09.05.2020
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "ResourceManager.h"
|
2021-10-18 06:44:04 +03:00
|
|
|
#include "utils/Log.h"
|
2021-09-13 15:53:43 +03:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2021-10-18 06:44:04 +03:00
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
2021-09-19 11:25:10 +03:00
|
|
|
|
2021-10-17 19:38:16 +03:00
|
|
|
ResourceManager* ResourceManager::_instance = nullptr;
|
2021-10-29 23:44:37 +03:00
|
|
|
bool ResourceManager::_validInstance = false;
|
|
|
|
|
2021-10-17 19:38:16 +03:00
|
|
|
|
|
|
|
void ResourceManager::init() {
|
|
|
|
_instance = new ResourceManager();
|
2021-10-29 23:44:37 +03:00
|
|
|
_validInstance = true;
|
2021-10-30 21:29:42 +03:00
|
|
|
|
|
|
|
Log::log("ResourceManager::init(): resource manager was initialized");
|
2021-10-17 19:38:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::unloadTextures() {
|
2021-10-30 22:01:54 +03:00
|
|
|
int texturesCounter = _instance->_textures.size();
|
2021-10-28 16:58:02 +03:00
|
|
|
for (auto & _texture : _instance->_textures) {
|
2021-10-17 19:38:16 +03:00
|
|
|
_texture.second.reset();
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
_instance->_textures.clear();
|
2021-10-30 21:29:42 +03:00
|
|
|
|
2021-10-30 22:01:54 +03:00
|
|
|
Log::log("ResourceManager::unloadTextures(): all " + std::to_string(texturesCounter) + " textures was unloaded");
|
2021-10-30 21:29:42 +03:00
|
|
|
|
2021-10-17 19:38:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::unloadSoundBuffers() {
|
2021-10-30 22:01:54 +03:00
|
|
|
int soundBuffersCounter = _instance->_soundBuffers.size();
|
2021-10-28 16:58:02 +03:00
|
|
|
for (auto & _soundBuffer : _instance->_soundBuffers) {
|
2021-10-17 19:38:16 +03:00
|
|
|
_soundBuffer.second.reset();
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
_instance->_soundBuffers.clear();
|
2021-10-30 21:29:42 +03:00
|
|
|
|
2021-10-30 22:01:54 +03:00
|
|
|
Log::log("ResourceManager::unloadSoundBuffers(): all " + std::to_string(soundBuffersCounter) + " soundBuffers was unloaded");
|
2021-10-17 19:38:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::unloadFonts() {
|
2021-10-30 22:01:54 +03:00
|
|
|
int fontsCounter = _instance->_fonts.size();
|
2021-10-28 16:58:02 +03:00
|
|
|
for (auto & _font : _instance->_fonts) {
|
2021-10-17 19:38:16 +03:00
|
|
|
_font.second.reset();
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
_instance->_fonts.clear();
|
2021-10-30 21:29:42 +03:00
|
|
|
|
2021-10-30 22:01:54 +03:00
|
|
|
Log::log("ResourceManager::unloadFonts(): all " + std::to_string(fontsCounter) + " fonts was unloaded");
|
2021-10-17 19:38:16 +03:00
|
|
|
}
|
|
|
|
|
2021-10-18 09:21:09 +03:00
|
|
|
void ResourceManager::unloadObjects() {
|
2021-10-30 22:01:54 +03:00
|
|
|
int objCounter = _instance->_objects.size();
|
2021-10-18 09:21:09 +03:00
|
|
|
_instance->_objects.clear();
|
2021-10-30 21:29:42 +03:00
|
|
|
|
2021-10-30 22:01:54 +03:00
|
|
|
Log::log("ResourceManager::unloadObjects(): all " + std::to_string(objCounter) + " objects was unloaded");
|
2021-10-18 09:21:09 +03:00
|
|
|
}
|
|
|
|
|
2021-10-17 19:38:16 +03:00
|
|
|
void ResourceManager::unloadAllResources() {
|
2021-10-29 23:44:37 +03:00
|
|
|
if(!_validInstance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-17 19:38:16 +03:00
|
|
|
unloadTextures();
|
|
|
|
unloadSoundBuffers();
|
|
|
|
unloadFonts();
|
2021-10-18 06:44:04 +03:00
|
|
|
unloadObjects();
|
2021-10-30 21:29:42 +03:00
|
|
|
|
|
|
|
Log::log("ResourceManager::unloadAllResources(): all resources was unloaded");
|
2021-10-17 19:38:16 +03:00
|
|
|
}
|
|
|
|
|
2021-10-18 09:21:09 +03:00
|
|
|
void ResourceManager::free() {
|
|
|
|
unloadAllResources();
|
2021-10-29 23:44:37 +03:00
|
|
|
_validInstance = false;
|
2021-10-18 09:21:09 +03:00
|
|
|
delete _instance;
|
2021-10-30 21:29:42 +03:00
|
|
|
|
|
|
|
Log::log("ResourceManager::free(): pointer to 'ResourceManager' was freed");
|
2021-10-18 09:21:09 +03:00
|
|
|
}
|
|
|
|
|
2021-10-17 19:38:16 +03:00
|
|
|
std::shared_ptr<sf::Texture> ResourceManager::loadTexture(const std::string& filename) {
|
2021-10-29 23:44:37 +03:00
|
|
|
if(!_validInstance) {
|
2021-10-17 19:38:16 +03:00
|
|
|
return nullptr;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
|
|
|
|
// If texture is already loaded - return pointer to it
|
|
|
|
auto it = _instance->_textures.find(filename);
|
2021-10-28 16:58:02 +03:00
|
|
|
if (it != _instance->_textures.end()) {
|
2021-10-17 19:38:16 +03:00
|
|
|
return it->second;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
|
|
|
|
// Otherwise - try to load it. If failure - return zero
|
|
|
|
std::shared_ptr<sf::Texture> texture(new sf::Texture);
|
2021-10-28 16:58:02 +03:00
|
|
|
if (!texture->loadFromFile(filename)) {
|
2021-10-30 21:29:42 +03:00
|
|
|
Log::log("ResourceManager::loadTexture: error with loading texture '" + filename + "'");
|
2021-10-17 19:38:16 +03:00
|
|
|
return nullptr;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
|
2021-10-30 21:29:42 +03:00
|
|
|
Log::log("ResourceManager::loadTexture: texture '" + filename + "' was loaded");
|
|
|
|
|
2021-10-17 19:38:16 +03:00
|
|
|
// If success - remember and return texture pointer
|
|
|
|
texture->setRepeated(true);
|
|
|
|
_instance->_textures.emplace(filename, texture);
|
|
|
|
|
|
|
|
return texture;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
|
|
|
|
std::shared_ptr<sf::SoundBuffer> ResourceManager::loadSoundBuffer(const std::string& filename) {
|
2021-10-29 23:44:37 +03:00
|
|
|
if(!_validInstance) {
|
2021-10-17 19:38:16 +03:00
|
|
|
return nullptr;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
|
|
|
|
// If sound buffer is already loaded - return pointer to it
|
|
|
|
auto it = _instance->_soundBuffers.find(filename);
|
2021-10-28 16:58:02 +03:00
|
|
|
if (it != _instance->_soundBuffers.end()) {
|
2021-10-17 19:38:16 +03:00
|
|
|
return it->second;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
|
|
|
|
// Otherwise - try to load it. If failure - return zero
|
|
|
|
std::shared_ptr<sf::SoundBuffer> soundBuffer(new sf::SoundBuffer);
|
2021-10-28 16:58:02 +03:00
|
|
|
if (!soundBuffer->loadFromFile(filename)) {
|
2021-10-30 21:29:42 +03:00
|
|
|
Log::log("ResourceManager::loadSoundBuffer: error with loading sound buffer '" + filename + "'");
|
2021-10-17 19:38:16 +03:00
|
|
|
return nullptr;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
|
2021-10-30 21:29:42 +03:00
|
|
|
Log::log("ResourceManager::loadSoundBuffer: sound buffer '" + filename + "' was loaded");
|
|
|
|
|
2021-10-18 06:44:04 +03:00
|
|
|
// If success - remember and return sound pointer
|
2021-10-17 19:38:16 +03:00
|
|
|
_instance->_soundBuffers.emplace(filename, soundBuffer);
|
|
|
|
|
|
|
|
return soundBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<sf::Font> ResourceManager::loadFont(const std::string& filename) {
|
2021-10-29 23:44:37 +03:00
|
|
|
if(!_validInstance) {
|
2021-10-17 19:38:16 +03:00
|
|
|
return nullptr;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
|
|
|
|
// If font is already loaded - return pointer to it
|
|
|
|
auto it = _instance->_fonts.find(filename);
|
2021-10-28 16:58:02 +03:00
|
|
|
if (it != _instance->_fonts.end()) {
|
2021-10-17 19:38:16 +03:00
|
|
|
return it->second;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
|
|
|
|
// Otherwise - try to load it. If failure - return zero
|
|
|
|
std::shared_ptr<sf::Font> font(new sf::Font);
|
2021-10-28 16:58:02 +03:00
|
|
|
if (!font->loadFromFile(filename)) {
|
2021-10-30 21:29:42 +03:00
|
|
|
Log::log("ResourceManager::loadFont: error with loading font: '" + filename + "'");
|
2021-10-17 19:38:16 +03:00
|
|
|
return nullptr;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
|
2021-10-30 21:29:42 +03:00
|
|
|
Log::log("ResourceManager::loadFont: font '" + filename + "' was loaded");
|
|
|
|
|
2021-10-18 06:44:04 +03:00
|
|
|
// If success - remember and return font pointer
|
2021-10-17 19:38:16 +03:00
|
|
|
_instance->_fonts.emplace(filename, font);
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2021-10-18 09:21:09 +03:00
|
|
|
std::vector<std::shared_ptr<Mesh>> ResourceManager::loadObjects(const std::string &filename) {
|
2021-10-29 23:44:37 +03:00
|
|
|
|
2021-10-18 18:30:02 +03:00
|
|
|
std::vector<std::shared_ptr<Mesh>> objects{};
|
|
|
|
std::map<std::string, sf::Color> maters{};
|
2021-10-18 06:44:04 +03:00
|
|
|
|
2021-10-29 23:44:37 +03:00
|
|
|
if(!_validInstance) {
|
|
|
|
return objects;
|
|
|
|
}
|
|
|
|
|
2021-10-18 06:44:04 +03:00
|
|
|
// If objects is already loaded - return pointer to it
|
|
|
|
auto it = _instance->_objects.find(filename);
|
2021-10-18 18:30:02 +03:00
|
|
|
if (it != _instance->_objects.end()) {
|
2021-10-18 06:44:04 +03:00
|
|
|
return it->second;
|
2021-10-18 18:30:02 +03:00
|
|
|
}
|
2021-10-18 06:44:04 +03:00
|
|
|
|
|
|
|
std::ifstream file(filename);
|
2021-10-28 16:58:02 +03:00
|
|
|
if (!file.is_open()) {
|
2021-10-30 21:29:42 +03:00
|
|
|
Log::log("Mesh::LoadObjects(): cannot load file from '" + filename + "'");
|
2021-10-18 06:44:04 +03:00
|
|
|
return objects;
|
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
std::vector<Vec4D> verts{};
|
2021-10-18 06:44:04 +03:00
|
|
|
std::vector<Triangle> tris{};
|
|
|
|
sf::Color currentColor = sf::Color(255, 245, 194, 255);
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
while (!file.eof()) {
|
2021-10-18 06:44:04 +03:00
|
|
|
char line[128];
|
|
|
|
file.getline(line, 128);
|
|
|
|
|
|
|
|
std::stringstream s;
|
|
|
|
s << line;
|
|
|
|
|
|
|
|
char junk;
|
|
|
|
if(line[0] == 'o') {
|
2021-10-18 09:21:09 +03:00
|
|
|
if(!tris.empty())
|
2021-10-28 16:58:02 +03:00
|
|
|
objects.push_back(std::make_shared<Mesh>(ObjectNameTag(filename + "_temp_obj_" + std::to_string(objects.size())), tris));
|
2021-10-18 06:44:04 +03:00
|
|
|
tris.clear();
|
|
|
|
}
|
|
|
|
if (line[0] == 'v')
|
|
|
|
{
|
|
|
|
double x, y, z;
|
|
|
|
s >> junk >> x >> y >> z;
|
2021-10-28 16:58:02 +03:00
|
|
|
verts.emplace_back(x, y, z, 1.0);
|
2021-10-18 06:44:04 +03:00
|
|
|
}
|
|
|
|
if(line[0] == 'g') {
|
|
|
|
std::string matInfo;
|
|
|
|
s >> junk >> matInfo;
|
|
|
|
std::string colorName = matInfo.substr(matInfo.size()-3, 3);
|
|
|
|
currentColor = maters[matInfo.substr(matInfo.size()-3, 3)];
|
|
|
|
}
|
|
|
|
if (line[0] == 'f')
|
|
|
|
{
|
|
|
|
int f[3];
|
|
|
|
s >> junk >> f[0] >> f[1] >> f[2];
|
|
|
|
tris.emplace_back(verts[f[0] - 1], verts[f[1] - 1], verts[f[2] - 1], currentColor);
|
|
|
|
}
|
|
|
|
if(line[0] == 'm')
|
|
|
|
{
|
|
|
|
int color[4];
|
|
|
|
std::string matName;
|
|
|
|
|
|
|
|
s >> junk >> matName >> color[0] >> color[1] >> color[2] >> color[3];
|
|
|
|
maters.insert({matName, sf::Color(color[0],color[1],color[2], color[3])});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
if(!tris.empty()) {
|
|
|
|
objects.push_back(
|
|
|
|
std::make_shared<Mesh>(ObjectNameTag(filename + "_temp_obj_" + std::to_string(objects.size())), tris));
|
|
|
|
}
|
|
|
|
tris.clear();
|
2021-10-18 06:44:04 +03:00
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
2021-10-30 21:29:42 +03:00
|
|
|
Log::log("Mesh::LoadObjects(): obj '" + filename + "' was loaded");
|
|
|
|
|
2021-10-18 06:44:04 +03:00
|
|
|
// If success - remember and return vector of objects pointer
|
|
|
|
_instance->_objects.emplace(filename, objects);
|
|
|
|
|
|
|
|
return objects;
|
|
|
|
}
|