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 4380 times)

0 Members and 1 Guest are viewing this topic.


  • 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 {
    sf::RenderWindow popWin;
    sf::Color popWinColor = sf::Color(50, 50, 50);

    // Default constructor

    // 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)) {
  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!


  • SFML Team
  • Hero Member
  • *****
  • Posts: 10280
    • 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://dev.my-gate.net/