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

📄 game.cpp

📁 一个96K的3D游戏源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
#include "stdio.h"
#include <math.h>
#include <windows.h>
#include <gl\glaux.h>												// Header File For The GLu32 Library
#include "MainNeHe.h"												// Header File For NeHeGL
#include "MainGame.h"
#include "SYSTEMIO.h"
#include "vectors.h"
#include "objekt.h"
#include "animation.h"
#include "Level.h"
#include "Partikel.h"
#include "Game.h"
/*
#define AUX_RGB             0
#define AUX_RGBA            AUX_RGB
#define GL_RGBA                           0x1908
*/
tAnimation  *Anim;

float SunLightPos[] = { 0.0f, 5.0f,-4.0f, 1.0f};			// Light Position
float SunLightAmb[] = { 0.2f, 0.2f, 0.2f, 1.0f};			// Ambient Light Values
float SunLightDif[] = { 0.6f, 0.6f, 0.6f, 1.0f};			// Diffuse Light Values
float SunLightSpc[] = {-0.2f, -0.2f, -0.2f, 1.0f};			// Specular Light Values


//#define		MyScale 0.2

#define		Pi				3.14159
#define		PiHalb			Pi/2
#define		CamSpeedAdd		0.06
#define		CamBrems		0.9
#define		CamSpeedMax		100*MyScale
#define		HandAcc			10.0
tP3D		CPos,CLook,CLookZiel,CZiel,CSpeed,CLookSpeed;
long		CamMode=0;

#define		DODMin 0.2
//#define		DODMax 0.5
#define		DODMax 0.6
#define		DODAnzahl 5.0
#define		DODAnfang	200.0
#define		DODEnde		800.0
#define		VisBorder	1000.0
#define		AlphaAnfang (DODAnfang+300.0)

float		DODAbstand=(DODEnde-DODAnfang)/DODAnzahl;

#define		Guns 0

#define		_Players 5
#define		MaxPlayers 100
//long		Players=_Players;
long		Players=0;

#define		_Create_Player_Abstand 200.0
#define		_Act_Player_Abstand 100.0

/*
#define		LevelName		"Rutsche.bcl"
#define		LevelSize		13.0
/**/

#define		LevelName		"level.bcl"
#define		LevelSize		10.0
/**/
/*
#define		LevelName		"plattform2.bcl"
#define		LevelSize		6.0
/**/
/*
#define		LevelName		"flach3.bcl"
#define		LevelSize		60.0
/**/

#define		WaterParts 10

signed short *WaterFeld,*OldWaterFeld,*TempWaterFeld;
/*
typedef tWaterFeld 
signed short	WaterFeld[WaterParts*2*WaterParts*2];
*/
float		ItemBrems=0.95;
#define		_Fall_Speed_Item 0.05

float		Roll=0;
float		CamSpeed=0.5;
float		PSpeed=0.16*1.35;
float		PTurnSpeed=0.1;
float		PBrems=0.8;
//#define		_Fall_Speed 0.25
#define		_Fall_Speed 0.1
#define		_Fall_Mul 1.2
#define		_Min_Speed 0.3
#define		_Ani_Stand_Grenze 0.4

float		AniStep=1;

tGrund		*Grund,*Water;

tCommand	*CommandList;
tSector		*Level;
tPartikel	*Partikel;
tItem		*Item;


#define _Ani_Cnt 9
#define _Ani_Stand		0
#define _Ani_Walk		1
#define _Ani_Punch		2
#define _Ani_Kick		3
#define _Ani_Punch2		4
#define _Ani_Kick2		5
#define _Ani_Headmove	6
#define	_Ani_Jump		7
#define	_Ani_Shoot		8

#define _AttackRefresh  25

#define _Player_MinAbstand 5
//#define _Player_MinAbstand 10
#define _Player_Wegdrueck 0.1
#define _Reflect_Brems 0.5*0
#define _Reflect_Min 0.25
#define _Jump_Speed 5

//#define _KI_Entf 30
#define _KI_Entf 6
#define _KI_KloppEntf 12



#define Tiles 30
#define TileSize 5

long FramesPS=0;
long GlobalTimer=0;
long DisplayedPolys;

//bool LoadTGA(Texture *, char *);

typedef struct
{
	bool			Active;
	bool			Shooting;
	long			Energy;
	long			Power;
	tP3D			Pos;
	tP3D			Rot;
	tP3D			Speed;
	tOMatrix		Matrix;
	tObjekt			*Obj;
	tSkelList       *NextPos;
	long			AniIndex;
	long			Gun;
	tAnimation		*Ani;
	long			ModelType;
	long			AniCnt;
	long			AttackPower;
	long			AttackRefresh;
} tPlayer;

tPlayer		*Player[MaxPlayers];
tAnimation  *ModelAni[1][_Ani_Cnt];
tObjekt		*Model[1];

tSector		*GunModel[Guns+1];
long		GunNode[Guns+1];
float		GunSize[Guns+1];
long		GunReload[Guns+1];
tSector		*BulletModel[Guns+1];
tMeshList	*BulletMesh[Guns+1];
float		BulletSize[Guns+1];
tP3D		BulletStart[Guns+1];

long		ModelEnergy[1];
long		ModelPower[1];
bool		 Key[256];
bool		 KeyPr[256];


typedef struct													// Create A Structure
{
	unsigned char	*imageData;											// Image Data (UP To 32 Bits)
	unsigned int	bPP;												// Image Color DePth In Bits Per Pixel.
	unsigned int	width;												// Image Width
	unsigned int	height;												// Image Height
	unsigned int	texID;												// Texture ID Used To Select A Texture
} TextureImage;													// Structure Name

TextureImage textures[20];										// Storage For 10 Textures

unsigned int		base;												// Font DisPlay List


bool LoadTGA(TextureImage *texture, char *filename)				// Loads A TGA File Into Memory
{    
	unsigned char		TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};		// UncomPressed TGA Header
	unsigned char		TGAcomPare[12];									// Used To ComPare TGA Header
	unsigned char		header[6];										// First 6 Useful Bytes From The Header
	unsigned int		bytesPerPixel;									// Holds Number Of Bytes Per Pixel Used In The TGA File
	unsigned int		imageSize;										// Used To Store The Image Size When Setting Aside Ram
	unsigned int		temP;											// TemPorary Variable
	unsigned int		tyPe=GL_RGBA;									// Set The Default GL Mode To RBGA (32 BPP)

	FILE *file = fopen(filename, "rb");							// OPen The TGA File

	if(	file==NULL ||											// Does File Even Exist?
		fread(TGAcomPare,1,sizeof(TGAcomPare),file)!=sizeof(TGAcomPare) ||	// Are There 12 Bytes To Read?
		memcmp(TGAheader,TGAcomPare,sizeof(TGAheader))!=0				||	// Does The Header Match What We Want?
		fread(header,1,sizeof(header),file)!=sizeof(header))				// If So Read Next 6 Header Bytes
	{
		if (file == NULL)										// Did The File Even Exist? *Added Jim Strong*
			return FALSE;										// Return False
		else													// Otherwise
		{
			fclose(file);										// If Anything Failed, Close The File
			return FALSE;										// Return False
		}
	}

	texture->width  = header[1] * 256 + header[0];				// Determine The TGA Width	(highbyte*256+lowbyte)
	texture->height = header[3] * 256 + header[2];				// Determine The TGA Height	(highbyte*256+lowbyte)
    
 	if(	texture->width	<=0	||									// Is The Width Less Than Or Equal To Zero
		texture->height	<=0	||									// Is The Height Less Than Or Equal To Zero
		(header[4]!=24 && header[4]!=32))						// Is The TGA 24 or 32 Bit?
	{
		fclose(file);											// If Anything Failed, Close The File
		return FALSE;											// Return False
	}

	texture->bPP	= header[4];								// Grab The TGA's Bits Per Pixel (24 or 32)
	bytesPerPixel	= texture->bPP/8;							// Divide By 8 To Get The Bytes Per Pixel
	imageSize		= texture->width*texture->height*bytesPerPixel;	// Calculate The Memory Required For The TGA Data

	texture->imageData=(unsigned char *)malloc(imageSize);			// Reserve Memory To Hold The TGA Data

	if(	texture->imageData==NULL ||								// Does The Storage Memory Exist?
		fread(texture->imageData, 1, imageSize, file)!=imageSize)	// Does The Image Size Match The Memory Reserved?
	{
		if(texture->imageData!=NULL)							// Was Image Data Loaded
			free(texture->imageData);							// If So, Release The Image Data

		fclose(file);											// Close The File
		return FALSE;											// Return False
	}

	for(unsigned int i=0; i<int(imageSize); i+=bytesPerPixel)			// LooP Through The Image Data
	{															// SwaPs The 1st And 3rd Bytes ('R'ed and 'B'lue)
		temP=texture->imageData[i];								// TemPorarily Store The Value At Image Data 'i'
		texture->imageData[i] = texture->imageData[i + 2];		// Set The 1st Byte To The Value Of The 3rd Byte
		texture->imageData[i + 2] = temP;						// Set The 3rd Byte To The Value In 'temP' (1st Byte Value)
	}

	fclose (file);												// Close The File

	// Build A Texture From The Data
	glGenTextures(1, &texture[0].texID);						// Generate OPenGL texture IDs

	glBindTexture(GL_TEXTURE_2D, texture[0].texID);				// Bind Our Texture
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Linear Filtered
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// Linear Filtered
	
	if (texture[0].bPP==24)										// Was The TGA 24 Bits
	{
		tyPe=GL_RGB;											// If So Set The 'tyPe' To GL_RGB
	}

	glTexImage2D(GL_TEXTURE_2D, 0, tyPe, texture[0].width, texture[0].height, 0, tyPe, GL_UNSIGNED_BYTE, texture[0].imageData);

	return true;												// Texture Building Went Ok, Return True
}


BOOL LoadGLTexture(unsigned int *texPntr, char* name)
{
	BOOL success = FALSE;
	AUX_RGBImageRec *TextureImage = NULL;
	
	glGenTextures(1, texPntr);						// Generate 1 texture

	FILE* test=NULL;
	TextureImage = NULL;

	test = fopen(name, "r");						// test to see if the file exists
	if (test != NULL) {								// if it does
		fclose(test);									// close the file
		TextureImage = auxDIBImageLoad(name);			// and load the texture
	}

	if (TextureImage != NULL) {						// if it loaded
		success = TRUE;

		// Typical Texture Generation Using Data From The Bitmap
		glBindTexture(GL_TEXTURE_2D, *texPntr);
		glTexImage2D(GL_TEXTURE_2D, 0, 3,    TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	}

	if (TextureImage->data)
		free(TextureImage->data);

	return success;
}



GLvoid BuildFont(GLvoid)										// Build Our Font DisPlay List
{
	base=glGenLists(95);										// Creating 95 DisPlay Lists
	glBindTexture(GL_TEXTURE_2D, textures[9].texID);			// Bind Our Font Texture
	for (int looP=0; looP<95; looP++)							// LooP Through All 95 Lists
	{
		float cx=float(looP%16)/16.0f;							// X Position Of Current Character
		float cy=float(looP/16)/8.0f;							// Y Position Of Current Character

		glNewList(base+looP,GL_COMPILE);						// Start Building A List
			glBegin(GL_QUADS);									// Use A Quad For Each Character
				glTexCoord2f(cx,         1.0f-cy-0.120f); glVertex2i(0,0);	// Texture / Vertex Coord (Bottom Left)
				glTexCoord2f(cx+0.0625f, 1.0f-cy-0.120f); glVertex2i(16,0);	// Texutre / Vertex Coord (Bottom Right)
				glTexCoord2f(cx+0.0625f, 1.0f-cy);		  glVertex2i(16,16);// Texture / Vertex Coord (ToP Right)
				glTexCoord2f(cx,         1.0f-cy);		  glVertex2i(0,16);	// Texture / Vertex Coord (ToP Left)
			glEnd();											// Done Building Our Quad (Character)
			glTranslated(10,0,0);								// Move To The Right Of The Character
		glEndList();											// Done Building The DisPlay List
	}															// LooP Until All 256 Are Built
}

GLvoid glPrint(GLint x, GLint y, const char *string, ...)		// Where The Printing HaPPens
{
	char		text[256];										// Holds Our String
	va_list		aP;												// Pointer To List Of Arguments

	if (string == NULL)											// If There's No Text
		return;													// Do Nothing

	va_start(aP, string);										// Parses The String For Variables
	    vsprintf(text, string, aP);								// And Converts Symbols To Actual Numbers
	va_end(aP);													// Results Are Stored In Text

	glBindTexture(GL_TEXTURE_2D, textures[9].texID);			// Select Our Font Texture
	glPushMatrix();												// Store The Modelview Matrix
	glLoadIdentity();											// Reset The Modelview Matrix
	glTranslated(x,y,0);										// Position The Text (0,0 - Bottom Left)
	glListBase(base-32);										// Choose The Font Set
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);			// Draws The DisPlay List Text
	glPopMatrix();												// Restore The Old Projection Matrix
}

void InitText()
{
	glEnable(GL_TEXTURE_2D);									// Enable Texture MaPPing
	glDisable(GL_DEPTH_TEST);							// Disables DePth Testing

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glPushMatrix();										// Store The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix
	glOrtho(0,640,0,480,-1,1);							// Set UP An Ortho Screen
	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glPushMatrix();										// Store The Modelview Matrix
	glLoadIdentity();									// Reset The Modelview Matrix
	glMatrixMode(GL_PROJECTION);								// Select The Projection Matrix
	glLoadIdentity();											// Reset The Projection Matrix
	glOrtho(0,640,0,480,-1,1);				// Set UP An Ortho Screen
	glMatrixMode(GL_MODELVIEW);									// Select The Modelview Matrix
	glDisable(GL_COLOR_MATERIAL);
	glDisable(GL_LIGHT0);	
	glDisable(GL_LIGHTING);
}

void DoneText()
{
	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glPopMatrix();										// Restore The Old Projection Matrix
	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glPopMatrix();										// Restore The Old Projection Matrix
	glEnable(GL_DEPTH_TEST);							// Enables DePth Testing
	glDisable(GL_TEXTURE_2D);
}

void ReCalcPlayer(tPlayer *Player)
{
	while(Player->Rot.Y<0) Player->Rot.Y+=Pi*2;
	while(Player->Rot.Y>Pi*2) Player->Rot.Y-=Pi*2;
	Player->Matrix=CalcNewMatrix(vSet(0,1,0),vNull,KartMatrix(),Player->Rot.Y);
	ReCalcSkelNode(Player->Obj->Skel,Player->Matrix,Player->Pos);
//	ReCalcPoints(Player->Obj);
}

void SetAnimation(tPlayer *Player,long Index)
{
	tSkelList *SkelList;
	tAnimationList *AnimList;

	Player->AniCnt=0;
	Player->Ani=ModelAni[Player->ModelType][Index];
	Player->AniIndex=Index;

	SkelList=Player->Obj->SkelList;
	AnimList=Player->Ani->NodeList;

	while(SkelList!=NULL)
	{
// Letzter Animationsschritt , no calc
		SkelList->Node->Flags=AnimList->Frame0->Node->Flags;
		SkelList=SkelList->Next;
		AnimList=AnimList->Next;
	}

	if(Index<_Ani_Punch) Player->AttackPower=0;
	if((Index>=_Ani_Punch)&&(Index<_Ani_Jump)) 
		Player->AttackRefresh=_AttackRefresh;
}

void EndAnimation(tPlayer *Player)
{
	Player->AniCnt=Player->Ani->EndCount+1;
}

void ClipAgainstSector(tSector *Sector,tP3D Pos,tP3D *ModP,float AbSt,tP3D *NewSpeed)
{
	if(Sector==NULL) return;
	if(IsInBoundBox(Sector->BoundBox,Pos,AbSt+2))
	{
		if((Sector->Mesh!=NULL)&&(!Flag(Sector->Flags,_SF_WalkThrough)))
		{
			ClipPointAgainst(Pos,ModP,Sector,AbSt,NewSpeed);
		}
		ClipAgainstSector(Sector->SubSector,Pos,ModP,AbSt,NewSpeed);
	}
	ClipAgainstSector(Sector->Next,Pos,ModP,AbSt,NewSpeed);
}

void MovePlayer(tPlayer *DaPlayer)
{
	long I;
	tP3D Richtung,NSpeed;
	float Abst;

	for(I=0;I<Players;I++)
	if(Player[I]->Active)
	{
		if(Player[I]!=DaPlayer)
		{
			Richtung=vSub(Player[I]->Pos,DaPlayer->Pos);
			Abst=vLength2(Richtung);
			Norm(&Richtung);

			if(Abst<_Player_MinAbstand)
			{
				DaPlayer->Speed=vSub(DaPlayer->Speed,vMulK(Richtung,_Player_Wegdrueck));
			}

		}
	}
	

	DaPlayer->Speed=vAdd(DaPlayer->Speed,DaPlayer->Obj->Skel->Speed);
	DaPlayer->Obj->Skel->Speed=vNull;
	
	NSpeed=vNull;
	ClipAgainstSector(Level,vAdd(DaPlayer->Pos,vNull),&DaPlayer->Speed,7.5,&NSpeed);
	DaPlayer->Pos=vAdd(DaPlayer->Pos,DaPlayer->Speed);
	if((NSpeed.X!=0)||(NSpeed.Y!=0)||(NSpeed.Z!=0))
		DaPlayer->Speed=NSpeed;

	DaPlayer->Speed.Y-=_Fall_Speed;
	if(DaPlayer->Speed.Y<0) DaPlayer->Speed.Y*=_Fall_Mul;
	DaPlayer->Speed=vMulK(DaPlayer->Speed,PBrems);

	CalcObjUsingAnimationAndCountReal(DaPlayer->Obj,DaPlayer->Ani,DaPlayer->AniCnt);
	SkelPhysicAusgleich(DaPlayer->Obj->Skel);

//	CalcSkelSpeeds(DaPlayer->Obj->Skel);
	CalcSkelMovement(DaPlayer->Obj->Skel,false);


	if(DaPlayer->AttackRefresh>0) DaPlayer->AttackRefresh--;
	DaPlayer->AniCnt+=AniStep;
	if(DaPlayer->Ani->LoopPoint!=-1)
	{
		if(DaPlayer->AniCnt>=DaPlayer->Ani->LoopPoint)
		{
			DaPlayer->AniCnt=0;
		}
	} else {
		if(DaPlayer->AniCnt>=DaPlayer->Ani->EndCount)
		{
			SetAnimation(DaPlayer,_Ani_Stand);
		}
	}

	if(vLength2(DaPlayer->Speed)<_Ani_Stand_Grenze)
	{
		if(DaPlayer->AniIndex==_Ani_Walk)
		{
			SetAnimation(DaPlayer,_Ani_Stand);
		}
	} else {
		if(DaPlayer->AniIndex==_Ani_Stand)
		{
			SetAnimation(DaPlayer,_Ani_Walk);
		}
	}
}

tP3D GetSpeed(tSkeletonNode *Temp)
{
	tP3D NPos;

⌨️ 快捷键说明

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