From 8678e8c929ca31b05657d6c7d99a77a5129c6177 Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Mon, 18 Oct 2021 13:21:09 +0700 Subject: [PATCH] Move LoadObjects -> ResourceManager to avoid repeated loading from files. --- Shooter.cpp | 5 +++-- engine/Mesh.cpp | 4 +++- engine/ResourceManager.cpp | 30 +++++++++++++----------------- engine/ResourceManager.h | 2 +- engine/World.cpp | 3 ++- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Shooter.cpp b/Shooter.cpp index 1656952..6d87f58 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -4,6 +4,7 @@ #include "Shooter.h" #include +#include #include "engine/animation/AColor.h" #include "engine/animation/AFunction.h" #include "engine/animation/ARotate.h" @@ -81,8 +82,8 @@ void Shooter::start() { player->setDamagePlayerCallBack([this] (sf::Uint16 targetId, double damage) { client->damagePlayer(targetId, damage); }); player->setRayCastFunction([this](const Vec3D& from, const Vec3D& to) { return world->rayCast(from, to, "Enemy"); }); player->setTakeBonusCallBack([this] (const string& bonusName) { client->takeBonus(bonusName); }); - player->setAddWeaponCallBack([this](std::shared_ptr weapon){ addWeapon(weapon); }); - player->setRemoveWeaponCallBack([this](std::shared_ptr weapon){ removeWeapon(weapon); }); + player->setAddWeaponCallBack([this](std::shared_ptr weapon){ addWeapon(std::move(weapon)); }); + player->setRemoveWeaponCallBack([this](std::shared_ptr weapon){ removeWeapon(std::move(weapon)); }); player->initWeapons(); diff --git a/engine/Mesh.cpp b/engine/Mesh.cpp index 4bb62b9..5acb16d 100644 --- a/engine/Mesh.cpp +++ b/engine/Mesh.cpp @@ -20,12 +20,14 @@ Mesh &Mesh::operator*=(const Matrix4x4 &matrix4X4) { Mesh &Mesh::loadObj(const std::string& filename, const Vec3D& scale) { - auto objects = ResourceManager::loadObjects(filename, scale); + auto objects = ResourceManager::loadObjects(filename); for(auto& obj : objects) { for (auto &tri : obj->triangles()) { _tris.push_back(tri); } } + this->scale(scale); + return *this; } diff --git a/engine/ResourceManager.cpp b/engine/ResourceManager.cpp index 5aa581d..1aafa6b 100644 --- a/engine/ResourceManager.cpp +++ b/engine/ResourceManager.cpp @@ -33,6 +33,10 @@ void ResourceManager::unloadFonts() { _instance->_fonts.clear(); } +void ResourceManager::unloadObjects() { + _instance->_objects.clear(); +} + void ResourceManager::unloadAllResources() { unloadTextures(); unloadSoundBuffers(); @@ -40,6 +44,12 @@ void ResourceManager::unloadAllResources() { unloadObjects(); } +void ResourceManager::free() { + unloadAllResources(); + delete _instance; + _instance = nullptr; +} + std::shared_ptr ResourceManager::loadTexture(const std::string& filename) { if(!_instance) return nullptr; @@ -101,7 +111,7 @@ std::shared_ptr ResourceManager::loadFont(const std::string& filename) return font; } -std::vector> ResourceManager::loadObjects(const std::string &filename, const Vec3D& scale) { +std::vector> ResourceManager::loadObjects(const std::string &filename) { // If objects is already loaded - return pointer to it auto it = _instance->_objects.find(filename); @@ -133,10 +143,8 @@ std::vector> ResourceManager::loadObjects(const std::strin char junk; if(line[0] == 'o') { - if(!tris.empty()) { + if(!tris.empty()) objects.push_back(make_shared(tris)); - objects.back()->scale(scale); - } tris.clear(); } if (line[0] == 'v') @@ -167,10 +175,8 @@ std::vector> ResourceManager::loadObjects(const std::strin } } - if(!tris.empty()) { + if(!tris.empty()) objects.push_back(make_shared(tris)); - objects.back()->scale(scale); - } file.close(); @@ -179,13 +185,3 @@ std::vector> ResourceManager::loadObjects(const std::strin 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 2423e91..91fcf2c 100644 --- a/engine/ResourceManager.h +++ b/engine/ResourceManager.h @@ -38,7 +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::vector> loadObjects(const std::string& filename); 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/World.cpp b/engine/World.cpp index 44ada40..ecd3a84 100644 --- a/engine/World.cpp +++ b/engine/World.cpp @@ -47,10 +47,11 @@ std::pair World::rayCast(const Vec3D& from, const Vec3D& t } void World::loadMap(const std::string& filename, const Vec3D& scale) { - auto objs = ResourceManager::loadObjects(filename, scale); + auto objs = ResourceManager::loadObjects(filename); for(unsigned i = 0; i < objs.size(); i++) { ObjectNameTag meshName = ObjectNameTag("map_" + to_string(i)); addBody(std::make_shared(*objs[i]), meshName); + body(meshName)->scale(scale); } }