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

Author Topic: RenderWindow dissapers when changing its size during my application  (Read 6859 times)

0 Members and 2 Guests are viewing this topic.

Tristanbox09

  • Newbie
  • *
  • Posts: 5
    • View Profile
Hello, I am using my window from Render Window to be the width and height of a file I input for Minesweeper

sf::RenderWindow window(sf::VideoMode(cfgWindowMaker.GetWidth(), cfgWindowMaker.GetHeight()), "Minesweeper!", sf::Style::Close);

and when a button is clicked, the window for Render Window would change to whatever the files dimensions are. This seems to draw the board correctly, but my window does not resize. My code is below


if (event.type == sf::Event::Resized) {
            // resize my view
            view.setSize({
                  static_cast<float>(event.size.width),
                  static_cast<float>(event.size.height)
               });

            sf::RenderWindow window(sf::VideoMode(cfgWindowMaker.GetWidth(), cfgWindowMaker.GetHeight()), "Minesweeper!", sf::Style::Close);
         }


if (test1Btn.contains(mousePosFloat))
               {
                  boardObject.ResetGame(cfgWindowMaker);
                  cfgWindowMaker.ReadingCFGFile("boards/configbeginner.cfg");
                  boardObject.RandomBombMaker(cfgWindowMaker, window);
                  sf::RenderWindow window(sf::VideoMode(cfgWindowMaker.GetWidth(), cfgWindowMaker.GetHeight()), "Minesweeper!", sf::Style::Close);
                  window.setView(view);
                  
               }


Any help is much appreciated. I am trying to have my window actually match the dimensions of my board when the new fie get added

Kvaz1r

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: RenderWindow dissapers when changing its size during my application
« Reply #1 on: December 06, 2020, 06:58:11 pm »
For resizing use code from there:

// the event loop
sf::Event event;
while (window.pollEvent(event))
{
    ...

    // catch the resize events
    if (event.type == sf::Event::Resized)
    {
        // update the view to the new size of the window
        sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
        window.setView(sf::View(visibleArea));
    }
}

Tristanbox09

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: RenderWindow dissapers when changing its size during my application
« Reply #2 on: December 06, 2020, 07:17:29 pm »
When I add that line, I still get the same issue... my window appears like this for a beginner board (seen in image), for some reason the resize on my board works, but the window does not resize

Kvaz1r

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: RenderWindow dissapers when changing its size during my application
« Reply #3 on: December 06, 2020, 07:56:59 pm »
In that case you have mistake somewhere else. Can you provide minimal, but complete code for reproducing the behaviour? Or maybe you already loaded your project on Github or something?

Tristanbox09

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: RenderWindow dissapers when changing its size during my application
« Reply #4 on: December 06, 2020, 08:31:34 pm »
Yea, So I initially read in a cfg file to have a default game setup


   /* ======== Loading in the board data ======= */
   cfgWindowMaker.ReadingCFGFile("boards/configexpert.cfg");


   /* ======== Creating the MineSweeper Window for gameplay ======== */
   sf::RenderWindow window(sf::VideoMode(cfgWindowMaker.GetWidth(), cfgWindowMaker.GetHeight()), "Minesweeper!", sf::Style::Close);


I than do my event for resize in the event loop


   sf::View view = window.getDefaultView();


   /* ======== When window is open ..... End of Initializations ======== */
   while (window.isOpen())
   {
      sf::Event event;
      while (window.pollEvent(event))
      {
         if (event.type == sf::Event::Closed)
            window.close();

         if (event.type == sf::Event::Resized) {
            // resize my view
            // update the view to the new size of the window
            sf::FloatRect visibleArea(0, 0, cfgWindowMaker.GetWidth(), cfgWindowMaker.GetHeight());
            window.setView(sf::View(visibleArea));

            sf::RenderWindow window(sf::VideoMode(cfgWindowMaker.GetWidth(), cfgWindowMaker.GetHeight()), "Minesweeper!", sf::Style::Close);
         }


and if I left click on beginner button it would load a smaller dimension of the board (including resizing the window)


if (test1Btn.contains(mousePosFloat))
               {
                  boardObject.ResetGame(cfgWindowMaker);
                  cfgWindowMaker.ReadingCFGFile("boards/configbeginner.cfg");
                  boardObject.RandomBombMaker(cfgWindowMaker, window);
                  sf::FloatRect visibleArea(0, 0, cfgWindowMaker.GetWidth(), cfgWindowMaker.GetHeight());



               }


               if (test2Btn.contains(mousePosFloat))
               {
                  cout << "Test 2" << endl;
                  boardObject.ResetGame(cfgWindowMaker);
                  cfgWindowMaker.ReadingCFGFile("boards/configintermediate.cfg");
                  boardObject.RandomBombMaker(cfgWindowMaker, window);
                  window.setView(view);

               }

As you can see I tried resizing the event two ways (with floatRect and with setView) where both outputs show the result of the image I posted in the above reply

Thank you for the help!

Kvaz1r

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: RenderWindow dissapers when changing its size during my application
« Reply #5 on: December 06, 2020, 08:48:21 pm »
If you want resize window inside handler than you should call window.setSize there.

So change it to:
if (test2Btn.contains(mousePosFloat))
               {
                  boardObject.ResetGame(cfgWindowMaker);
                  cfgWindowMaker.ReadingCFGFile("boards/configintermediate.cfg");
                  boardObject.RandomBombMaker(cfgWindowMaker, window);
                  window.setSize({ cfgWindowMaker.GetWidth(), cfgWindowMaker.GetHeight() });
                  //view will change in resize handler
               }

Tristanbox09

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: RenderWindow dissapers when changing its size during my application
« Reply #6 on: December 06, 2020, 08:55:46 pm »
Ahhh thank you!!!