⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo12_8.cpp

📁 windows游戏编程大师源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DEMO12_8.CPP - path finding racing demo
// to compile make sure to include DDRAW.LIB, DSOUND.LIB,
// DINPUT.LIB, WINMM.LIB, and of course 
// T3DLIB1.CPP,T3DLIB2.CPP,T3DLIB3.CPP,

// INCLUDES ///////////////////////////////////////////////

#define INITGUID

#define WIN32_LEAN_AND_MEAN  

#include <windows.h>   // include important windows stuff
#include <windowsx.h> 
#include <mmsystem.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h> 
#include <math.h>
#include <io.h>
#include <fcntl.h>

#include <ddraw.h>  // directX includes
#include <dsound.h>
#include <dmksctrl.h>
#include <dmusici.h>
#include <dmusicc.h>
#include <dmusicf.h>
#include <dinput.h>
#include "T3DLIB1.h" // game library includes
#include "T3DLIB2.h"
#include "T3DLIB3.h"

// DEFINES ////////////////////////////////////////////////

// defines for windows 
#define WINDOW_CLASS_NAME "WINCLASS"  // class name

#define WINDOW_WIDTH    320   // size of window
#define WINDOW_HEIGHT   240

#define NUM_WAYPOINTS 30 // number of waypoints in path

// PROTOTYPES /////////////////////////////////////////////

// game console
int Game_Init(void *parms=NULL);
int Game_Shutdown(void *parms=NULL);
int Game_Main(void *parms=NULL);

// TYPES /////////////////////////////////////////////////

typedef struct WAYPOINT_TYP
        {
        float x,y;

        } WAYPOINT, *WAYPOINT_PTR;


// GLOBALS ////////////////////////////////////////////////

HWND main_window_handle           = NULL; // save the window handle
HINSTANCE main_instance           = NULL; // save the instance
char buffer[256];                          // used to print text

BITMAP_IMAGE background_bmp;   // holds the background

BOB          car; // this little race car

int wind_sound_id = -1;        // the ambient wind
int car_sound_id = -1;         // the engine of the car

int vector_display_on = 1;     // used to toggle the informational vector rendering

// the way point list hand compiled :)
WAYPOINT path[NUM_WAYPOINTS] = { 

{332,122}, 
{229,108}, 
{155,97}, 
{104,100}, 
{67,119}, 
{46,159}, 
{55,229}, 
{74,283}, 
{132,364}, 
{206,407}, 
{268,412}, 
{291,405}, 
{303,379}, 
{312,274}, 
{336,244}, 
{383,233}, 
{417,240}, 
{434,278}, 
{426,328}, 
{407,388}, 
{418,415}, 
{452,429}, 
{501,419}, 
{534,376}, 
{562,263}, 
{562,188}, 
{556,112}, 
{530,100}, 
{484,97},
{404,116},};                     


// FUNCTIONS //////////////////////////////////////////////

LRESULT CALLBACK WindowProc(HWND hwnd, 
						    UINT msg, 
                            WPARAM wparam, 
                            LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT	ps;		   // used in WM_PAINT
HDC			hdc;	   // handle to a device context

// what is the message 
switch(msg)
	{	
	case WM_CREATE: 
        {
		// do initialization stuff here
		return(0);
		} break;

    case WM_PAINT:
         {
         // start painting
         hdc = BeginPaint(hwnd,&ps);

         // end painting
         EndPaint(hwnd,&ps);
         return(0);
        } break;

	case WM_DESTROY: 
		{
		// kill the application			
		PostQuitMessage(0);
		return(0);
		} break;

	default:break;

    } // end switch

// process any messages that we didn't take care of 
return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

// WINMAIN ////////////////////////////////////////////////

int WINAPI WinMain(	HINSTANCE hinstance,
					HINSTANCE hprevinstance,
					LPSTR lpcmdline,
					int ncmdshow)
{
// this is the winmain function

WNDCLASS winclass;	// this will hold the class we create
HWND	 hwnd;		// generic window handle
MSG		 msg;		// generic message
HDC      hdc;       // generic dc
PAINTSTRUCT ps;     // generic paintstruct

// first fill in the window class stucture
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;

// register the window class
if (!RegisterClass(&winclass))
	return(0);

// create the window, note the use of WS_POPUP
if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
						  "Waypoint Path finding demo",	 // title
						  WS_POPUP | WS_VISIBLE,
					 	  0,0,	   // x,y
						  WINDOW_WIDTH,  // width
                          WINDOW_HEIGHT, // height
						  NULL,	   // handle to parent 
						  NULL,	   // handle to menu
						  hinstance,// instance
						  NULL)))	// creation parms
return(0);

// save the window handle and instance in a global
main_window_handle = hwnd;
main_instance      = hinstance;

// perform all game console specific initialization
Game_Init();

// enter main event loop
while(1)
	{
	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

// shutdown game and release all resources
Game_Shutdown();

// return to Windows like this
return(msg.wParam);

} // end WinMain

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

inline int Draw_Pixel(int x, int y,int color,
               UCHAR *video_buffer, int lpitch)
{
// this function plots a single pixel at x,y with color
video_buffer[x + y*lpitch] = color;

// return success
return(1);

} // end Draw_Pixel

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

int Find_Nearest_Waypoint(float x, float y)
{
// this function finds the nearest waypoint to the sent position

int near_id = 0;
int near_dist = 1000,
    test_dist;

for (int index=0; index<NUM_WAYPOINTS; index++)
    {
    // is this waypoint closer?
    if ((test_dist = Fast_Distance_2D(path[index].x - x,path[index].y - y)) < near_dist)
       {
       // set as nearest waypoint
       near_id = index;
       near_dist = test_dist;   
       } // end if

    } // end for index

// test if user want to see all those lines
if (vector_display_on==1)
   {
   // draw it
   DDraw_Lock_Back_Surface();
   Draw_Line(path[near_id].x, path[near_id].y - 8,path[near_id].x, path[near_id].y + 8,
              250, back_buffer, back_lpitch); 

   Draw_Line(path[near_id].x-8, path[near_id].y ,path[near_id].x+8, path[near_id].y,
              250, back_buffer, back_lpitch); 
   DDraw_Unlock_Back_Surface();
   } // end if

// return it
return(near_id);

} // end Find_Nearest_Waypoint

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

void Draw_Waypoints(int mode=1)
{
// this function draws the waypoints and the path network

// lock back surface
DDraw_Lock_Back_Surface();

for (int index=0; index < NUM_WAYPOINTS; index++)
    {
    // draw network line too?
    if (mode > 0)
       Draw_Line(path[index].x, path[index].y,
                 path[(index+1)%NUM_WAYPOINTS].x, path[(index+1)%NUM_WAYPOINTS].y,
                 249,   
                 back_buffer, back_lpitch); // video buffer and memory pitch

   Draw_Pixel(path[index].x, path[index].y, 250, back_buffer, back_lpitch);
   Draw_Pixel(path[index].x+1, path[index].y, 250, back_buffer, back_lpitch);
   Draw_Pixel(path[index].x, path[index].y+1, 250, back_buffer, back_lpitch);
   Draw_Pixel(path[index].x+1, path[index].y+1, 250, back_buffer, back_lpitch);

    } // end for index

// lock back surface
DDraw_Unlock_Back_Surface();

} // end Draw_Waypoints

// T3D GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////

int Game_Init(void *parms)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -