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.