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

📄 hge_tut06.cpp

📁 一个利用hge游戏引擎
💻 CPP
字号:
////  http://dotlive.cnblogs.com/ 

#include "include\hge.h"
#include "include\hgefont.h"
#include "include\hgegui.h"
#include <math.h>

#include "menuitem.h"
#include "TetrisLogic.h"

//////////////////////////////////////////////////////////////////////////
// Game Data
int g_nGamestatue = 0;// 0  1  2
// hgeQuad				quadStage;
TetrisLogic* g_pGameLogic;
//////////////////////////////////////////////////////////////////////////


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

// Some resource handles
HEFFECT				snd;
HTEXTURE			tex;
hgeQuad				quad;

// Pointers to the HGE objects we will use
hgeGUI				*gui;
hgeFont				*fnt;
hgeSprite			*spr;
#define OFFX 250
#define OFFY 50
#define BOXW 15
hgeQuad quadCell;
class IGE_HEGImp : public IGameEngine
{
public:
	IGE_HEGImp(HGE *pHge);
	virtual void IGE_DrawABoxCell( int x,int y,DWORD color,int id );
	virtual void IGE_DrawText( int x,int y,DWORD c,const char* pStr );
	virtual int IGE_GetRandomInt( int min, int max );

protected:
	HGE *m_pHge;

};

IGE_HEGImp::IGE_HEGImp( HGE *pHge ) :m_pHge(pHge)
{
	quadCell.blend = BLEND_ALPHABLEND | BLEND_COLORMUL | BLEND_ZWRITE;
	for(int i=0;i<4;i++)
	{
		quadCell.v[i].z=0.15f;
		quadCell.v[i].col=0xFF0000FF;
	}
}

void IGE_HEGImp::IGE_DrawABoxCell( int x,int y,DWORD color,int id ) 
{
	x++;
	y++;
	quadCell.v[0].x=OFFX+x*BOXW; quadCell.v[0].y=OFFY+y*BOXW; 
	quadCell.v[1].x=OFFX+(x+1)*BOXW; quadCell.v[1].y=OFFY+y*BOXW; 
	quadCell.v[2].x=OFFX+(x+1)*BOXW; quadCell.v[2].y=OFFY+(y+1)*BOXW; 
	quadCell.v[3].x=OFFX+x*BOXW; quadCell.v[3].y=OFFY+(y+1)*BOXW; 
	for (int i=0;i<4;i++)
	{
		quadCell.v[i].col = color;
	}
	m_pHge->Gfx_RenderQuad(&quadCell);
}

void IGE_HEGImp::IGE_DrawText( int x,int y,DWORD c,const char* pStr ) 
{
	fnt->SetColor(c);
	fnt->printf(OFFX+x*BOXW,OFFY+y*BOXW, HGETEXT_LEFT, pStr);
}

int IGE_HEGImp::IGE_GetRandomInt( int min, int max ) 
{
	return m_pHge->Random_Int(min,max);
}

bool MenuFrameFunc()
{
	float dt=hge->Timer_GetDelta();
	int id;
	static int lastid=0;

	// If ESCAPE was pressed, tell the GUI to finish
//	if(hge->Input_GetKeyState(HGEK_ESCAPE)) 
	if (hge->Input_GetKey()==HGEK_ESCAPE)
	{
		lastid=5; 
		gui->Leave();
	}

	// We update the GUI and take an action if
	// one of the menu items was selected
	id=gui->Update(dt);
	if(id == -1)
	{
		switch(lastid)
		{
		case 1:
			g_nGamestatue = 1;
			g_pGameLogic->Init();
			gui->SetFocus(1);
			gui->Enter();
			break;
		case 2:
		case 3:
		case 4:
			g_nGamestatue = 0;
			gui->SetFocus(1);
			gui->Enter();
			break;
			
		case 5: return true;
		}
	}
	else if(id) { lastid=id; gui->Leave(); }
	
	return false;
}

int GetTLKey( int HGEKeyCode )
{
	int nTLKeyCode = 0;
	switch(hge->Input_GetKey())
	{
	case HGEK_RIGHT:
		nTLKeyCode = TetrisLogic::KeyRight;
		break;
	case HGEK_LEFT:
		nTLKeyCode = TetrisLogic::KeyLeft;
		break;
	case HGEK_DOWN:
		nTLKeyCode = TetrisLogic::KeyDown;
		break;
	case HGEK_ENTER:
		nTLKeyCode = TetrisLogic::KeyTurn;
		break;
	case HGEK_ESCAPE:
		nTLKeyCode = TetrisLogic::KeyEsc;
		break;
	default:
		nTLKeyCode = TetrisLogic::KeyNothing;
		break;
	}

	return nTLKeyCode;
}

bool GamePlayFrameFunc()
{
//	if(hge->Input_GetKeyState(HGEK_ENTER)) {
//		g_nGamestatue = 0;
//	}

	float dt=hge->Timer_GetDelta();
	g_pGameLogic->GameStep(dt);
	g_pGameLogic->GameInput( GetTLKey(hge->Input_GetKey()) );

	// Process keys
	int i=0;
	switch(hge->Input_GetKey())
	{
	case HGEK_ESCAPE:
			g_nGamestatue = 0;
			break;
// 		
// 	case HGEK_RIGHT:
// 		for(i=0;i<4;i++)
// 			quadStage.v[i].x += speed;
// 		break;
// 	case HGEK_LEFT:
// 		for(i=0;i<4;i++)
// 			quadStage.v[i].x -= speed;
// 		break;
// 	case HGEK_DOWN:
// 		for(i=0;i<4;i++)
// 			quadStage.v[i].y += speed;
// 		break;
// 	case HGEK_UP:
// 		for(int i=0;i<4;i++)
// 			quadStage.v[i].y -= speed;
// 		break;
	}
	
	return false;
}

bool FrameFunc()
{
	float dt=hge->Timer_GetDelta();
	static float t=0.0f;
	float tx,ty;
	bool bRet = false;
	
	switch(g_nGamestatue)
	{
	case 0:
		bRet = MenuFrameFunc();
		break;
	case 1:
		bRet = GamePlayFrameFunc();
		break;
// 	case 2:		
// 		break;
	default:
		;
	}

	// Here we update our background animation
	t+=dt;
	tx=50*cosf(t/60);
	ty=50*sinf(t/60);

	quad.v[0].tx=tx;        quad.v[0].ty=ty;
	quad.v[1].tx=tx+800/64; quad.v[1].ty=ty;
	quad.v[2].tx=tx+800/64; quad.v[2].ty=ty+600/64;
	quad.v[3].tx=tx;        quad.v[3].ty=ty+600/64;

	return bRet;
}

void PrintLogo()
{
	fnt->SetColor(0xFFFFFFFF);
	fnt->printf(575, 572, HGETEXT_LEFT, "dotlive.cnblogs.com");
}

bool GamePlayRenderFunc()
{
//	hge->Gfx_RenderQuad(&quadStage);
	fnt->SetColor(0xFFFFFFFF);
	fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d", hge->Timer_GetDelta(), hge->Timer_GetFPS());
	TetrisBox stFallingBox = g_pGameLogic->GetFallingBox();

//	PrintLogo();

	g_pGameLogic->Render();

//	hge->Gfx_RenderQuad();

	return false;
}

bool RenderFunc()
{
	// Render graphics
	hge->Gfx_BeginScene();

//	hge->Gfx_RenderQuad(&quad);
	hge->Gfx_Clear(0);
	switch(g_nGamestatue)
	{
		case 0:
			gui->Render();
			break;
		case 1:
			GamePlayRenderFunc();
//			gui->Render();
			break;
// 		case 2:
// 
// 			break;
		default:
			;
	}

	PrintLogo();

	hge->Gfx_EndScene();
	return false;
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	int i=0;

	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "hge_tut06.log");
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
	hge->System_SetState(HGE_TITLE, "HGE Tutorial 06 - Creating menus");
	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_FPS, 20);

//	hge->System_Initiate();

	if(hge->System_Initiate())
	{
// 		quadStage.tex = hge->Texture_Load("bg2.png");
// 		if(!quadStage.tex)
// 		{
// 			return 0;
// 		}
// 		quadStage.blend = BLEND_ALPHABLEND | BLEND_COLORMUL | BLEND_ZWRITE;
// 		for(i=0;i<4;i++)
// 		{
// 			// Set up z-coordinate of vertices
// 			quadStage.v[i].z=0.15f;
// 			// Set up color. The format of DWORD col is 0xAARRGGBB
// 			// quadStage.v[i].col=0xFFFFFFFF;
// 			quadStage.v[i].col=0xFFFFFFFF;
// 		}
// 		quadStage.v[0].x=250; quadStage.v[0].y=50;
// 		quadStage.v[1].x=550; quadStage.v[1].y=50; 
// 		quadStage.v[2].x=550; quadStage.v[2].y=550;
// 		quadStage.v[3].x=250; quadStage.v[3].y=550; 
// 		quadStage.v[0].tx=0;       quadStage.v[0].ty=0;
// 		quadStage.v[1].tx=300/64;		  quadStage.v[1].ty=0;
// 		quadStage.v[2].tx=300/64; 	  quadStage.v[2].ty=500/64;
// 		quadStage.v[3].tx=0;       quadStage.v[3].ty=500/64;


		// Load sound and textures
		quad.tex=hge->Texture_Load("bg.png");
		tex=hge->Texture_Load("cursor.png");
		snd=hge->Effect_Load("menu.wav");
		if(!quad.tex || !tex || !snd)
		{
			// If one of the data files is not found, display
			// an error message and shutdown.
			MessageBox(NULL, "Can't load BG.PNG, CURSOR.PNG or MENU.WAV", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
			hge->System_Shutdown();
			hge->Release();
			return 0;
		}

		// Set up the quad we will use for background animation
		quad.blend=BLEND_ALPHABLEND | BLEND_COLORMUL | BLEND_NOZWRITE;

		for(i=0;i<4;i++)
		{
			// Set up z-coordinate of vertices
			quad.v[i].z=0.5f;
			// Set up color. The format of DWORD col is 0xAARRGGBB
			quad.v[i].col=0xFFFFFFFF;
		}

		quad.v[0].x=0; quad.v[0].y=0; 
		quad.v[1].x=800; quad.v[1].y=0; 
		quad.v[2].x=800; quad.v[2].y=600; 
		quad.v[3].x=0; quad.v[3].y=600; 


		// Load the font, create the cursor sprite
		fnt=new hgeFont("font1.fnt");
		spr=new hgeSprite(tex,0,0,32,32);

		// Create and initialize the GUI
		gui=new hgeGUI();

		gui->AddCtrl(new hgeGUIMenuItem(1,fnt,snd,400,200,0.0f,"Play"));
		gui->AddCtrl(new hgeGUIMenuItem(2,fnt,snd,400,240,0.1f,"Options"));
		gui->AddCtrl(new hgeGUIMenuItem(3,fnt,snd,400,280,0.2f,"Instructions"));
		gui->AddCtrl(new hgeGUIMenuItem(4,fnt,snd,400,320,0.3f,"Credits"));
		gui->AddCtrl(new hgeGUIMenuItem(5,fnt,snd,400,360,0.4f,"Exit"));

		gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
		gui->SetCursor(spr);
		gui->SetFocus(1);
		gui->Enter();

		IGameEngine* pGE = new IGE_HEGImp(hge);
		g_pGameLogic = new TetrisLogic (pGE);

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

		delete g_pGameLogic;
		delete pGE;

		// Delete created objects and free loaded resources
		delete gui;
		delete fnt;
		delete spr;
		hge->Effect_Free(snd);
		hge->Texture_Free(tex);
		hge->Texture_Free(quad.tex);
// 		hge->Texture_Free(quadStage.tex);
	}
/**/
	// Clean up and shutdown
	hge->System_Shutdown();
	hge->Release();
	return 0;
}

⌨️ 快捷键说明

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