GLICT/fonts.cpp

Go to the documentation of this file.
00001 /*
00002         GLICT - Graphics Library Interface Creation Toolkit
00003         Copyright (C) 2006-2007 OBJECT Networks
00004 
00005         This library is free software; you can redistribute it and/or
00006         modify it under the terms of the GNU Library General Public
00007         License as published by the Free Software Foundation; either
00008         version 2 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         Library General Public License for more details.
00014 
00015         You should have received a copy of the GNU Library General Public
00016         License along with this library; if not, write to the Free
00017         Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00036 #ifdef WIN32
00037         // GL.h doesnt like it that we dont include windows.h in MSVC
00038         #include <windows.h>
00039 #endif
00040 
00041 #include <vector>
00042 #include <GL/gl.h>
00043 #include "fonts.h"
00044 
00045 _GLICTFONTVECTOR glictFonts;
00046 
00061 glictFont* glictCreateFont(const char* name) {
00062 
00063         if (glictFindFont(name)) return NULL;
00064 
00065 
00066         glictFont* fnt = new glictFont;
00067         if (!fnt) return NULL;
00068 
00069 
00070         glictFonts.insert(glictFonts.end(), fnt);
00071         fnt->SetName(name);
00072         return fnt;
00073 }
00074 
00075 glictFont* glictFindFont(const char* name) {
00076 
00077         _GLICTFONTVECTORITERATOR it;
00078         for (it = glictFonts.begin(); it<glictFonts.end() && strcmp((*it)->GetName().c_str(), name); it++);
00079         if (it!=glictFonts.end()) return *it;
00080         return NULL;
00081 }
00082 
00083 bool glictDeleteFont(const char* name) {
00084         _GLICTFONTVECTORITERATOR it;
00085 
00086         for (it = glictFonts.begin(); it!=glictFonts.end() && strcmp((*it)->GetName().c_str(), name); it++);
00087         if (it!=glictFonts.end()) {
00088                 delete *it;
00089                 glictFonts.erase(it);
00090                 return true;
00091 
00092         }
00093         return false;
00094 }
00095 
00119 bool glictFontRender(const char* text, const char* fontname, float x, float y) {
00120         return glictFontRender(text, fontname, 10, x, y);
00121 }
00122 
00123 
00148 #ifdef NO_GL
00149 #define glMatrixMode(x)
00150 #define glScalef(x,y,z)
00151 
00152 #endif
00153 bool glictFontRender(const char* text, const char* fontname, float fontsize, float x, float y) {
00154 
00155         glictFont* fnt;
00156     glMatrixMode(GL_MODELVIEW);
00157 
00158         fnt = glictFindFont(fontname);
00159         if (!fnt) {
00160                 printf("*** glictFontRender: Font %s not loaded\n", fontname);
00161                 return false;
00162         }
00163         if (strcmp(fnt->GetName().c_str(), fontname)) printf("*** glictFontRender: Font name different from what we searched for\n");
00164 
00165 
00166         if (fnt->RenderBoolSize) {
00167                 return fnt->RenderBoolSize(text, fnt->fontparam, fontsize, x, y); //
00168         }
00169         if (fnt->RenderBoolNoSize) {
00170                 //glPushMatrix();
00171 
00172                 glScalef(fontsize, fontsize, fontsize);
00173 
00174                 bool r = fnt->RenderBoolNoSize(text, fnt->fontparam, x/fontsize, y/fontsize);
00175                 glScalef(1./fontsize, 1./fontsize, 1./fontsize);
00176                 //glPopMatrix();
00177                 return r;
00178         }
00179         if (fnt->RenderVoidSize) {
00180                 fnt->RenderVoidSize(text, fnt->fontparam, fontsize, x, y); //
00181                 return true;
00182         }
00183         if (fnt->RenderVoidNoSize) {
00184                 //glPushMatrix();
00185                 glScalef(fontsize, fontsize, fontsize);
00186                 fnt->RenderVoidNoSize(text, fnt->fontparam, x/fontsize, y/fontsize);
00187                 glScalef(1./fontsize, 1./fontsize, 1./fontsize);
00188                 //glPopMatrix();
00189                 return true;
00190         }
00191 
00192         printf("*** glictFontRender: Font %s not renderable\n", fontname);
00193         return false;
00194 }
00195 
00196 float glictFontSize(const char* text, const char* font) {
00197         return glictFontSize(text, font, 10);
00198 }
00199 
00200 float glictFontSize(const char* text, const char* font, float size) {
00201 
00202         glictFont* fnt = glictFindFont(font);
00203         if (!fnt) printf("*** glictFontSize: Font %s not found\n", font);
00204         if (!fnt) return false;
00205         if (fnt->SizeSize) {
00206                 return fnt->SizeSize(text, fnt->fontparam, size); //
00207         }
00208         if (fnt->SizeNoSize) {
00209                 return fnt->SizeNoSize(text, fnt->fontparam)*size;
00210         }
00211         return 0;
00212 }
00213 
00214 int glictFontNumberOfLines(const char* txt) {
00215         int count=1; // at least 1 line
00216         for (unsigned int i=0;i<strlen(txt);i++) {
00217                 if (txt[i]=='\n') {
00218                         count++;
00219                 }
00220         }
00221 
00222         return count;
00223 }
00224 
00226 
00227 
00228 glictFont::glictFont() {
00229         // nope
00230 }
00231 
00232 glictFont::~glictFont() {
00233         // nope
00234 }
00235 
00236 void glictFont::SetName(const char* name) {
00237         this->name = name;
00238 }
00239 std::string glictFont::GetName() {
00240         return this->name;
00241 }
00242 
00244 void glictFont::SetRenderFunc(_glictFontRenderFuncBoolSize funcpointer) {
00245         this->RenderBoolNoSize = NULL;
00246         this->RenderBoolSize = funcpointer;
00247         this->RenderVoidNoSize = NULL;
00248         this->RenderVoidSize = NULL;
00249 }
00250 
00252 void glictFont::SetRenderFunc(_glictFontRenderFuncBoolNoSize funcpointer) {
00253         this->RenderBoolNoSize = funcpointer;
00254         this->RenderBoolSize = NULL;
00255         this->RenderVoidNoSize = NULL;
00256         this->RenderVoidSize = NULL;
00257 }
00258 
00260 void glictFont::SetRenderFunc(_glictFontRenderFuncVoidSize funcpointer) {
00261         this->RenderBoolNoSize = NULL;
00262         this->RenderBoolSize = NULL;
00263         this->RenderVoidNoSize = NULL;
00264         this->RenderVoidSize = funcpointer;
00265 }
00266 
00268 void glictFont::SetRenderFunc(_glictFontRenderFuncVoidNoSize funcpointer) {
00269         this->RenderBoolNoSize = NULL;
00270         this->RenderBoolSize = NULL;
00271         this->RenderVoidNoSize = funcpointer;
00272         this->RenderVoidSize = NULL;
00273 }
00274 
00276 void glictFont::SetSizeFunc(_glictFontSizeFuncSize funcpointer) {
00277         this->SizeNoSize = NULL;
00278         this->SizeSize = funcpointer;
00279 }
00280 
00282 void glictFont::SetSizeFunc(_glictFontSizeFuncNoSize funcpointer) {
00283         this->SizeNoSize = funcpointer;
00284         this->SizeSize = NULL;
00285 }
00286 
00287 void glictFont::SetFontParam(void* fp) {
00288         this->fontparam = fp;
00289 }
00290 

SourceForge.net Logo
generated with doxygen 1.5.3 on Mon Oct 29 18:09:26 2007