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

Author Topic: Weirdly slow event loop  (Read 3683 times)

0 Members and 1 Guest are viewing this topic.

tastyham

  • Newbie
  • *
  • Posts: 6
    • View Profile
Weirdly slow event loop
« on: April 21, 2021, 03:28:16 am »
Hello,

I've recently been working on a school project and wanted to create a RPG in The Escapists style.

I've created sprites arrays and drew them according to the view I set (basically drawing just what I need, not the whole map).
This was going fine, however I noticed that sometimes when moving my character, the game would just slow for no reason.

After a while I noticed it was slowing down when I was moving my mouse. I looked it up on the forum but nothing would come up to really fix the issue. One was talking about removing everything from the event loop (I even completly removed the event loop at some point) but it was still slowing down... for.. well I don't know.

I'm not sure it's coming from another place because on how much more characters were printed when it was going in the event loop (0-2 characters when doing nothing or moving with WASD, however 10/15 when just moving the mouse).

I've seen that it could be related to drivers from other posts. So I just pulled my project onto my laptop instead of my desktop computer, different computer, mouse, etc... but it was still going slow at some point. Not as slow as it was on my desktop computer, but still slow for any reason.

I tried using my bluetooth mouse from my laptop computer on my desktop computer... It was better but still slow.
I tried to not draw my sprite array... nope.

Just moving a sprite around, even without my view moving with the sprite, was laggy when I'm moving my mouse.

I'm not sure what to do anymore, I've been working the same way I did on a lot of other projects in SFML (CSFML in this case but as it's basically the same, I wanted to post it in here as it would get viewed much more than in the C binding section in the forum).

Here's my "main" loop :

int play(game_s *game)
{
    play_s *play = NULL;

    play = play_init(play);
    if (play == NULL)
        return (-1);

    while (sfRenderWindow_isOpen(game->window) && game->scene == 1) {
        sfRenderWindow_clear(game->window, sfBlack);

        // checks, keycheck, draw...
        play_checks(game, play);
        play_draw(game, play);

        sfRenderWindow_display(game->window);
        sfRenderWindow_setView(game->window, play->view);
    }
    play_dispose(play);
    return (0);
}

and the code in the "play_checks" :

void play_checks(game_s *game, play_s *play)
{
    int speed = 1;
    sfVector2f view_size = {0, 0};

    if (sfKeyboard_isKeyPressed(sfKeyE))
        sfRenderWindow_close(game->window);

    //TODO testing to move view, to remove
    if (sfKeyboard_isKeyPressed(sfKeyZ))
        sfSprite_move(play->player_sprite, (sfVector2f){0, -speed});
    if (sfKeyboard_isKeyPressed(sfKeyQ))
        sfSprite_move(play->player_sprite, (sfVector2f){-speed, 0});
    if (sfKeyboard_isKeyPressed(sfKeyS))
        sfSprite_move(play->player_sprite, (sfVector2f){0, speed});
    if (sfKeyboard_isKeyPressed(sfKeyD))
        sfSprite_move(play->player_sprite, (sfVector2f){speed, 0});
    sfView_setCenter(play->view, sfSprite_getPosition(play->player_sprite));

    while (sfRenderWindow_pollEvent(game->window, &game->event)) {
        if (game->event.type == sfEvtClosed)
            sfRenderWindow_close(game->window);

        //TODO testing to zoom in & out, to remove
        if (game->event.type == sfEvtMouseWheelScrolled) {
            view_size = sfView_getSize(play->view);
            view_size.y += game->event.mouseWheelScroll.delta * -100;
            view_size.x += game->event.mouseWheelScroll.delta * ((float)-100 * (float)1.77777777778);
            sfView_setSize(play->view, view_size);
        }
    }
}

Note : the view doesn't seem to have any effect, removing it just cause me to.. not have a view.. but still having a slow event loop.

If anyone has any idea, I'd just try any of them.

I'm running on Fedora 30 on both laptop & desktop computers. If you need any more informations, please let me know.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Weirdly slow event loop
« Reply #1 on: April 21, 2021, 08:34:11 am »
What GPU driver are you using?
Do you have the proper driver for your mouse installed?
Do you have any weird joystick/gamepad like controllers plugged in that have a broken driver?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tastyham

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Weirdly slow event loop
« Reply #2 on: April 21, 2021, 12:22:55 pm »
What GPU driver are you using?
Do you have the proper driver for your mouse installed?
Do you have any weird joystick/gamepad like controllers plugged in that have a broken driver?

Hello,

To be honest I’m not sure on how to check on those on Linux. I’m probably certain that I don’t have any installed by myself, either for the GPU or the mouse. I don’t use any controllers so I don’t think this will be an issue right now.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Weirdly slow event loop
« Reply #3 on: April 21, 2021, 12:32:31 pm »
in the piece of code you posted I couldn't find anything that could lead to that.
next step is to try a minimal complete code that reproduces the problem.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

tastyham

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Weirdly slow event loop
« Reply #4 on: April 21, 2021, 12:44:25 pm »
in the piece of code you posted I couldn't find anything that could lead to that.
next step is to try a minimal complete code that reproduces the problem.

What do you mean by that? That it wasn’t complete so you couldn’t run it properly? It wasn’t intended to be ran but mostly to see if there was any issues from what appears to be the only piece of code that would lead to it. The only things more to run this is like a main with a game structure having the window, event, videomode, etc... and qi highly doubt it could come from here.

As I’m not working alone I asked one of my team mate to run it and he said that he didn’t notice anything weird. It would probably be, like exploiter said, related to drivers or something else on my computer, but it’s weird that it’s also lagging on my laptop.

kojack

  • Sr. Member
  • ****
  • Posts: 310
  • C++/C# game dev teacher.
    • View Profile
Re: Weirdly slow event loop
« Reply #5 on: April 21, 2021, 12:50:48 pm »
Are you doing anything to limit frame rate?  (like vsync or the window's setFramerateLimit)
The player is moving at 1 pixel per frame with no delta time use, which is pretty slow at 60Hz (depends on your sprite size and world scale I guess).
I'm guessing it's not frame rate limited, so the actual rate is going to vary a fair bit depending on your system's activity, resulting in the player moving at different speeds.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Weirdly slow event loop
« Reply #6 on: April 21, 2021, 01:34:10 pm »
What do you mean by that? That it wasn’t complete so you couldn’t run it properly? It wasn’t intended to be ran but mostly to see if there was any issues from what appears to be the only piece of code that would lead to it. The only things more to run this is like a main with a game structure having the window, event, videomode, etc... and qi highly doubt it could come from here.

yes, the problem is half the times someone posts 'what appears to be the only piece of code that would lead to it', it's not  :P
if you have a small code that reproduces it, but it does not happens to us when testing it, then it would be mostly related to things like drivers or other setups. if it still happens to others users when testing it, in a smaller code is easier to find the issue.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

tastyham

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Weirdly slow event loop
« Reply #7 on: April 21, 2021, 01:51:40 pm »
Are you doing anything to limit frame rate?  (like vsync or the window's setFramerateLimit)
The player is moving at 1 pixel per frame with no delta time use, which is pretty slow at 60Hz (depends on your sprite size and world scale I guess).
I'm guessing it's not frame rate limited, so the actual rate is going to vary a fair bit depending on your system's activity, resulting in the player moving at different speeds.

It wasn't but adding it didn't do much, it did slow it down (the slowness is less noticeable) but it's still there. I'm also thinking about clocks and will try to add one to see if anything changes significantly better with that, and if not I'll try to run a sample code, if it still occurs I'll share it. Same thing for the drivers, i'm not sure on how to update any on linux but I'll figure it out.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Weirdly slow event loop
« Reply #8 on: April 21, 2021, 01:58:13 pm »
How do you determine "slowness"?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tastyham

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Weirdly slow event loop
« Reply #9 on: April 21, 2021, 04:42:37 pm »
How do you determine "slowness"?

I would say that the character movement is slower than it should be. More "a"s are printed in the console and the movements (1 pixel / (frame?milisecond?...) is reduced to 0.X pixel.

I just tried using a clock and it does the same thing. Every 10 milisecond I'm checking if my input is WASD, if so I move the sprite X pixels. I stopped using my view and it's doing the same thing.

Here's a gif to see what I mean by "slowness". It's not that easy to see as a gif but look closely when I move my mouse and when I don't.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Weirdly slow event loop
« Reply #10 on: April 21, 2021, 05:38:47 pm »
Sounds much more like a coding issue then.

I suggest to create a minimal example or use like SFML's pong example and see if you can reproduce it with that.
If not, then it's more likely an issue in your own code.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tastyham

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Weirdly slow event loop
« Reply #11 on: April 21, 2021, 06:55:51 pm »
Sounds much more like a coding issue then.

I suggest to create a minimal example or use like SFML's pong example and see if you can reproduce it with that.
If not, then it's more likely an issue in your own code.

I'll try making a code sample for you, but I'm almost certain it isn't. I asked other of my teammates and they don't have issues with what I did.

 

anything