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

Author Topic: Able To Open a New Window But Not Close it  (Read 5993 times)

0 Members and 1 Guest are viewing this topic.

jl1532

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Able To Open a New Window But Not Close it
« on: June 07, 2021, 02:25:17 am »
Hi, I create a feature that if the user clicks the menu, a window pops out. I'm able to display the pop window but not close it.
class PopWindow {
private:
    sf::RenderWindow popWin;
    sf::Color popWinColor = sf::Color(50, 50, 50);

public:
    // Default constructor
    PopWindow();

    // While pop is Open
    int winOpen();
};
#include "PopWindow.h"

PopWindow::PopWindow() {
  popWin.create(sf::VideoMode(500, 500), "Pop Window");
}

int PopWindow::winOpen() {
  sf::Event popWinEvent{};
  while (popWin.isOpen()) {
    while (popWin.pollEvent(popWinEvent)) {
      if (popWinEvent.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::F2)) {
        popWin.close();
      }
      popWin.clear(popWinColor);
      popWin.display();
    }
  }
  return 0;
}

The weird thing is if I declare a PopWindow object and call object.winOpen() in the main class, everything works fine, however, if the object is declared in one of other classes, I can only open a new window but not close it.

How to handle events for multiple windows?
Thanks for your help!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Able To Open a New Window But Not Close it
« Reply #1 on: June 09, 2021, 08:20:29 am »
You have to combine the event processing, update and drawing of both windows in one main/game loop.

Maybe not the best design, but one way would be to change the while (popWin.isOpen()) to a if(popWin.isOpen()) and call it from your main window loop every frame.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/