![[]](/images/special/trans.gif)
LibN2L-4 Library Code ReferenceClassesCompounds Files Members Method Index Full Reference cTTFSurfaceFactory.cppGo to the documentation of this file.00001 /************************************************************************ 00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library 00003 Copyright (C) 2003 Aaron Cameron 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 A copy of the GNU Lesser General Public License has been provided with 00020 this library in the file 'COPYING'. 00021 00022 Contact information for the author of this library has been provided 00023 with this library in the file 'AUTHOR'. 00024 ************************************************************************/ 00025 #include "cTTFSurfaceFactory.h" 00026 00027 #include "n2l/vfs.h" 00028 #include "n2l/video.h" 00029 00030 #include "SDL_video.h" 00031 00032 namespace n2l 00033 { 00034 00035 /* ************************************************************ */ 00036 cTTFSurfaceFactory::cTTFSurfaceFactory( const cVfsNodeInterface & iFile, 00037 const tFontPt iPtSize ) : 00038 mFont(0) 00039 { 00040 requireSDLTTF(); 00041 load(iFile,iPtSize); 00042 } 00043 00044 /* ************************************************************ */ 00045 cTTFSurfaceFactory::~cTTFSurfaceFactory() 00046 { 00047 TTF_CloseFont(mFont); 00048 releaseSDLTTF(); 00049 } 00050 00051 /* ************************************************************ */ 00052 void cTTFSurfaceFactory::load( const cVfsNodeInterface & iFile, 00053 const tFontPt iPtSize ) 00054 { 00055 // Make sure that the node is like a file. 00056 if (!iFile.likeFile()) 00057 throw cBadDataUseException( "cTTFSurfaceFactory::load", 00058 "Vfs Node isn\'t like a file" ); 00059 00060 // Keep track of the point size we loaded with for later. 00061 mPtSize = iPtSize; 00062 00063 // Let's just try to load it 00064 TTF_Font * font = TTF_OpenFontRW( iFile.getRWops(), 1, mPtSize ); 00065 if (0==font) 00066 throw cDriverLayerException("cTTFSurfaceFactory::load", 00067 "Failed to load font from memory", 00068 "SDL_ttf", 00069 SDL_GetError() ); 00070 mFont = font; 00071 } 00072 00073 /* ************************************************************ */ 00074 const cAutoPtr<cSurface> cTTFSurfaceFactory::generate( const tGlyphID iGlyph ) const 00075 { 00076 const char TempText[2] = { iGlyph, '\0' }; 00077 SDL_Surface * glyph; 00078 SDL_Color white; 00079 white.r = 0xff; 00080 white.g = 0xff; 00081 white.b = 0xff; 00082 glyph = TTF_RenderText_Blended( mFont, TempText, white ); 00083 if (0==glyph) 00084 throw cDriverLayerException( "cTTFSurfaceFactory::generate", 00085 "Failed to generate glyph", 00086 "SDL_ttf", 00087 SDL_GetError() ); 00088 return cAutoPtr<cSurface>( new cSurface(glyph) ); 00089 } 00090 00091 00092 /* ************************************************************ */ 00093 const tVector2u cTTFSurfaceFactory::glyphSize( const tGlyphID iGlyph ) const 00094 { 00095 const char TempText[2] = { iGlyph, '\0' }; 00096 00097 int x,y; 00098 TTF_SizeText(mFont,TempText, &x,&y); 00099 return tVector2u(x,y); 00100 } 00101 00102 } // namespace |