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

📄 hge_tut05.cpp

📁 这是我用HGE引擎开发的一个小程序,刚学习到的,请大家下载!
💻 CPP
字号:
/*
** Haaf's Game Engine 1.5
** Copyright (C) 2003-2005, Relish Games
** hge.relishgames.com
**
** hge_tut05 - Using distortion mesh
*/


// Copy the files "particles.png", "menu.wav",
// "font1.fnt", "font1.png" and "trail.psi" from
// the folder "precompiled" to the folder with
// executable file. Also copy hge.dll and bass.dll
// to the same folder.


#include "hge.h"
#include "hgefont.h"
#include "hgedistort.h"

#include <math.h>


// Pointer to the HGE interface.
// Helper classes require this to work.
HGE *hge=0;

HTEXTURE			tex;				//纹理句柄

// Pointers to the HGE objects we will use
hgeDistortionMesh*	dis;				//变形网格指针	
hgeFont*			fnt;				//字体指针

// Some "gameplay" variables
const int nRows=16;						//行数
const int nCols=16;						//列数
const float cellw=512.0f/(nCols-1);		//节点宽度
const float cellh=512.0f/(nRows-1);		//节点高度

const float meshx=144;					//网格左上角坐标x
const float meshy=44;                   //网格左上角坐标y


bool FrameFunc()						//帧函数
{
	float dt=hge->Timer_GetDelta();		//从上一次调用帧函数到现在所经历的时间
	static float t=0.0f;				//累计时间的静态变量
	static int trans=0;					//模式

	int i;
	int j;
	int col;
	float r;
	float a;
	float dx;
	float dy;
	
	t+=dt;								//累计时间
	

	// Process keys
	switch(hge->Input_GetKey())			//检查按键
	{
		case HGEK_ESCAPE:				//按下ESC
			return true;
		case HGEK_SPACE:				//按下空格键	
			if(++trans > 2)				//按下空格键,模式加1
				trans=0;
			dis->Clear(0xFF000000);		//网格颜色清零
			break;
	}
	
	// Calculate new displacements and coloring for one of the three effects
	switch(trans)							//根据模式作变换
	{
		case 0:								//模式0
			for(i=1;i<nRows-1;i++)			//两重循环对每个节点进行变形	
			{
				for(j=1;j<nCols-1;j++)
				{
					//这里让每个节点绕自己的左上角坐标旋转,选装半径是5,旋转角度根据时间计算
					dis->SetDisplacement(j,i,
			cosf(t*10+(i+j)/2)*5,sinf(t*10+(i+j)/2)*5,HGEDISP_NODE);
				}
			}
			break;

		case 1:								//模式1
			for(i=0;i<nRows;i++)			//两重循环对每个节点进行变形	
			{
				for(j=1;j<nCols-1;j++)
				{
					//让每个节点在x方向上按余弦变化,其变化的半径是15,角度是根据时间计算的
					dis->SetDisplacement(j,i,
						cosf(t*5+j/2)*15,0,HGEDISP_NODE);
					//设置颜色变换 ,颜色根据时间和行列的位置做余弦变化
					col=int((cosf(t*5+(i+j)/2)+1)*35);
					//设置颜色
					//颜色值= 透明度左移24 位 | 红色左移16位 | 绿色左移8位 | 蓝色
					dis->SetColor(j,i,
						0xFF<<24 | col<<16 | col<<8 | col * 2);
					
				}
			}
			break;

		case 2:	
			for(i=0;i<nRows;i++)
			{
				for(j=0;j<nCols;j++)
				{
					r=sqrtf(powf(j-(float)nCols/2,2)+powf(i-(float)nRows/2,2));	//每个网格距离中心的距离
					a=r*cosf(t*2)*0.1f;					//根据距离算出每个网格根据时间相对于中心旋转的角度
					dx=sinf(a)*(i*cellh-256)+cosf(a)*(j*cellw-256); //计算旋转后的x位移
					dy=cosf(a)*(i*cellh-256)-sinf(a)*(j*cellw-256);	//计算旋转后的y位移
					dis->SetDisplacement(j,i,dx,dy,HGEDISP_CENTER);	//变形
					col=int((cos(r+t*4)+1)*50);                     //根据时间和距离计算颜色
					dis->SetColor(j,i,0xFF<<24 | col<<16 | (col/2)<<8 | col / 4); //设置混合后的颜色
				}
			}
			break;
	}

	// Render graphics
	hge->Gfx_BeginScene();
		hge->Gfx_Clear(0);
		dis->Render(meshx, meshy);									//渲染网格
		fnt->printf(5,5,"dt:%.3f\nFPS:%d\n\nUse your\nSPACE!", dt, hge->Timer_GetFPS());
	hge->Gfx_EndScene();

	return false;
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "hge_tut05.log");
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_TITLE, "HGE Tutorial 05 - Using distortion mesh");
	hge->System_SetState(HGE_WINDOWED, true);
	hge->System_SetState(HGE_SCREENWIDTH, 800);
	hge->System_SetState(HGE_SCREENHEIGHT, 600);
	hge->System_SetState(HGE_SCREENBPP, 32);
	hge->System_SetState(HGE_HIDEMOUSE, false);
	hge->System_SetState(HGE_USESOUND, false);

	if(hge->System_Initiate()) {

		// Load sound and texture
		tex=hge->Texture_Load("texture.jpg");
		if(!tex)
		{
			// If one of the data files is not found, display
			// an error message and shutdown.
			MessageBox(NULL, "Can't load TEXTURE.JPG", "Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
			hge->System_Shutdown();
			hge->Release();
			return 0;
		}

		// Create a distortion mesh
		dis=new hgeDistortionMesh(nCols, nRows);		//创建变形网格
		dis->SetTexture(tex);							//设置纹理
		dis->SetTextureRect(0,0,512,512);				
		dis->SetBlendMode(BLEND_COLORADD | BLEND_ALPHAADD | BLEND_ZWRITE);
		dis->Clear(0xFF000000);	

		// Load a font
		fnt=new hgeFont("font1.fnt");				//加载字体

		// Let's rock now!
		hge->System_Start();

		// Delete created objects and free loaded resources
		//释放资源
		if (fnt)
			delete fnt;
		if (dis)
			delete dis;
		hge->Texture_Free(tex);
	}

	// Clean up and shutdown
	hge->System_Shutdown();
	hge->Release();
	return 0;
}

⌨️ 快捷键说明

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