TextPrimitive.cpp

Go to the documentation of this file.
00001 // Copyright (C) 2005 Dave Griffiths
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 
00017 #include "Renderer.h"
00018 #include "TextPrimitive.h"
00019 #include "State.h"
00020 
00021 using namespace Fluxus;
00022     
00023 TextPrimitive::TextPrimitive(float charw, float charh, int charstride, int wrapchars) :
00024 PolyPrimitive(PolyPrimitive::QUADS),
00025 m_CharWidth(charw),
00026 m_CharHeight(charh),
00027 m_CharStride(charstride),
00028 m_WrapChars(wrapchars)
00029 {
00030 }
00031 
00032 TextPrimitive::TextPrimitive(const TextPrimitive &other) :
00033 PolyPrimitive(other),
00034 m_CharWidth(other.m_CharWidth),
00035 m_CharHeight(other.m_CharHeight),
00036 m_CharStride(other.m_CharStride),
00037 m_TextWidth(other.m_TextWidth),
00038 m_TextHeight(other.m_TextHeight),
00039 m_WrapChars(other.m_WrapChars)
00040 {
00041 }
00042 
00043 TextPrimitive* TextPrimitive::Clone() const 
00044 {
00045     return new TextPrimitive(*this); 
00046 }
00047 
00048 void TextPrimitive::SetText(const string &s, float Width, float Height, float Zoom)
00049 {
00050     float x=0,y=0;
00051     dVector Normal(0,0,1);
00052     
00053     float w=m_CharWidth*Width;
00054     float h=m_CharHeight*Height;
00055     
00056     m_TextWidth=w*s.size();
00057     m_TextHeight=h;
00058     int wrapcount=0;
00059         
00060     w-=Zoom*50; //??? some constant scaling to covert from texture 
00061                 // coordinates to world space
00062         
00063     for (unsigned int n=0; n<s.size(); n++)
00064     {
00065         int pos=(int)s[n];
00066 
00067         float S=(pos%m_CharStride)*m_CharWidth;
00068         float T=(pos/m_CharStride)*m_CharHeight;
00069         
00070         dVector min(S,T,0);
00071         dVector max(S+m_CharWidth,T+m_CharHeight,0);
00072                 
00073         min.x+=Zoom;
00074         max.x-=Zoom;
00075         
00076         AddVertex(dVertex(dVector(x,y,0),Normal,min.x,min.y));
00077         AddVertex(dVertex(dVector(x+w,y,0),Normal,max.x,min.y));
00078         AddVertex(dVertex(dVector(x+w,y+h,0),Normal,max.x,max.y));
00079         AddVertex(dVertex(dVector(x,y+h,0),Normal,min.x,max.y));
00080         if (m_WrapChars) wrapcount++;
00081         
00082         if (s[n]=='\n' || (m_WrapChars && wrapcount>m_WrapChars))
00083         {
00084             y+=h;
00085             m_TextHeight+=h;
00086             x=0;
00087             wrapcount=0;
00088         }
00089         else
00090         {
00091             x+=w;
00092         }
00093     }
00094 }
00095 
00096 void TextPrimitive::Render()
00097 {
00098     glDisable(GL_CULL_FACE);
00099     PolyPrimitive::Render();
00100     glEnable(GL_CULL_FACE);
00101 }
00102 
00103 istream &Fluxus::operator>>(istream &s, TextPrimitive &o)
00104 {
00105     return s;
00106 }
00107 
00108 ostream &Fluxus::operator<<(ostream &s, TextPrimitive &o)
00109 {
00110     return s;
00111 }

Generated on Mon Feb 11 06:54:25 2008 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1