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

📄 raiders3d_2b.cpp

📁 3D游戏编程大师技巧第九章的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// RAIDERS3D_2b.CPP - 3D star raiders game version 2.0, 16-bit 
// READ THIS!
// To compile make sure to include DDRAW.LIB, DSOUND.LIB,
// DINPUT.LIB, DINPUT8.LIB, WINMM.LIB in the project link list, and of course 
// the C++ source modules T3DLIB1-7.CPP and the headers T3DLIB1-7.H
// be in the working directory of the compiler

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

#define DEBUG_OFF

#define INITGUID       // make sure al the COM interfaces are available
                       // instead of this you can include the .LIB file
                       // DXGUID.LIB

#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"
#include "T3DLIB4.h"
#include "T3DLIB5.h"
#include "T3DLIB6.h"
#include "T3DLIB7.h"

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

// defines for windows interface
#define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
#define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
#define WINDOW_WIDTH      800  // size of window
#define WINDOW_HEIGHT     600

#define WINDOW_BPP        16    // bitdepth of window (8,16,24 etc.)
                                // note: if windowed and not
                                // fullscreen then bitdepth must
                                // be same as system bitdepth
                                // also if 8-bit the a pallete
                                // is created and attached
   
#define WINDOWED_APP      0     // 0 not windowed, 1 windowed

// 3D engine constants for overlay star field
#define NEAR_Z            10    // the near clipping plane
#define FAR_Z             2000  // the far clipping plane    
#define VIEW_DISTANCE      400  // viewing distance from viewpoint 
                                // this gives a field of view of 90 degrees
                                // when projected on a window of 800 wide

// create some constants for ease of access
#define AMBIENT_LIGHT_INDEX   0 // ambient light index
#define INFINITE_LIGHT_INDEX  1 // infinite light index
#define POINT_LIGHT_INDEX     2 // point light index
#define POINT_LIGHT2_INDEX    3 // point light index
#define SPOT_LIGHT2_INDEX     4 // spot light index

// alien defines
#define NUM_ALIENS            16 // total number of ALIEN fighters 

// states for aliens
#define ALIEN_STATE_DEAD      0  // alien is dead
#define ALIEN_STATE_ALIVE     1  // alien is alive
#define ALIEN_STATE_DYING     2  // alien is in process of dying

// star defines
#define NUM_STARS           256  // number of stars in sim

// game state defines
#define GAME_STATE_INIT       0  // game initializing for first time
#define GAME_STATE_RESTART    1  // game restarting after game over
#define GAME_STATE_RUN        2  // game running normally
#define GAME_STATE_DEMO       3  // game in demo mode
#define GAME_STATE_OVER       4  // game over
#define GAME_STATE_EXIT       5  // game state exit

#define MAX_MISSES_GAME_OVER  25 // miss this many and it's game over man

// interface text positions
#define TPOS_SCORE_X          346  // players score
#define TPOS_SCORE_Y          4

#define TPOS_HITS_X           250  // total kills
#define TPOS_HITS_Y           36

#define TPOS_ESCAPED_X        224  // aliens that have escaped
#define TPOS_ESCAPED_Y        58

#define TPOS_GLEVEL_X         430 // diff level of game
#define TPOS_GLEVEL_Y         58

#define TPOS_SPEED_X          404 // diff level of game
#define TPOS_SPEED_Y          36

#define TPOS_GINFO_X          400  // general information
#define TPOS_GINFO_Y          240

#define TPOS_ENERGY_X         158 // weapon energy level
#define TPOS_ENERGY_Y         6

// player defines
#define CROSS_START_X         0
#define CROSS_START_Y         0
#define CROSS_WIDTH           56
#define CROSS_HEIGHT          56

// font display stuff
#define FONT_HPITCH           12  // space between chars
#define FONT_VPITCH           14  // space between lines

// difficulty stuff
#define DIFF_RATE             (float)(.001f)  // rate to advance  difficulty per frame
#define DIFF_PMAX             75            


// explosion stuff
#define NUM_EXPLOSIONS        16

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

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

// starfield functions
void Draw_Starfield(UCHAR *video_buffer, int lpitch);
void Move_Starfield(void);
void Init_Starfield(void);

// alien functions
void Process_Aliens(void);
void Draw_Aliens(void);
void Init_Aliens(void);
int  Start_Alien(void);

// explosions
int Start_Mesh_Explosion(OBJECT4DV2_PTR obj, // object to destroy
                         MATRIX4X4_PTR mrot, // initial orientation
                         int detail,         // the detail level,1 highest detail
                         float rate,         // velocity of explosion shrapnel
                         int lifetime);      // total lifetime of explosion

void Draw_Mesh_Explosions(void);

// font functions
int Draw_Bitmap_Font_String(BOB_PTR font, 
                            int x, int y, 
                            char *string, 
                            int hpitch, int vpitch, 
                            LPDIRECTDRAWSURFACE7 dest); 

int Load_Bitmap_Font(char *fontfile, BOB_PTR font);

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

// this a 3D star
typedef struct STAR3D_TYP
        {
        USHORT color;     // color of point 16-bit
        float x,y,z;      // coordinates of point in 3D
        } STAR3D, *STAR3D_PTR;

// ALIEN fighter object
typedef struct ALIEN_TYP
        {
        int      state;  // state of this ALIEN fighter
        int      count;  // generic counter
        int      aux;    // generic auxialiary var
        POINT3D  pos;    // position of ALIEN fighter
        VECTOR3D vel;    // velocity of ALIEN fighter
        VECTOR3D rot;    // rotational velocities
        VECTOR3D ang;    // current orientation
        } ALIEN, *ALIEN_PTR;

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

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

// initialize camera position and direction
POINT4D  cam_pos    = {0,0,0,1};
POINT4D  cam_target = {0,0,0,1};
VECTOR4D cam_dir    = {0,0,0,1};

// all your initialization code goes here...
VECTOR4D vscale={1.0,1.0,1.0,1}, 
         vpos = {0,0,0,1}, 
         vrot = {0,0,0,1};

CAM4DV1        cam;       // the single camera
RENDERLIST4DV2 rend_list; // the render list


// color constants for line drawing
USHORT rgb_green,  
       rgb_white,
       rgb_red,
       rgb_blue;

// colors for lights
RGBAV1 white, light_gray, gray, black, red, green, blue;


// music and sound stuff
int main_track_id      = -1, // main music track id
    laser_id           = -1, // sound of laser pulse
    explosion_id       = -1, // sound of explosion
    flyby_id           = -1, // sound of ALIEN fighter flying by
    game_over_id       = -1, // game over
    ambient_systems_id = -1; // ambient sounds

// star globals
STAR3D stars[NUM_STARS]; // the starfield

// player and game globals
int player_z_vel = 4; // virtual speed of viewpoint/ship

float cross_x = 0, // cross hairs
      cross_y = 0; 

int cross_x_screen  = WINDOW_WIDTH/2,   // used for cross hair
    cross_y_screen  = WINDOW_HEIGHT/2, 
    target_x_screen = WINDOW_WIDTH/2,   // used for targeter
    target_y_screen = WINDOW_HEIGHT/2;

int escaped        = 0;   // tracks number of missed ships
int hits           = 0;   // tracks number of hits
int score          = 0;   // take a guess :)
int weapon_energy  = 100; // weapon energy
int weapon_active  = 0;   // tracks if weapons are energized

int game_state           = GAME_STATE_INIT; // state of game
int game_state_count1    = 0;               // general counters
int game_state_count2    = 0;
int restart_state        = 0;               // state when game is restarting
int restart_state_count1 = 0;               // general counter

// diffiuculty
float  diff_level    = 1;                   // difficulty level used in velocity and probability calcs

// alien globals
ALIEN      aliens[NUM_ALIENS];   // the ALIEN fighters
OBJECT4DV2 obj_alien;            // the master object

// explosions
OBJECT4DV2 explobj[NUM_EXPLOSIONS]; // the explosions


// game imagery assets
BOB cockpit;               // the cockpit image
BOB starscape;             // the background starscape
BOB tech_font;             // the bitmap font for the engine
BOB crosshair;             // the player's cross hair

// these are the positions of the energy binding on the main lower control panel
POINT2D energy_bindings[6] = { {342, 527}, {459, 527}, 
                               {343, 534}, {458, 534}, 
                               {343, 540}, {458, 540} };
// these hold the positions of the weapon burst which use lighting too
// the starting points are known, but the end points are computed on the fly
// based on the cross hair
POINT2D weapon_bursts[4] = { {78, 500}, {0,0},     // left energy weapon
                             {720, 500}, {0,0} };  // right energy weapon

int px = 0, py = 0, pz = 500 ; // debug stuff for object tracking

// 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 test to see if WINDOWED_APP is
// true to select the appropriate window flags
if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
						  WINDOW_TITLE,	 // title

⌨️ 快捷键说明

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