From 2b35b393203403704b740f7627af701968c2dfcc Mon Sep 17 00:00:00 2001 From: cflip Date: Tue, 29 Mar 2022 16:29:56 -0600 Subject: Fix sprite sorting when rendering the level This fixes a misuse of std::find_if and vector iterators. Previously, the first vehicle in the currently drawing tile would be drawn, and then every other vehicle would be drawn regardless of position. --- src/level.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/level.cpp b/src/level.cpp index e88191a..e09f74a 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -76,13 +76,14 @@ void Level::draw(Bitmap& bitmap, int xo, int yo) for (int y = 0; y < m_height; ++y) { for (int x = 0; x < m_width; ++x) { - auto vehiclesInTile = std::find_if(m_vehicles.begin(), m_vehicles.end(), [x, y](const auto& vehicle) { + auto isInTile = [x, y](const auto& vehicle) { return vehicle.getSpritePosition().x == x && vehicle.getSpritePosition().y == y; - }); + }; + auto vehiclesInTile = std::find_if(m_vehicles.begin(), m_vehicles.end(), isInTile); while (vehiclesInTile != m_vehicles.end()) { vehiclesInTile->draw(bitmap, xo, yo); - ++vehiclesInTile; + vehiclesInTile = std::find_if(++vehiclesInTile, m_vehicles.end(), isInTile); } auto tile = get(x, y); -- cgit v1.2.3