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

Author Topic: [solved]sf::Event::Closed is sent immediately after opening a new window ?  (Read 2766 times)

0 Members and 1 Guest are viewing this topic.

jonnection

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Hello all

I have done everything mentioned in the "read this before posting". I'm not able to find an explanation to this problem.

The problem is that if I open a new window, unless my mouse is on top of that window, it immediately gets sent a "sf::Event::Closed" event, even though it has just been created ! If however my mouse cursor happens to be on top of where the window opens, the event is not sent.

And, as I am polling for this event to close the window, the result is that the window gets closed immediately after it has been created - unless the mouse cursor is in the area of the window when it appears.

My environment is SFML 2.1 with MinGW.

Below is the code to reproduce the problem:

main.cpp
#include "problem.h"

int main()
{
    test_SFML();
    while (1)
    {
        pollEsc();
    }

}

problem.cpp
#include "problem.h"

using namespace sf;

sf::RenderWindow simWindow;
sf::View simScreen(sf::FloatRect(0, 0, 320, 240));
sf::Color col0(250, 250, 250); // background
sf::Event simEvent;

void test_SFML(void)
{
    simWindow.create(sf::VideoMode(320, 240), "test");
    simScreen.setViewport(sf::FloatRect(0, 0, 1, 1));
    simWindow.setView(simScreen);
    simWindow.clear(col0);
    simWindow.display();
}

int pollEsc() {
simWindow.pollEvent(simEvent);
    if(simEvent.type == sf::Event::Closed) {
       simWindow.close();
       return -1;
    }
return 0;
}
 

problem.h
#ifndef PROBLEM_H
#define PROBLEM_H

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

extern void test_SFML();
extern int pollEsc();

#endif // PROBLEM_H

 
« Last Edit: January 10, 2016, 11:32:32 pm by jonnection »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::Event::Closed is sent immediately after opening a new window ?
« Reply #1 on: January 10, 2016, 10:43:25 pm »
pollEvent returns true if it retrieves an event and false if it does not so it should be tested before reading the contents of the sf::Event.
In your case, it will not be finding an event and then testing the value of the sf::Event's type. Since it's an enum and the closed type is first (with a value of zero), and it will be being initialised with values of zero, the sf::Event will test positive for a closed type. It is for this reason that you must test to see if pollEvent actually received an event because only then will the sf::Event be updated with the correct data.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

jonnection

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Ofcourse...

I somehow automatically thought NULL would be a no event value.

Thank you!

 

anything