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

📄 game_font.cpp

📁 自己写的一个D3D游戏Dome。实现了基本的游戏功能
💻 CPP
字号:
#include "..\Include\Game_Font.h"
#include "..\Include\Game_Input.h"
#include "..\Include\Game_App.h"
//#include "..\Include\Game_QuietMesh.h"
//#include "..\Include\Game_Hero.h"

Game_Font::Game_Font(Game_Camera * Camera,Game_Hero * Hero){
	pGame_Camera=Camera;
	pGame_Hero=Hero;
	m_bShow=false;
	m_bEnable=true;
	m_iFrameCount=0;
	m_fFPS=0;
	m_fTime1=0;
	m_fTime2=0;
	m_fTimeSlice=0;
	m_nX=0;
	m_nY=70;
	if(QueryPerformanceFrequency((LARGE_INTEGER*) &m_int64OneSecondTicks)){
		m_bUseLargeTime=true;
		QueryPerformanceCounter((LARGE_INTEGER*) &m_int64TimeTickStartCounts);
	}
	else{
		m_bUseLargeTime=false;
		m_ulTimeStart=timeGetTime();
	}
}

Game_Font::~Game_Font(){
}

WCHAR * Game_Font::Change(WCHAR * wszBuf,char * strInf){
	MultiByteToWideChar( CP_ACP, 0, strInf, -1, wszBuf, MAX_PATH );
	wszBuf[MAX_PATH - 1] = L'\0';
	return wszBuf;
}

VOID Game_Font::Render(float timeStep){
	UpdateFPS();

	sprintf(m_cFps,"FPS :%0.0f",m_fFPS);
	DrawText(Change(m_wFps,m_cFps), m_nX, m_nY, D3DCOLOR_XRGB(0, 255, 255));
	DrawText(L"调试信息(F1)", m_nX, m_nY+15, D3DCOLOR_XRGB(0, 255, 255));
	DrawText(L"碰撞盒子(F2)", m_nX, m_nY+30, D3DCOLOR_XRGB(0, 255, 255));
	DrawText(L"网格显示(F3)", m_nX, m_nY+45, D3DCOLOR_XRGB(0, 255, 255));
	POINT pt;
	GetCursorPos(&pt);
	ScreenToClient(g_sGlobal.g_hwnd,&pt);
	if(m_bShow){
		sprintf(m_cCameraX,"CameraX  : %0.0f",pGame_Camera->GetCameraPos()->x);
		sprintf(m_cCameraY,"CameraY  : %0.0f",pGame_Camera->GetCameraPos()->y);
		sprintf(m_cCameraZ,"CameraZ  : %0.0f",pGame_Camera->GetCameraPos()->z);
		DrawText(Change(m_wCameraX,m_cCameraX), m_nX, m_nY+60, D3DCOLOR_XRGB(0, 0, 255));
		DrawText(Change(m_wCameraY,m_cCameraY), m_nX, m_nY+75, D3DCOLOR_XRGB(0, 0, 255));
		DrawText(Change(m_wCameraZ,m_cCameraZ), m_nX, m_nY+90, D3DCOLOR_XRGB(0, 0, 255));

		sprintf(m_cHeroX,"HeroX : %0.0f",pGame_Hero->m_pGame_SkinMesh->GetPosition()->x);
		sprintf(m_cHeroY,"HeroY : %0.0f",pGame_Hero->m_pGame_SkinMesh->GetPosition()->y);
		sprintf(m_cHeroZ,"HeroZ : %0.0f",pGame_Hero->m_pGame_SkinMesh->GetPosition()->z);
		DrawText(Change(m_wHeroX,m_cHeroX), m_nX, m_nY+105, D3DCOLOR_XRGB(255, 0, 0));
		DrawText(Change(m_wHeroY,m_cHeroY), m_nX, m_nY+120, D3DCOLOR_XRGB(255, 0, 0));
		DrawText(Change(m_wHeroZ,m_cHeroZ), m_nX, m_nY+135, D3DCOLOR_XRGB(255, 0, 0));

		sprintf(m_cMouseXY,"Mouse2Dxy:%0.0d , %0.0d",pt.x,pt.y);
		DrawText(Change(m_wMouseXY,m_cMouseXY), m_nX, m_nY+150, D3DCOLOR_XRGB(255, 0, 0));
	}
	if(g_sGlobal.g_pGame_Input->GetIsKeyDown(DIK_F1)){//帮助信息F1
		if(g_sGlobal.g_pGameApp->m_pGame_Font->m_bEnable){
			g_sGlobal.g_pGameApp->m_pGame_Font->m_bShow=!g_sGlobal.g_pGameApp->m_pGame_Font->m_bShow;
			g_sGlobal.g_pGameApp->m_pGame_Font->m_bEnable=false;
		}
	}
	else
		g_sGlobal.g_pGameApp->m_pGame_Font->m_bEnable=true;

	static bool g_bBoxShow=true;
	if(g_sGlobal.g_pGame_Input->GetIsKeyDown(DIK_F2)){//盒子显示F2
		if(g_bBoxShow){
			g_sGlobal.g_pGameApp->m_pGame_Hero->m_pGame_SkinMesh->m_BoundingBox=!g_sGlobal.g_pGameApp->m_pGame_Hero->m_pGame_SkinMesh->m_BoundingBox;
			for(vector<Game_QuietMesh*>::iterator i=g_sGlobal.g_pGameApp->m_Game_QuietMesh.begin();
				i!=g_sGlobal.g_pGameApp->m_Game_QuietMesh.end();
				i++){
					(*i)->m_pMesh->m_BoundingBox=!((*i)->m_pMesh->m_BoundingBox);
			}
			for(vector<Game_EnemyNpc*>::iterator i=g_sGlobal.g_pGameApp->m_Game_EnemyNpc.begin();
				i!=g_sGlobal.g_pGameApp->m_Game_EnemyNpc.end();
				i++){
					(*i)->m_pGame_SkinMesh->m_BoundingBox=!((*i)->m_pGame_SkinMesh->m_BoundingBox);
			}
			g_bBoxShow=false;
		}
	}
	else
		g_bBoxShow=true;

	if(g_sGlobal.g_pGame_Input->GetIsKeyDown(DIK_F3)){
		g_sGlobal.g_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);//D3DFILL_SOLID
	}
	else
		g_sGlobal.g_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
}

VOID Game_Font::DrawText(LPCWSTR pText, int x, int y, D3DCOLOR rgbFontColour){
	RECT Rect;
	GetClientRect(g_sGlobal.g_hwnd, &Rect);
	Rect.left = x;
	Rect.top = y;
	g_sGlobal.g_pFont->DrawText(0, pText, (int)wcslen(pText), &Rect,DT_LEFT, rgbFontColour);
}

FLOAT Game_Font::GetGamePlayTime(){  //返回已进行的时间,单位毫秒
	__int64 int64TimeCurrentCounts;
	if(m_bUseLargeTime){
		QueryPerformanceCounter((LARGE_INTEGER*) &int64TimeCurrentCounts);
		return ((float)(int64TimeCurrentCounts - m_int64TimeTickStartCounts)*(1.0f/m_int64OneSecondTicks)*1000.0f);
	}
	else{
		return ((float)(timeGetTime() - m_ulTimeStart));  //timeGetTime函数返回的时间单位为毫秒
	}
}

VOID Game_Font::UpdateFPS(){
	m_iFrameCount++;  //调用一次,增加一次
	if(m_iFrameCount%5==1)  //每当余数为1时,记数入m_fTime1
		m_fTime1=GetGamePlayTime()/1000;
	else if(m_iFrameCount%5==0){  //每当余数为5时,记数入m_fTime2
		m_fTime2=GetGamePlayTime()/1000;
		m_fTimeSlice=(float)fabs(m_fTime1-m_fTime2); //计算时间差,非负数
	}
	//更新FPS
	m_fFPS=5/m_fTimeSlice;
}

⌨️ 快捷键说明

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