?? asl_font.h
字號:
//-----------------------------------------------------------------------------
//
// ____ Azure Star Game Engine 藍(lán)星游戲引擎 ____
//
// Copyright (c) 2006, 藍(lán)星工作室
// All rights reserved.
//
// 文件名稱: asl_font.h
// 摘 要: 字體類定義
//
// 當(dāng)前版本: 1.0
// 作 者: 湯 祺
// 創(chuàng)建日期: 2006-8-6
//
//-----------------------------------------------------------------------------
#ifndef ASL_FONT_INCLUDE
#define ASL_FONT_INCLUDE
#pragma once
#include "asl_utils.h"
#include "asl_bitmap.h"
//-----------------------------------------------------------------------------
namespace ASL
{
//-----------------------------------------------------------------------------
// 類名: ASLFont
// 功能: 字體類
// 本類使用LRU算法管理一塊字符信息緩存,以提高字符顯示速度。
// 同時支持平滑字體的顯示(通過alpha通道混合實現(xiàn))。
// 提速的原理是:游戲中的同一段文字一般要顯示幾秒鐘時間,在這
// 段時間中可能會刷新數(shù)百次。若每次刷新都調(diào)用GDI函數(shù)繪制文字則開
// 銷巨大。如果使用緩存機制,則只在第一次顯示文字時調(diào)用GDI函數(shù),
// 以后數(shù)百次刷新均可從緩存中直接取數(shù)據(jù),極大的提高了效率。在更新
// 文字后,那些使用率較高的字保留在緩存中,只替換低頻率的字,所以
// 此時需要調(diào)用GDI函數(shù)更新緩存的部分是比較少的,速度同樣有保證。
// 若緩存開的較小,不能容納屏幕中所有要顯示的文字,則每次刷新
// 要重新調(diào)用GDI函數(shù)替換部分使用率低的緩存塊。經(jīng)測試,此時速度仍
// 比直接調(diào)用GDI函數(shù)快,但比足緩存時速度有很大下降,這是由LRU算法
// 的性質(zhì)決定的。因此建議設(shè)置緩存的大小為足夠容納屏幕中所有要顯示
// 的文字。
//-----------------------------------------------------------------------------
class ASLFont
{
// 字符信息結(jié)構(gòu)定義
private:
struct CharInfo
{
CharInfo() : ciChar(0), ciData(NULL), ciTime(0) {}
~CharInfo() { SAFE_DELETE_ARRAY(ciData); }
UINT ciChar; // 字符表示數(shù)
BYTE *ciData; // 字符點陣數(shù)據(jù)或alpha通道數(shù)據(jù)
int ciWidth; // 字符寬度
int ciHeight; // 字符高度
int ciPitch; // 字符每行字節(jié)數(shù)
int ciOx; // 數(shù)據(jù)起點到字符左側(cè)的偏移
int ciOy; // 數(shù)據(jù)起點到字符上端的偏移
int ciInc; // 字符占據(jù)的寬度(用于計算下一字符起點)
int ciTime; // 最后使用時間(偽時間,每調(diào)用_GetChar()加1)
};
// 構(gòu)造/析構(gòu)函數(shù)
public:
ASLFont(void);
ASLFont(HFONT font, bool bSmooth, int nCacheSize = 256);
~ASLFont(void);
// 公有函數(shù)
public:
// 創(chuàng)建字體
void Create(HFONT font, bool bSmooth, int nCacheSize = 256);
// 銷毀字體
void Destroy(void);
// 設(shè)置字符間距
inline void SetSpace(int nSpace) { m_nSpace = nSpace; }
// 取單個字符寬度
inline int GetCharWidth(void) { return m_nAveWidth; }
// 取單個字符高度
inline int GetCharHeight(void) { return m_nCharBmpHeight; }
// 繪制字符串
void DrawText(ASLBitmap &bmDest, int x, int y, LPCSTR str, COLOR color);
void DrawText(ASLBitmap &bmDest, int x, int y, COLOR color, LPCSTR format, ...);
// 自動分行繪制字符串
void DrawTextEx(ASLBitmap &bmDest, int x, int y, LPCSTR str, COLOR color,
int length, int vspace);
// 私有函數(shù)
private:
// 取字符信息
const CharInfo& _GetChar(UINT ch);
// 設(shè)置字符信息
void _SetChar(CharInfo &ci, UINT ch);
// 繪制字符
void _DrawChar(const CharInfo &ci, ASLBitmap &bmDest, int x, int y,
COLOR color) const;
// 私有變量
private:
CharInfo* m_pCache; // 字符信息緩存(LRU算法管理)
HDC m_hDC; // 內(nèi)存DC
HBITMAP m_hBitmap; // 字符位圖
BYTE* m_pCharBmpBuf; // 字符位圖數(shù)據(jù)
int m_nCharBmpWidth; // 字符位圖寬度
int m_nCharBmpHeight; // 字符位圖高度
int m_nSize; // 字符信息緩存大小
int m_nAscent; // 字符基準(zhǔn)線上最大高度
int m_nSpace; // 字符間距
int m_nTime; // 當(dāng)前時間(偽時間,每調(diào)用_GetChar()加1)
bool m_bSmooth; // 是否平滑字體
int m_nAveWidth; // 平均字寬
}; // ASLFont類定義結(jié)束
} // namespace ASL
#endif // ASL_FONT_INCLUDE
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -