?? outofmouth.cpp
字號:
// clear ddsd and set size
DDRAW_INIT_STRUCT(ddsd);
// enable valid fields
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
// set the backbuffer count field to 1, use 2 for triple buffering
ddsd.dwBackBufferCount = 1;
// request a complex, flippable
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
// create the primary surface
if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsprimary, NULL)))
return(0);
// now query for attached surface from the primary surface
// this line is needed by the call
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
// get the attached back buffer surface
if (FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsback)))
return(0);
// 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)))
return(0);
// finally attach the palette to the primary surface
if (FAILED(lpddsprimary->SetPalette(lpddpal)))
return(0);
// set clipper up on back buffer since that's where well clip
RECT screen_rect= {0,0,SCREEN_WIDTH-1,SCREEN_HEIGHT-1};
lpddclipper = DDraw_Attach_Clipper(lpddsback,1,&screen_rect);
// load the 8-bit image
if (!Load_Bitmap_File(&bitmap,"b1.bmp"))
return(0);
// load it's palette into directdraw
if (FAILED(lpddpal->SetEntries(0,0,MAX_COLORS_PALETTE,bitmap.palette)))
return(0);
// clean the surfaces
DDraw_Fill_Surface(lpddsprimary,0);
DDraw_Fill_Surface(lpddsback,0);
// create the buffer to hold the background
lpddsbackground = DDraw_Create_Surface(1024,768,0,-1);
// copy the background bitmap image to the background surface
// lock the surface
lpddsbackground->Lock(NULL,&ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
// get video pointer to primary surfce
UCHAR *image_buffer = (UCHAR *)ddsd.lpSurface;
// test if memory is linear
if (ddsd.lPitch == SCREEN_WIDTH)
{
// copy memory from double buffer to primary buffer
memcpy((void *)image_buffer, (void *)bitmap.buffer, SCREEN_WIDTH*SCREEN_HEIGHT);
} // end if
else
{ // non-linear
// make copy of source and destination addresses
UCHAR *dest_ptr = image_buffer;
UCHAR *src_ptr = bitmap.buffer;
// 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(lpddsbackground->Unlock(NULL)))
return(0);
// unload the bitmap file, we no longer need it
Unload_Bitmap_File(&bitmap);
// seed random number generator
srand(GetTickCount());
// initialize all the aliens
// alien on level 1 of complex
aliens[0].x = rand()%SCREEN_WIDTH;
aliens[0].y = 116 - 72;
aliens[0].velocity = 2+rand()%4;
aliens[0].current_frame = 0;
aliens[0].counter = 0;
// alien on level 2 of complex
aliens[1].x = rand()%SCREEN_WIDTH;
aliens[1].y = 246 - 72;
aliens[1].velocity = 2+rand()%4;
aliens[1].current_frame = 0;
aliens[1].counter = 0;
// alien on level 3 of complex
aliens[2].x = rand()%SCREEN_WIDTH;
aliens[2].y = 382 - 72;
aliens[2].velocity = 2+rand()%4;
aliens[2].current_frame = 0;
aliens[2].counter = 0;
// now load the bitmap containing the alien imagery
// then scan the images out into the surfaces of alien[0]
// and copy then into the other two, be careful of reference counts!
// load the 8-bit image
if (!Load_Bitmap_File(&bitmap,"d1.bmp"))
return(0);
// create each surface and load bits
for (int index = 0; index < 3; index++)
{
// create surface to hold image
aliens[0].frames[index] = DDraw_Create_Surface(72,80,0);
// now load bits...
Scan_Image_Bitmap(&bitmap, // bitmap file to scan image data from
aliens[0].frames[index], // surface to hold data
index, 0); // cell to scan image from
} // end for index
// unload the bitmap file, we no longer need it
Unload_Bitmap_File(&bitmap);
// now for the tricky part. There is no need to create more surfaces with the same
// data, so I'm going to copy the surface pointers member for member to each alien
// however, be careful, since the reference counts do NOT go up, you still only need
// to release() each surface once!
for (index = 0; index < 3; index++)
aliens[1].frames[index] = aliens[2].frames[index] = aliens[0].frames[index];
//init bow//////////////////////////////////
bow.x=400;
bow.y=350;
bow.current_frame=0;
bow.counter=0;
if (!Load_Bitmap_File(&bitmap,"e8.bmp"))
return(0);
for (index = 0; index < ALL_BOW; index++)
{
// create surface to hold image
bow.frames[index] = DDraw_Create_Surface(BOW_WIDTH,BOW_HEIGHT,0);
if(index>=6)
{
Scan_Image_Bitmap(&bitmap, // bitmap file to scan image data from
bow.frames[index], // surface to hold data
index-6, 1);
}
else if(index>=12)
{
Scan_Image_Bitmap(&bitmap, // bitmap file to scan image data from
bow.frames[index], // surface to hold data
index-12, 2);
}
else
{
// now load bits...
Scan_Image_Bitmap(&bitmap, // bitmap file to scan image data from
bow.frames[index], // surface to hold data
index, 0); // cell to scan image from
}
} // end for index
// unload the bitmap file, we no longer need it
Unload_Bitmap_File(&bitmap);
//init bow2//////////////////////////////////
bow2.x=400;
bow2.y=350;
bow2.current_frame=0;
bow2.counter=0;
if (!Load_Bitmap_File(&bitmap,"eb8.bmp"))
return(0);
for (index = 0; index < ALL_BOW2; index++)
{
// create surface to hold image
bow2.frames[index] = DDraw_Create_Surface(BOW2_WIDTH,BOW2_HEIGHT,0);
if(index>=6)
{
Scan_Image_Bitmap(&bitmap, // bitmap file to scan image data from
bow2.frames[index], // surface to hold data
index-6, 1);
}
else if(index>=12)
{
Scan_Image_Bitmap(&bitmap, // bitmap file to scan image data from
bow2.frames[index], // surface to hold data
index-12, 2);
}
else
{
// now load bits...
Scan_Image_Bitmap(&bitmap, // bitmap file to scan image data from
bow2.frames[index], // surface to hold data
index, 0); // cell to scan image from
}
} // end for index
// unload the bitmap file, we no longer need it
Unload_Bitmap_File(&bitmap);
//load tooth/////////////////////////
if (!Load_Bitmap_File(&bitmap,"tooth.bmp"))
return(0);
for (index = 0; index < ALL_SHAPE_NUM; index++)
{
// create surface to hold image
tooth[index].frames[0] = DDraw_Create_Surface(TOOTH_WIDTH,TOOTH_HEIGHT,0);
// now load bits...
Scan_Image_Bitmap(&bitmap, // bitmap file to scan image data from
tooth[index].frames[0], // surface to hold data
index, 0); // cell to scan image from
} // end for index
// unload the bitmap file, we no longer need it
Unload_Bitmap_File(&bitmap);
//build map//////////
if(!Build_Map())
{
return(0);
}
if (!Load_Bitmap_File(&bitmap,"small.bmp"))
return(0);
for (index = 0; index < 2; index++)
{
// create surface to hold image
smallone.frames[index] = DDraw_Create_Surface(TOOTH_WIDTH,TOOTH_HEIGHT,0);
// now load bits...
Scan_Image_Bitmap(&bitmap, // bitmap file to scan image data from
smallone.frames[index], // surface to hold data
index, 0); // cell to scan image from
} // end for index
// unload the bitmap file, we no longer need it
Unload_Bitmap_File(&bitmap);
// return success or failure or your own return code here
return(1);
} // end Game_Init
/////////////////////////////////////////////////////////////
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
// kill all the surfaces
// 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
// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
HDC hdc; // graphics device context
// first fill in the window class stucture
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// save hinstance in global
hinstance_app = hinstance;
// register the window class
if (!RegisterClassEx(&winclass))
return(0);
// create the window
if (!(hwnd = CreateWindowEx(NULL, // extended style
WINDOW_CLASS_NAME, // class
"OutofMouth Demo", // title
WS_POPUP | WS_VISIBLE,
0,0, // initial x,y
SCREEN_WIDTH,SCREEN_HEIGHT, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL))) // extra creation parms
return(0);
// hide mouse
ShowCursor(FALSE);
// save main window handle
main_window_handle = hwnd;
// initialize game here
Game_Init();
// enter main event loop
while(TRUE)
{
// test if there is a message in queue, if so get it
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// test if this is a quit
if (msg.message == WM_QUIT)
break;
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);
} // end if
// main game processing goes here
Game_Main();
} // end while
// closedown game here
Game_Shutdown();
// show mouse
ShowCursor(TRUE);
// return to Windows like this
return(msg.wParam);
} // end WinMain
///////////////////////////////////////////////////////////
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -