diff --git a/mario/blocks.cpp b/mario/blocks.cpp index 0157657..3aef38a 100644 --- a/mario/blocks.cpp +++ b/mario/blocks.cpp @@ -656,3 +656,12 @@ void MarioBlock::setBaseRect(SDL_Rect rect) { _base_src = rect; setType(getType()); } +void MarioBlock::checkVisibility(double rightmost_x) { + // we assume that object's X > 0 as otherwise it would be destroyed + if(!getHidden() && getAbsolutePos().getX() < rightmost_x) { + _was_visible = true; + } +} +bool MarioBlock::wasVisible() const { + return _was_visible; +} diff --git a/mario/blocks.hpp b/mario/blocks.hpp index c4b1d8b..0decef8 100644 --- a/mario/blocks.hpp +++ b/mario/blocks.hpp @@ -42,6 +42,8 @@ public: bool isBouncing() const; bool isTraveling() const; void setBaseRect(SDL_Rect rect); + void checkVisibility(double rightmost_x); + bool wasVisible() const; protected: double bounce_speed = 0.5; @@ -83,6 +85,7 @@ private: int _base_gravity_ticks = 1000 / 60; int _ticks_till_gravity = 0; double _gravity_acceleration = 1.0/(64.0/7.0); + bool _was_visible = false; }; extern const std::vector< uint64_t > possibleBlocks; diff --git a/mario/main.cpp b/mario/main.cpp index 6b044b8..d37bae8 100644 --- a/mario/main.cpp +++ b/mario/main.cpp @@ -142,8 +142,12 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) { std::lock_guard< std::mutex > lock( render_mutex ); scene->updateScene(); auto prev_coin_count = coin_count; - // TODO only start doing visits once the object has been on screen at least once + auto rightmost_x = renderer->getDoubleDimensions().getX(); for(size_t i = 0; i < moving_objects.size(); i++) { + moving_objects[i]->checkVisibility(rightmost_x); + if(!moving_objects[i]->wasVisible()) { + continue; + } auto visitor = getVisitor( *moving_objects[i], *scene, quit, coin_count, moving_objects ); scene->visitCollisions( *moving_objects[i], *visitor );