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.