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

📄 gameobjecttext.cpp

📁 游戏框架
💻 CPP
字号:
// ***************************************************************
//  GameObjectText   version:  1.0
//  -------------------------------------------------------------
//	File Name:	GameObjectText.cpp
//	Created:	2007/07/18
//	Modified:	2007/07/18   15:56
//	Author:		William.Liang
//  Msn:		lwq49@msn.com
//  Email:		lwq49@21cn.com, lwq49@msn.com
//	Description:
//
//	Purpose:	
//  -------------------------------------------------------------
//  license:
//
//  The contents of this file are subject to the Mozilla Public
//  License Version 1.1 (the "License"); you may not use this file
//  except in compliance with the License. You may obtain a copy
//  of the License at http://www.mozilla.org/MPL/ Software dis-
//  tributed under the License is distributed on an "AS IS" 
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or im-
//  plied. See the License for the specific language governing
//  rights and limitations under the License.
//
//  The Initial Developer of the Original Code is William.Liang .
//  Copyright (C) 2007 - All Rights Reserved.
// ***************************************************************
#include "StdAfx.h"
#include ".\gameobjecttext.h"

_declspec(dllimport) CResource* g_lpDefaultResource;

//************************************
// <p>Description: 构造函数</p>
//************************************
CGameObjectText::CGameObjectText(void)
{
	m_szText			= NULL;
	m_hgeFont			= NULL;
	m_dwColor			= 0;
	m_nAlign			= HGETEXT_LEFT;
	m_bSupportUnicode	= false;
	m_nBufferSize		= (WORD)-1;
	m_nTextLength		= 0;
	m_nFontSize			= 0;
}

//************************************
// <p>Description: 析构函数</p>
//************************************
CGameObjectText::~CGameObjectText(void)
{
	SafeDelete(m_szText);
	SafeDelete(m_hgeFont);
}

//************************************
// <p>Description: 返回缓冲指针</p>
//
// <p>Returns:   LPCTSTR</p>
//************************************
LPCTSTR CGameObjectText::GetBuffer(){
	return m_szText;
}

//************************************
// <p>Description: 文本对象</p>
// <p>Parameters:  </p>
// <p>    LPCTSTR lpName</p>
// <p>    _SIZE siSize</p>
// <p>    LPCTSTR szFont</p>
// <p>    int nFontSize</p>
//
// <p>Returns:   bool</p>
//************************************
bool CGameObjectText::Create(LPCTSTR lpName, _SIZE siSize, LPCTSTR szFont, int nFontSize){
	__super::Create(lpName, siSize, Type_Text);
	memcpy(&m_szFont, szFont, 32*sizeof(TCHAR));

	//判断是否默认字体
	_CString strFont = szFont;
	if(strFont=="default")
		strFont = "default.fnt";
	m_hgeFont = new hgeFont(strFont);

	SetFontSize(nFontSize);

	return m_hgeFont!=NULL;
}

//************************************
// <p>Description: 文本渲染</p>
// <p>Parameters:  </p>
// <p>    WPARAM wParam</p>
//
// <p>Returns:   void</p>
//************************************
void CGameObjectText::Render(WPARAM wParam){
	if(m_szText){
		if(m_bClipping){
			g_lpDefaultResource->GetGDI()->Gfx_SetClipping((int)m_BasalInfo.LocalPos.x, (int)m_BasalInfo.LocalPos.y, (int)m_BasalInfo.Size.cx, (int)m_BasalInfo.Size.cy);
			m_hgeFont->printfb(m_BasalInfo.ScreenPos.x, m_BasalInfo.ScreenPos.y, m_BasalInfo.Size.cx, m_BasalInfo.Size.cy, m_nAlign, "%s\n", m_szText);
			g_lpDefaultResource->GetGDI()->Gfx_SetClipping(0, 0, 0, 0);
		}
		else
			m_hgeFont->printfb(m_BasalInfo.ScreenPos.x, m_BasalInfo.ScreenPos.y, m_BasalInfo.Size.cx, m_BasalInfo.Size.cy, m_nAlign, "%s\n", m_szText);
	}
}

//************************************
// <p>Description: 设置模式</p>
// <p>Parameters:  </p>
// <p>    WORD wMode</p>
//
// <p>Returns:   bool</p>
//************************************
bool CGameObjectText::SetMode(WORD wMode){
	return false;
}

//************************************
// <p>Description: 设置文本</p>
// <p>Parameters:  </p>
// <p>    LPCTSTR lpText</p>
// <p>    WORD wSize</p>
//
// <p>Returns:   void</p>
//************************************
void CGameObjectText::SetText(LPCTSTR lpText, WORD wSize){

	if(wSize==0)
		wSize = (WORD)strlen(lpText);

	if(wSize>m_nTextLength){
		//当没有设置文本上限时才重新分配内存
		if(m_nBufferSize==(WORD)-1){
			SafeDelete(m_szText);
			m_szText = new TCHAR[wSize+1];
		}
	}

	if(m_szText==NULL){
		m_szText = new TCHAR[1];
	}

	//计算带缓冲区限制后的文本长度
	if(m_nBufferSize==(WORD)-1)
		m_nTextLength = wSize;
	else
		m_nTextLength = m_nBufferSize-1>wSize?wSize:m_nBufferSize-1;

	strncpy(m_szText, lpText, m_nTextLength);
	m_szText[m_nTextLength] = 0;
}

//************************************
// <p>Description: 设置缓冲区上限</p>
// <p>Parameters:  </p>
// <p>    WORD wSize</p>
//
// <p>Returns:   void</p>
//************************************
void CGameObjectText::SetMaxLength(WORD wSize){

	if(wSize==0) return;

	TCHAR*		pNewBuffer;
	pNewBuffer = new TCHAR[wSize+1];
	WORD		pNewLength = (wSize>m_nTextLength?m_nTextLength:wSize);

	//复制旧内容
	strncpy(pNewBuffer, m_szText, pNewLength);
	pNewBuffer[pNewLength] = 0;
	SafeDelete(m_szText);
	m_szText = pNewBuffer;

	m_nBufferSize = wSize;
}

//************************************
// <p>Description: 返回缓冲区上限</p>
// <p>Parameters:  </p>
//
// <p>Returns:   LPCTSTR</p>
//************************************
WORD CGameObjectText::GetMaxLength(){
	return m_nBufferSize;
}

//************************************
// <p>Description: 返回文本</p>
// <p>Parameters:  </p>
//
// <p>Returns:   LPCTSTR</p>
//************************************
LPCTSTR CGameObjectText::GetText(){
	return m_szText;
}

//************************************
// <p>Description: 返回文本长度</p>
//
// <p>Returns:   WORD</p>
//************************************
WORD CGameObjectText::GetTextLength(){
	return m_nTextLength;
}

//************************************
// <p>Description: 设置颜色</p>
// <p>Parameters:  </p>
// <p>    DWORD dwColor</p>
//
// <p>Returns:   void</p>
//************************************
void CGameObjectText::SetColor(DWORD dwColor){
	m_dwColor = dwColor;
	m_hgeFont->SetColor(dwColor);
}

//************************************
// <p>Description: 返回颜色</p>
//
// <p>Returns:   DWORD</p>
//************************************
DWORD CGameObjectText::GetColor(){
	return m_dwColor;
}

//************************************
// <p>Description: 设置字体</p>
// <p>Parameters:  </p>
// <p>    LPCTSTR lpFont</p>
//
// <p>Returns:   void</p>
//************************************
void CGameObjectText::SetFont(LPCTSTR lpFont){
}

//************************************
// <p>Description: 返回字体</p>
//
// <p>Returns:   LPCTSTR</p>
//************************************
LPCTSTR CGameObjectText::GetFont(){
	return m_szFont;
}

//************************************
// <p>Description: 是否支持宽文本</p>
//
// <p>Returns:   bool</p>
//************************************
bool CGameObjectText::IsSupportUnicode(){
	return m_bSupportUnicode;
}

//************************************
// <p>Description: 设置行间距</p>
// <p>Parameters:  </p>
// <p>    float fSpacing</p>
//
// <p>Returns:   void</p>
//************************************
void CGameObjectText::SetSpacing(float fSpacing){
	m_hgeFont->SetSpacing(fSpacing);
}

//************************************
// <p>Description: 设置对齐</p>
// <p>Parameters:  </p>
// <p>    int nAlign</p>
//
// <p>Returns:   void</p>
//************************************
void CGameObjectText::SetAlign(int nAlign){
	m_nAlign = 0;
	int		nAlignType = 0;

	nAlignType = nAlign&0xF;
	if(nAlignType){
		if(nAlignType==Align_Left)
			m_nAlign |= HGETEXT_LEFT;
		if(nAlignType==Align_Right)
			m_nAlign |= HGETEXT_RIGHT;
		if(nAlignType==Align_Center)
			m_nAlign |= HGETEXT_CENTER;
	}

	nAlignType = nAlign&0xF0;
	if(nAlignType){
		if(nAlignType==Align_Top)
			m_nAlign |= HGETEXT_TOP;
		if(nAlignType==Align_Bottom)
			m_nAlign |= HGETEXT_BOTTOM;
		if(nAlignType==Align_Middle)
			m_nAlign |= HGETEXT_MIDDLE;
	}
}

//************************************
// <p>Description: 设置字体大小</p>
// <p>Parameters:  </p>
// <p>    int nSize</p>
//
// <p>Returns:   void</p>
//************************************
void CGameObjectText::SetFontSize(int nSize){
	if(nSize <=0) return;

	int		nOrgSize = (int)GetHeight();

	//返回无效
	if(nOrgSize==-1) return;

	if(nSize==0){
		m_nFontSize = nOrgSize;
	}
	else{
		m_nFontSize = nOrgSize;
		m_hgeFont->SetScale(nSize/(float)nOrgSize);
	}
}

//************************************
// <p>Description: 设置角度</p>
// <p>Parameters:  </p>
// <p>    float fAngle</p>
//
// <p>Returns:   void</p>
//************************************
void CGameObjectText::SetAngle(float fAngle){
	float		fOffset = fAngle - m_GameBasalInfo.Angle*180.0f/M_PI;
	SetAngleOffset(fOffset);
}

//************************************
// <p>Description: 设置角度</p>
// <p>Parameters:  </p>
// <p>    float fAngle</p>
//
// <p>Returns:   void</p>
//************************************
void CGameObjectText::SetAngleOffset(float fAngle){
	m_GameBasalInfo.Angle += fAngle*M_PI/180.0f;

	m_hgeFont->SetRotation(fAngle*M_PI/180.0f);
}

//************************************
// <p>Description: 返回字串宽度</p>
//
// <p>Returns:   float</p>
//************************************
float CGameObjectText::GetStringWidth(){
	return m_hgeFont->GetStringWidth(m_szText);
}

//************************************
// <p>Description: 返回字体高度</p>
//
// <p>Returns:   float</p>
//************************************
float CGameObjectText::GetHeight(){
	if(m_hgeFont)
		return m_hgeFont->GetHeight();

	return -1;
}

//************************************
// <p>Description: 返回行间距</p>
//
// <p>Returns:   float</p>
//************************************
float CGameObjectText::GetSpacing(){
	return m_hgeFont->GetSpacing();
}

//************************************
// <p>Description: 返回对齐</p>
//
// <p>Returns:   int</p>
//************************************
int CGameObjectText::GetAlign(){
	return m_nAlign;
}

//************************************
// <p>Description: 返回字体大小</p>
// <p>Parameters:  </p>
//
// <p>Returns:   int</p>
//************************************
int CGameObjectText::GetFontSize(){
	return m_nFontSize;
}

//************************************
// <p>Description: 接口查询</p>
// <p>Parameters:  </p>
// <p>    const IID & Guid</p>
// <p>    DWORD dwQueryVer</p>
//
// <p>Returns:   void*</p>
//************************************
void* CGameObjectText::QueryInterface(const IID &Guid, DWORD dwQueryVer){
	QUERYINTERFACE(IGameObjectText,Guid,dwQueryVer);
	QUERYINTERFACE(IGameObject,Guid,dwQueryVer);
	QUERYINTERFACE(IBaseObject,Guid,dwQueryVer);
	QUERYINTERFACE_IUNKNOWNEX(IGameObjectText,Guid,dwQueryVer);
	return NULL;
}

//************************************
// <p>Description: =操作重载</p>
// <p>Parameters:  </p>
// <p>    const CGameObjectText & GameObject</p>
//
// <p>Returns:   CGameObjectText&</p>
//************************************
CGameObjectText& CGameObjectText::operator=(CGameObjectText& GameObject){
	CGameObject*	pOtherBaseClass = static_cast<CGameObject *>(&GameObject);
	CGameObject*	pMySelfBaseClass = static_cast<CGameObject *>(this);
	*pMySelfBaseClass = *pOtherBaseClass;

	//删除旧指针
	if(m_szText)
		SafeDelete(m_szText);
	if(m_hgeFont)
		SafeDelete(m_hgeFont);

	//拷贝内容
	//this->SetText(GameObject.m_szText, GameObject.m_nTextLength);
	m_szText = new TCHAR[GameObject.m_nTextLength+1];
	CopyMemory(m_szText, GameObject.m_szText, GameObject.m_nTextLength+1);
	CopyMemory(m_szFont, GameObject.m_szFont, sizeof(m_szFont));

	//重建hgeFont
	//m_hgeFont			= new hgeFont(*GameObject.m_hgeFont);
	m_hgeFont			= new hgeFont(m_szFont);
	m_dwColor			= GameObject.m_dwColor;
	m_bSupportUnicode	= GameObject.m_bSupportUnicode;
	m_nAlign			= GameObject.m_nAlign;
	m_nFontSize			= GameObject.m_nFontSize;
	m_nBufferSize		= GameObject.m_nBufferSize;
	//m_nTextLength		= GameObject.m_nTextLength;

	m_hgeFont->SetColor(m_dwColor);

	return *this;
}

⌨️ 快捷键说明

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