SDLPP: Add scene pause function

This commit is contained in:
zv0n 2022-09-24 22:51:32 +02:00
parent aa5043a811
commit 9e4cc97f79
2 changed files with 15 additions and 0 deletions

View File

@ -90,6 +90,9 @@ Scene::getObjects( const std::unordered_set< int > &objectIDs ) {
return ret; return ret;
} }
void Scene::updateScene() { void Scene::updateScene() {
if(paused) {
return;
}
// check for objects that should be removed // check for objects that should be removed
checkKilled(); checkKilled();
std::lock_guard< std::mutex > lock( render_mutex ); std::lock_guard< std::mutex > lock( render_mutex );
@ -102,6 +105,14 @@ void Scene::updateScene() {
} }
prev_ticks = now_ticks; prev_ticks = now_ticks;
} }
void Scene::pauseScene() {
paused = true;
diff_ticks = SDL_GetTicks() - prev_ticks;
}
void Scene::unpauseScene() {
paused = false;
prev_ticks = SDL_GetTicks() - diff_ticks;
}
std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > > std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > >
Scene::getCollisions( RenderObject &r ) { Scene::getCollisions( RenderObject &r ) {
if ( r.getHidden() ) if ( r.getHidden() )

View File

@ -28,6 +28,8 @@ public:
std::vector< std::shared_ptr< RenderObject > > std::vector< std::shared_ptr< RenderObject > >
getObjects( const std::unordered_set< int > &objectIDs ); getObjects( const std::unordered_set< int > &objectIDs );
void updateScene(); void updateScene();
void pauseScene();
void unpauseScene();
std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > > std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > >
getCollisions( RenderObject &r ); getCollisions( RenderObject &r );
void visitCollisions( RenderObject &r, Visitor &v ); void visitCollisions( RenderObject &r, Visitor &v );
@ -63,12 +65,14 @@ private:
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;
int diff_ticks = 0;
std::shared_ptr< RenderObject > leftmost_obj; std::shared_ptr< RenderObject > leftmost_obj;
std::shared_ptr< RenderObject > rightmost_obj; std::shared_ptr< RenderObject > rightmost_obj;
uint64_t max_object_id = 0; uint64_t max_object_id = 0;
std::mutex render_mutex; std::mutex render_mutex;
std::unordered_set<uint64_t> background_ids; std::unordered_set<uint64_t> background_ids;
uint64_t first_non_background_index = 1; uint64_t first_non_background_index = 1;
bool paused = false;
}; };
} // end of namespace SDLPP } // end of namespace SDLPP
#endif #endif