My Documents/dxframework/dxf/dxf-engine/dxf_font.cpp

Go to the documentation of this file.
00001 /*      DXFramework Copyright (c) 2006, Jonathan Voigt, University of Michigan.
00002         See http://dxframework.sourceforge.net/ for a list of contributors.
00003         All rights reserved.
00004 
00005         Redistribution and use in source and binary forms, with or without modification, 
00006         are permitted provided that the following conditions are met:
00007 
00008                 * Redistributions of source code must retain the above copyright notice, 
00009                 this list of conditions and the following disclaimer.
00010 
00011                 * Redistributions in binary form must reproduce the above copyright notice, 
00012                 this list of conditions and the following disclaimer in the documentation 
00013                 and/or other materials provided with the distribution.
00014 
00015                 * Neither the name of the DXFramework project nor the names of its 
00016                 contributors may be used to endorse or promote products derived from this 
00017                 software without specific prior written permission.
00018 
00019         THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
00020         ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
00021         WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00022         DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
00023         ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00024         (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00025         LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
00026         ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00027         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00028         SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 } // namespace dxf

Generated on Fri Aug 18 12:01:27 2006 for DXFramework by  doxygen 1.4.7