From 93d47d988333faf0d4a7e8ae1ca913def23f827a Mon Sep 17 00:00:00 2001 From: zvon Date: Mon, 24 Aug 2020 18:21:03 +0200 Subject: [PATCH] Add save/restore to scene --- sdlpp.hpp | 94 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 19 deletions(-) diff --git a/sdlpp.hpp b/sdlpp.hpp index aee0531..06b432f 100644 --- a/sdlpp.hpp +++ b/sdlpp.hpp @@ -403,13 +403,21 @@ public: virtual void custom_move( int ticks ) = 0; virtual void updateSizeAndPosition() = 0; virtual SDL_Rect getRect() = 0; - void setPermanent( bool perm ) { + void setPermanent( bool perm = true ) { permanent = perm; + setStatic(perm); } bool getPermanent() const { return permanent; } virtual void centerX() = 0; + virtual std::shared_ptr copySelf() = 0; + bool isStatic() { + return is_static; + } + void setStatic(bool stat = true) { + is_static = stat; + } protected: std::vector< std::shared_ptr< CollisionPolygon > > collisions; @@ -425,6 +433,7 @@ protected: std::tuple< int, int, int, int > colider_color = { 0x00, 0xFF, 0xFF, 0xFF }; uint64_t scene_id; bool permanent = false; + bool is_static = true; private: void setSceneID( int id ) { @@ -442,13 +451,13 @@ public: } void addObject( const std::shared_ptr< RenderObject > &obj ) { render_mutex.lock(); - renderObjects.push_back( obj ); + render_objects.push_back( obj ); obj->setSceneID( ++max_object_id ); if ( obj->hasCollisions() ) { - collisionObjects.push_back( obj ); + collision_objects.push_back( obj ); } - if ( renderObjects.size() == 1 ) { + if ( render_objects.size() == 1 ) { leftmost_obj = obj; rightmost_obj = obj; } else { @@ -463,17 +472,18 @@ public: } render_mutex.unlock(); } + //TODO addCollision std::shared_ptr< RenderObject > getObject( int index ) { - return renderObjects[index]; + return render_objects[index]; } std::vector< std::shared_ptr< RenderObject > > getObjects() { - return renderObjects; + return render_objects; } void movement() { checkKilled(); render_mutex.lock(); int now_ticks = SDL_GetTicks(); - for ( const auto &x : renderObjects ) { + for ( const auto &x : render_objects ) { x->move( now_ticks - prev_ticks ); } prev_ticks = now_ticks; @@ -484,7 +494,7 @@ public: if ( r.getHidden() ) return {}; std::vector< std::shared_ptr< RenderObject > > ret{}; - for ( const auto &x : collisionObjects ) { + for ( const auto &x : collision_objects ) { if ( x->colidesWith( r ) ) { ret.push_back( x ); } @@ -497,7 +507,7 @@ public: if ( r.getHidden() ) return {}; std::vector< std::shared_ptr< RenderObject > > ret{}; - for ( const auto &x : collisionObjects ) { + for ( const auto &x : collision_objects ) { if ( objectIDs.find( x->getId() ) != objectIDs.end() && x->colidesWith( r ) ) { ret.push_back( x ); @@ -513,7 +523,7 @@ public: if ( background && background->getTexturePtr() ) SDL_RenderCopy( renderer->getRendererPtr(), background->getTexturePtr(), NULL, NULL ); - for ( const auto &x : renderObjects ) { + for ( const auto &x : render_objects ) { x->render(); } render_mutex.unlock(); @@ -530,7 +540,7 @@ public: void updateSizeAndPosition() { checkKilled(); render_mutex.lock(); - for ( auto &x : renderObjects ) { + for ( auto &x : render_objects ) { x->updateSizeAndPosition(); for ( auto &col : x->getCollisions() ) { col->updateCollision( x->collisionPushX(), x->collisionPushY(), @@ -543,7 +553,7 @@ public: void moveEverything( double x, double y ) { checkKilled(); render_mutex.lock(); - for ( auto &obj : renderObjects ) { + for ( auto &obj : render_objects ) { if ( obj->getPermanent() ) continue; auto curPos = obj->getDoubleRect(); @@ -569,34 +579,72 @@ public: Renderer &getRenderer() { return *renderer; } + std::shared_ptr getRendererShared() { + return renderer; + } void setPrevTicks( int ticks ) { prev_ticks = ticks; } + void saveScene() { + saved_render_objects.clear(); + saved_collision_objects.clear(); + for(auto &obj : render_objects) { + if(!obj->isStatic()) + saved_render_objects.push_back(obj->copySelf()); + else + saved_render_objects.push_back(obj); + } + for(auto &obj : collision_objects) { + if(!obj->isStatic()) + saved_collision_objects.push_back(obj->copySelf()); + else + saved_collision_objects.push_back(obj); + } + } + void resetScene() { + render_objects.clear(); + collision_objects.clear(); + for(auto &obj : saved_render_objects) { + if(!obj->isStatic()) + render_objects.push_back(obj->copySelf()); + else + render_objects.push_back(obj); + } + for(auto &obj : saved_collision_objects) { + if(!obj->isStatic()) + collision_objects.push_back(obj->copySelf()); + else + collision_objects.push_back(obj); + } + } private: void checkKilled() { render_mutex.lock(); std::vector< int > killed; std::vector< int > killed_collisions; - for ( long unsigned int i = 0; i < renderObjects.size(); i++ ) { - if ( renderObjects[i]->getKilled() ) + for ( long unsigned int i = 0; i < render_objects.size(); i++ ) { + if ( render_objects[i]->getKilled() ) killed.push_back( i ); - if ( i < collisionObjects.size() && collisionObjects[i]->getKilled() ) + if ( i < collision_objects.size() && collision_objects[i]->getKilled() ) killed_collisions.push_back( i ); } + // reverse so we don't screw up indexing while going thorugh the kill indices std::reverse( killed.begin(), killed.end() ); std::reverse( killed_collisions.begin(), killed_collisions.end() ); for ( auto &index : killed ) { - renderObjects.erase( renderObjects.begin() + index ); + render_objects.erase( render_objects.begin() + index ); } for ( auto &index : killed_collisions ) { - collisionObjects.erase( collisionObjects.begin() + index ); + collision_objects.erase( collision_objects.begin() + index ); } render_mutex.unlock(); } - std::vector< std::shared_ptr< RenderObject > > renderObjects; - std::vector< std::shared_ptr< RenderObject > > collisionObjects; + std::vector< std::shared_ptr< RenderObject > > render_objects; + std::vector< std::shared_ptr< RenderObject > > collision_objects; + std::vector< std::shared_ptr< RenderObject > > saved_render_objects; + std::vector< std::shared_ptr< RenderObject > > saved_collision_objects; std::shared_ptr< Renderer > renderer; std::shared_ptr< Texture > background; int prev_ticks = 0; @@ -930,6 +978,10 @@ public: centerx = true; updateSizeAndPosition(); } + virtual std::shared_ptr copySelf() override { + // TODO ACTUALLY copy, don't just copy pointers to textures and whatnot, create new textures!!! + return std::make_shared(*this); + } protected: void updateXY() { @@ -1005,6 +1057,10 @@ public: RectangleRender::updateSizeAndPosition(); updateDstRect(); } + virtual std::shared_ptr copySelf() override { + // TODO ACTUALLY copy, don't just copy pointers to textures and whatnot, create new textures!!! + return std::make_shared(*this); + } private: void updateDstRect() {