fps problem with al_draw_bitmap
EdadTace

Hi

Before I start telling you about my problem, I'd like to point out that I'm pretty new to C++ in general. All I know is from youtube tutorials so there is a few things that I still don't have a complete understanding of yet.

Now with that aside, I have a problem with al_draw_bitmap function causing a huge FPS drop on my game. I've got a timer limiting my game to 60 FPS, but the function it all the way down to 25 - and after running for some time it drops down to 9-10 and a bit later all the way down to 6, so it's definitely a issue I need to get solved somehow.

Here's all the code I believe is relevant - if you need to see more just ask and I'll post that too

First of we have the constructor of the class that contains the function that draws the bitmaps. This is where I load the images in.
This is only fired once so the problem is not caused by the images being loaded in multiple times (the cout acts as proof of that ).

I've also got a deconstruct which destroy the loaded files, but that's not important for this.

#SelectExpand
1objects::objects() 2: skyHeight( 500 ) 3{ 4 cout << "Objects object created" << endl; 5 grassBit = al_load_bitmap( "grass.png" ); 6 objectBit = al_load_bitmap( "object.png" ); 7}

Here the function I where the problem lies with - I've called it DrawGrass

#SelectExpand
8void objects::DrawGrass( int screenW, int camX, int camY ){ 9 float side = 50.0; 10 int posY = skyHeight - camY; 11 12 float startX = camX / side; 13 int roundX = startX; 14 startX = ( startX - roundX ) * side; 15 int posX = 0 - startX; 16 17 if( posY > 0 - side ){ 18 while( posX < screenW ){ 19 //al_draw_filled_rectangle( posX, posY, posX + side, posY + side, al_map_rgb( 0,255,0 ) ); 20 al_draw_bitmap( grassBit, posX, posY, ALLEGRO_VIDEO_BITMAP ); 21 posX += 50; 22 } 23 } 24}

Basicly it draws the bitmap from left to right using a while loop based on input of screenW, camX and camY making sure not to draw anymore than what is needed.

You can also see that I've commented out an al_draw_filled_rectangle. It's something I used for testing and it worked just fine without any fps drop what so ever.

Here's where I call the function

#SelectExpand
25void game::ComposeFrame( ALLEGRO_DISPLAY *display ){ 26 if( gameState == MENU ){ 27 mob.DrawMenu(); 28 mob.DrawMenuText(); 29 } 30 else if( gameState == GAME ){ 31 /* Game drawing ************************************************************************************************* 32 ************************************************************************************************************** */ 33 34 // Background drawing 35 al_draw_filled_rectangle( 0, 0, screenWidth, screenHeight, al_map_rgb( 50, 50, 50 ) ); 36 obj.DrawSky( screenWidth, screenHeight, cam.GetCamX(), cam.GetCamY() ); 37 38 // Game object drawing 39 obj.DrawObject( screenWidth, screenHeight, cam.GetCamX(), cam.GetCamY(), 7450, 400 ); 40 obj.DrawGrass( screenWidth, cam.GetCamX(), cam.GetCamY() ); 41 42 // Ingame menu bar drawing 43 mob.DrawGameMenu(); 44 45 /* ************************************************************************************************************** 46 ************************************************************************************************************** */ 47 48 if( pause ){ 49 /* Pause menu drawing *************************************************************************************** 50 ********************************************************************************************************** */ 51 52 al_draw_filled_rectangle( 0, 0, screenWidth, screenHeight, al_map_rgba( 0,0,0,50 ) ); 53 mob.DrawMenu(); 54 mob.DrawMenuText(); 55 56 /* ********************************************************************************************************** 57 ********************************************************************************************************** */ 58 } 59 } 60}

The function at question get called on line 40.

Here's an image of what it looks like just to give you a better idea of what is going on.

{"name":"digginholeswip.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/c\/1c01c75bf89b2b08a2b870db4f691805.jpg","w":815,"h":638,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/c\/1c01c75bf89b2b08a2b870db4f691805"}digginholeswip.jpg

Should note that as soon as the grass strip gets out of the screen the FPS goes right back up to FPS until it comes back in.

I have tried a few things on fixing this. One of them was making my entire draw routine draw to a bitmap object which then got drawn to the backbuffer, but never managed to that working.

I've also tried using al_set_new_bitmap_flags( ALLEGRO_VIDEO_BITMAP ), which didn't help, but I'm not sure I used it right either. What I did was adding that to the constructor before I loaded the bitmaps and then added ALLEGRO_VIDEO_BITMAP to the flag in the DrawGrass function

Hope you can help me with this. As I said if you need to see more of the code I'll get it posted asap.

Edad Tace

SiegeLord

Three things.

1. ALLEGRO_VIDEO_BITMAP does not go here, look in the documentation of the al_draw_bitmap function for what goes into that argument:
al_draw_bitmap( grassBit, posX, posY, ALLEGRO_VIDEO_BITMAP );

2. Try using deferred bitmap drawing like so:


        while( posX < screenW ){
            //al_draw_filled_rectangle( posX, posY, posX + side, posY + side, al_map_rgb( 0,255,0 ) );
            al_draw_bitmap( grassBit, posX, posY, ALLEGRO_VIDEO_BITMAP );
            posX += 50;
        }

3. Make sure you load your bitmaps after the display is created. I'm betting that this is in fact the problem with your code.

Edgar Reynaldo
EdadTace said:

            al_draw_bitmap( grassBit, posX, posY, ALLEGRO_VIDEO_BITMAP );

The fourth parameter passed to al_draw_bitmap is supposed to be zero, or a combination of ALLEGRO_FLIP_HORIZONTAL and/or ALLEGRO_FLIP_VERTICAL. So passing ALLEGRO_VIDEO_BITMAP makes no sense here.

Check what kind of bitmap the grass is by using al_get_bitmap_flags. Make sure it is actually a video bitmap.

EdadTace

thanks a lot for your replies

SiegeLord you was complete right in that it was caused by the bitmaps being loaded before I created the display. I havn't tried the deferred bitmap drawing yet, but I'll definitely check it out

about the ALLEGRO_VIDEO_BITMAP, I kinda figure it wasn't right, but again I had wasn't sure on how I was suppose to use it, so I just tried something - actually thought that I set it back to 0, but apparently not.

Edgar Reynaldo definitely try using that al_get_bitmap_flags and make sure they are video bitmaps

and again thanks you so much for your help - it have helped me bigtime ;D

Thread #610016. Printed from Allegro.cc