📄 demo12_8.cpp
字号:
DDraw_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
// load background image
Load_Bitmap_File(&bitmap8bit, "RACETRACK.BMP");
Create_Bitmap(&background_bmp,0,0,640,480);
Load_Image_Bitmap(&background_bmp, &bitmap8bit,0,0,BITMAP_EXTRACT_MODE_ABS);
Set_Palette(bitmap8bit.palette);
Unload_Bitmap_File(&bitmap8bit);
// load the car bitmaps
Load_Bitmap_File(&bitmap8bit, "SIMIMG.BMP");
// create car bob
Create_BOB(&car,334,64, 16,16, 16, BOB_ATTR_MULTI_FRAME | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY);
// set floating position and direction to west
car.varsF[0] = car.x;
car.varsF[1] = car.y;
car.curr_frame = 12;
// load the car in
for (index=0; index < 16; index++)
Load_Frame_BOB(&car, &bitmap8bit, index, index, 3, BITMAP_EXTRACT_MODE_CELL);
// unload car
Unload_Bitmap_File(&bitmap8bit);
// initialize directinput
DInput_Init();
// acquire the keyboard only
DInput_Init_Keyboard();
// initilize DirectSound
DSound_Init();
// load background sounds
wind_sound_id = DSound_Load_WAV("WIND.WAV");
car_sound_id = DSound_Load_WAV("CARIDLE.WAV");
// start the sounds
DSound_Play(wind_sound_id, DSBPLAY_LOOPING);
DSound_Play(car_sound_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);
// 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(&car);
// 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
/////////////////////////////////////////////////////////////////
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
static float speed = 0; // speed of car
static int curr_waypoint = -1,
test_waypoint;
static int display_debounce = 0; // used to debounce keyboard
int ai_on = 1; // used to enable display ai based on user taking controls
// 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();
// draw the waypoints
if (vector_display_on==1)
Draw_Waypoints();
// read keyboard
DInput_Read_Keyboard();
// is user toggling vector display
if (keyboard_state[DIK_D] && !display_debounce)
{
display_debounce = 1;
vector_display_on=-vector_display_on;
} // end if
if (!keyboard_state[DIK_D])
display_debounce = 0;
// these controls not really needed since AI drives...
// allow player to move
if (keyboard_state[DIK_RIGHT])
{
if (++car.curr_frame > 15)
car.curr_frame = 0;
// turn off ai this cycle
ai_on = 0;
} // end if
else
if (keyboard_state[DIK_LEFT])
{
if (--car.curr_frame < 0)
car.curr_frame = 15;
// turn off ai this cycle
ai_on = 0;
} // end if
if (keyboard_state[DIK_UP])
{
if ((speed+=0.1) > 4)
speed = 4.0;
// turn off ai this cycle
ai_on = 0;
} // end if
else
if (keyboard_state[DIK_DOWN])
{
speed-=0.1;
// turn off ai this cycle
ai_on = 0;
} // end if
// ai speed control -- pretty dumb huh :)
// better to tie it to angle that it's turning
if (ai_on)
if ((speed+=0.05) > 4)
speed = 4.0;
// apply friction
if ((speed-=0.01) < 0)
speed = 0;
// make engine sound
DSound_Set_Freq(car_sound_id,11000+speed*2000);
// let ai turn the car
// find nearest waypoint first
test_waypoint = (Find_Nearest_Waypoint(car.varsF[0],car.varsF[1])+1)%NUM_WAYPOINTS;
// test if this is a new waypoint and not the last
if (test_waypoint != curr_waypoint)
curr_waypoint = test_waypoint;
// compute vector toward waypoint
float wx = (path[curr_waypoint].x - car.varsF[0]);
float wy = (path[curr_waypoint].y - car.varsF[1]);
// normalize wx, wy
float length = Fast_Distance_2D(wx+0.5,wy+0.5);
wx/=length;
wy/=length;
// compute angle in radians
float car_angle = ((-90+car.curr_frame*22.5)*PI)/180;
// compute velocity vector of car (use look up for cos, sin in real life
float xv = cos(car_angle);
float yv = sin(car_angle);
// draw vectors
if (vector_display_on==1)
{
DDraw_Lock_Back_Surface();
Draw_Line(car.varsF[0],car.varsF[1], path[curr_waypoint].x, path[curr_waypoint].y,
250, back_buffer, back_lpitch);
Draw_Line(car.varsF[0],car.varsF[1], car.varsF[0]+16*xv,car.varsF[1]+16*yv,
246, back_buffer, back_lpitch);
DDraw_Unlock_Back_Surface();
} // end if
// now turn car into waypoint direction, get sign of cross product
// between each vector
float sign_cross = (xv*wy - yv*wx);
if (ai_on)
{
// test the sign to determine which way to turn plus a little slosh
if (sign_cross > 0.3)
{
if (++car.curr_frame > 15)
car.curr_frame = 0;
sprintf(buffer,"AI Turning RIGHT", sign_cross);
Draw_Text_GDI(buffer,320, 460,RGB(255,255,255),lpddsback);
} // end if
else
if (sign_cross < -0.3)
{
if (--car.curr_frame < 0 )
car.curr_frame = 15;
sprintf(buffer,"AI Turning LEFT", sign_cross);
Draw_Text_GDI(buffer,320, 460,RGB(255,255,255),lpddsback);
} // end if
else
{
sprintf(buffer,"AI Turning 0", sign_cross);
Draw_Text_GDI(buffer,320, 460,RGB(255,255,255),lpddsback);
} // end else
} // end if
else
{
sprintf(buffer,"AI Disabled by user override", sign_cross);
Draw_Text_GDI(buffer,320, 460,RGB(255,255,255),lpddsback);
} // end if
// adjust floating position
car.varsF[0] += (xv*speed);
car.varsF[1] += (yv*speed);
// test for off screen -- user could cause this
if (car.varsF[0] > screen_width)
car.varsF[0] = 0;
else
if (car.varsF[0] < 0)
car.varsF[0] = screen_width;
if (car.varsF[1] > screen_height)
car.varsF[1] = 0;
else
if (car.varsF[1] < 0)
car.varsF[1] = screen_height;
// position sprite on pixel center
car.x = (0.5+car.varsF[0] - 8);
car.y = (0.5+car.varsF[1] - 8);
// draw the car
Draw_BOB(&car, lpddsback);
// show nearest waypoint
sprintf(buffer,"Nearest Waypoint %d", Find_Nearest_Waypoint(car.varsF[0],car.varsF[1]));
Draw_Text_GDI(buffer,64, 460,RGB(255,255,255),lpddsback);
sprintf(buffer,"Path following demo. Use arrow keys to override AI, <D> to toggle info, and <ESC> to Exit.");
Draw_Text_GDI(buffer,8, 8,RGB(255,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_DARKNESS,NULL,0);
} // end if
// return success
return(1);
} // end Game_Main
//////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -