📄 demo9_3_16b.cpp
字号:
// unlock surface
DDraw_Unlock_Back_Surface();
// return success
return(1);
} // end if
// return failure
return(0);
} // end Move_Missle
///////////////////////////////////////////////////////////
int Draw_Missile(void)
{
// this function draws the missile
// test if missile is alive
if (missile_state==1)
{
// lock secondary buffer
DDraw_Lock_Back_Surface();
// draw the missile in green
Draw_Clip_Line16(missile_x, missile_y,
missile_x, missile_y+6,
RGB16Bit(255,255,255),back_buffer, back_lpitch);
// unlock surface
DDraw_Unlock_Back_Surface();
// return success
return(1);
} // end if
// return failure
return(0);
} // end Draw_Missle
///////////////////////////////////////////////////////////
// GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
int Game_Init(void *parms, int num_parms)
{
// this function is where you do all the initialization
// for your game
int index; // looping var
char filename[80]; // used to build up files names
// start up DirectDraw (replace the parms as you desire)
DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
// joystick creation section ////////////////////////////////
// first create the direct input object
if (DirectInput8Create(main_instance,DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&lpdi,NULL)!=DI_OK)
return(0);
// first find the fucking GUID of your particular joystick
lpdi->EnumDevices(DI8DEVCLASS_GAMECTRL,
DI_Enum_Joysticks,
&joystickGUID,
DIEDFL_ATTACHEDONLY);
if (lpdi->CreateDevice(joystickGUID, &lpdijoy, NULL)!=DI_OK)
return(0);
// set cooperation level
if (lpdijoy->SetCooperativeLevel(main_window_handle,
DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK)
return(0);
// set data format
if (lpdijoy->SetDataFormat(&c_dfDIJoystick)!=DI_OK)
return(0);
// set the range of the joystick
DIPROPRANGE joy_axis_range;
// first x axis
joy_axis_range.lMin = -24;
joy_axis_range.lMax = 24;
joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE);
joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
joy_axis_range.diph.dwObj = DIJOFS_X;
joy_axis_range.diph.dwHow = DIPH_BYOFFSET;
lpdijoy->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);
// now y-axis
joy_axis_range.lMin = -24;
joy_axis_range.lMax = 24;
joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE);
joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
joy_axis_range.diph.dwObj = DIJOFS_Y;
joy_axis_range.diph.dwHow = DIPH_BYOFFSET;
lpdijoy->SetProperty(DIPROP_RANGE,&joy_axis_range.diph);
// and now the dead band
DIPROPDWORD dead_band; // here's our property word
dead_band.diph.dwSize = sizeof(dead_band);
dead_band.diph.dwHeaderSize = sizeof(dead_band.diph);
dead_band.diph.dwObj = DIJOFS_X;
dead_band.diph.dwHow = DIPH_BYOFFSET;
// 10% will be used on both sides of the range +/-
dead_band.dwData = 1000;
// finally set the property
lpdijoy->SetProperty(DIPROP_DEADZONE,&dead_band.diph);
dead_band.diph.dwSize = sizeof(dead_band);
dead_band.diph.dwHeaderSize = sizeof(dead_band.diph);
dead_band.diph.dwObj = DIJOFS_Y;
dead_band.diph.dwHow = DIPH_BYOFFSET;
// 10% will be used on both sides of the range +/-
dead_band.dwData = 1000;
// finally set the property
lpdijoy->SetProperty(DIPROP_DEADZONE,&dead_band.diph);
// acquire the joystick
if (lpdijoy->Acquire()!=DI_OK)
return(0);
///////////////////////////////////////////////////////////
// load the background
Load_Bitmap_File(&bitmap16bit, "MUSH_24.BMP");
// load in the four frames of the mushroom
for (index=0; index<4; index++)
{
// create mushroom bitmaps
Create_Bitmap(&mushrooms[index],0,0,32,32,16);
Load_Image_Bitmap16(&mushrooms[index],&bitmap16bit,index,0,BITMAP_EXTRACT_MODE_CELL);
} // end for index
// now create the bug blaster bob
Create_BOB(&blaster,0,0,32,32,3,
BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_ANIM | BOB_ATTR_ANIM_ONE_SHOT,
DDSCAPS_SYSTEMMEMORY,0,16);
// load in the four frames of the mushroom
for (index=0; index < 3; index++)
Load_Frame_BOB16(&blaster,&bitmap16bit,index,index,1,BITMAP_EXTRACT_MODE_CELL);
// unload the bitmap file
Unload_Bitmap_File(&bitmap16bit);
// set the animation sequences for bug blaster
Load_Animation_BOB(&blaster,0,5,blaster_anim);
// set up stating state of bug blaster
Set_Pos_BOB(&blaster,320, 400);
Set_Anim_Speed_BOB(&blaster,3);
// create mushroom playfield bitmap
Create_Bitmap(&playfield,0,0,SCREEN_WIDTH,SCREEN_HEIGHT, 16);
playfield.attr |= BITMAP_ATTR_LOADED;
// fill in the background
Load_Bitmap_File(&bitmap16bit, "GRASS_24.BMP");
// load the grass bitmap image
Load_Image_Bitmap16(&playfield,&bitmap16bit,0,0,BITMAP_EXTRACT_MODE_ABS);
Unload_Bitmap_File(&bitmap16bit);
// seed random number generator
srand(Start_Clock());
// create the random mushroom patch
for (index=0; index<50; index++)
{
// select a mushroom
int mush = rand()%4;
// set mushroom to random position
mushrooms[mush].x = rand()%(SCREEN_WIDTH-32);
mushrooms[mush].y = rand()%(SCREEN_HEIGHT-128);
// now draw the mushroom into playfield
Draw_Bitmap16(&mushrooms[mush], playfield.buffer, playfield.width*2,1);
} // end for
// hide the mouse
if (!WINDOWED_APP)
ShowCursor(FALSE);
// return success
return(1);
} // end Game_Init
///////////////////////////////////////////////////////////
int Game_Shutdown(void *parms, int num_parms)
{
// this function is where you shutdown your game and
// release all resources that you allocated
// kill the bug blaster
Destroy_BOB(&blaster);
// kill the mushroom maker
for (int index=0; index<4; index++)
Destroy_Bitmap(&mushrooms[index]);
// kill the playfield bitmap
Destroy_Bitmap(&playfield);
// release joystick
lpdijoy->Unacquire();
lpdijoy->Release();
lpdi->Release();
// shutdonw directdraw
DDraw_Shutdown();
// return success
return(1);
} // end Game_Shutdown
///////////////////////////////////////////////////////////
int Game_Main(void *parms, int num_parms)
{
// this is the workhorse of your game it will be called
// continuously in real-time this is like main() in C
// all the calls for you game go here!
int index; // looping var
int dx,dy; // general deltas used in collision detection
// check of user is trying to exit
if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE))
PostMessage(main_window_handle, WM_DESTROY,0,0);
// start the timing clock
Start_Clock();
// clear the drawing surface
DDraw_Fill_Surface(lpddsback, 0);
// get joystick data
lpdijoy->Poll(); // this is needed for joysticks only
lpdijoy->GetDeviceState(sizeof(DIJOYSTATE), (LPVOID)&joy_state);
// lock the back buffer
DDraw_Lock_Back_Surface();
// draw the background reactor image
Draw_Bitmap16(&playfield, back_buffer, back_lpitch, 0);
// unlock the back buffer
DDraw_Unlock_Back_Surface();
// is the player moving?
blaster.x+=joy_state.lX;
blaster.y+=joy_state.lY;
// test bounds
if (blaster.x > SCREEN_WIDTH-32)
blaster.x = SCREEN_WIDTH-32;
else
if (blaster.x < 0)
blaster.x = 0;
if (blaster.y > SCREEN_HEIGHT-32)
blaster.y = SCREEN_HEIGHT-32;
else
if (blaster.y < SCREEN_HEIGHT-128)
blaster.y = SCREEN_HEIGHT-128;
// is player firing?
if (joy_state.rgbButtons[0])
Start_Missile();
// move and draw missle
Move_Missile();
Draw_Missile();
// is it time to blink eyes
if ((rand()%100)==50)
Set_Animation_BOB(&blaster,0);
// draw blaster
Animate_BOB(&blaster);
Draw_BOB16(&blaster,lpddsback);
// draw some text
Draw_Text_GDI("Make My Centipede!",0,0,RGB(255,255,255),lpddsback);
// display joystick and buttons 0-7
sprintf(buffer,"Joystick Stats: X-Axis=%d, Y-Axis=%d, buttons(%d,%d,%d,%d,%d,%d,%d,%d)",
joy_state.lX,joy_state.lY,
joy_state.rgbButtons[0],
joy_state.rgbButtons[1],
joy_state.rgbButtons[2],
joy_state.rgbButtons[3],
joy_state.rgbButtons[4],
joy_state.rgbButtons[5],
joy_state.rgbButtons[6],
joy_state.rgbButtons[7]);
Draw_Text_GDI(buffer,0,SCREEN_HEIGHT-20,RGB(255,255,50),lpddsback);
// print out name of joystick
sprintf(buffer, "Joystick Name & Vendor: %s",joyname);
Draw_Text_GDI(buffer,0,SCREEN_HEIGHT-40,RGB(255,255,50),lpddsback);
// flip the surfaces
DDraw_Flip();
// sync to 30 fps
Wait_Clock(30);
// return success
return(1);
} // end Game_Main
//////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -