📄 demo13_10.cpp
字号:
// DEMO13_10.CPP - particle demo
// to compile make sure to include DDRAW.LIB, DSOUND.LIB,
// DINPUT.LIB, WINMM.LIB, and of course the T3DLIB files
// 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 "WINXCLASS" // class name
#define WINDOW_WIDTH 320 // size of window
#define WINDOW_HEIGHT 240
// defines for polygon cannon
#define CANNON_X0 39 // position of tip of cannon
#define CANNON_Y0 372
#define NUM_PROJECTILES 16 // number of projectiles
// defines for particle system
#define PARTICLE_STATE_DEAD 0
#define PARTICLE_STATE_ALIVE 1
// types of particles
#define PARTICLE_TYPE_FLICKER 0
#define PARTICLE_TYPE_FADE 1
// color of particle
#define PARTICLE_COLOR_RED 0
#define PARTICLE_COLOR_GREEN 1
#define PARTICLE_COLOR_BLUE 2
#define PARTICLE_COLOR_WHITE 3
#define MAX_PARTICLES 256
// color ranges
#define COLOR_RED_START 32
#define COLOR_RED_END 47
#define COLOR_GREEN_START 96
#define COLOR_GREEN_END 111
#define COLOR_BLUE_START 144
#define COLOR_BLUE_END 159
#define COLOR_WHITE_START 16
#define COLOR_WHITE_END 31
// MACROS ///////////////////////////////////////////////
#define RAND_RANGE(x,y) ( (x) + (rand()%((y)-(x)+1)))
// TYPES ///////////////////////////////////////////////
typedef struct PROJ_TYP
{
int state; // state 0 off, 1 on
float x,y; // position
float xv, yv; // velocity
int detonate; // tracks when the projectile detonates
} PROJECTILE, *PROJECTILE_PTR;
// a single particle
typedef struct PARTICLE_TYP
{
int state; // state of the particle
int type; // type of particle effect
float x,y; // world position of particle
float xv,yv; // velocity of particle
int curr_color; // the current rendering color of particle
int start_color; // the start color or range effect
int end_color; // the ending color of range effect
int counter; // general state transition timer
int max_count; // max value for counter
} PARTICLE, *PARTICLE_PTR;
// PROTOTYPES /////////////////////////////////////////////
// game console
int Game_Init(void *parms=NULL);
int Game_Shutdown(void *parms=NULL);
int Game_Main(void *parms=NULL);
// missile interface
void Init_Projectiles(void);
void Move_Projectiles(void);
void Draw_Projectiles(void);
void Fire_Projectile(int angle, float vel);
void Init_Reset_Particles(void);
void Draw_Particles(void);
void Move_Particles(void);
void Start_Particle(int type, int color, int count, int x, int y, int xv, int yv);
void Start_Particle_Explosion(int type, int color, int count,
int x, int y, int xv, int yv, int num_particles);
void Start_Particle_Ring(int type, int color, int count,
int x, int y, int xv, int yv, int num_particles);
// 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
int cannon_ids[8]; // sound ids for cannon
int explosion_ids[8]; // explosion ids
POLYGON2D cannon; // the ship
PROJECTILE missiles[NUM_PROJECTILES]; // array of missiles
float gravity_force = 0.2; // gravity
float wind_force = -0.01; // wind resistance
float particle_wind = 0; // assume it operates in the X direction
float particle_gravity = .02; // assume it operates in the Y direction
PARTICLE particles[MAX_PARTICLES]; // the particles for the particle engine
// 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
"Collision 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
// T3D GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
int Game_Init(void *parms)
{
// this function is where you do all the initialization
// for your game
int index; // looping varsIable
char filename[80]; // used to build up filenames
// seed random number generate
srand(Start_Clock());
// start up DirectDraw (replace the parms as you desire)
DDraw_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
// load background image
Load_Bitmap_File(&bitmap8bit, "PARTICLEGRID.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);
// hide the mouse
ShowCursor(FALSE);
// initialize directinput
DInput_Init();
// acquire the keyboard only
DInput_Init_Keyboard();
// initilize DirectSound
DSound_Init();
// initialize particles
Init_Reset_Particles();
// load background sounds
cannon_ids[0] = DSound_Load_WAV("CANNON.WAV");
// clone sounds
for (index=1; index < 8; index++)
cannon_ids[index] = DSound_Replicate_Sound(cannon_ids[0]);
// load background sounds
explosion_ids[0] = DSound_Load_WAV("EXP1.WAV");
// clone sounds
for (index=1; index < 8; index++)
explosion_ids[index] = DSound_Replicate_Sound(explosion_ids[0]);
// define points of cannon
VERTEX2DF cannon_vertices[4] = { 0,-2, 30,0, 30,2, 0,2, };
// initialize ship
cannon.state = 1; // turn it on
cannon.num_verts = 4;
cannon.x0 = CANNON_X0; // position it
cannon.y0 = CANNON_Y0;
cannon.xv = 0;
cannon.yv = 0;
cannon.color = 95; // green
cannon.vlist = new VERTEX2DF [cannon.num_verts];
for (index = 0; index < cannon.num_verts; index++)
cannon.vlist[index] = cannon_vertices[index];
// build the 360 degree look ups
Build_Sin_Cos_Tables();
// initialize the missiles
Init_Projectiles();
// 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);
// set clipping region
min_clip_x = 0;
max_clip_x = screen_width - 1;
min_clip_y = 0;
max_clip_y = screen_height - 1;
// 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
// 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 Init_Reset_Particles(void)
{
// this function serves as both an init and reset for the particles
// loop thru and reset all the particles to dead
for (int index=0; index<MAX_PARTICLES; index++)
{
particles[index].state = PARTICLE_STATE_DEAD;
particles[index].type = PARTICLE_TYPE_FADE;
particles[index].x = 0;
particles[index].y = 0;
particles[index].xv = 0;
particles[index].yv = 0;
particles[index].start_color = 0;
particles[index].end_color = 0;
particles[index].curr_color = 0;
particles[index].counter = 0;
particles[index].max_count = 0;
} // end if
} // end Init_Reset_Particles
/////////////////////////////////////////////////////////////////////////
void Start_Particle(int type, int color, int count, int x, int y, int xv, int yv)
{
// this function starts a single particle
int pindex = -1; // index of particle
// first find open particle
for (int index=0; index < MAX_PARTICLES; index++)
if (particles[index].state == PARTICLE_STATE_DEAD)
{
// set index
pindex = index;
break;
} // end if
// did we find one
if (pindex==-1)
return;
// set general state info
particles[pindex].state = PARTICLE_STATE_ALIVE;
particles[pindex].type = type;
particles[pindex].x = x;
particles[pindex].y = y;
particles[pindex].xv = xv;
particles[pindex].yv = yv;
particles[pindex].counter = 0;
particles[pindex].max_count = count;
// set color ranges, always the same
switch(color)
{
case PARTICLE_COLOR_RED:
{
particles[pindex].start_color = COLOR_RED_START;
particles[pindex].end_color = COLOR_RED_END;
} break;
case PARTICLE_COLOR_GREEN:
{
particles[pindex].start_color = COLOR_GREEN_START;
particles[pindex].end_color = COLOR_GREEN_END;
} break;
case PARTICLE_COLOR_BLUE:
{
particles[pindex].start_color = COLOR_BLUE_START;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -