Link to home
Start Free TrialLog in
Avatar of gmacdonald
gmacdonald

asked on

Double Buffering in C

Hi,

Thank you for taking the time to possibly answer my question.

I am using the Atari ST emulator to develop an application which must use Double Buffered Graphics to write to video memory.

Here's what I know;

Physbase() = start location of frame buffer.

Can instantiate pointers which point to start of frame buffer, and manipulate them accordingly in order to draw to memory.

Can create a secondary buffer, which is an invisible back buffer, write to it while a primary buffer is being displayed on the screen, and then swap the two images as soon as the vertical retrace is complete.

Here's what I don't know: HOW .. lol

I am also aware that, because I am working with a monochrome monitor, which only displays two colors;
I need to allocate the second buffer (outside of main) globally as a 32000 byte chunk of memory, which is 256 byte alligned.

Can someone please point me in the right direction, as far as coding and syntax is concerned. I am a relatively fast learner, but this is my first video programming experience, and I am very shaky when it comes to conceptual design issues.
Avatar of jgordos
jgordos
Flag of United States of America image

Generally, you need to have a pretty solid understanding of  "why", before you go on to "what", and a bit of "what" before you go on to "how".

let's talk, generally, about double buffering.

The idea behind double-buffering is to eliminate screen "flicker" or screen "tearing"... if your drawing loop takes longer than 60 milliseconds or so, people will be able to 'see' the computer drawng, and that breaks the illusion for the animation.... Double buffering eliminates one of the tell-tale artifacts of a long render loop... the screen updating itself in the middle of you drawing on the screen, so that the top of the screen doesn't exactly match the bottom.... that's called "tearing".  We don't like that because it looks bad ("why").

So, to combat this issue, we want to complete drawing COMPLETELY before we tell the computer to draw it on the screen.  The way we do this (prevent the drawing in the middle of our drawing operation) is to draw to an offscreen, in memory, buffer.  This buffer will match the screen buffer in every way... it matches, size, shape, depth... And then, when vertical retrace happens, we tell the computer to use the buffer we just finished updating ("what").

Now, about the "how".

I haven't worked on an ST specifically, so I'll need some answers from you...

Does the ST's video subsystem have the concept of "pages"?
How does the rasterizing chip decide where the start of screen memory is?  Physbase()?  Can we set this value, or is it read only?

-john

Avatar of gmacdonald
gmacdonald

ASKER

What's up John.

Yes, Physbase() is the start of screen memory.

We can write to it, but from what I gather that is unadvised.

Instead I basically pass it to all my rendering functions as char* variable for referencing purposes..

small example:

void draw(char *base, int x, int y);

int main()
{
 int x, y;

 char *base = Physbase();

 draw(base, x, y);

 //loop
}

draw(char *base, int x, int y)
{
 char *image = base;
 //plot bitmap to frame buffer using image pointer as base simulator.
}

All of this works just fine.

The only thing I need now, is a method to allocate two 32000byte chunks of memory (WHICH ARE 256byte aligned, why they must be aligned this way; I would REALLY like to know) and how to swap between them in order to accomplish the "page flipping" effect.

Thank you for your help and info so far. And let me know if you have any other questions..

Oh, and as for the concept of pages; Im really not sure how that works, but due to the fact that my game animates just fine as of now, I would have to say that its irrelevant at this point.

Unless of course, it is necessary to incorporate the double buffering effect.

If so, I will gladly do the extra research necessary.
ASKER CERTIFIED SOLUTION
Avatar of jgordos
jgordos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial