1
General / Re: window.display() alone toggles between the last and current buffer displayed
« on: July 01, 2018, 11:32:52 am »
A bit late, but in case someone else will come by..
You could also use callbacks to swap between waitEvent and pollEvent, allowing you to use the same event handling loop:
You could also use callbacks to swap between waitEvent and pollEvent, allowing you to use the same event handling loop:
sf::Window window;
//An array of callbacks, in which we store both functions:
bool (sf::Window::*windowEventCallbacks[])(sf::Event&) = {
&sf::Window::pollEvent,
&sf::Window::waitEvent
};
unsigned currentCallbackIndex = 0;
while (window.isOpen)
{
sf::Event e;
//use the right callback, using the current index:
while ( (window.*windowEventCallbacks[currentCallbackIndex])(e) )
{
switch (e.type)
{
case sf::Event::KeyPressed:
if (e.key.code == sf::Keyboard::Escape)
{
//pause game - swap callbacks, to use waitEvent:
currentCallbackIndex++;
currentCallbackIndex %= 2; //to loop back when it is greater than 1.
}
break;
/* ... handle events ... */
}
}
}
//An array of callbacks, in which we store both functions:
bool (sf::Window::*windowEventCallbacks[])(sf::Event&) = {
&sf::Window::pollEvent,
&sf::Window::waitEvent
};
unsigned currentCallbackIndex = 0;
while (window.isOpen)
{
sf::Event e;
//use the right callback, using the current index:
while ( (window.*windowEventCallbacks[currentCallbackIndex])(e) )
{
switch (e.type)
{
case sf::Event::KeyPressed:
if (e.key.code == sf::Keyboard::Escape)
{
//pause game - swap callbacks, to use waitEvent:
currentCallbackIndex++;
currentCallbackIndex %= 2; //to loop back when it is greater than 1.
}
break;
/* ... handle events ... */
}
}
}