Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] Keyobard input lowers FPS and almost pauses the game  (Read 6330 times)

0 Members and 1 Guest are viewing this topic.

Jimmyee

  • Newbie
  • *
  • Posts: 21
    • View Profile
Can anyone tell me why is that? When I hold any key longer than 1 sec FPS of my game goes down from 32-35 to 1-5. I guess the game is flooded by input events and it doesn't process rendering so fast.
So my questions are:
is it common?
do I have to implement multithreading to avoid that?
« Last Edit: May 02, 2016, 08:16:56 pm by Jimmyee »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #1 on: May 02, 2016, 09:22:05 am »
As you would expect, slowing down the whole app just by holding a key is not a normal thing, so it most likely has something to do with your code. So please start by showing how you handle the key press event ;)
Laurent Gomila - SFML developer

Jimmyee

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #2 on: May 02, 2016, 09:41:02 am »
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...
« Last Edit: May 02, 2016, 09:49:38 am by Jimmyee »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #3 on: May 02, 2016, 10:23:33 am »
What's your OS/window manager? The OS usually has a short delay between repeated key-press events, so it shouldn't flood your event loop.
Laurent Gomila - SFML developer

Jimmyee

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #4 on: May 02, 2016, 11:08:55 am »
Ubuntu 14.04 LTS is my OS, window manager: Compiz.

nicox11

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #5 on: May 02, 2016, 11:18:46 am »
Maybe your player->Walk() or this->editor->MoveMarker() isn't working correctly and takes too much time ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Keyobard input lowers FPS and almost pauses the game
« Reply #6 on: May 02, 2016, 11:30:55 am »
Run it through a profiler and see where it gets stuck.

Alternatively you can add a counter to the event loop and see how many events get generated.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jimmyee

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #7 on: May 02, 2016, 11:38:17 am »
@Nicox11:
Quote
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;
 
Quote
FPS: 7
Events per second: 2164

Then when I stop moving the mouse it goes back to normal:

Quote
FPS: 31
Events per second: 0
« Last Edit: May 02, 2016, 12:10:04 pm by Jimmyee »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #8 on: May 02, 2016, 12:28:38 pm »
What's up with with your SFML .lib files, reinstalling SFML from scratch because some .lib issues can cause slowdown.

P.S

Which SFML versions you have?
« Last Edit: May 02, 2016, 12:35:34 pm by Mr_Blame »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #9 on: May 02, 2016, 01:10:40 pm »
What's up with with your SFML .lib files, reinstalling SFML from scratch because some .lib issues can cause slowdown.

P.S

Which SFML versions you have?
Please stop giving random suggestions that don't even remotely make sense...


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.
Well mouse and keyboard events are something different. What mouse do you have? What's the DPI settings?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jimmyee

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #10 on: May 02, 2016, 02:36:49 pm »
Even if they are different they cause the same effect - if I hold a key or move the mouse the game will slowdown and will process the events until I release a key or stop moving the mouse.
Anyway, here are details of my mouse:
A4TECH, V-Track
Model: F4

@Mr_Blame: my version if SFML-2.3.2.
« Last Edit: May 02, 2016, 02:38:37 pm by Jimmyee »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #11 on: May 02, 2016, 02:40:24 pm »
Show us game.render. It will be interesting to look inside, rendering because 30 fps with no events processed seems weird to me... It invokes a simple question: "what are you rendering".
« Last Edit: May 02, 2016, 02:53:18 pm by Mr_Blame »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #12 on: May 02, 2016, 02:44:11 pm »
Even if they are different they cause the same effect - if I hold a key or move the mouse the game will slowdown and will process the events until I release a key or stop moving the mouse.
Yes, but the point is that the mouse generates events whenever you move it. If the driver acts up (or maybe it's "intentional") it can end up generating hunderds of move events, while a single key press would never do that. ;)
Do you have any special driver installed for your mouse?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jimmyee

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Keyobard input lowers FPS and almost pauses the game
« Reply #13 on: May 02, 2016, 02:58:26 pm »
OK I see. I HAD no driver installed for my mouse. Uppercase HAD because I was checking if same happens when using other window manager and for some reason my OS doesn't load now. I'm going to reinstall my OS now and see if the bug with events still will occur. I will give you the information about my DPI and mouse driver after installation of the system.
Anyway, I have changed the window manager and the bug still occured ;)

EDIT 19:57 PM:
After reinstall of my OS and compiler it works fine now. it's like 1000 FPS and max 4 events handled without any lag. The only difference which I made was installing g++ instead of gcc :P
« Last Edit: May 02, 2016, 08:16:18 pm by Jimmyee »