Click to See Complete Forum and Search --> : Triple buffering?


Coder Noob
June 29th, 2007, 09:29 PM
Hi. I'm making a 2D map editor and trying to update the window that shows the map. Anyway, the way I have it now works good, but there is a lot of flicker because I clear the screen to a white color and then update it. This is how I do it now.

Pseudocode

begin
Create Device Context
Create Memory Device Context
begin
Clear map window
Select tileset into memory device context
for(short x = 0; x < map width; x++)
begin
for(short y = 0; y < map height; y++)
begin
Update tile[x][y]
end
end
end
release device contexts
end


So as you can see, I use 2 device contexts. Then I use 2 for(;;) loops to update the map 1 tile at a time. This leaves a problem though. Since I am selecting a tileset into the backbuffer and drawing to the main DC, I can't just blit the complete picture to update the map. The tiles need to be drawn 1 at a time.

So I was thinking maybe I use 3 device contexts. Select the tileset into a memory device context, then draw to the other memory device context. Finally, blit everything to the main device context. Could this work? I've tried it before, but I had to replace my old files as it got seriously screwed up. Any source/pseudocode would be appreciated as would other ideas as to how I could handle this. Thank you.

JVene
June 30th, 2007, 10:28 AM
Triple buffering isn't required unless you're doing some kind of alpha blending or other image processing prior to the blit to the display.

I think you're problem is the 'Clear Map Window' step. This hints that you're clearing the display, not the buffer, which would cause the flicker.

When you blit in a double buffering design, you don't bother with the existing material in view, you simply replace it with the blit. That's what eliminates flicker.

VladimirF
June 30th, 2007, 11:05 AM
...So as you can see, I use 2 device contexts. Then I use 2 for(;; ) loops to update the map 1 tile at a time. This leaves a problem though. Since I am selecting a tileset into the backbuffer and drawing to the main DC, I can't just blit the complete picture to update the map. The tiles need to be drawn 1 at a time.
Although you use 2 DCs, it looks like you do NOT do double-buffering.
Your second DC is only used to select tiles into it for blitting to another DC - right?
Double-buffering means that you prepare a COMPLETE content for you screen DC in a secondary buffer, and when you are done - blit it to the screen.
You might need tird (or even more) temporary DCs for composing your secondary DC, but, it won't be "tripple-buffering", and as JVene said - you do not need tripple buffering for this task.

Coder Noob
June 30th, 2007, 01:56 PM
I know that the clear map step is what causes the flicker. If I updated the map the way I am now, but removed the clear map step, there would be no flicker, but the tiles would leave a streak when I scroll the map. That's why I wanted to know if it was possible to use 3 or more device contexts to take care of this problem. From the looks of VladimirF's reply, it is possible, I just wasn't coding it right.

BTW, I wasn't sure if anyone used 3 or more DCs before, so I just called it triple buffering or whatever. I guess a better title for the thread would have been 'Triple blitting' or something to that effect.

Thanks for the replies. I've got some work to do.

JVene
June 30th, 2007, 02:39 PM
but the tiles would leave a streak when I scroll the map


That's an alignment problem, or a coverage problem (bitmap size or placement).