The problem is that you need to reset the view parameters every time you resize the window.
#include <SFML/Graphics.hpp>
#include <algorithm>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Chess");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::Resized)
{
sf::View view = window.getView();
view.setCenter(event.size.width / 2, event.size.height / 2);
view.setSize(event.size.width, event.size.height);
window.setView(view);
}
}
window.clear(sf::Color::Blue);
//Relevant Code:
sf::Vector2u wsize = window.getSize();
int length = std::min(wsize.x, wsize.y) / 8;
int offsetx = (wsize.x - (length * 8)) / 2;
int offsety = (wsize.y - (length * 8)) / 2;
bool white = true;
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
sf::RectangleShape box(sf::Vector2f(length, length));
box.setFillColor(white ? sf::Color::White : sf::Color::Black);
box.setPosition(offsetx + col * length, offsety + row * length);
window.draw(box);
white = !white;
}
white = !white;
}
//Relevant Code End
window.display();
}
}
Whenever the window is resized, the view size and center position will not change, contrary to what newcomers might expect. You need to reset them to what you expect them to be, in your case, fitting to the new window dimensions. You might wonder why it doesn't do so automatically and the answer is that if you keep drawing relative to the original window size, everything will be scaled proportionally. Something that was in the center of the screen previously will still be in the center of the screen after the resize. Since it wouldn't look so nice if the chess board wasn't a square any more, you need to manually resize the view and re-center it again every time the window changes its size.
I also cleaned up and rewrote a bit of your code because I thought it was overly complicated and hard to follow in its initial state
.