DOC PREVIEW
U-M CIS 587 - DirectDraw and Bitmaps Part 1

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

DirectDraw and Bitmaps Part 1Screen Basics16 Bit EncodingWriting a Screen PixelLaMothe ExamplesDdraw 16-bit Pixel PlottingGame_Main( )Game_Init( )Game_Shutdown( )Plotting 24 Bit PixelsPlotting 32 Bit PixelsAnimation and BitmapsOverview of Double BufferingSlide 14Slide 15Slide 16Slide 17Slide 18Back Buffer Steps1DirectDraw and BitmapsPart 1CIS 487/587Bruce R. MaximUM-Dearborn2Screen 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 critical316 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 wish4Writing 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 unlock5LaMothe Examples6Ddraw 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 itUSHORT pixel = _RGB16BIT565(red,green,blue); // first build up color WORDvideo_buffer[x + y*lpitch16] = pixel; // write the data} // end Plot_Pixel_Faster167Game_Main( )// clear ddsd and set size, never assume it's cleanDDRAW_INIT_STRUCT(ddsd); // lock the primary surfaceif (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 setPlot_Pixel_Faster16(x,y,red,green,blue,video_buffer,lpitch16);// now unlock the primary surfaceif (FAILED(lpddsprimary->Unlock(NULL))) return(0);8Game_Init( )// create IDirectDraw interface 7.0 object and test for errorif (FAILED(DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL))) return(0);// set cooperation to full screenif (FAILED(lpdd->SetCooperativeLevel(main_window_handle, DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT))) return(0);// set display mode to 640x480x16if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,0,0))) return(0);// clear ddsd and set sizememset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd);ddsd.dwFlags = DDSD_CAPS; // enable valid fieldsddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; // request primary surface// create the primary surfaceif (FAILED(lpdd->CreateSurface(&ddsd, &lpddsprimary, NULL))) return(0);9Game_Shutdown( )// blow away the primary surfaceif (lpddsprimary){ lpddsprimary->Release(); lpddsprimary = NULL;} // end if // blow away the IDirectDraw interfaceif (lpdd){ lpdd->Release(); lpdd = NULL;} // end if10Plotting 24 Bit Pixelsinline 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 orderDWORD pixel_addr = (x+x+x) + y*lpitch;// write the data, first bluevideo_buffer[pixel_addr] = blue;// now redvideo_buffer[pixel_addr+1] = green;// finally greenvideo_buffer[pixel_addr+2] = red;} // end Plot_Pixel_2411Plotting 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 WORDUINT pixel = _RGB32BIT(alpha,red,green,blue);// write the datavideo_buffer[x + y*lpitch32] = pixel;} // end Plot_Pixel_3212Animation 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)13Overview 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 memory14Game_Init( )// once the primary surface, cooperation, display mode, and palette// are defined// allocate double bufferif ((double_buffer=new UCHAR[SCREEN_WIDTH * SCREEN_HEIGHT])==NULL) return(0);15Game_Shutdown( )// blow away the palette, surface, and direct draw interface first// release the memory used for double bufferif (double_buffer){ delete double_buffer; double_buffer = NULL;} // end if16Game_Main( )UCHAR *primary_buffer = NULL; // alias to primary surface buffer// erase double buffermemset((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 bufferDDRAW_INIT_STRUCT(ddsd);// lock the primary surfacelpddsprimary->Lock(NULL,&ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL); // get video pointer to primary surfceprimary_buffer = (UCHAR *)ddsd.lpSurface;17Game_Main( )// test if memory is linearif (ddsd.lPitch == SCREEN_WIDTH){ // copy memory from double buffer to primary buffer memcpy((void *)primary_buffer, (void *)double_buffer, SCREEN_WIDTH*SCREEN_HEIGHT);} // end ifelse{ // non-linear // make copy of source and destination addresses UCHAR *dest_ptr =


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
Download DirectDraw and Bitmaps Part 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?