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

Author Topic: How come sf::Event type == sf::Event::Closed when I'm not trying to close window  (Read 3581 times)

0 Members and 1 Guest are viewing this topic.

BiiXteR

  • Newbie
  • *
  • Posts: 20
    • View Profile
I'm having a problem where sf::Event::type is by "default" (as soon as the window opens) equal to sf::Event::Closed, which results in as soon as the window opening, it runs trough the game loop once and then exits the game.

My game loop looks like this :

        while (InputManager::CloseEventDetected() == false)
        {
                m_displayMgr->PollWindowEvents(*InputManager::GetEventClass());
                InputManager::HandleInput();

                m_displayMgr->GetRenderWindow()->draw(*m_levelMgr);
                m_displayMgr->Render();

                std::cout << "Closed evnt : " << InputManager::CloseEventDetected() << std::endl;
        }
 

And my InputManager header file looks like this :
#pragma once
#include <SFML\Graphics.hpp>

class InputManager
{
public:

        static void HandleInput();

        static bool IsKeyDown(sf::Keyboard::Key key);
        static bool isKeyDownRepeat(sf::Keyboard::Key key);

        static bool IsKeyUp(sf::Keyboard::Key key);
        static bool IsKeyUpRepeat(sf::Keyboard::Key key);

        static bool CloseEventDetected();

        static sf::Event* GetEventClass();

private:

        InputManager();

        static bool m_keys[sf::Keyboard::KeyCount];
        static bool m_keysLastFrame[sf::Keyboard::KeyCount];

        static bool m_closeEventDetected;

        static sf::Event* m_evnt;
};
 

And the cpp file looks like this :
#include "InputManager.h"

bool InputManager::m_keys[sf::Keyboard::KeyCount];
bool InputManager::m_keysLastFrame[sf::Keyboard::KeyCount];

bool InputManager::m_closeEventDetected = false;

sf::Event* InputManager::m_evnt = new sf::Event();

InputManager::InputManager()
{
        m_evnt = new sf::Event();
}

void InputManager::HandleInput()
{
        for (int i = 0; i < sf::Keyboard::KeyCount; i++)
        {
                m_keys[i] = (sf::Keyboard::isKeyPressed((sf::Keyboard::Key)i));

                m_keysLastFrame[i] = m_keys[i];
        }

        if (m_evnt->type == sf::Event::Closed)
        {
                printf("close detect");
                m_closeEventDetected = true;
        }

}

bool InputManager::IsKeyDown(sf::Keyboard::Key key)
{
        if (m_keys[key] == true && m_keysLastFrame[key] != true)
        {
                return true;
        }
        else
        {
                return false;
        }
}

bool InputManager::isKeyDownRepeat(sf::Keyboard::Key key)
{
        return m_keys[key];
}

bool InputManager::IsKeyUp(sf::Keyboard::Key key)
{
        if (m_keys[key] == true && m_keysLastFrame[key] != true) //TODO: Not sure if it works.
        {
                return false;
        }
        else
        {
                return true;
        }
}

bool InputManager::CloseEventDetected()
{
        return m_closeEventDetected;
}

sf::Event* InputManager::GetEventClass()
{
        return m_evnt;
}

bool InputManager::IsKeyUpRepeat(sf::Keyboard::Key key)
{
        return !m_keys[key];
}
 

Basically what happens is :

The window is created, and the game loop runs trough correctly once, but on the second loop InputManager::CloseEventDetected() returns true which results in the game loop breaking and the game closing.

But I can't seem to find any reason to why the sf::Event::type would be equal to Sf::Event::Closed.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
The event object is only valid once you past it to pollEvent or waitEvent and they have returned true. After creating an event object any access to the fields is undefined and contains random data.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BiiXteR

  • Newbie
  • *
  • Posts: 20
    • View Profile
I do that in the game loop, the line :
m_displayMgr->PollWindowEvents(*InputManager::GetEventClass());
 

The PollWindowEvents method looks like this :

void DisplayManager::PollWindowEvents(sf::Event evnt) const
{
        m_window->pollEvent(evnt);
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Only if pollEvent returns true is the Event valid, as already mentioned by eXpl0it3r.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

JayhawkZombie

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
To expand on what they said, sf::Event is a union.  It is undefined behavior to access a union whose fields have not been set. See: http://en.cppreference.com/w/cpp/language/union