| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> |
| <title>FTGL: FTGLTextureFont.cpp Source File</title> |
| <link href="doxygen.css" rel="stylesheet" type="text/css"> |
| </head><body> |
| <!-- Generated by Doxygen 1.3.6 --> |
| <div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a></div> |
| <h1>FTGLTextureFont.cpp</h1><a href="FTGLTextureFont_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="preprocessor">#include <string></span> <span class="comment">// For memset</span> |
| 00002 |
| 00003 <span class="preprocessor">#include "<a class="code" href="FTGLTextureFont_8h.html">FTGLTextureFont.h</a>"</span> |
| 00004 <span class="preprocessor">#include "<a class="code" href="FTTextureGlyph_8h.html">FTTextureGlyph.h</a>"</span> |
| 00005 |
| 00006 |
| <a name="l00007"></a><a class="code" href="FTGLTextureFont_8cpp.html#a0">00007</a> <span class="keyword">inline</span> GLuint <a class="code" href="FTGLTextureFont_8cpp.html#a0">NextPowerOf2</a>( GLuint in) |
| 00008 { |
| 00009 in -= 1; |
| 00010 |
| 00011 in |= in >> 16; |
| 00012 in |= in >> 8; |
| 00013 in |= in >> 4; |
| 00014 in |= in >> 2; |
| 00015 in |= in >> 1; |
| 00016 |
| 00017 <span class="keywordflow">return</span> in + 1; |
| 00018 } |
| 00019 |
| 00020 |
| <a name="l00021"></a><a class="code" href="classFTGLTextureFont.html#a0">00021</a> <a class="code" href="classFTGLTextureFont.html#a0">FTGLTextureFont::FTGLTextureFont</a>( <span class="keyword">const</span> <span class="keywordtype">char</span>* fontFilePath) |
| 00022 : <a class="code" href="classFTFont.html">FTFont</a>( fontFilePath), |
| 00023 maximumGLTextureSize(0), |
| 00024 textureWidth(0), |
| 00025 textureHeight(0), |
| 00026 glyphHeight(0), |
| 00027 glyphWidth(0), |
| 00028 padding(3), |
| 00029 xOffset(0), |
| 00030 yOffset(0) |
| 00031 { |
| 00032 remGlyphs = numGlyphs = face.<a class="code" href="classFTFace.html#a11">GlyphCount</a>(); |
| 00033 } |
| 00034 |
| 00035 |
| <a name="l00036"></a><a class="code" href="classFTGLTextureFont.html#a1">00036</a> <a class="code" href="classFTGLTextureFont.html#a0">FTGLTextureFont::FTGLTextureFont</a>( <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *pBufferBytes, size_t bufferSizeInBytes) |
| 00037 : <a class="code" href="classFTFont.html">FTFont</a>( pBufferBytes, bufferSizeInBytes), |
| 00038 maximumGLTextureSize(0), |
| 00039 textureWidth(0), |
| 00040 textureHeight(0), |
| 00041 glyphHeight(0), |
| 00042 glyphWidth(0), |
| 00043 padding(3), |
| 00044 xOffset(0), |
| 00045 yOffset(0) |
| 00046 { |
| 00047 remGlyphs = numGlyphs = face.<a class="code" href="classFTFace.html#a11">GlyphCount</a>(); |
| 00048 } |
| 00049 |
| 00050 |
| <a name="l00051"></a><a class="code" href="classFTGLTextureFont.html#a2">00051</a> <a class="code" href="classFTGLTextureFont.html#a2">FTGLTextureFont::~FTGLTextureFont</a>() |
| 00052 { |
| 00053 glDeleteTextures( textureIDList.<a class="code" href="classFTVector.html#a3">size</a>(), (<span class="keyword">const</span> GLuint*)&textureIDList[0]); |
| 00054 } |
| 00055 |
| 00056 |
| 00057 <a class="code" href="classFTGlyph.html">FTGlyph</a>* <a class="code" href="classFTFont.html#b0">FTGLTextureFont::MakeGlyph</a>( <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> glyphIndex) |
| 00058 { |
| 00059 FT_GlyphSlot ftGlyph = face.<a class="code" href="classFTFace.html#a10">Glyph</a>( glyphIndex, FT_LOAD_NO_HINTING); |
| 00060 |
| 00061 <span class="keywordflow">if</span>( ftGlyph) |
| 00062 { |
| 00063 glyphHeight = static_cast<int>( charSize.<a class="code" href="classFTSize.html#a6">Height</a>()); |
| 00064 glyphWidth = static_cast<int>( charSize.<a class="code" href="classFTSize.html#a7">Width</a>()); |
| 00065 |
| 00066 <span class="keywordflow">if</span>( textureIDList.<a class="code" href="classFTVector.html#a9">empty</a>()) |
| 00067 { |
| 00068 textureIDList.<a class="code" href="classFTVector.html#a14">push_back</a>( CreateTexture()); |
| 00069 xOffset = yOffset = padding; |
| 00070 } |
| 00071 |
| 00072 <span class="keywordflow">if</span>( xOffset > ( textureWidth - glyphWidth)) |
| 00073 { |
| 00074 xOffset = padding; |
| 00075 yOffset += glyphHeight; |
| 00076 |
| 00077 <span class="keywordflow">if</span>( yOffset > ( textureHeight - glyphHeight)) |
| 00078 { |
| 00079 textureIDList.<a class="code" href="classFTVector.html#a14">push_back</a>( CreateTexture()); |
| 00080 yOffset = padding; |
| 00081 } |
| 00082 } |
| 00083 |
| 00084 <a class="code" href="classFTTextureGlyph.html">FTTextureGlyph</a>* tempGlyph = <span class="keyword">new</span> <a class="code" href="classFTTextureGlyph.html">FTTextureGlyph</a>( ftGlyph, textureIDList[textureIDList.<a class="code" href="classFTVector.html#a3">size</a>() - 1], |
| 00085 xOffset, yOffset, textureWidth, textureHeight); |
| 00086 xOffset += static_cast<int>( tempGlyph-><a class="code" href="classFTGlyph.html#a4">BBox</a>().<a class="code" href="classFTBBox.html#o3">upperX</a> - tempGlyph-><a class="code" href="classFTGlyph.html#a4">BBox</a>().<a class="code" href="classFTBBox.html#o0">lowerX</a> + padding); |
| 00087 |
| 00088 --remGlyphs; |
| 00089 <span class="keywordflow">return</span> tempGlyph; |
| 00090 } |
| 00091 |
| 00092 <a class="code" href="classFTFont.html#p3">err</a> = <a class="code" href="classFTFont.html#p0">face</a>.<a class="code" href="classFTFace.html#a12">Error</a>(); |
| 00093 <span class="keywordflow">return</span> NULL; |
| 00094 } |
| 00095 |
| 00096 |
| 00097 <span class="keywordtype">void</span> FTGLTextureFont::CalculateTextureSize() |
| 00098 { |
| 00099 <span class="keywordflow">if</span>( !maximumGLTextureSize) |
| 00100 { |
| 00101 glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*)&maximumGLTextureSize); |
| 00102 assert(maximumGLTextureSize); <span class="comment">// If you hit this then you have an invalid OpenGL context.</span> |
| 00103 } |
| 00104 |
| 00105 textureWidth = <a class="code" href="FTGLTextureFont_8cpp.html#a0">NextPowerOf2</a>( (remGlyphs * glyphWidth) + ( padding * 2)); |
| 00106 textureWidth = textureWidth > maximumGLTextureSize ? maximumGLTextureSize : textureWidth; |
| 00107 |
| 00108 <span class="keywordtype">int</span> h = static_cast<int>( (textureWidth - ( padding * 2)) / glyphWidth); |
| 00109 |
| 00110 textureHeight = <a class="code" href="FTGLTextureFont_8cpp.html#a0">NextPowerOf2</a>( (( numGlyphs / h) + 1) * glyphHeight); |
| 00111 textureHeight = textureHeight > maximumGLTextureSize ? maximumGLTextureSize : textureHeight; |
| 00112 } |
| 00113 |
| 00114 |
| 00115 GLuint FTGLTextureFont::CreateTexture() |
| 00116 { |
| 00117 CalculateTextureSize(); |
| 00118 |
| 00119 <span class="keywordtype">int</span> totalMemory = textureWidth * textureHeight; |
| 00120 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* textureMemory = <span class="keyword">new</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>[totalMemory]; |
| 00121 memset( textureMemory, 0, totalMemory); |
| 00122 |
| 00123 GLuint textID; |
| 00124 glGenTextures( 1, (GLuint*)&textID); |
| 00125 |
| 00126 glBindTexture( GL_TEXTURE_2D, textID); |
| 00127 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); |
| 00128 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); |
| 00129 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 00130 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 00131 |
| 00132 glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, textureWidth, textureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, textureMemory); |
| 00133 |
| 00134 <span class="keyword">delete</span> [] textureMemory; |
| 00135 |
| 00136 <span class="keywordflow">return</span> textID; |
| 00137 } |
| 00138 |
| 00139 |
| <a name="l00140"></a><a class="code" href="classFTGLTextureFont.html#a3">00140</a> <span class="keywordtype">bool</span> <a class="code" href="classFTFont.html#a9">FTGLTextureFont::FaceSize</a>( <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size, <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> res) |
| 00141 { |
| 00142 <span class="keywordflow">if</span>( !textureIDList.<a class="code" href="classFTVector.html#a9">empty</a>()) |
| 00143 { |
| 00144 glDeleteTextures( textureIDList.<a class="code" href="classFTVector.html#a3">size</a>(), (<span class="keyword">const</span> GLuint*)&textureIDList[0]); |
| 00145 textureIDList.<a class="code" href="classFTVector.html#a12">clear</a>(); |
| 00146 remGlyphs = numGlyphs = face.<a class="code" href="classFTFace.html#a11">GlyphCount</a>(); |
| 00147 } |
| 00148 |
| 00149 <span class="keywordflow">return</span> <a class="code" href="classFTFont.html#a9">FTFont::FaceSize</a>( size, res); |
| 00150 } |
| 00151 |
| 00152 |
| <a name="l00153"></a><a class="code" href="classFTGLTextureFont.html#a4">00153</a> <span class="keywordtype">void</span> <a class="code" href="classFTGLTextureFont.html#a4">FTGLTextureFont::Render</a>( <span class="keyword">const</span> <span class="keywordtype">char</span>* string) |
| 00154 { |
| 00155 glPushAttrib( GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT); |
| 00156 |
| 00157 glEnable(GL_BLEND); |
| 00158 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); <span class="comment">// GL_ONE</span> |
| 00159 |
| 00160 <a class="code" href="classFTTextureGlyph.html#e0">FTTextureGlyph::ResetActiveTexture</a>(); |
| 00161 |
| 00162 <a class="code" href="classFTFont.html#a19">FTFont::Render</a>( string); |
| 00163 |
| 00164 glPopAttrib(); |
| 00165 } |
| 00166 |
| 00167 |
| <a name="l00168"></a><a class="code" href="classFTGLTextureFont.html#a5">00168</a> <span class="keywordtype">void</span> <a class="code" href="classFTGLTextureFont.html#a4">FTGLTextureFont::Render</a>( <span class="keyword">const</span> <span class="keywordtype">wchar_t</span>* string) |
| 00169 { |
| 00170 glPushAttrib( GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT); |
| 00171 |
| 00172 glEnable(GL_BLEND); |
| 00173 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); <span class="comment">// GL_ONE</span> |
| 00174 |
| 00175 <a class="code" href="classFTTextureGlyph.html#e0">FTTextureGlyph::ResetActiveTexture</a>(); |
| 00176 |
| 00177 <a class="code" href="classFTFont.html#a19">FTFont::Render</a>( string); |
| 00178 |
| 00179 glPopAttrib(); |
| 00180 } |
| 00181 |
| </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Dec 5 22:24:06 2004 for FTGL by |
| <a href="http://www.doxygen.org/index.html"> |
| <img src="doxygen.png" alt="doxygen" align="middle" border=0 > |
| </a>1.3.6 </small></address> |
| </body> |
| </html> |