|
|
是《windows游戏编程大师技巧》上的一个例子,在窗口中绘制象素。其中的一段代码如下:
int Game_Main(void *parms = NULL, int num_parms = 0)
{
// this is the main loop of the game, do all your processing
// here
DDSURFACEDESC2 ddsd; // directdraw surface description
RECT client; // used to hold client rectangle
// make sure this isn't executed again
if (window_closed)
return(0);
// for now test if user is hitting ESC and send WM_CLOSE
if (KEYDOWN(VK_ESCAPE))
{
PostMessage(main_window_handle,WM_CLOSE,0,0);
window_closed = 1;
} // end if
// get the window's client rectangle in screen coordinates
GetWindowRect(main_window_handle, &client);
// initialize structure
DDRAW_INIT_STRUCT(ddsd);
// lock the primary surface
lpddsprimary->Lock(NULL,&ddsd,
DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
// get video pointer to primary surface
// cast to VOID * since we don't know what we are
// dealing with yet
UCHAR *primary_buffer = (UCHAR *)ddsd.lpSurface;
// what is the color depth?
if (pixel_format == 32)
{
// draw 10 random pixels in 32 bit mode
for (int index=0; index<10; index++)
{
int x=rand()%(client.right - client.left) + client.left;
int y=rand()%(client.bottom - client.top) + client.top;
DWORD color = _RGB32BIT(0,rand()%256, rand()%256, rand()%256);
*((DWORD *)(primary_buffer + x*4 + y*ddsd.lPitch)) = color;
} // end for index
} // end if 24 bit
else
if (pixel_format == 24)
{
// draw 10 random pixels in 24 bit mode (very rare???)
for (int index=0; index<10; index++)
{
int x=rand()%(client.right - client.left) + client.left;
int y=rand()%(client.bottom - client.top) + client.top;
((primary_buffer + x*3 + y*ddsd.lPitch))[0] = rand()%256;
((primary_buffer + x*3 + y*ddsd.lPitch))[1] = rand()%256;
((primary_buffer + x*3 + y*ddsd.lPitch))[2] = rand()%256;
} // end for index
} // end if 24 bit
else
if (pixel_format == 16)
{
// draw 10 random pixels in 16 bit mode
for (int index=0; index<10; index++)
{
int x=rand()%(client.right - client.left) + client.left;
int y=rand()%(client.bottom - client.top) + client.top;
USHORT color = _RGB16BIT565(rand()%256, rand()%256, rand()%256);
*((USHORT *)(primary_buffer + x*2 + y*ddsd.lPitch)) = color;
} // end for index
} // end if 16 bit
else
{// assume 8 bits per pixel
// draw 10 random pixels in 8 bit mode
for (int index=0; index<10; index++)
{
int x=rand()%(client.right - client.left) + client.left;
int y=rand()%(client.bottom - client.top) + client.top;
UCHAR color = rand()%256;
primary_buffer[x + y*ddsd.lPitch] = color;
} // end for index
} // end else
// unlock primary buffer
if (FAILED(lpddsprimary->Unlock(NULL)))
return(0);
// wait a sec
Sleep(1);
// return success or failure or your own return code here
return(1);
} // end Game_Main
////////////////////////////////////////////////////////////
int Game_Init(void *parms = NULL, int num_parms = 0)
{
// this is called once after the initial window is created and
// before the main event loop is entered, do all your initialization
// here
DDPIXELFORMAT ddpixelformat; // hold the pixel format
// 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_NORMAL)))
return(0);
// clear ddsd and set size
DDRAW_INIT_STRUCT(ddsd);
// enable valid fields
ddsd.dwFlags = DDSD_CAPS;
// request primary surface
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
// create the primary surface
if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsprimary, NULL)))
return(0);
// get pixel format
// clean out the structure and set it up
DDRAW_INIT_STRUCT(ddpixelformat);
// get the pixel format
lpddsprimary->GetPixelFormat(&ddpixelformat);
// set global pixel format
pixel_format = ddpixelformat.dwRGBBitCount;
// return success or failure or your own return code here
return(1);
} // end Game_Init
请问// get the window's client rectangle in screen coordinates
GetWindowRect(main_window_handle, &client);
是如何将client和DD表面关联的,我换成GetClientRect()却无法做出同样的结果,为什么? |
|