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

📄 main.cpp

📁 《光线引擎演示程序》执行程序+源代码,《光线引擎演示程序》执行程序+源代码
💻 CPP
字号:


#include "character.h"


LightEngine *engine;
LightCamera *camera;
LightFrustum *frustum;
LightInput *input;
LightBsp *g_Level;
vec3_t *aabb;

LightVideo *video;	
LightText *text;
LightBrush *brush;
LightSolid *foot, *body;
LightLensFlare *flare;
LightTexture *texture[8];
LightTexture *solid_tex;
LightTexture *shadow;
LightLight *light;
int cx, cy;
RECT rect;
bool quit;
// 摄像机参数
vec3_t eye, center;

// 角色实例
Character player;
Character boss;
Character boss2;
Character boss3;
Character boss4;
Character boss5;

// This is our velocity vector that is added to our camera position for gravity/jumping
CVector3 g_vVelocity = CVector3(0, 0, 0);

// Our global float that stores the elapsed time between the current and last frame
double g_FrameInterval = 0.0f;


//  处理玩家输入
void Input()
{
	// 处理玩家指令		
	POINT point;
	int x, y, cx, cy;
	vec3_t rotate;
	vec3_t upright;
	RECT rect;
	int time;
	unsigned int key;

	time = engine->GetTickCount();

	// 根据光标位置计算摄像机旋转角度,
	// 并将光标位置设置为窗口中心
	input->GetCursorPos( &point );
	GetClientRect( engine->GetWindowHandle(), &rect );
	cx = (rect.right - rect.left) / 2;
	cy = (rect.bottom - rect.top) / 2;
	input->SetCursorPos( cx, cy );
	x = point.x - cx;
	y = point.y - cy;	
	rotate[0] = player.player.rotate[0] + (float)x / player.player.mouse_speed;
	rotate[1] = player.player.rotate[1] - (float)y / player.player.mouse_speed;
	rotate[2] = 0;

	// 限制摄像机旋转角度
	if( rotate[0] > 2 * G_PI )
		rotate[0] -= 2 * G_PI;
	if( rotate[0] < 0 )
		rotate[0] += 2 * G_PI;
	//if( rotate[1] > 0.075 * G_PI )
	//	rotate[1] = 0.075 * G_PI;
	if( rotate[1] > 0.375 * G_PI )
		rotate[1] = 0.375 * G_PI;
	if( rotate[1] < -0.425 * G_PI )
		rotate[1] = -0.425 * G_PI;
	player.rotate[0] = rotate[0];
	player.rotate[1] = rotate[1];
	player.rotate[2] = rotate[2];

	// 更新角色状态
	player.Update();
	boss.Update();
	/*boss2.Update();
	boss3.Update();
	boss4.Update();
	boss5.Update();*/

	// 读取按键信息
		player.player.forward = 0;
		player.player.right = 0;
		key = input->GetKey();
		if( input->GetKeyState( KEY_W ) ) // 前进
			player.player.forward = 1;
		else
		if( input->GetKeyState( KEY_S ) ) // 后退
			player.player.forward = -1;
		if( input->GetKeyState( KEY_A ) ) // 左移
			player.player.right = -1;
		else
		if( input->GetKeyState( KEY_D ) ) // 右移
			player.player.right = 1;
		if( input->GetKeyState( KEY_MOUSE_BUTTON0 ) ) // 射击
			if( player.player.shoot == 0 )
			{
				player.player.shoot = 1;
				player.player.boss = boss.player.md3;
			}
		if( input->GetKeyState( KEY_MOUSE_BUTTON1 ) ) // 射击
			if( player.player.upper == 0 )
				player.player.shoot = 2;
		if( input->GetKeyState( KEY_SPACE ) ) // 跳		
				player.player.jump = 1;

		if( key == KEY_ESCAPE )
			quit = TRUE;

		// 设置听众位置
		player.player.step[0]->SetListenerPos( player.player.pos[0], player.player.pos[1], player.player.pos[2] );
		//player.player.sjump->SetListenerPos( player.player.pos[0], player.player.pos[1], player.player.pos[2] );

}

// 显示屏幕信息
void ShowInfo()
{
	// 显示帧速(FPS)
	char fps[100];
	sprintf( fps, "帧速: %d 帧/秒", engine->GetFramePerSecond() );
	text->Color( 0, 255, 0 );
	text->SetTextPos( 40, rect.bottom-50 );
	text->DrawText( fps );
	text->SetTextPos( 500, 490 );
	sprintf( fps, "本程序使用光线游戏引擎编写,请访问下面的网址了解更多情况:" );
	text->SetTextPos( 20, 20 );		
	text->DrawText( fps );
	sprintf( fps, "http://bbs.gameres.com/showforum.asp?forumid=54" );
	text->SetTextPos( 20, 40 );		
	text->DrawText( fps );
	sprintf( fps, "按键:W S A D 鼠标左右键、空格,ESC 退出" );
	text->SetTextPos( rect.right-265, rect.bottom - 50 );
	text->DrawText( fps );
		
	// 显示准星
	brush->Line2D( cx-10, cy,    cx-5,  cy    );
	brush->Line2D( cx+5,  cy,    cx+10, cy    );
	brush->Line2D( cx,    cy-10, cx,    cy-5  );
	brush->Line2D( cx,    cy+5,  cx,    cy+10 );
}

// 主程序
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	float angle;
	int seq = 0;

	// 初始化光线引擎
	engine = InitialLightEngine();

	// 查询输入设备句柄
	input = engine->NewInput();

	// 设置显示模式为窗口模式
	engine->SetWindowMode(80, 60, 640, 480, 0);
	//engine->SetDisplayMode( 0, 0, 0, 0, 0 );
	// 关闭垂直同步
	engine->Disable( LIGHT_SWAP_INTERVAL );
	// 初始化摄像机
	camera = engine->NewCamera();
	// 初始化视锥体
	frustum = engine->NewFrustum();
	// 初始化视频(音频)接口
	video = engine->NewVideo();
	// 初始化文字
	text = engine->NewText();
	// 初始化画笔
	brush = engine->NewBrush();
    // 初始人物物理模型
	foot = engine->NewSolid();
	body = engine->NewSolid();
	// 初始化镜头光晕
	flare = engine->NewLensFlare();
	// 初始化光源
	light = engine->NewLight();
	// 初始化几何体纹理
	solid_tex = engine->NewTexture();

	ShowCursor( FALSE );
	ShowCursor( FALSE );	
	engine->Enable( LIGHT_COLOR_KEY );
	engine->SetColorKey( 0, 0, 255 );	

	// 计算屏幕大小
	GetClientRect( engine->GetWindowHandle(), &rect );
	cx = (rect.right - rect.left) / 2;
	cy = (rect.bottom - rect.top) / 2;

	// 显示正在读取资料的提示信息
	// 清除屏幕内容	
	text->Color( 0, 255, 0 );
	text->SetTextPos( cx - 95, cy - 18 );
	text->DrawText( "正在读取资料,请稍候......" );
	engine->SwapBuffers();
	
	// 设置缺省目录
	//engine->SetString( LIGHT_WORK_DIRECTORY,    "e:\\Light Game Engine\\example\\室内场景" );
	engine->SetString( LIGHT_WORK_DIRECTORY,    "" );
	engine->SetString( LIGHT_MUSIC_DIRECTORY,   "musics" );
	engine->SetString( LIGHT_SOUND_DIRECTORY,   "sounds" );
	engine->SetString( LIGHT_TEXTURE_DIRECTORY, "" );
	engine->SetString( LIGHT_MAP_DIRECTORY,     "maps" );
	engine->SetString( LIGHT_MODEL_DIRECTORY,   "models\\girl" );
	engine->SetString( LIGHT_SCRIPT_DIRECTORY,  "" );
	engine->SetString( LIGHT_IMAGE_DIRECTORY,   "images" );

	// 读取地图文件
	int p = 0;	
	g_Level = engine->NewBsp();                    // 分配一个地图对象
	//g_Level->Lighteness( 30 );
	g_Level->Lighteness( 3 );                           // 设置地图亮度	
	bool bResult = g_Level->Load( "wyg.bsp" ); // 读取地图文件	
	if(bResult == false)
	{	
		PostQuitMessage(0);
		return 1;
	}
	// 设置地图渲染模式
	//g_Level->RenderMode( LIGHT_RENDERMODE_LINE | IGHT_RENDERMODE_TEXTURE | LIGHT_RENDERMODE_LIGHTMAP );	
	quit = FALSE;

	// 初始化玩家信息
	player.engine = engine;
	player.camera = camera;	
	player.Load();
	player.player.md3->SetPos( player.player.pos[0], player.player.pos[1], player.player.pos[2] );
	player.player.rotate[0] = 2.7f;
	player.player.rotate[1] = -0.3f;

	boss.engine = engine;
	boss.camera = camera;
	boss.Load();
	boss.player.md3->SetPos( player.player.pos[0], player.player.pos[1], player.player.pos[2] );
	//boss.player.pos[0] = -400;
	//boss.player.pos[1] = 130;
	//boss.player.pos[2] = 750;
	boss.rotate[0] = 0.0f;

	boss2.engine = engine;
	boss2.camera = camera;
	boss2.Load();
	boss2.player.pos[0] = 650;
	boss2.player.pos[1] = 100;
	boss2.player.pos[2] = -550;
	boss2.rotate[0] = 2.7f;

	boss3.engine = engine;
	boss3.camera = camera;
	boss3.Load();
	boss3.player.pos[0] = 100;
	boss3.player.pos[1] = 800;
	boss3.player.pos[2] = -350;
	boss3.rotate[0] = 3.5f;

	boss4.engine = engine;
	boss4.camera = camera;
	boss4.Load();
	boss4.player.pos[0] = -900;
	boss4.player.pos[1] = 800;
	boss4.player.pos[2] = -450;
	boss4.rotate[0] = 3.5f;

	boss5.engine = engine;
	boss5.camera = camera;
	boss5.Load();
	boss5.player.pos[0] = 100;
	boss5.player.pos[1] = 50;
	boss5.player.pos[2] = 400;
	boss5.rotate[0] = 5.5f;

	ShowCursor( TRUE );
	input->SetCursorPos( 400, 300 );

	// 设置距离因子
	//gSetRolloffFactor( 0.035 );

	// 创建阴影纹理
	shadow = engine->NewTexture();
	shadow->Create( 256, 256, 1 );

	// 设置透镜光晕
	texture[0] = engine->NewTexture();
	texture[1] = engine->NewTexture();
	texture[2] = engine->NewTexture();
	texture[3] = engine->NewTexture();
	texture[4] = engine->NewTexture();
	texture[5] = engine->NewTexture();
	texture[0]->Load( "textures\\flare\\main1.jpg" );
	texture[1]->Load( "textures\\flare\\main2.jpg" );
	texture[2]->Load( "textures\\flare\\second1.jpg" );
	texture[3]->Load( "textures\\flare\\second2.jpg" );
	texture[4]->Load( "textures\\flare\\cross.jpg" );
	texture[5]->Load( "textures\\flare\\ring.jpg" );	

	flare->SetPosition( 100, 100, 100 );
	//flare->SetPosition( player.pos[0], player.pos[1], player.pos[2] );

	flare->AddMainNode(100,texture[0]);
	flare->AddMainNode(30,texture[4]);
	flare->AddMainNode(30,texture[5]);
	flare->AddMainNode(80,texture[1]);

	flare->AddSubNode(180,2.0,texture[2]);
	flare->AddSubNode(80,1.0,texture[2]);
	flare->AddSubNode(60,1.3,texture[3]);
	flare->AddSubNode(6,1.1,texture[0]);
	flare->AddSubNode(15,1.0,texture[2]);
	flare->AddSubNode(20,1.0,texture[3]);
	flare->AddSubNode(30,-0.4,texture[2]);
	flare->AddSubNode(0,0.90,texture[0]);
	flare->AddSubNode(10,-0.20,texture[0]);
	
	// 播放背景音乐
	video->Open( "dielian.mp3" );
	video->Play();

	// 将模型绑定到地图
	g_Level->AttachModel( player.player.md3 );
	bsp_data_t data;
	g_Level->GetData( &data );
	player.player.md3->SetPos( data.Homeplaces[0].pos[0], data.Homeplaces[0].pos[1], data.Homeplaces[0].pos[2] );
	player.player.rotate[0] = data.Homeplaces[0].angle;
	g_Level->AttachModel( boss.player.md3 );

	// 初始化几何体
	solid_tex->Load( "..\\..\\resource\\wood.jpg" );
	solid_t sc;
	sc.type = LIGHT_SOLID_SPHERE;
	//sc.sphere.radius = 16;  
	sc.sphere.radius = 8;
	sc.quality_slices = 10;
	sc.quality_stacks = 10;	
	VEC3( sc.pos, data.Homeplaces[0].pos[0], data.Homeplaces[0].pos[1], data.Homeplaces[0].pos[2] );
	VEC3( sc.rotate, 1.0f, 0.0f, 0.0f );
	//sc.angle = data.Homeplaces[0].angle;
	sc.angle = 0.0f;
	sc.mass = 0.00001f;
	foot->Build( &sc );
	sc.type = LIGHT_SOLID_CAPSULE;
	sc.capsule.radius = 8;
	sc.capsule.height = 20;
	VEC3( sc.pos, data.Homeplaces[0].pos[0], data.Homeplaces[0].pos[1]+20, data.Homeplaces[0].pos[2] );
	//sc.angle = 90.0f;
	//body->Build( &sc );

	do
	{	
		float angle;
		
		// 设置玩家位置
		//player.player.md3->SetPos( player.player.pos[0], player.player.pos[1]-16, player.player.pos[2] );
		angle = 360 - (player.player.rotate[0] * (( float)180 / G_PI )) + 90;
		player.player.md3->SetRotate( angle, 0, 1.0f, 0 );

		// 设置灯光
		engine->LightAmbient( 1.0f, 1.0f, 1.0f, 1.0f );
		light->Enable();
		//camera->UpdateCamera();
		light->SetPosition( 0, 100, 0, 0.0f );
		light->SetColor( LIGHT_AMBIENT, 1.0, 1.0, 1.0, 1.0f );
		light->SetColor( LIGHT_DIFFUSE, 1.0f, 1.0f, 1.0f, 1.0f );
		light->SetColor( LIGHT_SPECULAR, 1.0f, 1.0f, 1.0f, 1.0f );
		light->SetAttenuation( LIGHT_CONSTANT, 0.0000 );
		light->SetAttenuation( LIGHT_LINEAR, 0.0000 );
		light->SetAttenuation( LIGHT_QUADRATIC, 0.000001 );
		//solid->SetPosition( 0, 300, 0 );
		//solid->Draw();
		//engine->UpdateLight();


		// 清除屏幕内容
		engine->ClearColor( 0, 0, 0, 0 );
		engine->Clear( LIGHT_COLOR_BUFFER_BIT | LIGHT_DEPTH_BUFFER_BIT );
		
		// 设置摄像机位置
		VEC3( eye, player.player.eyes[0], player.player.eyes[1], player.player.eyes[2] );
		VEC3( center, player.player.pos[0],  player.player.pos[1]+40,  player.player.pos[2] );
		camera->LookAt( eye[0], eye[1], eye[2],
			            center[0], center[1], center[2],
						player.player.up[0], player.player.up[1], player.player.up[2] );
		
		// 显示场景
		g_Level->Render( player.player.eyes[0], player.player.eyes[1], player.player.eyes[2] );		

		// 显示人物模型
		//engine->UpdatePhysics();
		//solid_tex->Active();
		//foot->Draw();
		//body->Draw();

		//  处理玩家输入
		Input();

		// 显示屏幕信息
		ShowInfo();

		// 显示调试信息
		/*char fps[100];
		sprintf( fps, "玩家出生点共 %d 个: %f %f %f   角度:%f", data.NumHomeplace, data.Homeplaces[0].pos[0],
			     data.Homeplaces[0].pos[1], data.Homeplaces[0].pos[2],
				 data.Homeplaces[0].angle );
		text->Color( 0, 255, 0 );
		text->SetTextPos( 40, rect.bottom-100 );
		text->DrawText( fps );*/

		// 更新显示
		engine->SwapBuffers();

		video->Play();
	}
	// 配送消息
	while( !quit && engine->DispatchMessage() );

	video->Close();
	// 从内存中释放地图文件
	// 从内存中释放模型文件

	// 释放透镜光晕
	flare->Release();
	
	return 0;
}


/*
typedef enum 
{
	// If one model is set to one of the BOTH_* animations, the other one should be too,
	// otherwise it looks really bad and confusing.

	BOTH_DEATH1 = 0,		// The first twirling death animation
	BOTH_DEAD1,				// The end of the first twirling death animation
	BOTH_DEATH2,			// The second twirling death animation
	BOTH_DEAD2,				// The end of the second twirling death animation
	BOTH_DEATH3,			// The back flip death animation
	BOTH_DEAD3,				// The end of the back flip death animation

	// The next block is the animations that the upper body performs

	TORSO_GESTURE,			// The torso's gesturing animation
	
	TORSO_ATTACK,			// The torso's attack1 animation
	TORSO_ATTACK2,			// The torso's attack2 animation

	TORSO_DROP,				// The torso's weapon drop animation
	TORSO_RAISE,			// The torso's weapon pickup animation

	TORSO_STAND,			// The torso's idle stand animation
	TORSO_STAND2,			// The torso's idle stand2 animation

	// The final block is the animations that the legs perform

	LEGS_WALKCR,			// The legs's crouching walk animation
	LEGS_WALK,				// The legs's walk animation
	LEGS_RUN,				// The legs's run animation
	LEGS_BACK,				// The legs's running backwards animation
	LEGS_SWIM,				// The legs's swimming animation
	
	LEGS_JUMP,				// The legs's jumping animation
	LEGS_LAND,				// The legs's landing animation

	LEGS_JUMPB,				// The legs's jumping back animation
	LEGS_LANDB,				// The legs's landing back animation

	LEGS_IDLE,				// The legs's idle stand animation
	LEGS_IDLECR,			// The legs's idle crouching animation

	LEGS_TURN,				// The legs's turn animation

	MAX_ANIMATIONS			// The define for the maximum amount of animations
} eAnimations;
*/

⌨️ 快捷键说明

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