Contadotempo > I'm using SFML 1.6, I should have mentioned it in the first post.
I assure you I understand pretty well how sf::Input works and I've read the doc. The solution I expected simply isn't available in SFML. I started 2D programming with SDL, that's why I was hoping for an SDL-ish solution.
Nexus > As it turned out I didn't go with SDL for event handling, I just modified my main loop:
Old loop:
while(app.IsOpened())
{
Event event;
while(app.GetEvent(event))
{
if(event.Type == Event::Closed)
app.Close();
if(event.Type == Event::KeyPressed || event.Type == Event::KeyReleased)
{
// this function moves a character on the screen toward
// a certain direction given which keys are pressed
GameInput::readInput(&(app.GetInput()), &hero);
}
}
app.Clear();
hero.draw(app);
app.Display();
}
New loop:
while(app.IsOpened())
{
// clk is the loop's clock
clk.Reset();
Event event;
while(app.GetEvent(event))
{
if(event.Type == Event::Closed)
app.Close();
}
// this time, the main function is called at each iteration of the loop
// regardless of whether a key's been pressed
GameInput::readInput(&(app.GetInput()), &hero);
app.Clear();
hero.draw(app);
app.Display();
// this line ensures that one iteration takes no less than 60 ms.
while(clk.GetElapsedTime() < 0.060);
}
The problem with the old loop was that my character moved oddly, since the KeyRepeat parameters depend on the OS settings: each time I ordered him to move, he would make a first step, pause for a second, then resume walking at a reasonable pace. Exactly what happens when you hold a key while editing a text file.
I didn't like this behavior but liked even less the idea of having to change the Windows settings to make him walk smoothly.
The optimal solution would have been to modify the first delay involved in the KeyRepeat process, like it's possible in SDL. But as I said enough times, this feature isn't available in SFML, as Laurent confirmed in the second post:
SFML uses the OS settings, you can't change them in your program.
So instead I wrote a new loop which involves a solution where the key states are checked at each iteration of the main loop, whether a key's been pressed or not.
A loop at the end of the main one ensures the refreshing rate is no less than 60 ms.
It works pretty fine, but I consider this to be too much DIY for my taste and it seems to make the processor work harder.
A fine way to improve this solution would be to replace the last loop:while(clk.GetElapsedTime() < 0.060);
With something along the lines of
SleepUntil(clk.GetElapsedTime() < 0.060);
I'll check in the documentation if there a sleep() function, but it doesn't seem so. Perhaps by using threads?