-
Notifications
You must be signed in to change notification settings - Fork 0
/
GutFontUnicodeWin32.cpp
148 lines (125 loc) · 2.63 KB
/
GutFontUnicodeWin32.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include "Gut.h"
#include "GutFontUnicodeWin32.h"
#include "GutWin32.hpp"
CGutFontUniCode::CGutFontUniCode(void)
{
m_frameCount = 0;
m_iTextureW = m_iTextureH = 0;
m_MemDC = NULL;
m_Bitmap = NULL;
m_Font = NULL;
m_pMap = NULL;
}
CGutFontUniCode::~CGutFontUniCode()
{
Release();
DeleteDC(m_MemDC);
DeleteObject(m_Bitmap);
DeleteObject(m_Font);
}
void CGutFontUniCode::Release(void)
{
CGutFont::Release();
m_Map.clear();
if ( m_pMap )
{
delete [] m_pMap;
m_pMap = NULL;
}
}
bool CGutFontUniCode::Initialize(void)
{
HWND hWnd = GutGetWindowHandleWin32();
HDC hDC = GetDC(hWnd);
m_MemDC = CreateCompatibleDC(hDC);
m_Bitmap = CreateCompatibleBitmap(m_MemDC, m_fFontWidth, m_fFontHeight);
m_Font = CreateFont(
m_fFontHeight, m_fFontWidth*0.5f,
0, 0,
FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,
FIXED_PITCH,
NULL);
SelectObject(m_MemDC, m_Bitmap);
SelectObject(m_MemDC, m_Font);
SetTextColor(m_MemDC, RGB(255,255,255));
SetBkColor(m_MemDC, RGB(0,0,0));
ReleaseDC(hWnd, hDC);
m_pMap = new sCharInfo[m_iNumCharactersSet];
ZeroMemory(m_pMap, sizeof(sCharInfo) * m_iNumCharactersSet);
return true;
}
bool CGutFontUniCode::AccessTexture(WCHAR c, int &x, int &y)
{
int loc = -1;
bool bUpdateTexture = false;
std::map<WCHAR, int>::iterator iter = m_Map.find(c);
if ( iter!=m_Map.end() )
{
loc = iter->second;
m_pMap[loc].m_ReferencedFrame = m_frameCount;
}
else
{
// `需要更新贴图`
bUpdateTexture = true;
// `找出贴图中还没有使用的空间`
for ( int i=0; i<m_iNumCharactersSet; i++ )
{
if ( m_pMap[i].m_Code==0 )
{
loc = i;
break;
}
}
// `整张贴图满了, 把很久没用到的字清除掉.`
if ( loc==-1 )
{
UINT oldest_frame = m_frameCount;
for ( int i=0; i<m_iNumCharactersSet; i++ )
{
if ( oldest_frame > m_pMap[i].m_ReferencedFrame )
{
loc = i;
oldest_frame = m_pMap[i].m_ReferencedFrame;
}
}
}
if ( loc != -1 )
{
m_pMap[loc].m_Code = c;
m_pMap[loc].m_ReferencedFrame = m_frameCount;
m_Map.insert(std::pair<WCHAR, int>(c, loc));
}
else
{
bUpdateTexture = false;
}
}
if ( loc != -1 )
{
y = loc / m_iLayoutW;
x = loc % m_iLayoutW;
if ( bUpdateTexture )
{
RECT rect = {0,0, (int)m_fFontWidth, (int)m_fFontHeight};
FillRect(m_MemDC, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));
TextOutW(m_MemDC, 0, 0, &c, 1);
}
}
else
{
// `找不到空间来放新的字`
// `返回 -1 代表找不到这个字`
x = y = -1;
}
return bUpdateTexture;
}