1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-03 17:19:46 +02:00

Use unique_ptr for GameWorld instance

This commit is contained in:
Daniel Evans 2016-10-20 00:49:15 +01:00
parent 81c27da97b
commit fb4d9ea8c3
6 changed files with 32 additions and 49 deletions

View File

@ -154,7 +154,7 @@ bool CollisionInstance::createPhysicsBody(GameObject* object,
void CollisionInstance::changeMass(float newMass) {
GameObject* object = static_cast<GameObject*>(m_body->getUserPointer());
auto dynamicsWorld = object->engine->dynamicsWorld;
auto& dynamicsWorld = object->engine->dynamicsWorld;
dynamicsWorld->removeRigidBody(m_body);
btVector3 inert;
m_body->getCollisionShape()->calculateLocalInertia(newMass, inert);

View File

@ -87,12 +87,15 @@ GameWorld::GameWorld(Logger* log, WorkContext* work, GameData* dat)
: logger(log), data(dat), randomEngine(rand()), _work(work), paused(false) {
data->engine = this;
collisionConfig = new btDefaultCollisionConfiguration;
collisionDispatcher = new WorldCollisionDispatcher(collisionConfig);
broadphase = new btDbvtBroadphase();
solver = new btSequentialImpulseConstraintSolver;
dynamicsWorld = new btDiscreteDynamicsWorld(collisionDispatcher, broadphase,
solver, collisionConfig);
collisionConfig = std::make_unique<btDefaultCollisionConfiguration>();
collisionDispatcher =
std::make_unique<WorldCollisionDispatcher>(collisionConfig.get());
broadphase = std::make_unique<btDbvtBroadphase>();
solver = std::make_unique<btSequentialImpulseConstraintSolver>();
dynamicsWorld = std::make_unique<btDiscreteDynamicsWorld>(
collisionDispatcher.get(), broadphase.get(), solver.get(),
collisionConfig.get());
dynamicsWorld->setGravity(btVector3(0.f, 0.f, -9.81f));
broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(
new btGhostPairCallback());
@ -104,14 +107,6 @@ GameWorld::~GameWorld() {
for (auto& p : allObjects) {
delete p;
}
delete dynamicsWorld;
delete solver;
delete broadphase;
delete collisionDispatcher;
delete collisionConfig;
/// @todo delete other things.
}
bool GameWorld::placeItems(const std::string& name) {

View File

@ -269,11 +269,11 @@ public:
/**
* Bullet
*/
btDefaultCollisionConfiguration* collisionConfig;
btCollisionDispatcher* collisionDispatcher;
btBroadphaseInterface* broadphase;
btSequentialImpulseConstraintSolver* solver;
btDiscreteDynamicsWorld* dynamicsWorld;
std::unique_ptr<btDefaultCollisionConfiguration> collisionConfig;
std::unique_ptr<btCollisionDispatcher> collisionDispatcher;
std::unique_ptr<btDbvtBroadphase> broadphase;
std::unique_ptr<btSequentialImpulseConstraintSolver> solver;
std::unique_ptr<btDiscreteDynamicsWorld> dynamicsWorld;
/**
* @brief physicsNearCallback

View File

@ -100,7 +100,7 @@ VehicleObject::VehicleObject(GameWorld* engine, const glm::vec3& pos,
collision = new CollisionInstance;
if (collision->createPhysicsBody(this, modelinfo->name, nullptr,
&info->handling)) {
physRaycaster = new VehicleRaycaster(this, engine->dynamicsWorld);
physRaycaster = new VehicleRaycaster(this, engine->dynamicsWorld.get());
btRaycastVehicle::btVehicleTuning tuning;
float travel = fabs(info->handling.suspensionUpperLimit -

View File

@ -103,31 +103,23 @@ RWGame::~RWGame() {
log.info("Game", "Cleaning up scripts");
delete script;
log.info("Game", "Cleaning up world");
delete world;
}
void RWGame::newGame() {
if (world != nullptr) {
log.error("Game", "Cannot start a new game: game is already running.");
return;
}
// Get a fresh state
state = GameState();
world = new GameWorld(&log, &work, &data);
// Destroy the current world and start over
world = std::make_unique<GameWorld>(&log, &work, &data);
world->dynamicsWorld->setDebugDrawer(&debug);
// Associate the new world with the new state and vice versa
state.world = world;
state.world = world.get();
world->state = &state;
for (std::map<std::string, std::string>::iterator it =
world->data->iplLocations.begin();
it != world->data->iplLocations.end(); ++it) {
world->data->loadZone(it->second);
world->placeItems(it->second);
for (auto ipl : world->data->iplLocations) {
world->data->loadZone(ipl.second);
world->placeItems(ipl.second);
}
}
@ -136,9 +128,7 @@ void RWGame::saveGame(const std::string& savename) {
}
void RWGame::loadGame(const std::string& savename) {
delete world;
delete state.script;
world = nullptr;
log.info("Game", "Loading game " + savename);
@ -152,7 +142,7 @@ void RWGame::loadGame(const std::string& savename) {
}
void RWGame::startScript(const std::string& name) {
SCMFile* f = world->data->loadSCM(name);
SCMFile* f = data.loadSCM(name);
if (f) {
if (script) delete script;
@ -480,7 +470,7 @@ int RWGame::run() {
void RWGame::tick(float dt) {
// Process the Engine's background work.
world->_work->update();
work.update();
State* currState = StateManager::get().states.back().get();
@ -625,7 +615,7 @@ void RWGame::render(float alpha, float time) {
renderer.getRenderer()->pushDebugGroup("World");
RW_PROFILE_BEGIN("world");
renderer.renderWorld(world, viewCam, alpha);
renderer.renderWorld(world.get(), viewCam, alpha);
RW_PROFILE_END();
renderer.getRenderer()->popDebugGroup();
@ -636,10 +626,8 @@ void RWGame::render(float alpha, float time) {
renderDebugStats(time);
break;
case DebugViewMode::Physics:
if (world) {
world->dynamicsWorld->debugDrawWorld();
debug.flush(&renderer);
}
world->dynamicsWorld->debugDrawWorld();
debug.flush(&renderer);
break;
case DebugViewMode::Navigation:
renderDebugPaths(time);
@ -652,7 +640,7 @@ void RWGame::render(float alpha, float time) {
}
RW_PROFILE_END();
drawOnScreenText(world, &renderer);
drawOnScreenText(world.get(), &renderer);
}
void RWGame::renderDebugStats(float time) {

View File

@ -21,7 +21,7 @@ class RWGame : public GameBase {
DebugDraw debug;
GameState state;
GameWorld* world = nullptr;
std::unique_ptr<GameWorld> world;
ScriptMachine* script = nullptr;
std::chrono::steady_clock clock;
std::chrono::steady_clock::time_point last_clock_time;
@ -60,8 +60,8 @@ public:
return &state;
}
GameWorld* getWorld() const {
return world;
GameWorld* getWorld() {
return world.get();
}
const GameData& getGameData() const {