Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Resizing Window, but contens don't update  (Read 5002 times)

0 Members and 2 Guests are viewing this topic.

zmertens

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Resizing Window, but contens don't update
« on: December 25, 2013, 07:37:56 am »
Hello, I just started using SFML not too long ago and I decided to try to make a checker game project. I'm also new to C++ as well (figured I should mention that as a disclaimer...).

This post is about an issue I have with resizing the window and the checkerboard not updating. The attatched screenshot ("Window_Resize_Test_1.png") shows how the checkerboard doesn't scale with the size of the window, but the checkerpieces do! In my code, I use a main game loop that does the event handling and a window.clear(), "draw stuff", and then window.display() cycle like in the tutorials. The checkerboard in the screenshot is made using the Shape class.

void Checkerboard :: drawGrid(sf::RenderWindow& window, int mouseOverX, int mouseOverY)
{
        int xOffset = 0, yOffset = 0;

        for(int i = 0; i < SQUARES_VERTICAL; ++i)
        {      
                for(int j = 0; j < SQUARES_HORIZONTAL; ++j)
                {
                        if((j % 2 == 0 && i % 2 == 0) || (j % 2 != 0 && i % 2 != 0)) // find the white squares
                                squareArray[i][j]->setFillColor(sf::Color::White);
                        else
                                squareArray[i][j]->setFillColor(sf::Color::Black);

                        squareArray[i][j]->setPosition(xOffset, yOffset); // set the position for drawing
                        squareArray[i][j]->setSize(sf::Vector2f(window.getSize().x / SQUARES_HORIZONTAL, window.getSize().y / SQUARES_VERTICAL));
                        window.draw(*squareArray[i][j]);

                        xOffset += window.getSize().x / SQUARES_HORIZONTAL;
                }

                yOffset += window.getSize().y / SQUARES_VERTICAL;
                xOffset = 0;
        }
}

It was my hope that passing the window reference in the draw cycle of the main game loop would keep the checkerboard size updated. What really confuses me is that the checkerpieces scale perfectly with the window, so I think I'm just overlooking something altogether (I can post the checkerpiece draw function but its functionally the same as drawGrid). I Googled the issue and saw this guy had a similiar issue http://chrisheydrick.com/2013/06/04/problems-re-sizing-sfml-renderwindow/. Needless to say I tried it and it didn't work out. Lastly, I should mention that I can initialize the window to any reasonable size when the RenderWindow object is constructed in the main function and the checkerboard looks fine. The issue is only when I resize the window.
The truth will set you free but first it will piss you off.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Resizing Window, but contens don't update
« Reply #1 on: December 25, 2013, 07:47:08 am »
Don't scale your position/size relative to the window. Instead scale them relative to a fixed size.

This will allow for when the window resizes that default view will scale/stretch the contents to fit the window.
« Last Edit: December 25, 2013, 07:50:52 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

zmertens

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Resizing Window, but contens don't update
« Reply #2 on: December 25, 2013, 08:43:47 am »
That makes sense. I'll try it out. So I could create my checkerboard Squares with an area of 125 square pixels, but if I wanted to resize the window down to something like 800 x 600, the window should automatically take care of resizing the contents? Or should I call the scale function on the squares and scale them up / down by any ratio that works for me? I guess I just want to double check about what you mean for a fixed size. I think what I've been doing for test cases, is to assume to x or y offset are about 125 pixels, since I usually use a 1000 x 1000 size window, and the checkerboard is 8 x 8 which cuts the squares down into a nice 125. I think I see what I need to do, I need to always keep the offset @ 125, and then the window will adjust?

Thanks!
The truth will set you free but first it will piss you off.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Resizing Window, but contens don't update
« Reply #3 on: December 25, 2013, 03:55:27 pm »
You should take a look at the views tutorial;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor