Allegro, sprites leaving trail

736 Views Asked by At

I'm getting the problem my sprites leave a trail behind when i move them. Tried drawning a BG with every refresh but then it start flickering.

This is what i do

// ...

int main(int argc, char *argv[])
{
    BITMAP *buffer = NULL;

    BITMAP *graphics = NULL; 
    buffer = create_bitmap(SCREEN_W, SCREEN_H);

    graphics = load_bitmap("my_graphics.bmp", NULL); 
        clear_to_color(screen, makecol(0, 0, 0));

    clear_to_color(buffer, makecol(0, 0, 0));

    while(!key[KEY_ESC]) 
    {

        // ...

        render_map(100,100);        

        // ...
    }
}

void render_map(int w, int h)

{
    // ...

    for(int i=0;i < w * h;i++)

    {
        masked_blit(graphics, buffer, 0, 0, pos_x, pos_y, 32, 32);  
    }

    // ...
        blit(buffer, screen, camera_x,camera_y,0,0,SCREEN_W, SCREEN_H);

    clear_to_color(buffer, makecol(0, 0, 0));

}

Thanks in advance for any help

1

There are 1 best solutions below

0
On

Your code is a little hard to read, and you've left out big pieces of it. So it's hard to say for sure, but this line looks suspicious:

blit(buffer, screen, camera_x,camera_y,0,0,SCREEN_W, SCREEN_H);

When using a buffer, you typically will always be calling it like:

blit(buffer, screen, 0,0, 0,0, SCREEN_W,SCREEN_H);

and that is the only time you ever draw to the screen. So the steps are:

  1. clear the buffer (by drawing a background image, tileset, color, etc)
  2. draw everything to the buffer
  3. copy the buffer to the screen
  4. repeat