- Online Community 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.
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.

Member #934
January 2001

Not an expert, but is al_install_keyboard not necessary?

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.

Member #2,727
September 2002

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

    update_mario(&mario, keyboardState);
    redraw = true;

And later you do:

if (redraw && al_is_event_queue_empty(queue)) {
    draw_world(camera, map, tiles);
    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.

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

You were probably being spammed with repeating ALLEGRO_EVENT_KEY_CHAR events.

By ignoring the unused events, the event queue then flowed smoothly again.

Go to: