Sorry for double-posting, I'm having a problem understanding the solution to this.
When zooming in and out of a grid, I see that lines would disappear, as seen in the video below:
https://streamable.com/e0bd53I googled the problem and found in an old thread here that the problem is setting the view to non-integer values. Makes sense. But how can I set the view to integer values in this case? All I do is call the 'zoom' function, I'm not moving the view or anything, that I can round my values.
Here is the "zoom event" in the event loop:
case sf::Event::MouseWheelScrolled:
if (evnt.mouseWheelScroll.delta <= -1) // Scroll down - zoom-out
zoom = std::min(2.0, zoom + 0.1); // By using 'min' with '2', we set it as a lower limit.
else if (evnt.mouseWheelScroll.delta >= 1) // Scroll up - zoom-in
zoom = std::max(0.5, zoom - 0.1); // By using 'max' with '0.5', we set it as an upper limit.
view.setSize(window.getDefaultView().getSize()); // Reset the size
view.zoom(zoom);
window.setView(view);
break;
And just for reference, here is the draw function:
void drawGrid(sf::RenderWindow& window, std::unordered_set<sf::Vector2i, pair_hash, pair_equal>& grid){
sf::RectangleShape cell(sf::Vector2f(CELL_SIZE, CELL_SIZE));
cell.setOutlineColor(sf::Color(200, 200, 200)); // Beige
cell.setOutlineThickness(1.25);
for (int i = 0; i < GRID_HEIGHT / CELL_SIZE; i++){
for (int j = 0; j < GRID_WIDTH / CELL_SIZE; j++){
if (grid.count({j,i})) cell.setFillColor(LIVE_CELL_COLOR);
else cell.setFillColor(DEAD_CELL_COLOR);
// Set cell position based on its grid coordinates.
cell.setPosition(j * CELL_SIZE, i * CELL_SIZE);
window.draw(cell);
}
}
}
Thanks in advance.