Double Buffering in C

Posted on 2008-11-14
Last Modified: 2012-05-05

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.
Question by:gmacdonald
    LVL 11

    Expert Comment

    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?



    Author Comment

    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.
    LVL 11

    Accepted Solution


    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....


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now