0

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?

rainbowunicorn
  • 63
  • 1
  • 10
  • How does each bullet get updated as time goes by? There's no loop in the code you've shown, but do you have to update each bullet as time passes? – Steve May 26 '13 at 12:36

2 Answers2

2

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.

  • 1
    Thank you so much for the answers. You were both correct, I was doing the blitting for each bullet, where I should've done all the drawing to the buffer and then when all the bullet location (and character) updates are finished, then blit that buffer to the screen once. Now it works perfectly without any slow down. Thanks again! – rainbowunicorn Apr 25 '15 at 22:47
1

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.

Steve
  • 7,171
  • 2
  • 30
  • 52