📄 fonttexture.cpp
字号:
//--------------------------------------------------
// Desc: Font Texture 512*32
// Author: artsylee/2006.11.02
//--------------------------------------------------
#include "../stdafx.h"
#include "FontTexture.h"
#include "Log.h"
CFontTexture::CFontTexture()
{
m_bEmpty = true;
m_bInit = false;
m_Width = FONT_TEXTURE_WIDTH;
m_Height = FONT_TEXTURE_HEIGHT;
}
//---------------------------------------------------
// 创建字体纹理
//---------------------------------------------------
void CFontTexture::CreateFontTexture(int width, int height)
{
CTexture::CreateTexture(width, height, D3DFMT_A4R4G4B4);
}
CFontTexture::~CFontTexture()
{
Release();
}
void CFontTexture::Release()
{
// Destroy(); virtual function destroy
DeleteObject(m_BmpInfo.m_hDC);
DeleteObject(m_BmpInfo.m_hBitMap);
m_bInit = false;
Clear();
}
//---------------------------------------------------
// 输出文字到纹理
//---------------------------------------------------
void CFontTexture::WriteString(const char *szText, HFONT hFont)
{
if(szText == NULL || szText[0] == '\0')
{
return;
}
if(!m_bInit)
{
BITMAPINFO bmi;
ZeroMemory(&bmi.bmiHeader, sizeof(BITMAPINFOHEADER));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = (int)m_Width;
bmi.bmiHeader.biHeight = -(int)m_Height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biBitCount = 32;
m_BmpInfo.m_hDC = CreateCompatibleDC(NULL);
m_BmpInfo.m_hBitMap = CreateDIBSection(m_BmpInfo.m_hDC, &bmi, DIB_RGB_COLORS,
(void **)&m_BmpInfo.m_pData, NULL, 0);
if(m_BmpInfo.m_pData==NULL)
{
WriteLog(INFO_ERROR, "Can't create font bitmap!");
}
m_bInit = true;
}
memset(m_BmpInfo.m_pData, 0, sizeof(DWORD)*m_Width*m_Height);
HBITMAP OldBitmap=(HBITMAP)SelectObject(m_BmpInfo.m_hDC, m_BmpInfo.m_hBitMap);
HFONT OldFont=(HFONT)SelectObject(m_BmpInfo.m_hDC, hFont);
SetMapMode(m_BmpInfo.m_hDC, MM_TEXT);
SetTextColor(m_BmpInfo.m_hDC, RGB(255,255,255));
SetBkColor(m_BmpInfo.m_hDC, 0x00000000);
SetTextAlign(m_BmpInfo.m_hDC, TA_TOP);
int len = strlen(szText);
::TextOut(m_BmpInfo.m_hDC, 0, 0, szText, strlen(szText));
//RECT rtPos = { 0, 0, FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT };
//::DrawText(m_BmpInfo.m_hDC, szText, len, &rtPos, DT_SINGLELINE | DT_NOPREFIX);
int count = 0;
int total = m_Width * m_Height;
D3DLOCKED_RECT d3dlr;
m_pTexture->LockRect(0, &d3dlr, 0, 0);
WORD *pASMDst = (WORD *)d3dlr.pBits;
DWORD *pBitmapBits = (DWORD *)m_BmpInfo.m_pData;
__asm
{
push esi
push edi
mov esi, pBitmapBits
mov edi, pASMDst
loop1:
lodsd
cmp eax, 0
jz zero
mov eax, 0x0000ffff
zero:
stosw
inc count
mov eax, count
cmp eax, total
jnz loop1
pop edi
pop esi
};
m_pTexture->UnlockRect(0);
SelectObject(m_BmpInfo.m_hDC, OldBitmap);
SelectObject(m_BmpInfo.m_hDC, OldFont);
m_szText = szText;
m_bEmpty = false;
}
void CFontTexture::Clear()
{
m_szText = "";
m_bEmpty = true;
}
bool CFontTexture::Compare(const char *szText) const
{
return m_szText == szText;
}
bool CFontTexture::IsEmpty() const
{
return m_bEmpty;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -