rainbowunicorn
rainbowunicorn

Reputation: 63

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?

Upvotes: 0

Views: 1160

Answers (2)

user2421739
user2421739

Reputation: 81

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.

Upvotes: 2

Steve
Steve

Reputation: 7271

According the the acquire_bitmap documentation, which acquire_screen calls, you should minimize the number of calls. Typically this would mean calling this at the start of the updating process and then perform all of the drawing before releasing the lock.

...most drawing functions need to lock a video bitmap before drawing to it. But doing this is very slow, so you will get much better performance if you acquire the screen just once at the start of your main redraw function, then call multiple drawing operations which need the bitmap locked, and only release it when done.

It's not possible to tell from the current example code, but I suspect that moveBullets is being called multiple times.

Upvotes: 1

Related Questions