查看文章 |
Direct Draw SurfacesOverviewPerformance ConsiderationsA surface can be stored in video memory or system memory, and Direct Draw gives you the ability to specify where to store a particular surface. Frequently used surfaces should ideally be kept in video memory, because blitting can be performed more quickly if the source exists in video memory. Surfaces that are either too large to fit in video memory or that are infrequently used should be kept in system memory. Also, note that most video chipsets support asynchronous blitting, so you should take advantage of this wherever possible. High-performance video cards support asynchronous blit queues, which further enhance performance by keeping the blitter busy at all times and by lowering CPU utilization. Creating the Primary SurfaceHRESULT CreateSurface( LPDDSURFACEDESC2 lpDDSurfaceDesc2, LPDIRECTDRAWSURFACE4FAR *lplpDDSurface, IUnknownFAR *pUnkOuter ); pUnkOuter must be NULL. The first parameter is a pointer to a structure that describes the surface you want to create, and the second parameter is a pointer to a pointer to a surface that will be created by the CreateSurface() function. If the surface is successfully created, the function return DD_OK. The following example demonstrates how to create a primary surface with a single back-buffer: LPDIRECTDRAWSURFACE4 primarySurface = NULL; DDSURFACEDESC2 primarySurfaceDesc; // zero out the surface description // set the surface description values // create the primary surface At this point, you have created a primary surface that has a single back-buffer. However, you do not have any way of accessing the back-buffer yet. To get a pointer to the back-buffer, call IDirectDrawSurface4::GetAttachedSurface() as shown below: LPDIRECTDRAWSURFACE4 backSurface = NULL; primarySurfaceCaps.dwCaps = DDSCAPS_BACKBUFFER; backSurface now points to the back-buffer. GetAttachedSurface() returns DD_OK if it is successful. Creating Off-Screen SurfacesDDSURFACEDESC2 surfaceDesc; // zero out the surface description // set the surface description values // specify where to create the surface displayDevice->CreateSurface(&surfaceDesc,&offScreenSurface, NULL); Flipping the Primary Surfacebool Flip() primarySurface->Flip(NULL, NULL); Note that this function is asynchronous. It will not wait until the flip operation can be performed before returning. Instead, you must call Flip() until it returns true, indicating a successful primary surface flip. A flip operation will always be synchronized with the video hardware's vertical blank, so if you are using a video mode that has a 70 hz refresh rate, then at most 70 flip operations can occur in one second. Blitting to a SurfaceRestoring Lost Surfaces
Once you have determined that a surface is lost, you can restore the memory that was allocated to a surface with a call to IDirectDrawSurface4::Restore(). This method only reallocates the memory associated with a surface; it does not restore the contents of a surface. Your program must recreate the contents of any lost surfaces on its own. In the games I've helped to develop, my team and I used a "SurfaceManager" class which took care of loading, restoring, reloading, and deleting surfaces as needed. Direct Draw 4 introduced the IDirectDraw4::RestoreAllSurfaces() method, which will restore the memory allocated to all of the surfaces that a program has created. Just like with the Restore() method, your program must explicitely restore the contents of the lost surfaces. A good time to call RestoreAllSurfaces() is after changing the display mode, since some or all of your surfaces will have been toasted by the mode change. GDI CompatibilityDefinitionsBack-Buffer -- An off-screen surface representing the future contents of the display screen that can be prepared before it is shown. Blit -- Short, pronounceable term that stands for block transfer. A block transfer is simply copying one block of memory to another. Double-Buffering -- When the primary surface has only one attached back-buffer. GDI -- An acronym for Graphics Device Interface. Windows applications have historically used the GDI to perform all graphics operations, but Direct Draw has extended the capabilities and performance of Windows graphics. Primary Surface -- A surface that consists of the visible, on-screen surface and zero or more back-buffers. The next back buffer in the circular queue can be made visible by flipping the primary surface. Tearing -- When on-screen images appear to be flashing. A characteristic of a poorly designed animation application. Author: Rodney Myers -- 5/15/99 http://www.mtsu.edu/~csjudy/directX/DirectDraw/Surfaces.html |