diff --git a/framework2/include/renderwure/engine/GTAEngine.hpp b/framework2/include/renderwure/engine/GTAEngine.hpp index 4cb40db8..d06df3d5 100644 --- a/framework2/include/renderwure/engine/GTAEngine.hpp +++ b/framework2/include/renderwure/engine/GTAEngine.hpp @@ -98,6 +98,11 @@ public: */ GTACharacter* createPedestrian(const uint16_t id, const glm::vec3& pos, const glm::quat& rot = glm::quat()); + /** + * Destroys an existing Object + */ + void destroyObject(GTAObject* object); + /** * Game Clock */ @@ -157,7 +162,7 @@ public: * Pedestrians and PCs. */ std::vector pedestrians; - + /** * AI Graph */ diff --git a/framework2/include/renderwure/engine/GTAObject.hpp b/framework2/include/renderwure/engine/GTAObject.hpp index 371b0755..6337ab49 100644 --- a/framework2/include/renderwure/engine/GTAObject.hpp +++ b/framework2/include/renderwure/engine/GTAObject.hpp @@ -36,6 +36,8 @@ struct GTAObject GTAObject(GTAEngine* engine, const glm::vec3& pos, const glm::quat& rot, Model* model) : position(pos), rotation(rot), model(model), engine(engine), animator(nullptr), mHealth(0.f) {} + + virtual ~GTAObject() {}; enum Type { diff --git a/framework2/src/engine/GTAEngine.cpp b/framework2/src/engine/GTAEngine.cpp index 42bf922d..9c8138f7 100644 --- a/framework2/src/engine/GTAEngine.cpp +++ b/framework2/src/engine/GTAEngine.cpp @@ -285,3 +285,41 @@ GTACharacter* GTAEngine::createPedestrian(const uint16_t id, const glm::vec3 &po } return nullptr; } + +void GTAEngine::destroyObject(GTAObject* object) +{ + if(object->type() == GTAObject::Character) + { + for(auto it = pedestrians.begin(); it != pedestrians.end(); ) { + if( *it == object ) { + pedestrians.erase(it++); + } + else { + ++it; + } + } + } + else if(object->type() == GTAObject::Vehicle) + { + for(auto it = vehicleInstances.begin(); it != vehicleInstances.end(); ) { + if( *it == object ) { + vehicleInstances.erase(it++); + } + else { + ++it; + } + } + } + else if(object->type() == GTAObject::Instance) + { + for(auto it = modelInstances.begin(); it != modelInstances.end(); ) { + if( it->second.get() == object ) { + modelInstances.erase(it++); + } + else { + ++it; + } + } + } + delete object; +} \ No newline at end of file diff --git a/tests/main.cpp b/tests/main.cpp index 1ba9240f..1c7af0b9 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,6 +1,7 @@ #define BOOST_TEST_MODULE gtfw #include #include +#include // Many tests require OpenGL be functional, seems like a reasonable solution. @@ -8,7 +9,7 @@ class GlobalFixture { public: sf::Window wnd; - + GlobalFixture() { wnd.create(sf::VideoMode(640, 360), "Testing"); } diff --git a/tests/test_world.cpp b/tests/test_world.cpp new file mode 100644 index 00000000..3f908901 --- /dev/null +++ b/tests/test_world.cpp @@ -0,0 +1,13 @@ +#include +#include + +BOOST_AUTO_TEST_SUITE(WorldTests) + +GTAEngine e(""); + +BOOST_AUTO_TEST_CASE(world_object_destroy) +{ + +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/viewer/main.cpp b/viewer/main.cpp index b61f30c0..c8779035 100644 --- a/viewer/main.cpp +++ b/viewer/main.cpp @@ -387,14 +387,16 @@ void update(float dt) gta->renderer.camera.worldPos = plyPos; gta->renderer.camera.frustum.view = view; - - // TODO: move this inside the engine - for( size_t p = 0; p < gta->pedestrians.size(); ++p ) { + + // Update all objects. + for( size_t p = 0; p < gta->pedestrians.size(); ++p) { gta->pedestrians[p]->tick(dt); } for( size_t v = 0; v < gta->vehicleInstances.size(); ++v ) { gta->vehicleInstances[v]->tick(dt); } + + gta->dynamicsWorld->stepSimulation(dt, 2, dt); }