Click here to Skip to main content
Licence 
First Posted 23 Sep 2001
Views 144,604
Downloads 4,259
Bookmarked 64 times

Double Buffered Graphics using DirectDraw

By Gertruud Lawrence | 23 Sep 2001
This article explains how my class CDXSurfaceMgr can be used to facilitate Double Buffered drawing.

1

2
1 vote, 11.1%
3
3 votes, 33.3%
4
5 votes, 55.6%
5
4.43/5 - 17 votes
μ 4.43, σa 1.27 [?]

Sample Image - DXSurfaceMgr.gif

Introduction

This article explains how my class CDXSurfaceMgr can be used to facilitate Double Buffered drawing/graphics.

Double Buffered Drawing

Normally when drawing using the GDI you do all your painting straight into the HDC that's provided by either BeginPaint or a CDC (if you are using MFC). Iif you draw a line it immediately appears on the surface of the window you are drawing on. If you draw a lot of graphic items the screen is updated with each item and this can result in flicker or stuttering as the surface is filled with the drawing. Double Buffering can be used to create smooth updates, you build a "frame" of graphics in a non visible place first then copy the whole thing onto the main windows surface.

There a lots of ways to implement double buffering, one way is to create a compatible HDC in memory somewhere and draw into that then Blit (copy) that memory into the visible windows HDC. My approach uses a subset of DirectX called DirectDraw which can be used to draw 2d Graphics. CDXSurfaceMgr is written using the DirectDraw interfaces supplied in VC6 (ddraw.h/lib) and should work on 95/98/Me/2k/XP and NT 4.0 (sp3 - I think).

Using CDXSurfaceMgr

Create an instance of CDXSurfaceMgr

#include "DXSurfaceMgr.h"
...
...
CDXSurfaceManager_NBP dxMgr_;
Initialise the instance like so
int CMfcdxView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (CView::OnCreate(lpCreateStruct) == -1)
         return -1;
 // Initialise CDXSurfaceMgr
 if(!dxMgr_.Initialise(m_hWnd)) return -1;

 return 0;
}
This should be done just once, the parameter to the Initialise method is the Handle to the window you want to draw on. Then when handling the WM_PAINT message (the place you would normally do the drawing) call the BeginPaint Method
void CMfcdxView::OnDraw(CDC* pDC)
{
 CMfcdxDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here

 HDC& hdc = dxMgr_.BeginPaint(GetSysColor(COLOR_3DFACE));
 if(NULL !=hdc)
 {
 ....

The parameter to BeginPaint is a COLORREF that BeginPaint will use to fill the background with. BeginPaint returns an HDC that that you can then use to draw things with, either via the standard GDI API;

 ::SetBkMode(hdc,TRANSPARENT);
 ::TextOut(hdc,10,10,"Please don't call me reg its not my name", 40);
Or if you are using MFC;
 CDC* cdc = CDC::FromHandle(hdc);
 cdc->SetBkMode(TRANSPARENT);
 cdc->(10,10, CString("All aboard Brendas iron sledge"));

Note all the usual GDI rules apply - if you select a brush remember to reselect the old one etc. When you have finished drawing, call the EndPaint method like so;

dxMgr_.EndPaint();

This will "flip" your drawing onto the main windows surface. and the drawing will instantaneously appear.

What CDXSurfaceMgr does

CDXSurfaceMgr creates a primary surface - one that is visible - and a secondary off screen surface that is the same size as the primary one but invisible. When you call BeginPaint an HDC attached to the secondary surface is returned; you do all your drawing into this; then EndPaint Blits(copies) the whole of the secondary surface onto the primary one and it appears on the window. If you have a capable graphics adapter on your machine then the Blit is very very fast, if you don't then the operation is obviously not as fast, but still very functional.

Demos

There are three demos included one that uses WFC and draws rectangles and ellipses moving up and down the screen, one that uses MFC, and draws a few lines and a bit of text, and one that does exactly the same again but is just a Win32 program.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Gertruud Lawrence



United Kingdom United Kingdom

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCDialog problem PinmemberPeterHo38617:23 18 Oct '11  
GeneralAlpha support Pinmembernesculcas7:16 10 Feb '07  
Can someone explain to me why the alpha (window transparency) is not working with this code.
 
I realy don't understand anything about directdraw, but my guess is that the Blt(...) function is doing something wrong. I'm saying this because if this function is not called then the alpha in the window works, my guess is that the second surface does not have the alpha information that exists in the first surface, and when the Blt(...) function is called then the alpha information of the first surface is override by the erroneous alpha information of the second surface.
 
Anyone knowns any solution to do it?
 

 
Thanks
Nuno
GeneralWorking sample Pinmembernesculcas14:40 8 Feb '07  
GeneralRe: Working sample Pinmembernesculcas5:49 10 Feb '07  
GeneralRe: Working sample Pinmembernesculcas7:17 10 Feb '07  
GeneralUuups, did you ever try... Pinmembera coder21:23 7 Apr '05  
GeneralRe: Uuups, did you ever try... Pinmemberx-plorer22:39 7 Apr '05  
Generalhuge memory leaks ! PinmembercnkKlau1:26 10 Mar '05  
Generalpull-down menue updates PinsussSagi dror1:33 10 May '04  
GeneralUse in Dialog bases app PinmemberRuination13:54 28 Aug '03  
GeneralCDXSurfaceMgr stops working after screen saver has been activated PinmemberJohanps4:22 17 Jun '03  
GeneralRe: CDXSurfaceMgr stops working after screen saver has been activated Pinmembershorer1:46 30 Aug '09  
QuestionWhy here not Visual Basic? Pinsussniekass3:06 12 Jun '03  
QuestionWhy here not Visual Basic? Pinmemberniekasss2:40 12 Jun '03  
AnswerRe: Why here not Visual Basic? PinmemberTwink14:57 9 Jan '05  
GeneralSetDIBitsToDevice PinmemberAnonymous23:19 1 Apr '02  
GeneralThanks so much Pinmemberackka11:11 7 Jan '02  
GeneralTheres a leak in my code Pinmembergerty13:08 29 Sep '01  
GeneralGreat code... and a question PinmemberMatt Ashland16:38 25 Sep '01  
GeneralRe: Great code... and a question Pinmembergerty12:41 29 Sep '01  
GeneralRe: Thanks... and a benchmark PinmemberMatt Ashland4:26 1 Oct '01  
Generalatlres.h missing PinmemberFriedhelm Schuetz5:33 25 Sep '01  
GeneralRe: atlres.h missing PinmemberMatt Ashland18:09 25 Sep '01  
GeneralRe: atlres.h missing Pinmembergerty12:44 29 Sep '01  
GeneralInteresting... PinmemberJ.G. Hattingh3:47 25 Sep '01  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 24 Sep 2001
Article Copyright 2001 by Gertruud Lawrence
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid