Double Buffering in C


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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?


gmacdonaldAuthor Commented:
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);


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.

I ask about page flipping because i've used several different techniques for accomplishing doublebuffering... and one of them is done as follows:

get the address of video memory page a
get the address of video memory page b

draw to page a
set the video controller to render on screen what's in page a
draw to page b
set the video controller to render on screen what's in page b
draw to page a

I can see the requirement for the memory alignment; that's not uncommon at all....

however, you haven't mentioned anywhere, how do you "blit" the contents of a buffer to video memory...

Do you have the concept (i think you do... i used to own an amiga around this same time frame) of a  "blitter" or a BLock Image Transfer of memory?

If so, this is how you do page flipping...

Allocate memory for Buffer A
Allocate memory for Buffer B
draw to buffer A using your existing draw routine... the difference will be in your main.. instead of passing "base" to draw, you'll pass "bufferA".
BLIT bufferA to base
draw to buffer B using the draw routine....
BLIT buffer B to base

repeat ad naseaum....


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.