Search code examples
c++spriteallegroperformance

C++ Allegro Drawing Sprites (even without any loop) Slows Graphics Down


I am developing a 2D platform game using Allegro with C++. However, there is a problem. In the game the character has rubber bullets and when a bullet is fired, it reflects from the walls forever. The character has ability to fire 30 bullets, however, the more the bullet count the slower the graphics becomes. Although I do not use any loops for drawing the movement of any bullets, the game inevitably slows down. Here is the function for moving bullets:

void Character :: moveBullets(void){
if(bullet_turn != bullet_count){
    bullet_chain[bullet_turn++]->move();        
else
    bullet_turn = 0;}

And here is the function move():

rectfill(buffer, getX(), getY(), getX() + bullet_image[direction]->h, getY() + bullet_image[direction]->w, 0);

//update direction 

acquire_screen();
draw_sprite(buffer, bullet_image[direction], x, y);
blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
release_screen();

}

What should I do? Does Allegro always slow down when drawing many sprites?


Solution

  • If I understand your code correctly, you are copying the whole screen from your buffer to the screen for every bullet. Is this really what you want?

    Also, are you sure you should use acquire_screen()? The documentation for acquire_bitmap

    https://www.allegro.cc/manual/4/api/bitmap-objects/acquire_bitmap

    says it can cause slowdown in some cases.