Lag when holding down key.
Gestaldi

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

Not an expert, but is al_install_keyboard not necessary?

Gestaldi

@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

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

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

Edgar Reynaldo

You were probably being spammed with repeating ALLEGRO_EVENT_KEY_CHAR events.

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

Thread #618449. Printed from Allegro.cc