Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);

    if(!buffer)
        return FALSE;

    return TRUE;
}

int main()
{
    int x, y;

    // install allegro, set graphics mode, etc
    allegro_init();
    install_keyboard();

    set_color_depth(32);
    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())
    {
        set_gfx_mode(GFX_TEXT,0,0,0,0);
        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
        clear(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!",
               x);

        // show the offscreen page
        update_screen();
    }

    return 0;
}
END_OF_MAIN()
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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