Yeah I guess so.
OK, here's how I handle the input:
// main.cpp
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "Mass Agreement");
Game game;
sf::Clock clock;
int fps = 0;
while (window.isOpen())
{
game.RealTimeInput();
sf::Event event;
while(window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
{
return 0;
}
else if(event.type == sf::Event::KeyPressed)
{
game.KeyPressed(event.key.code);
}
}
if(clock.getElapsedTime().asSeconds() >= 1)
{
clock.restart();
printf("FPS: %i\n", fps);
fps = 0;
}
window.clear(sf::Color::Black);
game.Draw(&window);
window.display();
fps++;
}
return 0;
}
// end of main.cpp
// Game::RealTimeInput()
void Game::RealTimeInput()
{
if(this->editor->active) return;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
if(this->player->GetY() > 0)
player->Walk(0);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
if(this->player->GetX() < map_->Width() - 1)
this->player->Walk(1);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
if(this->player->GetY() < map_->Height() - 1)
this->player->Walk(2);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
if(this->player->GetX() > 0)
this->player->Walk(3);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
unsigned int id = this->editor->GetMarkerID();
shared_ptr<Map::Tile> tile = this->map_->GetTile(this->player->GetX(), this->player->GetY());
if(id != tile->id)
{
tile->id = id;
shared_ptr<sf::Texture> tex = this->gfxhandler->LoadTexture("gfx/map/1.png");
tile->sprite = gfxhandler->GetSprite(tex, tile->id - 1);
}
}
}
// Game::KeyPressed()
void Game::KeyPressed(sf::Keyboard::Key &key)
{
if(key == sf::Keyboard::T)
{
this->editor->active = !this->editor->active;
}
if(this->editor->active)
{
if(key == sf::Keyboard::Up)
{
this->editor->MoveMarker(0);
}
else if(key == sf::Keyboard::Right)
{
this->editor->MoveMarker(1);
}
else if(key == sf::Keyboard::Down)
{
this->editor->MoveMarker(2);
}
else if(key == sf::Keyboard::Left)
{
this->editor->MoveMarker(3);
}
return;
}
else
{
if(key == sf::Keyboard::S)
{
this->map_->Save();
}
}
}
I think that's it. I have posted functions of the Game class related to keyboard input but I don't think it's because of them since the game slows down even when I hold a key that isn't handled by the game.
EDIT:
Everything works fine when I use "if(window.pollEvent(event))" instead of "while(window.pollEvent(event))" but I guess it won't process all the events if I do that this way...
@Nicox11:
I think that's it. I have posted functions of the Game class related to keyboard input but I don't think it's because of them since the game slows down even when I hold a key that isn't handled by the game.
...and yes it's a good idea, let's see how many events are generated. BTW I have never used a profiler and IDK what's that xD
EDIT 11:55 AM:
Same happens while using the mouse, I mean... If I keep to move the mouse then the game will slow down and will keep processing mouse events until I stop moving it. While moving the mouse it doesn't even print the FPS counter - it freezes completely.
EDIT 12:07 AM:
// main.cpp
sf::RenderWindow window(sf::VideoMode(640, 480), "Mass Agreement");
Game game;
sf::Clock clock;
int fps = 0;
while (window.isOpen())
{
game.RealTimeInput();
sf::Event event;
int event_count = 0;
while(window.pollEvent(event))
{
event_count++;
if (event.type == sf::Event::Closed)
{
window.close();
}
else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
{
return 0;
}
else if(event.type == sf::Event::KeyPressed)
{
game.KeyPressed(event.key.code);
}
}
if(clock.getElapsedTime().asSeconds() >= 1)
{
clock.restart();
printf("FPS: %i\nEvents per second: %i\n", fps, event_count);
fps = 0;
}
window.clear(sf::Color::Black);
game.Draw(&window);
window.display();
fps++;
}
return 0;
FPS: 7
Events per second: 2164
Then when I stop moving the mouse it goes back to normal:
FPS: 31
Events per second: 0