您的位置:首页 > 其它

一个不完整的总结性程序

2012-07-17 16:06 204 查看
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

// create IDirectDraw interface 7.0 object and test for error 创建DirectDraw主对象

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)))

{

// error

return(0);

} // end if

// set display mode to 640x480x8

if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,0,0)))

{

// error

return(0);

} // end if

// clear ddsd and set size

memset(&ddsd,0,sizeof(ddsd));

ddsd.dwSize = sizeof(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)))

{

// error

return(0);

} // end if

// build up the palette data array 初始化调色板

for (int color=1; color < 255; color++)

{

// fill with random RGB values

palette[color].peRed = rand()%256;

palette[color].peGreen = rand()%256;

palette[color].peBlue = rand()%256;

// set flags field to PC_NOCOLLAPSE

palette[color].peFlags = PC_NOCOLLAPSE;

} // end for color

// now fill in entry 0 and 255 with black and white

palette[0].peRed = 0;

palette[0].peGreen = 0;

palette[0].peBlue = 0;

palette[0].peFlags = PC_NOCOLLAPSE;

palette[255].peRed = 255;

palette[255].peGreen = 255;

palette[255].peBlue = 255;

palette[255].peFlags = PC_NOCOLLAPSE;

// create the palette object

if (FAILED(lpdd->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 |

DDPCAPS_INITIALIZE,

palette,&lpddpal, NULL)))

{

// error

return(0);

} // end if

// finally attach the palette to the primary surface 调色板与主表面关联

if (FAILED(lpddsprimary->SetPalette(lpddpal)))

{

// error

return(0);

} // end if

// return success or failure or your own return code here

return(1);

} // end Game_Init

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int Game_Main(void *parms = NULL, int num_parms = 0)

{

// this is the main loop of the game, do all your processing

// here

// for now test if user is hitting ESC and send WM_CLOSE

if (KEYDOWN(VK_ESCAPE))

SendMessage(main_window_handle,WM_CLOSE,0,0);

// plot 1000 random pixels to the primary surface and return

// clear ddsd and set size, never assume it's clean

memset(&ddsd,0,sizeof(ddsd));

ddsd.dwSize = sizeof(ddsd);

/////获取对内存的控制权限

if (FAILED(lpddsprimary->Lock(NULL, &ddsd,

DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,

NULL)))

{

// error

return(0);

} // end if

// now ddsd.lPitch is valid and so is ddsd.lpSurface

// make a couple aliases to make code cleaner, so we don't

// have to cast

int mempitch = (int)ddsd.lPitch;

UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;

// plot 1000 random pixels with random colors on the

// primary surface, they will be instantly visible

for (int index=0; index < 1000; index++) //////绘制像素

{

// select random position and color for 640x480x8

UCHAR color = rand()%256;

int x = rand()%640;

int y = rand()%480;

// plot the pixel

video_buffer[x+y*mempitch] = color; //////说明color是系统硬件那个调色板的索引

} // end for index

// now unlock the primary surface

if (FAILED(lpddsprimary->Unlock(NULL)))

return(0);

// sleep a bit

Sleep(30);

// return success or failure or your own return code here

return(1);

} // end Game_Main

////////////////////////////////////////////////////////////清理原则:后创建的指针先释放

int Game_Shutdown(void *parms = NULL, int num_parms = 0)

{

// this is called after the game is exited and the main event

// loop while is exited, do all you cleanup and shutdown here

// first the palette

if (lpddpal)

{

lpddpal->Release();

lpddpal = NULL;

} // end if

// now the primary surface

if (lpddsprimary)

{

lpddsprimary->Release();

lpddsprimary = NULL;

} // end if

// now blow away the IDirectDraw4 interface

if (lpdd)

{

lpdd->Release();

lpdd = NULL;

} // end if

// return success or failure or your own return code here

return(1);

} // end Game_Shutdown
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: