DirectDraw Technology CML Training Course 2001/07/24 Alex Ma
Outline DirectDraw Introduction DirectDraw Functionality –Setup –Surface –Rendering –Blitting –Flipping –Overlay
DirectDraw Introduction
Whats DirectDraw For Basic of the DirectX graphics Provide several simple methods to manipulate the display system
Before DirectDraw Windows Graphics Device Interface (GDI) –Font, pen, brush, basic shapes –Bitmap and more raster functions –Device independent Display Driver Interface (DDI)
Before DirectDraw
With DirectDraw Hardware Abstraction Layer (HAL) –Corresponds to the display system –Exercises hardware supports Hardware Emulation Layer (HEL) –Software emulation or hardware independent methods for specific functionalities
With DirectDraw
Direct access to the display hardware device with HAL/HEL supports DirectDraw is not much device- independent as GDI does HEL cant emulate everything
DirectDraw Functionality
Setup Enumerate DirectDraw devices BOOL WINAPI EnumDDrawDevice(GUID FAR *lpGUID, LPSTR lpDriverDescription,LPSTR lpDriverName, LPVOID lpContext) DirectDrawEnumerate(EnumDDrawDevice, (LPVOID)GetDlgItem(hWnd, IDC_DEVICE)) ;
Setup Create DirectDraw object LPDIRECTDRAWpDDraw ; DirectDrawCreate(&pDDraw) ;
Setup Set cooperative level pDDraw->SetCooperativeLevel(hWnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_NOWINDOWCHANGES) ;
Setup Enumerate video display mode BOOL WINAPI EnumDisplayModes(LPDDSURFACEDESC lpDDSurfaceDesc, LPVOID lpContext) pDDraw3->EnumDisplayModes(0, NULL, (LPVOID)GetDlgItem(hWnd, IDC_MODES), (LPDDENUMMODESCALLBACK)EnumDisplayModes) ;
Setup Set video display mode pDDraw->SetDisplayMode(Width,Height,PixelFormat) ;
Surface A memory buffer managed as a rectangle Surface type –Primary (display) –Off-screen –Overlay –Z-buffer and more
Surface DirectDraw surface descriptor typedef DDSURFACEDESC { LPVIODlpSurface DWORDdwHeight DWORDdwWidth LONGlPitch DDPIXELFORMATddpfPixelFormat … } ;
Surface Create surface LPDIRECTDRAWSURFACEpDDrawSurface ; DDSURFACEDESCddsd ; pDDraw3->CreateSurface(&ddsd, pDDrawSurface) ;
Surface Primary surface always exists, so do not assign size or format when calling CreateSurface to get primary surface pointer Use a large surface instead of several small surfaces for better memory management
Rendering Direct memory access // Fill the screen with white color WORD *vmem = (WORD *)ddsd.lpSurface for(DWORD y=0; y<ddsd.dwHeight; y++) { for(DWORD x=0; x<ddsd.dwWidth; x++) vmem[x] = 0xffff ; vmem += ddsd.lPitch/sizeof(WORD) ; }
Rendering Using GDI pDDrawSurface->GetDC(&hDC) ; // GDI functionality tests TextOut(hDC, 320, 0, msg, strlen(msg)) ; Ellipse(hDC, 300, 125, 400, 250) ; pDDrawSurface->ReleaseDC(hDC) ;
Rendering Access control pDDrawSurface->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL) ; // Rendering pDDrawSurface->UnLock(NULL) ;
Blitting Bit block transfer (Blit)
Blitting Using blitting pDDrawSurface->Blt(&destRect, pDDrawSource, &srcRect, DDBLT_WAIT, NULL) ; pDDrawSurface->BltFast(200, 240, pDDrawSource, NULL, DDBLTFAST_WAIT) ;
Blitting BltFast is a little bit fast than Blt when using HEL. If have hardware support, then there is no difference between them
Blitting Special effects –Filling –Transparency –Scaling –Mirroring –Rotation Use DDBLTFX structure to control
Blitting Example - mirroring DDBLTFXddbltfx ; memset(&ddbltfx, 0, sizeof(DDBLTFX)) ; ddbltfx.dwSize = sizeof(DDBLTFX) ; ddbltfx.dwDDFX = DDBLTFX_MIRRORLEFTRIGHT | DDBLTFX_MIRRORUPDOWN ; pDDrawSurface->Blt(NULL, pDDrawSource, NULL, DDBLT_DDFX, &ddbltfx) ;
Flipping Tearing problem
Flipping Using flipping pPrimarySurface->GetAttachedSurface(&pBackSurface) ; // Rendering on the back surface pBackSurface->Blt(&destRect, pDDrawSource, &srcRect, DDBLT_WAIT, NULL) ; pPrimarySurface->Flip(DDFLIP_WAIT) ;
Overlay Display a surface without changing the image data in the primary surface
Overlay Using overlay pDDraw3->CreateSurface(&ddsd, &pOverlaySurface) ; // Rendering pOverlaySurface->UpdateOverlay(&srcRect, pPrimarySurface, &destRect, DDOVER_SHOW) ;
Overlay Show/Hide overlay pOverlaySurface->UpdateOverlay(&srcRect, pPrimarySurface, &destRect, DDOVER_SHOW) ; pOverlaySurface->UpdateOverlay(NULL, pPrimarySurface, NULL, DDOVER_HIDE) ;
Discuss and Q&A Time