Jump to content
  • Advertisement
Sign in to follow this  
mattor

Allegro screen flickering

This topic is 4985 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay coders maximus: I am using allegro and dev-c++. I am trying to write a simple space invaders type deal where the ship only moves left and right at the bottom of the screen. I have implemented double buffering with the theory in mind that I can paint everything to the buffer, then flip the buffer to the screen when it's all done so as to avoid flickering. So when I try to do that, I *still* get flickering. WHY must it always be this way? The ship is what flickers, not the background. Here's my code:

buffer = create_video_bitmap(WIDTH,HEIGHT);

 do{
    
    erase(ship);
    move(ship);
    draw(ship);
    
    blit(buffer,screen,0,0,0,0,WIDTH,HEIGHT);
    rest(10);
    
   }while(!key[KEY_ESC]);

and the three functions:
void erase(CShip ship)
{
  int left = ship.x-SPEED;
  int right = ship.x + ship.width +SPEED;
  int h = HEIGHT-ship.height;
  
  rectfill(buffer,left,h,right,HEIGHT,DBLUE);
} 

void move(CShip &ship)
{
  if(key[KEY_LEFT]) ship.x -= SPEED;
  if(key[KEY_RIGHT])ship.x += SPEED;
  
  if(ship.x < 0) ship.x =0;
  if(ship.x > WIDTH-ship.width) ship.x = WIDTH-ship.width;  
}

inline void draw(CShip ship)
{ 
   int h = HEIGHT-ship.height;
   rectfill(buffer,ship.x,h,ship.x+ship.width,HEIGHT,ship.color);
}

Share this post


Link to post
Share on other sites
Advertisement
If only the ship is flickering, than it means that it has to do with how your clearing the ship. I can't really see a reason for drawing a box over the ship is your just going to flip the buffer.

Share this post


Link to post
Share on other sites
I believe that your problem is that you are not in fact using double buffereing. When you create the video bitmap called buffer, the memory allocated is actually from the screen itself. Any drawing you do is likely directly to the screen, hence the flickering.

Try something along the lines of:



BITMAP * buffer[2];

buffer[0] = create_video_bitmap(WIDTH,HEIGHT);
buffer[1] = create_video_bitmap(WIDTH,HEIGHT);
int currentBuffer = 0;



The following is pseudocode although I think the point is clear:



void drawStuff()
{
/*
* Put all the drawing stuff here. Always draw to:
* buffer[currrentBuffer]
*/

// The drawn to buffer is currently off screen so show it
show_video_bitmap(buffer[currentBuffer]);

// alternate the offscreen buffer
currentBuffer = 1 - currentBuffer;
}



[edit]

What I've described is page flipping. The other option (if you really want to go the double buffering route) would be to change create_video_bitmap to a plain create_bitmap and then throw in a vsync to stop the tearing. Read up in the allegro docs on vsync.

[Edited by - Oralloy on March 29, 2005 3:54:14 PM]

Share this post


Link to post
Share on other sites
I do it like this:


BITMAP* Offscreen = create_bitmap(SCREEN_W, SCREEN_H);

// whenever I draw, I always draw to the Offscreen
draw_sprite(Offscreen,SpaceShip,100,200);

// finally, the main loop
while (!key[KEY_ESC])
{
clear_bitmap(Offscreen);
DrawStuff();
blit(Offscreen,screen,0,0,0,0,SCREEN_W,SCREEN_H); // this won't flicker
}

// finally free up the memory
destroy_bitmap(Offscreen);



The cool thing if "Offscreen" is always the first parameter in every drawing function, whether it is blit() or draw_sprite() or something else. So you don't have to think about the arguements ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!