Add save/restore to scene
This commit is contained in:
parent
29b750cf9a
commit
93d47d9883
94
sdlpp.hpp
94
sdlpp.hpp
@ -403,13 +403,21 @@ public:
|
|||||||
virtual void custom_move( int ticks ) = 0;
|
virtual void custom_move( int ticks ) = 0;
|
||||||
virtual void updateSizeAndPosition() = 0;
|
virtual void updateSizeAndPosition() = 0;
|
||||||
virtual SDL_Rect getRect() = 0;
|
virtual SDL_Rect getRect() = 0;
|
||||||
void setPermanent( bool perm ) {
|
void setPermanent( bool perm = true ) {
|
||||||
permanent = perm;
|
permanent = perm;
|
||||||
|
setStatic(perm);
|
||||||
}
|
}
|
||||||
bool getPermanent() const {
|
bool getPermanent() const {
|
||||||
return permanent;
|
return permanent;
|
||||||
}
|
}
|
||||||
virtual void centerX() = 0;
|
virtual void centerX() = 0;
|
||||||
|
virtual std::shared_ptr<RenderObject> copySelf() = 0;
|
||||||
|
bool isStatic() {
|
||||||
|
return is_static;
|
||||||
|
}
|
||||||
|
void setStatic(bool stat = true) {
|
||||||
|
is_static = stat;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector< std::shared_ptr< CollisionPolygon > > collisions;
|
std::vector< std::shared_ptr< CollisionPolygon > > collisions;
|
||||||
@ -425,6 +433,7 @@ protected:
|
|||||||
std::tuple< int, int, int, int > colider_color = { 0x00, 0xFF, 0xFF, 0xFF };
|
std::tuple< int, int, int, int > colider_color = { 0x00, 0xFF, 0xFF, 0xFF };
|
||||||
uint64_t scene_id;
|
uint64_t scene_id;
|
||||||
bool permanent = false;
|
bool permanent = false;
|
||||||
|
bool is_static = true;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setSceneID( int id ) {
|
void setSceneID( int id ) {
|
||||||
@ -442,13 +451,13 @@ public:
|
|||||||
}
|
}
|
||||||
void addObject( const std::shared_ptr< RenderObject > &obj ) {
|
void addObject( const std::shared_ptr< RenderObject > &obj ) {
|
||||||
render_mutex.lock();
|
render_mutex.lock();
|
||||||
renderObjects.push_back( obj );
|
render_objects.push_back( obj );
|
||||||
obj->setSceneID( ++max_object_id );
|
obj->setSceneID( ++max_object_id );
|
||||||
if ( obj->hasCollisions() ) {
|
if ( obj->hasCollisions() ) {
|
||||||
collisionObjects.push_back( obj );
|
collision_objects.push_back( obj );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( renderObjects.size() == 1 ) {
|
if ( render_objects.size() == 1 ) {
|
||||||
leftmost_obj = obj;
|
leftmost_obj = obj;
|
||||||
rightmost_obj = obj;
|
rightmost_obj = obj;
|
||||||
} else {
|
} else {
|
||||||
@ -463,17 +472,18 @@ public:
|
|||||||
}
|
}
|
||||||
render_mutex.unlock();
|
render_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
//TODO addCollision
|
||||||
std::shared_ptr< RenderObject > getObject( int index ) {
|
std::shared_ptr< RenderObject > getObject( int index ) {
|
||||||
return renderObjects[index];
|
return render_objects[index];
|
||||||
}
|
}
|
||||||
std::vector< std::shared_ptr< RenderObject > > getObjects() {
|
std::vector< std::shared_ptr< RenderObject > > getObjects() {
|
||||||
return renderObjects;
|
return render_objects;
|
||||||
}
|
}
|
||||||
void movement() {
|
void movement() {
|
||||||
checkKilled();
|
checkKilled();
|
||||||
render_mutex.lock();
|
render_mutex.lock();
|
||||||
int now_ticks = SDL_GetTicks();
|
int now_ticks = SDL_GetTicks();
|
||||||
for ( const auto &x : renderObjects ) {
|
for ( const auto &x : render_objects ) {
|
||||||
x->move( now_ticks - prev_ticks );
|
x->move( now_ticks - prev_ticks );
|
||||||
}
|
}
|
||||||
prev_ticks = now_ticks;
|
prev_ticks = now_ticks;
|
||||||
@ -484,7 +494,7 @@ public:
|
|||||||
if ( r.getHidden() )
|
if ( r.getHidden() )
|
||||||
return {};
|
return {};
|
||||||
std::vector< std::shared_ptr< RenderObject > > ret{};
|
std::vector< std::shared_ptr< RenderObject > > ret{};
|
||||||
for ( const auto &x : collisionObjects ) {
|
for ( const auto &x : collision_objects ) {
|
||||||
if ( x->colidesWith( r ) ) {
|
if ( x->colidesWith( r ) ) {
|
||||||
ret.push_back( x );
|
ret.push_back( x );
|
||||||
}
|
}
|
||||||
@ -497,7 +507,7 @@ public:
|
|||||||
if ( r.getHidden() )
|
if ( r.getHidden() )
|
||||||
return {};
|
return {};
|
||||||
std::vector< std::shared_ptr< RenderObject > > ret{};
|
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() &&
|
if ( objectIDs.find( x->getId() ) != objectIDs.end() &&
|
||||||
x->colidesWith( r ) ) {
|
x->colidesWith( r ) ) {
|
||||||
ret.push_back( x );
|
ret.push_back( x );
|
||||||
@ -513,7 +523,7 @@ public:
|
|||||||
if ( background && background->getTexturePtr() )
|
if ( background && background->getTexturePtr() )
|
||||||
SDL_RenderCopy( renderer->getRendererPtr(),
|
SDL_RenderCopy( renderer->getRendererPtr(),
|
||||||
background->getTexturePtr(), NULL, NULL );
|
background->getTexturePtr(), NULL, NULL );
|
||||||
for ( const auto &x : renderObjects ) {
|
for ( const auto &x : render_objects ) {
|
||||||
x->render();
|
x->render();
|
||||||
}
|
}
|
||||||
render_mutex.unlock();
|
render_mutex.unlock();
|
||||||
@ -530,7 +540,7 @@ public:
|
|||||||
void updateSizeAndPosition() {
|
void updateSizeAndPosition() {
|
||||||
checkKilled();
|
checkKilled();
|
||||||
render_mutex.lock();
|
render_mutex.lock();
|
||||||
for ( auto &x : renderObjects ) {
|
for ( auto &x : render_objects ) {
|
||||||
x->updateSizeAndPosition();
|
x->updateSizeAndPosition();
|
||||||
for ( auto &col : x->getCollisions() ) {
|
for ( auto &col : x->getCollisions() ) {
|
||||||
col->updateCollision( x->collisionPushX(), x->collisionPushY(),
|
col->updateCollision( x->collisionPushX(), x->collisionPushY(),
|
||||||
@ -543,7 +553,7 @@ public:
|
|||||||
void moveEverything( double x, double y ) {
|
void moveEverything( double x, double y ) {
|
||||||
checkKilled();
|
checkKilled();
|
||||||
render_mutex.lock();
|
render_mutex.lock();
|
||||||
for ( auto &obj : renderObjects ) {
|
for ( auto &obj : render_objects ) {
|
||||||
if ( obj->getPermanent() )
|
if ( obj->getPermanent() )
|
||||||
continue;
|
continue;
|
||||||
auto curPos = obj->getDoubleRect();
|
auto curPos = obj->getDoubleRect();
|
||||||
@ -569,34 +579,72 @@ public:
|
|||||||
Renderer &getRenderer() {
|
Renderer &getRenderer() {
|
||||||
return *renderer;
|
return *renderer;
|
||||||
}
|
}
|
||||||
|
std::shared_ptr<Renderer> getRendererShared() {
|
||||||
|
return renderer;
|
||||||
|
}
|
||||||
void setPrevTicks( int ticks ) {
|
void setPrevTicks( int ticks ) {
|
||||||
prev_ticks = 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:
|
private:
|
||||||
void checkKilled() {
|
void checkKilled() {
|
||||||
render_mutex.lock();
|
render_mutex.lock();
|
||||||
std::vector< int > killed;
|
std::vector< int > killed;
|
||||||
std::vector< int > killed_collisions;
|
std::vector< int > killed_collisions;
|
||||||
for ( long unsigned int i = 0; i < renderObjects.size(); i++ ) {
|
for ( long unsigned int i = 0; i < render_objects.size(); i++ ) {
|
||||||
if ( renderObjects[i]->getKilled() )
|
if ( render_objects[i]->getKilled() )
|
||||||
killed.push_back( i );
|
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 );
|
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.begin(), killed.end() );
|
||||||
std::reverse( killed_collisions.begin(), killed_collisions.end() );
|
std::reverse( killed_collisions.begin(), killed_collisions.end() );
|
||||||
for ( auto &index : killed ) {
|
for ( auto &index : killed ) {
|
||||||
renderObjects.erase( renderObjects.begin() + index );
|
render_objects.erase( render_objects.begin() + index );
|
||||||
}
|
}
|
||||||
for ( auto &index : killed_collisions ) {
|
for ( auto &index : killed_collisions ) {
|
||||||
collisionObjects.erase( collisionObjects.begin() + index );
|
collision_objects.erase( collision_objects.begin() + index );
|
||||||
}
|
}
|
||||||
render_mutex.unlock();
|
render_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector< std::shared_ptr< RenderObject > > renderObjects;
|
std::vector< std::shared_ptr< RenderObject > > render_objects;
|
||||||
std::vector< std::shared_ptr< RenderObject > > collisionObjects;
|
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< Renderer > renderer;
|
||||||
std::shared_ptr< Texture > background;
|
std::shared_ptr< Texture > background;
|
||||||
int prev_ticks = 0;
|
int prev_ticks = 0;
|
||||||
@ -930,6 +978,10 @@ public:
|
|||||||
centerx = true;
|
centerx = true;
|
||||||
updateSizeAndPosition();
|
updateSizeAndPosition();
|
||||||
}
|
}
|
||||||
|
virtual std::shared_ptr<RenderObject> copySelf() override {
|
||||||
|
// TODO ACTUALLY copy, don't just copy pointers to textures and whatnot, create new textures!!!
|
||||||
|
return std::make_shared<RectangleRender>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void updateXY() {
|
void updateXY() {
|
||||||
@ -1005,6 +1057,10 @@ public:
|
|||||||
RectangleRender::updateSizeAndPosition();
|
RectangleRender::updateSizeAndPosition();
|
||||||
updateDstRect();
|
updateDstRect();
|
||||||
}
|
}
|
||||||
|
virtual std::shared_ptr<RenderObject> copySelf() override {
|
||||||
|
// TODO ACTUALLY copy, don't just copy pointers to textures and whatnot, create new textures!!!
|
||||||
|
return std::make_shared<TextRenderer>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updateDstRect() {
|
void updateDstRect() {
|
||||||
|
Loading…
Reference in New Issue
Block a user