1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-25 03:42:48 +01:00

Use only static memory allocations of DynamicObjectData

This commit is contained in:
Filip Gawin 2018-11-08 11:57:56 +01:00
parent dce5257eb3
commit 3c98a7222d
8 changed files with 51 additions and 49 deletions

View File

@ -484,7 +484,6 @@ public:
* Instances different physical properties. * Instances different physical properties.
*/ */
struct DynamicObjectData { struct DynamicObjectData {
std::string modelName;
float mass; // Kg float mass; // Kg
float turnMass; // Kg m^3 float turnMass; // Kg m^3
float airRes; // fraction float airRes; // fraction

View File

@ -314,7 +314,7 @@ public:
/** /**
* DynamicObjectData * DynamicObjectData
*/ */
std::map<std::string, std::shared_ptr<DynamicObjectData>> dynamicObjectData; std::unordered_map<std::string, DynamicObjectData> dynamicObjectData;
std::vector<WeaponData> weaponData; std::vector<WeaponData> weaponData;

View File

@ -138,10 +138,12 @@ InstanceObject* GameWorld::createInstance(const uint16_t id,
} }
// Check for dynamic data. // Check for dynamic data.
auto dyit = data->dynamicObjectData.find(oi->name); const auto dyIt = data->dynamicObjectData.find(oi->name);
std::shared_ptr<DynamicObjectData> dydata;
if (dyit != data->dynamicObjectData.end()) { DynamicObjectData* dydata = nullptr;
dydata = dyit->second;
if (dyIt != data->dynamicObjectData.end()) {
dydata = &dyIt->second;
} }
if (oi->name.empty()) { if (oi->name.empty()) {

View File

@ -12,8 +12,9 @@
#include <data/WeaponData.hpp> #include <data/WeaponData.hpp>
#include <objects/VehicleInfo.hpp> #include <objects/VehicleInfo.hpp>
void GenericDATLoader::loadDynamicObjects(const std::string& name, void GenericDATLoader::loadDynamicObjects(
DynamicObjectDataPtrs& data) { const std::string& name,
std::unordered_map<std::string, DynamicObjectData>& data) {
std::ifstream dfile(name.c_str()); std::ifstream dfile(name.c_str());
if (dfile.is_open()) { if (dfile.is_open()) {
@ -24,39 +25,40 @@ void GenericDATLoader::loadDynamicObjects(const std::string& name,
if (lineBuff.at(0) == '*') continue; if (lineBuff.at(0) == '*') continue;
std::stringstream ss(lineBuff); std::stringstream ss(lineBuff);
auto dyndata = std::make_shared<DynamicObjectData>(); DynamicObjectData dyndata{};
std::string modelName;
ss >> dyndata->modelName; ss >> modelName;
auto cpos = dyndata->modelName.find(','); auto cpos = modelName.find(',');
if (cpos != dyndata->modelName.npos) { if (cpos != modelName.npos) {
dyndata->modelName.erase(cpos); modelName.erase(cpos);
} }
ss >> dyndata->mass; ss >> dyndata.mass;
if (ss.peek() == ',') ss.ignore(1); if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->turnMass; ss >> dyndata.turnMass;
if (ss.peek() == ',') ss.ignore(1); if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->airRes; ss >> dyndata.airRes;
if (ss.peek() == ',') ss.ignore(1); if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->elasticity; ss >> dyndata.elasticity;
if (ss.peek() == ',') ss.ignore(1); if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->buoyancy; ss >> dyndata.buoyancy;
if (ss.peek() == ',') ss.ignore(1); if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->uprootForce; ss >> dyndata.uprootForce;
if (ss.peek() == ',') ss.ignore(1); if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->collDamageMulti; ss >> dyndata.collDamageMulti;
if (ss.peek() == ',') ss.ignore(1); if (ss.peek() == ',') ss.ignore(1);
int tmp; int tmp;
ss >> tmp; ss >> tmp;
dyndata->collDamageEffect = tmp; dyndata.collDamageEffect = tmp;
if (ss.peek() == ',') ss.ignore(1); if (ss.peek() == ',') ss.ignore(1);
ss >> tmp; ss >> tmp;
dyndata->collResponseFlags = tmp; dyndata.collResponseFlags = tmp;
if (ss.peek() == ',') ss.ignore(1); if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->cameraAvoid; ss >> dyndata.cameraAvoid;
RW_CHECK(ss.eof() || ss.good(), "Loading dynamicsObject data file " << name << " failed"); RW_CHECK(ss.eof() || ss.good(), "Loading dynamicsObject data file " << name << " failed");
data.insert({dyndata->modelName, dyndata}); data.emplace(modelName, std::move(dyndata));
} }
} }
} }

View File

@ -4,12 +4,10 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
struct DynamicObjectData; struct DynamicObjectData;
typedef std::shared_ptr<DynamicObjectData> DynamicObjectDataPtr;
typedef std::map<std::string, DynamicObjectDataPtr> DynamicObjectDataPtrs;
struct WeaponData; struct WeaponData;
struct VehicleInfo; struct VehicleInfo;
typedef std::shared_ptr<VehicleInfo> VehicleInfoPtr; typedef std::shared_ptr<VehicleInfo> VehicleInfoPtr;
@ -17,8 +15,9 @@ typedef std::map<std::string, VehicleInfoPtr> VehicleInfoPtrs;
class GenericDATLoader { class GenericDATLoader {
public: public:
void loadDynamicObjects(const std::string& name, void loadDynamicObjects(
DynamicObjectDataPtrs& data); const std::string& name,
std::unordered_map<std::string, DynamicObjectData>& data);
void loadWeapons(const std::string& name, void loadWeapons(const std::string& name,
std::vector<WeaponData>& weaponData); std::vector<WeaponData>& weaponData);

View File

@ -22,7 +22,7 @@
InstanceObject::InstanceObject(GameWorld* engine, const glm::vec3& pos, InstanceObject::InstanceObject(GameWorld* engine, const glm::vec3& pos,
const glm::quat& rot, const glm::vec3& scale, const glm::quat& rot, const glm::vec3& scale,
BaseModelInfo* modelinfo, BaseModelInfo* modelinfo,
const std::shared_ptr<DynamicObjectData>& dyn) DynamicObjectData* dyn)
: GameObject(engine, pos, rot, modelinfo) : GameObject(engine, pos, rot, modelinfo)
, scale(scale) , scale(scale)
, dynamics(dyn) { , dynamics(dyn) {
@ -175,7 +175,7 @@ void InstanceObject::changeModel(BaseModelInfo* incoming, int atomicNumber) {
if (collision) { if (collision) {
body = std::make_unique<CollisionInstance>(); body = std::make_unique<CollisionInstance>();
body->createPhysicsBody(this, collision, dynamics.get()); body->createPhysicsBody(this, collision, dynamics);
} }
} }
} }

View File

@ -32,12 +32,12 @@ class InstanceObject final : public GameObject {
public: public:
glm::vec3 scale; glm::vec3 scale;
std::unique_ptr<CollisionInstance> body; std::unique_ptr<CollisionInstance> body;
std::shared_ptr<DynamicObjectData> dynamics; DynamicObjectData* dynamics;
InstanceObject(GameWorld* engine, const glm::vec3& pos, InstanceObject(GameWorld* engine, const glm::vec3& pos,
const glm::quat& rot, const glm::vec3& scale, const glm::quat& rot, const glm::vec3& scale,
BaseModelInfo* modelinfo, BaseModelInfo* modelinfo,
const std::shared_ptr<DynamicObjectData>& dyn); DynamicObjectData* dyn);
~InstanceObject() override; ~InstanceObject() override;
Type type() const override { Type type() const override {

View File

@ -44,28 +44,28 @@ BOOST_AUTO_TEST_CASE(test_weapon_dat) {
BOOST_AUTO_TEST_CASE(test_dynamic_dat_loader) { BOOST_AUTO_TEST_CASE(test_dynamic_dat_loader) {
GenericDATLoader l; GenericDATLoader l;
DynamicObjectDataPtrs loaded; std::unordered_map<std::string, DynamicObjectData> dynamicObjects;
l.loadDynamicObjects(Global::get().getGamePath() + "/data/object.dat", l.loadDynamicObjects(Global::get().getGamePath() + "/data/object.dat",
loaded); dynamicObjects);
BOOST_ASSERT(loaded.size() > 0); BOOST_ASSERT(!dynamicObjects.empty());
BOOST_ASSERT(loaded.find("wastebin") != loaded.end()); BOOST_ASSERT(dynamicObjects.find("wastebin") != dynamicObjects.end());
BOOST_ASSERT(loaded.find("lamppost1") != loaded.end()); BOOST_ASSERT(dynamicObjects.find("lamppost1") != dynamicObjects.end());
DynamicObjectDataPtr lamp = loaded["lamppost1"]; auto lamp = dynamicObjects.at("lamppost1");
BOOST_CHECK_EQUAL(lamp->mass, 600.0); BOOST_CHECK_EQUAL(lamp.mass, 600.0);
BOOST_CHECK_EQUAL(lamp->turnMass, 4000.0); BOOST_CHECK_EQUAL(lamp.turnMass, 4000.0);
BOOST_CHECK_CLOSE(lamp->airRes, 0.99, 1.0); BOOST_CHECK_CLOSE(lamp.airRes, 0.99, 1.0);
BOOST_CHECK_CLOSE(lamp->elasticity, 0.05, 0.01); BOOST_CHECK_CLOSE(lamp.elasticity, 0.05, 0.01);
BOOST_CHECK_EQUAL(lamp->buoyancy, 50.0); BOOST_CHECK_EQUAL(lamp.buoyancy, 50.0);
BOOST_CHECK_EQUAL(lamp->uprootForce, 400); BOOST_CHECK_EQUAL(lamp.uprootForce, 400);
BOOST_CHECK_EQUAL(lamp->collDamageMulti, 1.0); BOOST_CHECK_EQUAL(lamp.collDamageMulti, 1.0);
BOOST_CHECK_EQUAL(lamp->collDamageEffect, 1); BOOST_CHECK_EQUAL(lamp.collDamageEffect, 1);
BOOST_CHECK_EQUAL(lamp->collResponseFlags, 1); BOOST_CHECK_EQUAL(lamp.collResponseFlags, 1);
BOOST_CHECK_EQUAL(lamp->cameraAvoid, false); BOOST_CHECK_EQUAL(lamp.cameraAvoid, false);
} }
BOOST_AUTO_TEST_CASE(test_handling_data_loader) { BOOST_AUTO_TEST_CASE(test_handling_data_loader) {