From 7cb3f95e44e9e1e7b4473f0227df6d328874ca36 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 7 Aug 2021 21:38:00 +0200 Subject: [PATCH] Mario: loop through all moving objects in doInput --- mario/main.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mario/main.cpp b/mario/main.cpp index 1369aba..9c4b3c2 100644 --- a/mario/main.cpp +++ b/mario/main.cpp @@ -135,17 +135,18 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) { FPSmanager gFPS; SDL_initFramerate( &gFPS ); SDL_setFramerate( &gFPS, 200 ); - while ( true ) { + while ( !quit ) { SDL_framerateDelay( &gFPS ); pollEvents( *scene ); std::lock_guard< std::mutex > lock( render_mutex ); scene->updateScene(); auto prev_coin_count = coin_count; - // TODO visit all moving objects - auto visitor = - getVisitor( *mario, *scene, quit, coin_count, moving_objects ); - scene->visitCollisions( *mario, *visitor ); - mario->handleVisitor( *visitor ); + for(size_t i = 0; i < moving_objects.size(); i++) { + auto visitor = + getVisitor( *moving_objects[i], *scene, quit, coin_count, moving_objects ); + scene->visitCollisions( *moving_objects[i], *visitor ); + moving_objects[i]->handleVisitor( *visitor ); + } if ( coin_count != prev_coin_count ) { coins->changeText( std::to_string( coin_count ) + " COINS" ); @@ -247,9 +248,9 @@ int main() { auto base = SDL_GetTicks(); int frames = 0; std::thread inputThread( doInput, scene ); - inputThread.detach(); scene->moveEverything( -mario->getDoubleRect().first.getX() + 0.2, 0 ); update = true; + moving_objects.push_back(mario); while ( !quit ) { SDL_PumpEvents(); SDL_framerateDelay( &gFPS ); @@ -272,6 +273,7 @@ int main() { base = SDL_GetTicks(); } } + inputThread.join(); return 0; }