// FILE: PIXEL.CPP // Provides 16 Bit Put Pixel, Locking, and Unlocking Methods. // By Sam Christiansen (schristi@cs.utexas.edu) #include "pixel.h" static BOOL locked = FALSE; static LPVOID lockedSurf = NULL; static int lockedWidth = 0; static int lockedHeight = 0; //Must run this proc to fill the RGB16 struct with the information needed to plot a pixel BOOL GetRGB16 ( LPDIRECTDRAWSURFACE Surface, RGB16 *rgb16) { DDSURFACEDESC ddsd; BYTE shiftcount; //get a surface description ddsd.dwSize = sizeof( ddsd ); ddsd.dwFlags = DDSD_PIXELFORMAT; if (Surface->GetSurfaceDesc ( &ddsd ) != DD_OK ) return FALSE; //get red shiftcount = 0; char rc=0, gc=0, bc=0; for(int n = 1; n<65536; n<<=1) { if(ddsd.ddpfPixelFormat.dwRBitMask & n) ++rc; if(ddsd.ddpfPixelFormat.dwGBitMask & n) ++gc; if(ddsd.ddpfPixelFormat.dwBBitMask & n) ++bc; } rgb16->dwRBitMask = ddsd.ddpfPixelFormat.dwRBitMask; rgb16->Position.rgbRed = gc + bc; rgb16->dwGBitMask = ddsd.ddpfPixelFormat.dwGBitMask; rgb16->Position.rgbGreen = bc; rgb16->dwBBitMask = ddsd.ddpfPixelFormat.dwBBitMask; rgb16->Position.rgbBlue = 0; return TRUE; }//GetRBG16 // Must have the surface locked, pass the surface description // that gets returned from the lock call void PlotPixel(int x, int y, int r, int g, int b, DDSURFACEDESC *ddsd, RGB16 *rgb16) { WORD Pixel; Pixel = (r << rgb16->Position.rgbRed) | (g << rgb16->Position.rgbGreen) | (b);// << rgb16->Position.rgbBlue); WORD *pixels = (WORD *)ddsd->lpSurface; DWORD pitch = ddsd->dwWidth; pixels[y*pitch + x] = Pixel; } // lock direct draw surface BOOL Lock(DDSURFACEDESC* ddsd) { HRESULT ddrval; // VERY IMPORTANT - the next two steps are crucial! memset( ddsd, 0, sizeof( *ddsd )); ddsd->dwSize = sizeof( *ddsd ); // attempt to Lock the surface ddrval = g_lpDDSBack->Lock(NULL, ddsd, DDLOCK_WAIT, NULL); // Always, always check for errors with DirectX! if( ddrval == DD_OK ) { locked = TRUE; lockedSurf = ddsd->lpSurface; lockedWidth = ddsd->lPitch; lockedHeight = ddsd->dwHeight; } else { locked = FALSE; lockedSurf = NULL; lockedWidth = 0; return FALSE; } return TRUE; } // unlock direct draw surface BOOL UnLock(void) { if (DD_OK != g_lpDDSBack->Unlock(lockedSurf)) return FALSE; locked = FALSE; lockedSurf = NULL; lockedWidth = 0; lockedHeight = 0; return TRUE; }