I'm trying to print the coordinates of a drawn square upon movement. The problem is that the x value doesn't get refreshed on screen.

This is the code I have:

#include <allegro.h>

BITMAP *buffer;

void update_screen(void)
    blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // blit the buffer to the screen

int init_double_buffering()
    // create the memory buffer
    buffer = create_bitmap(SCREEN_W, SCREEN_H);

        return FALSE;

    return TRUE;

int main()
    int x, y;

    // install allegro, set graphics mode, etc

    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640,480, 0,0))
        allegro_message("Unable to set graphics mode.\n");
        return 1;

    // initialize page flipping
    if (!init_double_buffering())
        allegro_message("Unable to initialize page flipping.\n");
        return 1;

    // default coordinates for the rectangle
    x = SCREEN_W / 2 - 5;
    y = SCREEN_H / 2 - 5;

    // main game loop
    while (!key[KEY_ESC])
        // game logic
        if (key[KEY_LEFT] && x) --x;
        if (key[KEY_RIGHT] && x < SCREEN_W-10) ++x;
        if (key[KEY_UP] && y) --y;
        if (key[KEY_DOWN] && y < SCREEN_H-10) ++y;

        // you draw everything to buffer
        rect(buffer, x,y, x+9,y+9, makecol(255,255,255));
        textprintf_centre_ex(screen, font, SCREEN_W / 2, SCREEN_H/2,
               makecol(255, 255, 255), -1,
               "X value is %d!",

        // show the offscreen page

    return 0;

The Problem is that you draw your text on the screen and then blit the doublebuffer over that.

Simply change the first argument to textprintf_centre_ex from screen to buffer and it works like a charm.


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.