?? demo12_5.cpp
字號:
// initialize directinput
DInput_Init();
// acquire the keyboard only
DInput_Init_Keyboard();
// initilize DirectSound
DSound_Init();
// load background sounds
engines_id = DSound_Load_WAV("ENGINES.WAV");
// start the sounds
DSound_Play(engines_id, DSBPLAY_LOOPING);
// set clipping rectangle to screen extents so objects dont
// mess up at edges
RECT screen_rect = {0,0,screen_width,screen_height};
lpddclipper = DDraw_Attach_Clipper(lpddsback,1,&screen_rect);
// hide the mouse
ShowCursor(FALSE);
// seed random number generate
srand(Start_Clock());
// return success
return(1);
} // end Game_Init
///////////////////////////////////////////////////////////
int Game_Shutdown(void *parms)
{
// this function is where you shutdown your game and
// release all resources that you allocated
// shut everything down
// kill all the bobs
Destroy_BOB(&bot);
// shutdown directdraw last
DDraw_Shutdown();
// now directsound
DSound_Stop_All_Sounds();
DSound_Shutdown();
// shut down directinput
DInput_Shutdown();
// return success
return(1);
} // end Game_Shutdown
//////////////////////////////////////////////////////////
void Bot_AI(void)
{
// this function controls the ai of the bot and the pattern
// processing
static int opcode, // current pattern opcode
operand; // current operand
// test if it's time to process a new instruction
if (curr_pattern==NULL)
{
// select a random pattern in pattern bank
bot_pattern_index = rand()%NUM_PATTERNS;
curr_pattern = patterns[bot_pattern_index];
// now reset instuction pointer
bot_ip = 0;
// reset counter
bot_counter = 0;
} // end if
// process next instruction if it's time
if (--bot_counter <= 0)
{
// get next instruction
opcode = curr_pattern[bot_ip++];
operand = curr_pattern[bot_ip++];
// test what the opcode is
switch(opcode)
{
case OPC_E:
{
// set direction to east
Set_Vel_BOB(&bot,6,0);
// set animation to east
Set_Animation_BOB(&bot,opcode);
// set counter to instuction operand
bot_counter = operand;
} break;
case OPC_NE:
{
// set direction to northeast
Set_Vel_BOB(&bot,6,-6);
// set animation to northeast
Set_Animation_BOB(&bot,opcode);
// set counter to instuction operand
bot_counter = operand;
} break;
case OPC_N:
{
// set direction to north
Set_Vel_BOB(&bot,0,-6);
// set animation to north
Set_Animation_BOB(&bot,opcode);
// set counter to instuction operand
bot_counter = operand;
} break;
case OPC_NW:
{
// set direction to northwest
Set_Vel_BOB(&bot,-6,-6);
// set animation to northwest
Set_Animation_BOB(&bot,opcode);
// set counter to instuction operand
bot_counter = operand;
} break;
case OPC_W:
{
// set direction to west
Set_Vel_BOB(&bot,-6,0);
// set animation to west
Set_Animation_BOB(&bot,opcode);
// set counter to instuction operand
bot_counter = operand;
} break;
case OPC_SW:
{
// set direction to southwest
Set_Vel_BOB(&bot,-6,6);
// set animation to southwest
Set_Animation_BOB(&bot,opcode);
// set counter to instuction operand
bot_counter = operand;
} break;
case OPC_S:
{
// set direction to south
Set_Vel_BOB(&bot,0,6);
// set animation to south
Set_Animation_BOB(&bot,opcode);
// set counter to instuction operand
bot_counter = operand;
} break;
case OPC_SE:
{
// set direction to southeast
Set_Vel_BOB(&bot,6,6);
// set animation to southeast
Set_Animation_BOB(&bot,opcode);
// set counter to instuction operand
bot_counter = operand;
} break;
case OPC_STOP:
{
// stop motion
Set_Vel_BOB(&bot,0,0);
// set counter to instuction operand
bot_counter = operand;
} break;
case OPC_RAND:
{
// set direction randomly
Set_Vel_BOB(&bot,-4+rand()%9,-4+rand()%9);
// set animation to south
Set_Animation_BOB(&bot,OPC_S);
// set counter to instuction operand
bot_counter = operand;
} break;
case OPC_END:
{
// stop motion
Set_Vel_BOB(&bot,0,0);
// select a random pattern in pattern bank
bot_pattern_index = rand()%NUM_PATTERNS;
curr_pattern = patterns[bot_pattern_index];
// now reset instuction pointer
bot_ip = 0;
// reset counter
bot_counter = 0;
} break;
default: break;
} // end switch
} // end if
// draw stats
sprintf(buffer,"Pattern #%d",bot_pattern_index);
Draw_Text_GDI(buffer,10, 400,RGB(0,255,0), lpddsback);
sprintf(buffer,"Opcode=%s Operand=%d",opcode_names[opcode],operand);
Draw_Text_GDI(buffer,10, 416,RGB(0,255,0), lpddsback);
sprintf(buffer,"Instruction Ptr=%d ", bot_ip);
Draw_Text_GDI(buffer,10, 432,RGB(0,255,0), lpddsback);
sprintf(buffer,"Counter=%d ", bot_counter);
Draw_Text_GDI(buffer,10, 448,RGB(0,255,0), lpddsback);
} // end Bot_AI
//////////////////////////////////////////////////////////
int Game_Main(void *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
// start the timing clock
Start_Clock();
// clear the drawing surface
DDraw_Fill_Surface(lpddsback, 0);
// lock back buffer and copy background into it
DDraw_Lock_Back_Surface();
// draw background
Draw_Bitmap(&background_bmp, back_buffer, back_lpitch,0);
// unlock back surface
DDraw_Unlock_Back_Surface();
// read keyboard
DInput_Read_Keyboard();
// do the ai on bot
Bot_AI();
// the animate the bot
Animate_BOB(&bot);
// move bot
Move_BOB(&bot);
// test if bot is off screen, if so wrap around
if (bot.x >= SCREEN_WIDTH)
bot.x = -bot.width;
else
if (bot.x < -bot.width)
bot.x = SCREEN_WIDTH;
if (bot.y >= SCREEN_HEIGHT)
bot.y = -bot.height;
else
if (bot.y < -bot.height)
bot.y = SCREEN_HEIGHT;
// draw the bot
Draw_BOB(&bot, lpddsback);
// draw title
Draw_Text_GDI("I-ROBOT 3D - Pattern Demo",10, 10,RGB(0,255,255), lpddsback);
// flip the surfaces
DDraw_Flip();
// sync to 30ish fps
Wait_Clock(30);
// check of user is trying to exit
if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
{
PostMessage(main_window_handle, WM_DESTROY,0,0);
// stop all sounds
DSound_Stop_All_Sounds();
// do a screen transition
Screen_Transitions(SCREEN_BLUENESS,NULL,0);
} // end if
// return success
return(1);
} // end Game_Main
//////////////////////////////////////////////////////////
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -