1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 02:12:45 +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.
*/
struct DynamicObjectData {
std::string modelName;
float mass; // Kg
float turnMass; // Kg m^3
float airRes; // fraction

View File

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

View File

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

View File

@ -12,8 +12,9 @@
#include <data/WeaponData.hpp>
#include <objects/VehicleInfo.hpp>
void GenericDATLoader::loadDynamicObjects(const std::string& name,
DynamicObjectDataPtrs& data) {
void GenericDATLoader::loadDynamicObjects(
const std::string& name,
std::unordered_map<std::string, DynamicObjectData>& data) {
std::ifstream dfile(name.c_str());
if (dfile.is_open()) {
@ -24,39 +25,40 @@ void GenericDATLoader::loadDynamicObjects(const std::string& name,
if (lineBuff.at(0) == '*') continue;
std::stringstream ss(lineBuff);
auto dyndata = std::make_shared<DynamicObjectData>();
DynamicObjectData dyndata{};
std::string modelName;
ss >> dyndata->modelName;
auto cpos = dyndata->modelName.find(',');
if (cpos != dyndata->modelName.npos) {
dyndata->modelName.erase(cpos);
ss >> modelName;
auto cpos = modelName.find(',');
if (cpos != modelName.npos) {
modelName.erase(cpos);
}
ss >> dyndata->mass;
ss >> dyndata.mass;
if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->turnMass;
ss >> dyndata.turnMass;
if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->airRes;
ss >> dyndata.airRes;
if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->elasticity;
ss >> dyndata.elasticity;
if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->buoyancy;
ss >> dyndata.buoyancy;
if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->uprootForce;
ss >> dyndata.uprootForce;
if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->collDamageMulti;
ss >> dyndata.collDamageMulti;
if (ss.peek() == ',') ss.ignore(1);
int tmp;
ss >> tmp;
dyndata->collDamageEffect = tmp;
dyndata.collDamageEffect = tmp;
if (ss.peek() == ',') ss.ignore(1);
ss >> tmp;
dyndata->collResponseFlags = tmp;
dyndata.collResponseFlags = tmp;
if (ss.peek() == ',') ss.ignore(1);
ss >> dyndata->cameraAvoid;
ss >> dyndata.cameraAvoid;
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 <memory>
#include <string>
#include <unordered_map>
#include <vector>
struct DynamicObjectData;
typedef std::shared_ptr<DynamicObjectData> DynamicObjectDataPtr;
typedef std::map<std::string, DynamicObjectDataPtr> DynamicObjectDataPtrs;
struct WeaponData;
struct VehicleInfo;
typedef std::shared_ptr<VehicleInfo> VehicleInfoPtr;
@ -17,8 +15,9 @@ typedef std::map<std::string, VehicleInfoPtr> VehicleInfoPtrs;
class GenericDATLoader {
public:
void loadDynamicObjects(const std::string& name,
DynamicObjectDataPtrs& data);
void loadDynamicObjects(
const std::string& name,
std::unordered_map<std::string, DynamicObjectData>& data);
void loadWeapons(const std::string& name,
std::vector<WeaponData>& weaponData);

View File

@ -22,7 +22,7 @@
InstanceObject::InstanceObject(GameWorld* engine, const glm::vec3& pos,
const glm::quat& rot, const glm::vec3& scale,
BaseModelInfo* modelinfo,
const std::shared_ptr<DynamicObjectData>& dyn)
DynamicObjectData* dyn)
: GameObject(engine, pos, rot, modelinfo)
, scale(scale)
, dynamics(dyn) {
@ -175,7 +175,7 @@ void InstanceObject::changeModel(BaseModelInfo* incoming, int atomicNumber) {
if (collision) {
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:
glm::vec3 scale;
std::unique_ptr<CollisionInstance> body;
std::shared_ptr<DynamicObjectData> dynamics;
DynamicObjectData* dynamics;
InstanceObject(GameWorld* engine, const glm::vec3& pos,
const glm::quat& rot, const glm::vec3& scale,
BaseModelInfo* modelinfo,
const std::shared_ptr<DynamicObjectData>& dyn);
DynamicObjectData* dyn);
~InstanceObject() 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) {
GenericDATLoader l;
DynamicObjectDataPtrs loaded;
std::unordered_map<std::string, DynamicObjectData> dynamicObjects;
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(loaded.find("lamppost1") != loaded.end());
BOOST_ASSERT(dynamicObjects.find("wastebin") != dynamicObjects.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->turnMass, 4000.0);
BOOST_CHECK_CLOSE(lamp->airRes, 0.99, 1.0);
BOOST_CHECK_CLOSE(lamp->elasticity, 0.05, 0.01);
BOOST_CHECK_EQUAL(lamp->buoyancy, 50.0);
BOOST_CHECK_EQUAL(lamp->uprootForce, 400);
BOOST_CHECK_EQUAL(lamp->collDamageMulti, 1.0);
BOOST_CHECK_EQUAL(lamp->collDamageEffect, 1);
BOOST_CHECK_EQUAL(lamp->collResponseFlags, 1);
BOOST_CHECK_EQUAL(lamp->cameraAvoid, false);
BOOST_CHECK_EQUAL(lamp.mass, 600.0);
BOOST_CHECK_EQUAL(lamp.turnMass, 4000.0);
BOOST_CHECK_CLOSE(lamp.airRes, 0.99, 1.0);
BOOST_CHECK_CLOSE(lamp.elasticity, 0.05, 0.01);
BOOST_CHECK_EQUAL(lamp.buoyancy, 50.0);
BOOST_CHECK_EQUAL(lamp.uprootForce, 400);
BOOST_CHECK_EQUAL(lamp.collDamageMulti, 1.0);
BOOST_CHECK_EQUAL(lamp.collDamageEffect, 1);
BOOST_CHECK_EQUAL(lamp.collResponseFlags, 1);
BOOST_CHECK_EQUAL(lamp.cameraAvoid, false);
}
BOOST_AUTO_TEST_CASE(test_handling_data_loader) {