Unformatted text preview:

DirectDraw and Bitmaps Part 1 CIS 487 587 Bruce R Maxim UM Dearborn 1 Screen Basics A standard 640x480x8 screen buffer has 307 200 pixels Without 3D acceleration hardware software based rasterization is not practical for more than 256 colors For bitmap work this is 3D acceleration is a little less critical 2 16 Bit Encoding There are several 16 bit high color pixel encoding schemes Alpha 5 5 5 X 5 5 5 5 6 5 most common Getting the pixel format right before plotting is important You can choose to set it to 5 6 5 yourself if you wish 3 Writing a Screen Pixel Lock the surface using Lock Build the 16 bit RGB word using the macro RGB16BIT5 6 5 Write the pixel using a USHORT pointer into VRAM Unlock the primary surface using unlock 4 LaMothe Examples 5 Ddraw 16 bit Pixel Plotting done as global declarations builds a 16 bit color value in 5 6 5 format green dominate mode define RGB16BIT565 r g b b 31 g 63 5 r 31 11 initializes a direct draw struct define DDRAW INIT STRUCT ddstruct memset ddstruct 0 sizeof ddstruct ddstruct dwSize sizeof ddstruct inline void Plot Pixel Faster16 int x int y int red int green int blue USHORT video buffer int lpitch16 plots a pixel in 16 bit color mode assumes caller already locked the surfaceand is sending a pointer and byte pitch to it USHORT pixel RGB16BIT565 red green blue first build up color WORD video buffer x y lpitch16 pixel write the data end Plot Pixel Faster16 6 Game Main clear ddsd and set size never assume it s clean DDRAW INIT STRUCT ddsd lock the primary surface if FAILED lpddsprimary Lock NULL ddsd DDLOCK SURFACEMEMORYPTR DDLOCK WAIT NULL return 0 int lpitch16 int ddsd lPitch 1 USHORT video buffer USHORT ddsd lpSurface plot the pixel once position and color is set Plot Pixel Faster16 x y red green blue video buffer lpitch16 now unlock the primary surface if FAILED lpddsprimary Unlock NULL return 0 7 Game Init create IDirectDraw interface 7 0 object and test for error if FAILED DirectDrawCreateEx NULL void lpdd IID IDirectDraw7 NULL return 0 set cooperation to full screen if FAILED lpdd SetCooperativeLevel main window handle DDSCL FULLSCREEN DDSCL ALLOWMODEX DDSCL EXCLUSIVE DDSCL ALLOWREBOOT return 0 set display mode to 640x480x16 if FAILED lpdd SetDisplayMode SCREEN WIDTH SCREEN HEIGHT SCREEN BPP 0 0 return 0 clear ddsd and set size memset ddsd 0 sizeof ddsd ddsd dwSize sizeof ddsd ddsd dwFlags DDSD CAPS enable valid fields ddsd ddsCaps dwCaps DDSCAPS PRIMARYSURFACE request primary surface create the primary surface if FAILED lpdd CreateSurface ddsd lpddsprimary NULL return 0 8 Game Shutdown blow away the primary surface if lpddsprimary lpddsprimary Release lpddsprimary NULL end if blow away the IDirectDraw interface if lpdd lpdd Release lpdd NULL end if 9 Plotting 24 Bit Pixels inline void Plot Pixel 24 int x int y int red int green int blue UCHAR video buffer int lpitch function plots pixel in 24 bit color mode assumes caller locked surface and is sending a pointer and byte pitch to it in byte or 8 bit math the proper address is 3 x y lpitch this is the address of low order byte which is the Blue channel since data is in RGB order DWORD pixel addr x x x y lpitch write the data first blue video buffer pixel addr blue now red video buffer pixel addr 1 green finally green video buffer pixel addr 2 red end Plot Pixel 24 10 Plotting 32 Bit Pixels builds a 32 bit color value in A 8 8 8 format 8 bit alpha mode define RGB32BIT a r g b b g 8 r 16 a 24 inline void Plot Pixel 32 int x int y int alpha int red int green int blue UINT video buffer int lpitch32 this function plots a pixel in 32 bit color mode assuming that the caller already locked the surface and is sending a pointer and DWORD aligned pitch to it first build up color WORD UINT pixel RGB32BIT alpha red green blue write the data video buffer x y lpitch32 pixel end Plot Pixel 32 11 Animation and Bitmaps DirectDraw gives you two choices for doing continuous animation Page flipping using primary and secondary surfaces Double buffering using system memory for back buffer and programmer to flips the pages In both cases DirectDraw does the hard work of page flipping regardless of whether VRAM is linear or non linear 12 Overview of Double Buffering Game Init new is used to allocate double buffer Game Sutdown Double buffer must be freed up Game Main Double buffer is erased and 5000 pixels are drawn offline Check is made for linear memory and then buffer is copied to screen memory 13 Game Init once the primary surface cooperation display mode and palette are defined allocate double buffer if double buffer new UCHAR SCREEN WIDTH SCREEN HEIGHT NULL return 0 14 Game Shutdown blow away the palette surface and direct draw interface first release the memory used for double buffer if double buffer delete double buffer double buffer NULL end if 15 Game Main UCHAR primary buffer NULL alias to primary surface buffer erase double buffer memset void double buffer 0 SCREEN WIDTH SCREEN HEIGHT double buffer x y SCREEN WIDTH col plot at least one pixel copy the double buffer into the primary buffer DDRAW INIT STRUCT ddsd lock the primary surface lpddsprimary Lock NULL ddsd DDLOCK SURFACEMEMORYPTR DDLOCK WAIT NULL get video pointer to primary surfce primary buffer UCHAR ddsd lpSurface 16 Game Main test if memory is linear if ddsd lPitch SCREEN WIDTH copy memory from double buffer to primary buffer memcpy void primary buffer void double buffer SCREEN WIDTH SCREEN HEIGHT end if else non linear make copy of source and destination addresses UCHAR dest ptr primary buffer UCHAR src ptr double buffer 17 Game Main memory is non linear copy line by line for int y 0 y SCREEN HEIGHT y copy line memcpy void dest ptr void src ptr SCREEN WIDTH advance pointers to next line dest ptr ddsd lPitch src ptr SCREEN WIDTH end for end else now unlock the primary surface if FAILED lpddsprimary Unlock NULL return 0 18 Back Buffer Steps 1 Allocate memory for back buffer with same pixel dimensions as primary 2 In main loop erase back buffer 3 Perform game logic 4 Render the next frame in the back buffer 5 Copy the back buffer to the primary buffer surface 6 Synchronize display to desired frame rate 7 Go to step 2 19


View Full Document

U-M CIS 587 - DirectDraw and Bitmaps Part 1

Documents in this Course
War Man

War Man

9 pages

BOOM BLOX

BOOM BLOX

13 pages

Barnes

Barnes

13 pages

Overview

Overview

10 pages

HALO 2

HALO 2

11 pages

Load more
Loading Unlocking...
Login

Join to view DirectDraw and Bitmaps Part 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view DirectDraw and Bitmaps Part 1 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?