3
\$\begingroup\$

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()
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .