Move LoadObjects -> ResourceManager to avoid repeated loading from files.

master
Vectozavr 2021-10-18 13:21:09 +07:00
parent b3682db958
commit 8678e8c929
5 changed files with 22 additions and 22 deletions

View File

@ -4,6 +4,7 @@
#include "Shooter.h"
#include <fstream>
#include <utility>
#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> weapon){ addWeapon(weapon); });
player->setRemoveWeaponCallBack([this](std::shared_ptr<Weapon> weapon){ removeWeapon(weapon); });
player->setAddWeaponCallBack([this](std::shared_ptr<Weapon> weapon){ addWeapon(std::move(weapon)); });
player->setRemoveWeaponCallBack([this](std::shared_ptr<Weapon> weapon){ removeWeapon(std::move(weapon)); });
player->initWeapons();

View File

@ -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;
}

View File

@ -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<sf::Texture> ResourceManager::loadTexture(const std::string& filename) {
if(!_instance)
return nullptr;
@ -101,7 +111,7 @@ std::shared_ptr<sf::Font> ResourceManager::loadFont(const std::string& filename)
return font;
}
std::vector<std::shared_ptr<Mesh>> ResourceManager::loadObjects(const std::string &filename, const Vec3D& scale) {
std::vector<std::shared_ptr<Mesh>> 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<std::shared_ptr<Mesh>> ResourceManager::loadObjects(const std::strin
char junk;
if(line[0] == 'o') {
if(!tris.empty()) {
if(!tris.empty())
objects.push_back(make_shared<Mesh>(tris));
objects.back()->scale(scale);
}
tris.clear();
}
if (line[0] == 'v')
@ -167,10 +175,8 @@ std::vector<std::shared_ptr<Mesh>> ResourceManager::loadObjects(const std::strin
}
}
if(!tris.empty()) {
if(!tris.empty())
objects.push_back(make_shared<Mesh>(tris));
objects.back()->scale(scale);
}
file.close();
@ -179,13 +185,3 @@ std::vector<std::shared_ptr<Mesh>> ResourceManager::loadObjects(const std::strin
return objects;
}
void ResourceManager::unloadObjects() {
_instance->_objects.clear();
}
void ResourceManager::free() {
unloadAllResources();
delete _instance;
_instance = nullptr;
}

View File

@ -38,7 +38,7 @@ public:
// Try to load texture from file.
// If success returns pointer to texture.
// Otherwise returns nullptr.
static std::vector<std::shared_ptr<Mesh>> loadObjects(const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1});
static std::vector<std::shared_ptr<Mesh>> loadObjects(const std::string& filename);
static std::shared_ptr<sf::Texture> loadTexture(const std::string& filename);
static std::shared_ptr<sf::Font> loadFont(const std::string& filename);
static std::shared_ptr<sf::SoundBuffer> loadSoundBuffer(const std::string& filename);

View File

@ -47,10 +47,11 @@ std::pair<Vec3D, ObjectNameTag> 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<RigidBody>(*objs[i]), meshName);
body(meshName)->scale(scale);
}
}