00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "dxframework.h"
00032
00033 namespace dxf {
00034 std::vector<FontNode*> Font::fonts;
00035
00036 Font::Font() {
00037 pFontNode = 0;
00038 }
00039
00040 HRESULT Font::Load(const std::wstring& name, LONG height, LONG weight) {
00041 assert (!pFontNode);
00042
00043 HRESULT hr;
00044
00045 FontNodeVectorIter iter = fonts.begin();
00046 while(iter != fonts.end()) {
00047 if (((*iter)->name == name) && ((*iter)->height == height) && ((*iter)->weight == weight)) {
00048 pFontNode = *iter;
00049 ++(pFontNode->count);
00050 return S_OK;
00051 }
00052 ++iter;
00053 }
00054
00055 pFontNode = new FontNode;
00056 if (!pFontNode) return E_OUTOFMEMORY;
00057 fonts.push_back(pFontNode);
00058
00059 pFontNode->name = name;
00060 pFontNode->height = height;
00061 pFontNode->weight = weight;
00062 pFontNode->count = 1;
00063
00064 V_RETURN(D3DXCreateFont(DXUTGetD3DDevice(), height, 0, weight, 0, FALSE,
00065 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
00066 DEFAULT_PITCH | FF_DONTCARE, name.c_str(), &pFontNode->pFont));
00067
00068 return S_OK;
00069 }
00070
00071 void Font::Unload() {
00072 assert(pFontNode);
00073
00074 --(pFontNode->count);
00075
00076 if (!pFontNode->count) {
00077 FontNodeVectorIter iter = fonts.begin();
00078 while(iter != fonts.end()) {
00079 if ((*iter) == pFontNode) {
00080 fonts.erase(iter);
00081 break;
00082 }
00083 ++iter;
00084 }
00085 SAFE_RELEASE(pFontNode->pFont);
00086 SAFE_DELETE(pFontNode);
00087 }
00088 pFontNode = 0;
00089 }
00090
00091 void Font::Render2D(const std::wstring& text, D3DXVECTOR2 position, D3DCOLOR color, D3DXVECTOR2 scaling, bool center) const {
00092
00093 D3DXMATRIX ident;
00094 D3DXVECTOR2 scaleCenter(0, static_cast<float>(GetHeight()) / 2);
00095 D3DXMatrixTransformation2D(&ident, &scaleCenter, 0, &scaling, 0, 0, &position);
00096
00097 DXFGetD3DXSprite()->SetTransform(&ident);
00098
00099 RECT font_rect;
00100 SetRect(&font_rect, 0, 0, 0, 0);
00101
00102 HRESULT hr;
00103 DWORD flags = DT_NOCLIP;
00104 flags |= center ? DT_CENTER : 0;
00105 V(pFontNode->pFont->DrawText(DXFGetD3DXSprite(), text.c_str(), static_cast<int>(text.length()), &font_rect, flags, color));
00106 }
00107
00108 LONG Font::GetHeight() const {
00109 return pFontNode->height;
00110 }
00111
00112 LONG Font::GetWeight() const {
00113 return pFontNode->weight;
00114 }
00115
00116 HRESULT Font::OnCreateDevice() {
00117 HRESULT hr;
00118
00119 FontNodeVectorIter iter = fonts.begin();
00120 while(iter != fonts.end()) {
00121 V_RETURN(D3DXCreateFont(DXUTGetD3DDevice(), (*iter)->height, 0, (*iter)->weight, 0, FALSE,
00122 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
00123 DEFAULT_PITCH | FF_DONTCARE, (*iter)->name.c_str(), &(*iter)->pFont));
00124 ++iter;
00125 }
00126
00127 return S_OK;
00128 }
00129
00130 HRESULT Font::OnResetDevice() {
00131 HRESULT hr;
00132
00133 FontNodeVectorIter iter = fonts.begin();
00134 while(iter != fonts.end()) {
00135 V_RETURN((*iter)->pFont->OnResetDevice());
00136 ++iter;
00137 }
00138
00139 return S_OK;
00140 }
00141
00142 void Font::OnLostDevice() {
00143 FontNodeVectorIter iter = fonts.begin();
00144 while(iter != fonts.end()) {
00145 if ((*iter)->pFont) (*iter)->pFont->OnLostDevice();
00146 ++iter;
00147 }
00148 }
00149
00150 void Font::OnDestroyDevice() {
00151 FontNodeVectorIter iter = fonts.begin();
00152 while(iter != fonts.end()) {
00153 SAFE_RELEASE((*iter)->pFont);
00154 ++iter;
00155 }
00156 }
00157
00158 void Font::GetTextExtent(const std::wstring& text, SIZE& size) const {
00159 D3DXMATRIX ident;
00160 D3DXVECTOR2 scaleCenter(0, static_cast<float>(GetHeight()) / 2);
00161 D3DXVECTOR2 scaling = D3DXVECTOR2(1, 1);
00162 D3DXVECTOR2 position = D3DXVECTOR2(0, 0);
00163
00164 D3DXMatrixTransformation2D(&ident, &scaleCenter, 0, &scaling, 0, 0, &position);
00165
00166 DXFGetD3DXSprite()->SetTransform(&ident);
00167
00168 RECT font_rect;
00169 SetRect(&font_rect, 0, 0, 0, 0);
00170
00171 HRESULT hr;
00172 V(pFontNode->pFont->DrawText(0, text.c_str(), static_cast<int>(text.length()), &font_rect, DT_NOCLIP | DT_CALCRECT, 0));
00173
00174 size.cx = font_rect.right-font_rect.left;
00175 size.cy = font_rect.bottom-font_rect.top;
00176 }
00177 }