Hello
,
while i was working on the performance of game i realized that sf::Keyboard::isKeyPressed returns slowly at random timings. The following minimal working example showed that on my system (ArchLinux, gcc 9.1, with sfml 2.5.1) one call to sf::Keyboard::isKeyPressed takes around 10 ticks normally. At random intervals it jumps to 110-150 ticks (at 1 million ticks per second)
, while I don't press any key and keeping the window just open.
#include "SFML/Graphics.hpp"
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
clock_t start = clock();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
std::cout << "A" << std::endl;
}
clock_t end = clock() - start;
if (end > 100) std::cout << end << std::endl;
window.clear(sf::Color::Blue);
window.display();
}
return 0;
}
In the game I currently check for 9 keys, where the times seems to add up leading to low minimal fps as I check during rendering. Am I missing something obvious here? Should I split the rendering and input into different threads? Moreover each call to isKeyPressed fetches the whole keymap of X11. Is it advisable to get it once and check all required keys against it?