Move LoadObjects -> ResourceManager to avoid repeated loading from files.
parent
4c768c5e78
commit
6d0202eaff
|
@ -110,7 +110,7 @@ void Player::previousWeapon() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::fire() {
|
void Player::fire() {
|
||||||
if(attached(ObjectNameTag("camera")) != nullptr) {
|
if(attached(ObjectNameTag("Camera")) != nullptr) {
|
||||||
auto damagedPlayers = _weapons[_selectedWeapon]->fire(_rayCastFunction);
|
auto damagedPlayers = _weapons[_selectedWeapon]->fire(_rayCastFunction);
|
||||||
for(auto& [damagedPlayerName, damage] : damagedPlayers) {
|
for(auto& [damagedPlayerName, damage] : damagedPlayers) {
|
||||||
sf::Uint16 targetId = std::stoi(damagedPlayerName.str().substr(6));
|
sf::Uint16 targetId = std::stoi(damagedPlayerName.str().substr(6));
|
||||||
|
|
|
@ -41,7 +41,7 @@ void PlayerController::update() {
|
||||||
Keyboard::isKeyPressed(sf::Keyboard::W) ||
|
Keyboard::isKeyPressed(sf::Keyboard::W) ||
|
||||||
Keyboard::isKeyPressed(sf::Keyboard::S));
|
Keyboard::isKeyPressed(sf::Keyboard::S));
|
||||||
|
|
||||||
std::shared_ptr<Object> camera = _player->attached(ObjectNameTag("camera"));
|
std::shared_ptr<Object> camera = _player->attached(ObjectNameTag("Camera"));
|
||||||
if(camera != nullptr && _inRunning && _player->inCollision()) {
|
if(camera != nullptr && _inRunning && _player->inCollision()) {
|
||||||
if (!Timeline::isInAnimList(AnimationListTag("camera_hor_oscil"))) {
|
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));
|
Timeline::animate(AnimationListTag("camera_hor_oscil"), new ATranslate(camera, -camera->left() / 6, 0.3,Animation::LoopOut::None, Animation::InterpolationType::cos));
|
||||||
|
|
|
@ -87,7 +87,7 @@ void Shooter::start() {
|
||||||
player->initWeapons();
|
player->initWeapons();
|
||||||
|
|
||||||
camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0});
|
camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0});
|
||||||
player->attach(camera, ObjectNameTag("camera"));
|
player->attach(camera, ObjectNameTag("Camera"));
|
||||||
|
|
||||||
world->addBody(player, ObjectNameTag("Player"));
|
world->addBody(player, ObjectNameTag("Player"));
|
||||||
player->translate(Vec3D{0, 10, 0});
|
player->translate(Vec3D{0, 10, 0});
|
||||||
|
|
|
@ -3,11 +3,10 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include "Mesh.h"
|
#include "Mesh.h"
|
||||||
#include "utils/Log.h"
|
#include "utils/Log.h"
|
||||||
|
#include "ResourceManager.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -23,7 +22,7 @@ Mesh &Mesh::operator*=(const Matrix4x4 &matrix4X4) {
|
||||||
|
|
||||||
Mesh &Mesh::loadObj(const std::string& filename, const Vec3D& scale) {
|
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& obj : objects) {
|
||||||
for (auto &tri : obj->triangles()) {
|
for (auto &tri : obj->triangles()) {
|
||||||
_tris.push_back(tri);
|
_tris.push_back(tri);
|
||||||
|
@ -79,75 +78,6 @@ void Mesh::setColor(const sf::Color& c) {
|
||||||
setTriangles(newTriangles);
|
setTriangles(newTriangles);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<Mesh>> Mesh::LoadObjects(const string &filename, const Vec3D &scale) {
|
|
||||||
std::vector<std::shared_ptr<Mesh>> objects;
|
|
||||||
map<string, sf::Color> maters;
|
|
||||||
|
|
||||||
ifstream file(filename);
|
|
||||||
if (!file.is_open())
|
|
||||||
{
|
|
||||||
Log::log("Mesh::LoadObjects(): cannot load file from " + filename);
|
|
||||||
return objects;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<Point4D> verts;
|
|
||||||
std::vector<Triangle> 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<Mesh>(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<Mesh>(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 Mesh::LineTo(const Vec3D& from, const Vec3D& to, double line_width, const sf::Color& color) {
|
||||||
|
|
||||||
Mesh line;
|
Mesh line;
|
||||||
|
|
|
@ -52,7 +52,6 @@ public:
|
||||||
~Mesh() override;
|
~Mesh() override;
|
||||||
|
|
||||||
Mesh static Obj(const std::string& filename);
|
Mesh static Obj(const std::string& filename);
|
||||||
std::vector<std::shared_ptr<Mesh>> 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});
|
Mesh static LineTo(const Vec3D& from, const Vec3D& to, double line_width = 0.1, const sf::Color& color = {150, 150, 150, 255});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,11 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "ResourceManager.h"
|
#include "ResourceManager.h"
|
||||||
|
#include "utils/Log.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <sstream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
ResourceManager* ResourceManager::_instance = nullptr;
|
ResourceManager* ResourceManager::_instance = nullptr;
|
||||||
|
|
||||||
|
@ -34,6 +37,7 @@ void ResourceManager::unloadAllResources() {
|
||||||
unloadTextures();
|
unloadTextures();
|
||||||
unloadSoundBuffers();
|
unloadSoundBuffers();
|
||||||
unloadFonts();
|
unloadFonts();
|
||||||
|
unloadObjects();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<sf::Texture> ResourceManager::loadTexture(const std::string& filename) {
|
std::shared_ptr<sf::Texture> ResourceManager::loadTexture(const std::string& filename) {
|
||||||
|
@ -71,7 +75,7 @@ std::shared_ptr<sf::SoundBuffer> ResourceManager::loadSoundBuffer(const std::str
|
||||||
if (!soundBuffer->loadFromFile(filename))
|
if (!soundBuffer->loadFromFile(filename))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
// If success - remember and return texture pointer
|
// If success - remember and return sound pointer
|
||||||
_instance->_soundBuffers.emplace(filename, soundBuffer);
|
_instance->_soundBuffers.emplace(filename, soundBuffer);
|
||||||
|
|
||||||
return soundBuffer;
|
return soundBuffer;
|
||||||
|
@ -91,15 +95,97 @@ std::shared_ptr<sf::Font> ResourceManager::loadFont(const std::string& filename)
|
||||||
if (!font->loadFromFile(filename))
|
if (!font->loadFromFile(filename))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
// If success - remember and return texture pointer
|
// If success - remember and return font pointer
|
||||||
_instance->_fonts.emplace(filename, font);
|
_instance->_fonts.emplace(filename, font);
|
||||||
|
|
||||||
return font;
|
return font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<Mesh>> 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<std::shared_ptr<Mesh>> objects{};
|
||||||
|
std::map<std::string, sf::Color> maters{};
|
||||||
|
|
||||||
|
std::ifstream file(filename);
|
||||||
|
if (!file.is_open())
|
||||||
|
{
|
||||||
|
Log::log("Mesh::LoadObjects(): cannot load file from " + filename);
|
||||||
|
return objects;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Point4D> verts{};
|
||||||
|
std::vector<Triangle> 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<Mesh>(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<Mesh>(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() {
|
void ResourceManager::free() {
|
||||||
unloadAllResources();
|
unloadAllResources();
|
||||||
delete _instance;
|
delete _instance;
|
||||||
_instance = nullptr;
|
_instance = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,12 +8,14 @@
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <SFML/Audio.hpp>
|
#include <SFML/Audio.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include "Mesh.h"
|
||||||
|
|
||||||
class ResourceManager final {
|
class ResourceManager final {
|
||||||
private:
|
private:
|
||||||
std::map<std::string, std::shared_ptr<sf::Texture>> _textures;
|
std::map<std::string, std::shared_ptr<sf::Texture>> _textures;
|
||||||
std::map<std::string, std::shared_ptr<sf::Font>> _fonts;
|
std::map<std::string, std::shared_ptr<sf::Font>> _fonts;
|
||||||
std::map<std::string, std::shared_ptr<sf::SoundBuffer>> _soundBuffers;
|
std::map<std::string, std::shared_ptr<sf::SoundBuffer>> _soundBuffers;
|
||||||
|
std::map<std::string, std::vector<std::shared_ptr<Mesh>>> _objects;
|
||||||
|
|
||||||
static ResourceManager* _instance;
|
static ResourceManager* _instance;
|
||||||
|
|
||||||
|
@ -23,10 +25,10 @@ public:
|
||||||
ResourceManager& operator=(ResourceManager&) = delete;
|
ResourceManager& operator=(ResourceManager&) = delete;
|
||||||
|
|
||||||
// Unloads all currently loaded textures.
|
// Unloads all currently loaded textures.
|
||||||
|
static void unloadObjects();
|
||||||
static void unloadTextures();
|
static void unloadTextures();
|
||||||
static void unloadSoundBuffers();
|
static void unloadSoundBuffers();
|
||||||
static void unloadFonts();
|
static void unloadFonts();
|
||||||
static void unloadShaders();
|
|
||||||
|
|
||||||
static void unloadAllResources();
|
static void unloadAllResources();
|
||||||
|
|
||||||
|
@ -36,6 +38,7 @@ public:
|
||||||
// Try to load texture from file.
|
// Try to load texture from file.
|
||||||
// If success returns pointer to texture.
|
// If success returns pointer to texture.
|
||||||
// Otherwise returns nullptr.
|
// Otherwise returns nullptr.
|
||||||
|
static std::vector<std::shared_ptr<Mesh>> loadObjects(const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1});
|
||||||
static std::shared_ptr<sf::Texture> loadTexture(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::Font> loadFont(const std::string& filename);
|
||||||
static std::shared_ptr<sf::SoundBuffer> loadSoundBuffer(const std::string& filename);
|
static std::shared_ptr<sf::SoundBuffer> loadSoundBuffer(const std::string& filename);
|
||||||
|
|
|
@ -45,8 +45,10 @@ sf::Sound::Status SoundController::getStatus(const SoundTag& soundTag) {
|
||||||
|
|
||||||
if(_instance->_sounds.count(soundTag) > 0)
|
if(_instance->_sounds.count(soundTag) > 0)
|
||||||
return _instance->_sounds[soundTag].getStatus();
|
return _instance->_sounds[soundTag].getStatus();
|
||||||
else
|
else {
|
||||||
|
_instance->_sounds.erase(soundTag);
|
||||||
return sf::Sound::Status::Stopped;
|
return sf::Sound::Status::Stopped;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SoundController::free() {
|
void SoundController::free() {
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include "World.h"
|
#include "World.h"
|
||||||
#include "utils/Log.h"
|
#include "utils/Log.h"
|
||||||
#include "Plane.h"
|
#include "Plane.h"
|
||||||
|
#include "ResourceManager.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -46,7 +47,7 @@ std::pair<Vec3D, ObjectNameTag> World::rayCast(const Vec3D& from, const Vec3D& t
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::loadMap(const std::string& filename, const Vec3D& scale) {
|
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++) {
|
for(unsigned i = 0; i < objs.size(); i++) {
|
||||||
ObjectNameTag meshName = ObjectNameTag("map_" + to_string(i));
|
ObjectNameTag meshName = ObjectNameTag("map_" + to_string(i));
|
||||||
addBody(std::make_shared<RigidBody>(*objs[i]), meshName);
|
addBody(std::make_shared<RigidBody>(*objs[i]), meshName);
|
||||||
|
|
Loading…
Reference in New Issue