From 6d0202eaff35882646788817169a3d820b3d1bb3 Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Mon, 18 Oct 2021 10:44:04 +0700 Subject: [PATCH] Move LoadObjects -> ResourceManager to avoid repeated loading from files. --- Player.cpp | 2 +- PlayerController.cpp | 2 +- Shooter.cpp | 2 +- engine/Mesh.cpp | 74 +----------------------------- engine/Mesh.h | 1 - engine/ResourceManager.cpp | 92 ++++++++++++++++++++++++++++++++++++-- engine/ResourceManager.h | 5 ++- engine/SoundController.cpp | 4 +- engine/World.cpp | 3 +- 9 files changed, 103 insertions(+), 82 deletions(-) diff --git a/Player.cpp b/Player.cpp index b37b8ec..fafbdc7 100644 --- a/Player.cpp +++ b/Player.cpp @@ -110,7 +110,7 @@ void Player::previousWeapon() { } void Player::fire() { - if(attached(ObjectNameTag("camera")) != nullptr) { + if(attached(ObjectNameTag("Camera")) != nullptr) { auto damagedPlayers = _weapons[_selectedWeapon]->fire(_rayCastFunction); for(auto& [damagedPlayerName, damage] : damagedPlayers) { sf::Uint16 targetId = std::stoi(damagedPlayerName.str().substr(6)); diff --git a/PlayerController.cpp b/PlayerController.cpp index 02bbcc5..73d8d89 100644 --- a/PlayerController.cpp +++ b/PlayerController.cpp @@ -41,7 +41,7 @@ void PlayerController::update() { Keyboard::isKeyPressed(sf::Keyboard::W) || Keyboard::isKeyPressed(sf::Keyboard::S)); - std::shared_ptr camera = _player->attached(ObjectNameTag("camera")); + std::shared_ptr camera = _player->attached(ObjectNameTag("Camera")); if(camera != nullptr && _inRunning && _player->inCollision()) { if (!Timeline::isInAnimList(AnimationListTag("camera_hor_oscil"))) { Timeline::animate(AnimationListTag("camera_hor_oscil"), new ATranslate(camera, -camera->left() / 6, 0.3,Animation::LoopOut::None, Animation::InterpolationType::cos)); diff --git a/Shooter.cpp b/Shooter.cpp index 257fc0e..1656952 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -87,7 +87,7 @@ void Shooter::start() { player->initWeapons(); camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0}); - player->attach(camera, ObjectNameTag("camera")); + player->attach(camera, ObjectNameTag("Camera")); world->addBody(player, ObjectNameTag("Player")); player->translate(Vec3D{0, 10, 0}); diff --git a/engine/Mesh.cpp b/engine/Mesh.cpp index cc7facd..03f66e9 100644 --- a/engine/Mesh.cpp +++ b/engine/Mesh.cpp @@ -3,11 +3,10 @@ // #include -#include -#include #include #include "Mesh.h" #include "utils/Log.h" +#include "ResourceManager.h" using namespace std; @@ -23,7 +22,7 @@ Mesh &Mesh::operator*=(const Matrix4x4 &matrix4X4) { Mesh &Mesh::loadObj(const std::string& filename, const Vec3D& scale) { - auto objects = Mesh::LoadObjects(filename, scale); + auto objects = ResourceManager::loadObjects(filename, scale); for(auto& obj : objects) { for (auto &tri : obj->triangles()) { _tris.push_back(tri); @@ -79,75 +78,6 @@ void Mesh::setColor(const sf::Color& c) { setTriangles(newTriangles); } -std::vector> Mesh::LoadObjects(const string &filename, const Vec3D &scale) { - std::vector> objects; - map maters; - - ifstream file(filename); - if (!file.is_open()) - { - Log::log("Mesh::LoadObjects(): cannot load file from " + filename); - return objects; - } - - vector verts; - std::vector tris; - sf::Color currentColor = sf::Color(255, 245, 194, 255); - - while (!file.eof()) - { - char line[128]; - file.getline(line, 128); - - stringstream s; - s << line; - - char junk; - if(line[0] == 'o') { - if(!tris.empty()) { - objects.push_back(make_shared(tris)); - objects.back()->scale(scale); - } - tris.clear(); - } - if (line[0] == 'v') - { - double x, y, z; - s >> junk >> x >> y >> z; - verts.emplace_back(x, y, z, 1); - } - if(line[0] == 'g') { - string matInfo; - s >> junk >> matInfo; - 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]; - 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])}); - } - } - - if(!tris.empty()) { - objects.push_back(make_shared(tris)); - objects.back()->scale(scale); - } - - file.close(); - - return objects; -} - Mesh Mesh::LineTo(const Vec3D& from, const Vec3D& to, double line_width, const sf::Color& color) { Mesh line; diff --git a/engine/Mesh.h b/engine/Mesh.h index 84c51af..7603195 100644 --- a/engine/Mesh.h +++ b/engine/Mesh.h @@ -52,7 +52,6 @@ public: ~Mesh() override; Mesh static Obj(const std::string& filename); - std::vector> static LoadObjects(const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1}); Mesh static LineTo(const Vec3D& from, const Vec3D& to, double line_width = 0.1, const sf::Color& color = {150, 150, 150, 255}); }; diff --git a/engine/ResourceManager.cpp b/engine/ResourceManager.cpp index 60ce0e3..5aa581d 100644 --- a/engine/ResourceManager.cpp +++ b/engine/ResourceManager.cpp @@ -3,8 +3,11 @@ // #include "ResourceManager.h" +#include "utils/Log.h" #include #include +#include +#include ResourceManager* ResourceManager::_instance = nullptr; @@ -34,6 +37,7 @@ void ResourceManager::unloadAllResources() { unloadTextures(); unloadSoundBuffers(); unloadFonts(); + unloadObjects(); } std::shared_ptr ResourceManager::loadTexture(const std::string& filename) { @@ -71,7 +75,7 @@ std::shared_ptr ResourceManager::loadSoundBuffer(const std::str if (!soundBuffer->loadFromFile(filename)) return nullptr; - // If success - remember and return texture pointer + // If success - remember and return sound pointer _instance->_soundBuffers.emplace(filename, soundBuffer); return soundBuffer; @@ -91,15 +95,97 @@ std::shared_ptr ResourceManager::loadFont(const std::string& filename) if (!font->loadFromFile(filename)) return nullptr; - // If success - remember and return texture pointer + // If success - remember and return font pointer _instance->_fonts.emplace(filename, font); return font; } +std::vector> ResourceManager::loadObjects(const std::string &filename, const Vec3D& scale) { + + // If objects is already loaded - return pointer to it + auto it = _instance->_objects.find(filename); + if (it != _instance->_objects.end()) + return it->second; + + + std::vector> objects{}; + std::map maters{}; + + std::ifstream file(filename); + if (!file.is_open()) + { + Log::log("Mesh::LoadObjects(): cannot load file from " + filename); + return objects; + } + + std::vector verts{}; + std::vector tris{}; + sf::Color currentColor = sf::Color(255, 245, 194, 255); + + while (!file.eof()) + { + char line[128]; + file.getline(line, 128); + + std::stringstream s; + s << line; + + char junk; + if(line[0] == 'o') { + if(!tris.empty()) { + objects.push_back(make_shared(tris)); + objects.back()->scale(scale); + } + tris.clear(); + } + if (line[0] == 'v') + { + double x, y, z; + s >> junk >> x >> y >> z; + verts.emplace_back(x, y, z, 1); + } + 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])}); + } + } + + if(!tris.empty()) { + objects.push_back(make_shared(tris)); + objects.back()->scale(scale); + } + + file.close(); + + // If success - remember and return vector of objects pointer + _instance->_objects.emplace(filename, objects); + + return objects; +} + +void ResourceManager::unloadObjects() { + _instance->_objects.clear(); +} + void ResourceManager::free() { unloadAllResources(); delete _instance; _instance = nullptr; } - diff --git a/engine/ResourceManager.h b/engine/ResourceManager.h index d500249..2423e91 100644 --- a/engine/ResourceManager.h +++ b/engine/ResourceManager.h @@ -8,12 +8,14 @@ #include #include #include +#include "Mesh.h" class ResourceManager final { private: std::map> _textures; std::map> _fonts; std::map> _soundBuffers; + std::map>> _objects; static ResourceManager* _instance; @@ -23,10 +25,10 @@ public: ResourceManager& operator=(ResourceManager&) = delete; // Unloads all currently loaded textures. + static void unloadObjects(); static void unloadTextures(); static void unloadSoundBuffers(); static void unloadFonts(); - static void unloadShaders(); static void unloadAllResources(); @@ -36,6 +38,7 @@ public: // Try to load texture from file. // If success returns pointer to texture. // Otherwise returns nullptr. + static std::vector> loadObjects(const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1}); static std::shared_ptr loadTexture(const std::string& filename); static std::shared_ptr loadFont(const std::string& filename); static std::shared_ptr loadSoundBuffer(const std::string& filename); diff --git a/engine/SoundController.cpp b/engine/SoundController.cpp index 1580447..0d7ed3e 100644 --- a/engine/SoundController.cpp +++ b/engine/SoundController.cpp @@ -45,8 +45,10 @@ sf::Sound::Status SoundController::getStatus(const SoundTag& soundTag) { if(_instance->_sounds.count(soundTag) > 0) return _instance->_sounds[soundTag].getStatus(); - else + else { + _instance->_sounds.erase(soundTag); return sf::Sound::Status::Stopped; + } } void SoundController::free() { diff --git a/engine/World.cpp b/engine/World.cpp index 38e69ef..44ada40 100644 --- a/engine/World.cpp +++ b/engine/World.cpp @@ -5,6 +5,7 @@ #include "World.h" #include "utils/Log.h" #include "Plane.h" +#include "ResourceManager.h" using namespace std; @@ -46,7 +47,7 @@ std::pair World::rayCast(const Vec3D& from, const Vec3D& t } void World::loadMap(const std::string& filename, const Vec3D& scale) { - auto objs = Mesh::LoadObjects(filename, scale); + auto objs = ResourceManager::loadObjects(filename, scale); for(unsigned i = 0; i < objs.size(); i++) { ObjectNameTag meshName = ObjectNameTag("map_" + to_string(i)); addBody(std::make_shared(*objs[i]), meshName);