Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Lag when holding down key.

Credits go to DanielH and torhu for helping out!
This thread is locked; no one can reply to it. rss feed Print
Lag when holding down key.
Gestaldi
Member #20,520
June 2021

Hello, I'm relatively new to programming and this is my first "game".
I had to program a Mario game for an assignment.
the little game there is so far works fine, the only problem is that when I hold down a key, any key, even one that I haven't programmed an event for, the whole game start to slow down.

this is the github repo: https://github (dot) com/RoccoGas/Mario_progra/blob/master/Mario_progra

I suppose the problem is somewhere in the "level.c" file, in the way that I structured the game loop, but I really have no idea.

DanielH
Member #934
January 2001
avatar

Not an expert, but is al_install_keyboard not necessary?

Gestaldi
Member #20,520
June 2021

@DanielH I install everything in an "initialize_allegro()" function that is called as soon as main starts. The declarataion of the function s in "allegro.c". al_install_keyboard() is in there.

torhu
Member #2,727
September 2002
avatar

Not sure exactly what the problem is. But try emptying the event queue before redrawing, and see if that helps:

case ALLEGRO_EVENT_TIMER:
    update_mario(&mario, keyboardState);
    redraw = true;
    break;

And later you do:

if (redraw && al_is_event_queue_empty(queue)) {
    draw_world(camera, map, tiles);
    draw_mario(mario);
    al_flip_display();
    redraw = false;
}

You could also call al_wait_for_event instead of al_get_next_event to avoid running the loop when there is nothing to do.

Gestaldi
Member #20,520
June 2021

@torhu Yah that worked lmao, thanks everybody for the help. Thank God forums like these are still alive. ;D

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: