bitmapFont.cpp

00001 
00002 /* Copyright (c) 2008-2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 // HACK: Get rid of deprecated warning for aglUseFont
00019 //   -Wno-deprecated-declarations would do as well, but here it is more isolated
00020 #ifdef AGL
00021 #  include <AvailabilityMacros.h>
00022 #  undef DEPRECATED_ATTRIBUTE
00023 #  define DEPRECATED_ATTRIBUTE
00024 #endif
00025 
00026 #include "bitmapFont.h"
00027 
00028 #include <eq/client/pipe.h>
00029 #include <eq/client/window.h>
00030 
00031 #ifdef WGL
00032 #  include <eq/client/wglWindow.h>
00033 #endif
00034 #ifdef AGL
00035 #  include <eq/client/aglWindow.h>
00036 #endif
00037 #ifdef GLX
00038 #  include <eq/client/glXPipe.h>
00039 #endif
00040 
00041 namespace eq
00042 {
00043 namespace util
00044 {
00045 
00046 template< typename OMT >
00047 BitmapFont< OMT >::BitmapFont( ObjectManager< OMT >& gl, const OMT& key )
00048         // We create a new shared object manager. Typically we are exited by
00049         // the last user, at which point the given OM may have been deleted
00050         : _gl( gl.glewGetContext(), &gl )
00051         , _key( key )
00052 {
00053 }
00054 
00055 template< typename OMT >
00056 BitmapFont< OMT >::~BitmapFont()
00057 {
00058     const GLuint lists = _gl.getList( _key );
00059     if( lists != Window::ObjectManager::INVALID )
00060         EQWARN << "OpenGL BitmapFont was not freed" << std::endl;
00061 }
00062 
00063 template< typename OMT >
00064 bool BitmapFont< OMT >::init( const WindowSystem ws, const std::string& name,
00065                               const uint32_t size )
00066 {
00067     switch( ws )
00068     {
00069         case WINDOW_SYSTEM_GLX:
00070             return _initGLX( name, size );
00071         case WINDOW_SYSTEM_WGL:
00072             return _initWGL( name, size );
00073         case WINDOW_SYSTEM_AGL:
00074             return _initAGL( name, size );
00075         default:
00076             return false;
00077     }
00078 
00079     EQASSERTINFO( _gl.getList( _key ) != Window::ObjectManager::INVALID, 
00080                   "Font initialization failed" );
00081 }
00082 
00083 template< typename OMT >
00084 void BitmapFont< OMT >::exit()
00085 {
00086     _setupLists( 0 );
00087 }
00088 
00089 template< typename OMT >
00090 bool BitmapFont< OMT >::_initGLX( const std::string& name, const uint32_t size )
00091 {
00092 #ifdef GLX
00093     Display* display = XGetCurrentDisplay();
00094     EQASSERT( display );
00095     if( !display )
00096     {
00097         EQWARN << "No current X11 display, see eq::XSetCurrentDisplay()"
00098                << std::endl;
00099         return false;
00100     }
00101 
00102     // see xfontsel
00103     std::stringstream font;
00104     font << "-*-";
00105 
00106     if( name.empty( ))
00107         font << "times";
00108     else
00109         font << name;
00110     font << "-*-r-*-*-" << size << "-*-*-*-*-*-*-*";
00111 
00112     XFontStruct* fontStruct = XLoadQueryFont( display, font.str().c_str( )); 
00113     if( !fontStruct )
00114     {
00115         EQWARN << "Can't load font " << font.str() << ", using fixed"
00116                << std::endl;
00117         fontStruct = XLoadQueryFont( display, "fixed" ); 
00118     }
00119 
00120     EQASSERT( fontStruct );
00121 
00122     const GLuint lists = _setupLists( 127 );
00123     glXUseXFont( fontStruct->fid, 0, 127, lists );
00124 
00125     XFreeFont( display, fontStruct );
00126     return true;
00127 #else
00128     return false;
00129 #endif
00130 }
00131 
00132 template< typename OMT >
00133 bool BitmapFont< OMT >::_initWGL( const std::string& name, const uint32_t size )
00134 {
00135 #ifdef WGL
00136     HDC dc = wglGetCurrentDC();
00137     if( !dc )
00138     {
00139         EQWARN << "No WGL device context current" << std::endl;
00140         return false;
00141     }
00142 
00143     LOGFONT font;
00144     memset( &font, 0, sizeof( font ));
00145     font.lfHeight = -static_cast< LONG >( size );
00146     font.lfWeight = FW_NORMAL;
00147     font.lfCharSet = ANSI_CHARSET;
00148     font.lfOutPrecision = OUT_DEFAULT_PRECIS;
00149     font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
00150     font.lfQuality = DEFAULT_QUALITY;
00151     font.lfPitchAndFamily = FF_DONTCARE | DEFAULT_QUALITY;
00152 
00153     if( name.empty( ))
00154         strncpy( font.lfFaceName, "Times New Roman", LF_FACESIZE );
00155     else
00156         strncpy( font.lfFaceName, name.c_str(), LF_FACESIZE );
00157 
00158     font.lfFaceName[ LF_FACESIZE-1 ] = '\0';
00159 
00160     HFONT newFont = CreateFontIndirect( &font );
00161     if( !newFont )
00162     {
00163         EQWARN << "Can't load font " << name << ", using Times New Roman" 
00164                << std::endl;
00165 
00166         strncpy( font.lfFaceName, "Times New Roman", LF_FACESIZE );
00167         newFont = CreateFontIndirect( &font );
00168     }
00169     EQASSERT( newFont );
00170 
00171     HFONT oldFont = static_cast< HFONT >( SelectObject( dc, newFont ));
00172 
00173     const GLuint lists = _setupLists( 256 );
00174     const bool ret = wglUseFontBitmaps( dc, 0 , 255, lists );
00175     
00176     SelectObject( dc, oldFont );
00177     //DeleteObject( newFont );
00178     
00179     if( !ret )
00180         _setupLists( 0 );
00181 
00182     return ret;
00183 #else
00184     return false;
00185 #endif
00186 }
00187 
00188 template< typename OMT >
00189 bool BitmapFont< OMT >::_initAGL( const std::string& name, const uint32_t size )
00190 {
00191 #ifdef AGL
00192     AGLContext context = aglGetCurrentContext();
00193     EQASSERT( context );
00194     if( !context )
00195     {
00196         EQWARN << "No AGL context current" << std::endl;
00197         return false;
00198     }
00199 
00200     CFStringRef cfFontName;
00201     if( name.empty( ))
00202         cfFontName = CFStringCreateWithCString( kCFAllocatorDefault, "Georgia",
00203                                                 kCFStringEncodingMacRoman );
00204     else
00205         cfFontName = 
00206             CFStringCreateWithCString( kCFAllocatorDefault, name.c_str(),
00207                                    kCFStringEncodingMacRoman );
00208 
00209     ATSFontFamilyRef font = ATSFontFamilyFindFromName( cfFontName, 
00210                                                        kATSOptionFlagsDefault );
00211     CFRelease( cfFontName );
00212 
00213     if( font == 0 )
00214     {
00215         EQWARN << "Can't load font " << name << ", using Georgia" << std::endl;
00216         cfFontName = 
00217             CFStringCreateWithCString( kCFAllocatorDefault, "Georgia",
00218                                        kCFStringEncodingMacRoman );
00219 
00220         font = ATSFontFamilyFindFromName( cfFontName, kATSOptionFlagsDefault );
00221         CFRelease( cfFontName );
00222     }
00223     EQASSERT( font );
00224 
00225     const GLuint lists = _setupLists( 127 );
00226     if( !aglUseFont( context, font, normal, size, 0, 256, (long)lists ))
00227     {
00228         _setupLists( 0 );
00229         return false;
00230     }
00231     
00232     return true;
00233 #else
00234     return false;
00235 #endif
00236 }
00237 
00238 template< typename OMT >
00239 GLuint BitmapFont< OMT >::_setupLists( const GLsizei num )
00240 {
00241     GLuint lists = _gl.getList( _key );
00242     if( lists != Window::ObjectManager::INVALID )
00243         _gl.deleteList( _key );
00244 
00245     if( num == 0 )
00246         lists = Window::ObjectManager::INVALID;
00247     else
00248     {
00249         lists = _gl.newList( _key, num );
00250         EQASSERT( lists != Window::ObjectManager::INVALID );
00251     }
00252     return lists;
00253 }
00254 
00255 template< typename OMT >
00256 void BitmapFont< OMT >::draw( const std::string& text ) const
00257 {
00258     const GLuint lists = _gl.getList( _key );
00259     EQASSERTINFO( lists != Window::ObjectManager::INVALID, 
00260                   "Font not initialized" );
00261 
00262     if( lists != Window::ObjectManager::INVALID )
00263     {
00264         glListBase( lists );
00265         glCallLists( text.size(), GL_UNSIGNED_BYTE, text.c_str( ));
00266         glListBase( 0 );
00267     }
00268 }
00269 
00270 // instantiate desired key types
00271 template class BitmapFont< const void* >;
00272 
00273 }
00274 }
Generated on Sat Feb 6 12:59:42 2010 for Equalizer 0.9.1 by  doxygen 1.6.1