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

📄 cidade.cpp

📁 3d游戏开发的基础应用,3d场景的真实展现,可以是很好的教材
💻 CPP
📖 第 1 页 / 共 5 页
字号:
#define STRICT
#define DIRECTINPUT_VERSION 0x0800
#include <windows.h>
#include <commctrl.h>
#include <commdlg.h>
#include <basetsd.h>
#include <math.h>
#include <stdio.h>
#include <d3dx9.h>
#include <dxerr9.h>
#include <tchar.h>
#include <dinput.h>
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include "D3DApp.h"
#include "D3DFont.h"
#include "D3DFile.h"
#include "D3DUtil.h"
#include "DIUtil.h"
#include "DSUtil.h"
#include "resource.h"
#include "cidade.h"



//-----------------------------------------------------------------------------
// Defines, and constants
//-----------------------------------------------------------------------------
// This GUID must be unique for every game, and the same for 
// every instance of this app.  // {5CE6C5BE-B7A1-44D6-A074-AB32C64A4109}
// The GUID allows DirectInput to remember input settings
GUID g_guidApp = { 0x5ce6c5be, 0xb7a1, 0x44d6, { 0xa0, 0x74, 0xab, 0x32, 0xc6, 0x4a, 0x41, 0x09 } };

#define	SPEED	60.0f
#define AnimSpeed	2.0f
#define ZDepth	3600.0f
#define	NumCar	1000			// Max..2000 -> Nqx=55 && Nqy=55 && Bdiv=6
#define	NumPea	750				// Max..1500 -> Nqx=55 && Nqy=55 && Bdiv=6
#define	NumRMem	4
#define Bdiv	6
#define	Nqx	55
#define	Nqz	55
#define MaxAlt	60
#define	MinAlt	15
#define	TelhAlt	30
#define	TelhAltura	15
#define	PasAlt	0.7f
#define	SemAlt	15
#define	SemRaio	0.4f
#define	MargSemaf	16.0f
#define	WalkAlt	0.1f
#define	NWalk	5
#define	SkyColor	0x003060D0
#define	MaxCtCrvH	100
#define	MaxCtCrvL	70
#define	CarH	2.5f
#define	CarL	8.0f
#define	CarW	6.0f
#define	PeaoDim	1.0f
#define ClkW	1
#define CClkW	2

#define	QUAD		0
#define	FAN4		1

const int BdivLat=2*Bdiv/3;
const float EstradaL=50.0f;
const float EstradaLim=EstradaL-0.1f;
const float PasseioL=7.0f;
const float PasseioL2=PasseioL/2.0f;
const float CenterX=-3.0f*EstradaL/2.0f;
const float CenterZ=-2.0f*EstradaL;
const float SemAltBox=SemAlt/3.0f;
const float D_2PI=2.0f*D3DX_PI;
const float D_PI2=D3DX_PI/2.0f;
const float	D_3PI2=3.0f*D_PI2;
const float D_PI4=D3DX_PI/4.0f;
const float PasCrvH=D_PI2/(float)MaxCtCrvH;
const float PasCrvL=D_PI2/(float)MaxCtCrvL;
const float Hd=EstradaL/4.0f+PasseioL/2.0f;
const float He=3.0f*EstradaL/4.0f-PasseioL/2.0f;
const float CarDim=CarL+1.0f;
const float MinVizCar=2.0f*CarDim;
const float	VizCar=EstradaL; // =MinVizCar;
const float EstLmVizCar=EstradaL-VizCar;
const float Wajs=2.0f;
const float WalkL=MargSemaf-Wajs;
const float Wpas=(EstradaL-2.0f*PasseioL)/(2*NWalk-1);
const float EstradaL2=EstradaL/2.0f;
const float SemAlt2=SemAlt/2.0f;
const float Hpd=PasseioL/2.0f;
const float Hpe=EstradaL-PasseioL/2.0f;
const float KProty=D3DX_PI/20.0f;

const float PSPEED=AnimSpeed*PeaoDim*2.5f;
const float BPeaoY=PeaoDim*2.7f;
const float PBody=PeaoDim*2.4f;
const float PeaoAlt=PeaoDim*2.0f;
const float LegX=PeaoDim*0.42f;
const float ArmX=PeaoDim*0.9f;
const float BodyY=BPeaoY+PBody/1.5f;
const float HeadRadius=PeaoDim/2.0f;
const float	HeadYinc=PBody/2.0f+HeadRadius*1.4f;

// Custom vertex Parede
struct PAREDEVERTEX
{
    D3DXVECTOR3 p;      // Vertex position
    DWORD       color;  // Vertex color
    static const DWORD FVF;
};
const DWORD PAREDEVERTEX::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;

typedef struct PAREDE {
	unsigned char tipo;
	LPDIRECT3DVERTEXBUFFER9 buf;
	PAREDE *nx;
} Parede;

typedef struct {
	float altura;
	DWORD color;
} Pattrib;

enum Direccoes { NORTE=0, SUL, ESTE, OESTE };
enum SemafEstado { G=0, Y, R };

typedef struct SEMAFORO {
	D3DXVECTOR3 pos;
	SemafEstado *estado;
	Direccoes dir;
	SEMAFORO *nx;
} Semaforo;

typedef struct CLOG {
	void *car;
	CLOG *nx;
} Clog;

typedef struct PLOG {
	void *pea;
	PLOG *nx;
} Plog;

typedef struct RUA {
	int x, z;
	RUA *n, *s, *e, *w, *nx;
	Pattrib Pn, Ps, Pe, Pw;
	Parede *raiz;
	Semaforo *rsem;
	Clog *rlog;
	Plog *rpea;
} rua;

typedef struct LISTARUA {
	rua *r;
	int aux;
	LISTARUA *nx;
} ListaRua;

typedef struct {
	ID3DXMesh* pMesh1;
	ID3DXMesh* pMesh2;
	ID3DXMesh* pMesh3;
	D3DMATERIAL9 material;
} Objecto;

typedef struct MOVEL {
	float x, z, gx, gz, roty, pex, pez, speed;
	int estado;
	rua *r, *rvs;
	Direccoes dir, tdir;
	D3DMATERIAL9 cor;
	MOVEL *pv, *nx;
} Movel;

typedef struct PEAO {
	float x, z, roty, pex, pez, speed;
	int estado;
	rua *r;
	Direccoes dir;
	D3DMATERIAL9 cor, hcor;
} Peao;

typedef struct LANIM {
	float tmg, pass;
	LANIM *nx;
} Lanim;

typedef struct MEMBRO {
	float altura, rotx, rxini, tanim;
	Lanim *ranim;
	ID3DXMesh* pMesh;
	D3DMATERIAL9 cor;
	MEMBRO *nx;
} Membro;

typedef struct {
	float bx, by;
	Membro *rzmem;
} BaseMembro;

const char Bussula[]="N\0S\0W\0E";
// Global Vars
BaseMembro rzbm[NumRMem];
Membro *rzglmem=NULL;
rua *raiz=NULL, *Kasa;
Movel carros[NumCar];
Peao peoes[NumPea];
Direccoes Dir=NORTE;
Objecto Semaf;
SemafEstado Sns=G, Sew=R;
LPDIRECT3DVERTEXBUFFER9 Walkbuf[4][NWalk];
float tempo=0, Speed=2.0f, Pspeed=0.5f;
bool RendFullMap=false, UserControl=false, Help=false;
int CarCnt, CarNView=NumCar/2;
	
D3DXVECTOR3 m_vPos(0.0f, -10.0f, 0.0f), m_vEyePt; // Camera position & PV
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );

float m_fWorldRotX = 0;           
float m_fWorldRotY = 0;

D3DMATERIAL9 GYR[3];
ID3DXMesh* pCarroMesh;
ID3DXMesh* pBodyMesh;
ID3DXMesh* pHeadMesh0;
ID3DXMesh* pHeadMesh1;

FLOAT                   m_fSoundPlayRepeatCountdown; // Sound repeat timer
CSoundManager*          m_pSoundManager;        // DirectSound manager class

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

void PushAnim(Membro *mem, float tm, float var) {
Lanim *ta=mem->ranim , *t=mem->ranim, *tn=(Lanim *) malloc(sizeof(Lanim));
	tn->tmg=tm;
	tn->pass=var;
	while(ta) { 
		if(tm<ta->tmg) {
			tn->nx=ta;
			if(t==ta) mem->ranim=tn;
			else t->nx=tn;
			return;
		}
		t=ta;
		ta=ta->nx;
	}
	tn->nx=ta;
	if(t==ta) mem->ranim=tn;
	else t->nx=tn;
}

void FreeMembro(Membro **rzm) {
Membro *m=*rzm;
Lanim *t;
	if(m) {
		*rzm=(*rzm)->nx;
		while(m->ranim) {
			t=m->ranim;
			m->ranim=m->ranim->nx;
			free(t);
		}
		SAFE_RELEASE( m->pMesh );
		free(m);
	}
}

void criaLog(Clog **rz, Movel *car) {
Clog *t=(Clog *) malloc(sizeof(Clog));
	t->car=(void *)car;
	t->nx=*rz;
	*rz=t;
}

void destroiLog(Clog **rz) {
Clog *t=*rz;	
	if(*rz) {
		*rz=(*rz)->nx;
		free(t);
	}
}

void retiraLog(Clog **rz, Movel *car) {
Clog *lg=*rz, *last=*rz;
	while(lg && lg->car!=(void *)car) {
		last=lg;	
		lg=lg->nx;
	}
	if(lg->car==(void *)car) {
		if(last==lg) *rz=(*rz)->nx;
		else last->nx=lg->nx;
		free(lg);
	}
}

void criaPLog(Plog **rz, Peao *pea) {
Plog *t=(Plog *) malloc(sizeof(Plog));
	t->pea=(void *)pea;
	t->nx=*rz;
	*rz=t;
}

void destroiPLog(Plog **rz) {
Plog *t=*rz;	
	if(*rz) {
		*rz=(*rz)->nx;
		free(t);
	}
}

void retiraPLog(Plog **rz, Peao *pea) {
Plog *lg=*rz, *last=*rz;
	while(lg && lg->pea!=(void *)pea) {
		last=lg;	
		lg=lg->nx;
	}
	if(lg->pea==(void *)pea) {
		if(last==lg) *rz=(*rz)->nx;
		else last->nx=lg->nx;
		free(lg);
	}
}

void criaSemaforo(Semaforo **rz, D3DXVECTOR3 p, SemafEstado *e, Direccoes d) {
Semaforo *t=(Semaforo *) malloc(sizeof(Semaforo));
	t->pos=p;
	t->estado=e;
	t->dir=d;
	t->nx=*rz;
	*rz=t;
}

void destroiSemaforo(Semaforo **rz) {
Semaforo *t=*rz;	
	if(*rz) {
		*rz=(*rz)->nx;
		free(t);
	}
}

void criaParede(Parede **rz, unsigned char tipo) {
Parede *t=(Parede *) malloc(sizeof(Parede));
	t->tipo=tipo;
	t->nx=*rz;
	*rz=t;
}

void destroiParede(Parede **rz) {
Parede *t=*rz;
	if(*rz) {
		*rz=(*rz)->nx;
		SAFE_RELEASE( t->buf );
		free(t);
	}
}

rua *cria(int cx, int cz) {
rua *t=(rua *) malloc(sizeof(rua));
	t->x=cx; t->z=cz;
	t->n=t->s=t->e=t->w=t->nx=NULL;
	t->raiz=NULL;
	t->rsem=NULL;
	t->rlog=NULL;
	t->rpea=NULL;
	return t;
}

rua *destroi(rua *k) {
rua *t=NULL;
	if(k) {
		t=k->nx;
		while(k->raiz) destroiParede(&(k->raiz));
		while(k->rsem) destroiSemaforo(&(k->rsem));
		while(k->rlog) destroiLog(&(k->rlog));
		while(k->rpea) destroiPLog(&(k->rpea));
		free(k);
	}
	return t;
}

rua *findrua(int fx,int fz) {
rua *f;
	for(f=raiz;f;f=f->nx)
		if(f->x==fx && f->z==fz) return f;
	return NULL;
}

void mapa() {
int mx,mz;
rua *t;
	for(mz=0;mz<Nqz;mz++)
		for(mx=0;mx<Nqx;mx++) 
			if(mz%Bdiv && mx%Bdiv) ;
			else {
				t=cria(mx+1,mz+1);
				if(!(mz%Bdiv) && mx) {         // L
					t->w=raiz;
					t->w->e=t;
				}
				if(!(mx%Bdiv) && mz) {
					t->n=findrua(mx+1,mz);	  // ^
					t->n->s=t;
				}				
				t->nx=raiz;
				raiz=t;
			}
}

bool Esquina(rua *r, Direccoes dir) {
	switch(dir) {
		case NORTE : if(r->e)
						 if(r->e->n) return true;
					 if(r->w)
						 if(r->w->n) return true;
					 break;
		case SUL  :  if(r->e)

⌨️ 快捷键说明

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