33
DirectDraw Technology CML Training Course 2001/07/24 Alex Ma

DirectDraw Technology CML Training Course 2001/07/24 Alex Ma

Embed Size (px)

Citation preview

DirectDraw Technology

CML Training Course2001/07/24

Alex Ma

Outline

• DirectDraw Introduction• DirectDraw Functionality

– Setup– Surface– Rendering– Blitting– Flipping– Overlay

DirectDraw Introduction

What’s 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

Application

Graphics Device Interface(GDI)

Display Driver Interface(DDI)

Display System

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

Application

Graphics Device Interface(GDI)

Display Driver Interface(DDI)

Display System

HardwareEmulation

Layer(HEL)

HardwareAbstractLayer(HAL)

DirectDrawObject

With DirectDraw

• Direct access to the display hardware device with HAL/HEL supports

• DirectDraw is not much device-independent as GDI does

• HEL can’t 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

LPDIRECTDRAW pDDraw ;

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{

LPVIOD lpSurfaceDWORD dwHeightDWORD dwWidthLONG lPitchDDPIXELFORMAT ddpfPixelFormat…

} ;

Surface

• Create surface

LPDIRECTDRAWSURFACE pDDrawSurface ;DDSURFACEDESC ddsd ;

pDDraw3->CreateSurface(&ddsd, pDDrawSurface) ;

Surface• Primary surface always exists, so do n

ot 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 colorWORD *vmem = (WORD *)ddsd.lpSurfacefor(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 testsTextOut(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)Surface #1

Surface #2

Surface #3

PrimarySurface

Blitter

Biltter Queue

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

DDBLTFX ddbltfx ;memset(&ddbltfx, 0, sizeof(DDBLTFX)) ;ddbltfx.dwSize = sizeof(DDBLTFX) ;ddbltfx.dwDDFX = DDBLTFX_MIRRORLEFTRIGHT | DD

BLTFX_MIRRORUPDOWN ;pDDrawSurface->Blt(NULL, pDDrawSource, NULL, DD

BLT_DDFX, &ddbltfx) ;

Flipping

• Tearing problem

Flipping

• Using flipping

pPrimarySurface->GetAttachedSurface(&pBackSurface) ;

// Rendering on the back surfacepBackSurface->Blt(&destRect, pDDrawSource, &srcRec

t, 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