I've been using allegro for some time, and the timer event worked in all my previous projects, but I don't know what happened this time, I want to move a piece as long as the game is going, but it isn't moving at all, so the timer event isn't working, why is that? I've made sure all event sources are registered. Here is the code that reproduces the problem:-

int firstStuffXPos = 100;
int firstStuffYPos = 100;
int width = 800;
int height = 600;
int FPS = 60;
 bool donePlaying = false;

class MyClass{
int getXPos()
    return x;
int getYPos()
    return y;
int x;
int y;
 MyClass(int x, int y){
    this->x = x;
    this->y = y;
MyClass stuff1[] = {
{ firstStuffXPos, firstStuffYPos},
void initialize(){
    if (!al_init()){
        std::cout << "Failed to initialize allegro5." << std::endl;
    if (!al_init_image_addon()){
        std::cout << "Failed to initialize image." << std::endl;
    display = al_create_display(width, height);
    if (!display){
        std::cout << "Failed to create display." << std::endl;
    if (!al_install_keyboard()){
        std::cout << "Failed to install keyboard." << std::endl;

    eventqueue = al_create_event_queue();
    timer = al_create_timer(1.0 / FPS);

    al_register_event_source(eventqueue, al_get_display_event_source(display));
    al_register_event_source(eventqueue, al_get_keyboard_event_source());
    al_register_event_source(eventqueue, al_get_timer_event_source(timer));

int main(){
for (int nostuff = 0; nostuff < 1; nostuff++){
    stuff1[nostuff].stuffone = al_load_bitmap("blueblock.png");
    while (!donePlaying){
        al_clear_to_color(al_map_rgb(0, 0, 0));
        ALLEGRO_EVENT ev;
        al_wait_for_event(eventqueue, &ev);
        if (ev.type == ALLEGRO_EVENT_TIMER){
            firstStuffXPos += 5;
            firstStuffYPos += 5;
        for (int i = 0; i < 1; i++){
            al_draw_bitmap(stuff1[i].stuffone, stuff1[i].getXPos(), stuff1[i].getYPos(), 0);


but it isn't moving at all, so the timer event isn't working, why is that?

What makes you so sure it isn't moving because the timer isn't working? Try inserting a print statement in the if (ev.type == ALLEGRO_EVENT_TIMER) block -- I think you'll see that your timer event is firing.

It isn't moving because you increment firstStuffXPos/firstStuffYPos, which never actually get used. Try incrementing stuffone[0].x/stuffone[0].y instead.

As a side note, there's no need to call al_start_timer every time you loop, just call it once before you start your main gaime loop.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.