2 CDTestFramework http://codercorner.com
3 Copyright (c) 2007-2008 Pierre Terdiman, pierre@codercorner.com
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
22 #include "GLFontData.h"
23 #include "GLFontRenderer.h"
25 bool GLFontRenderer::m_isInit=false;
26 unsigned int GLFontRenderer::m_textureObject=0;
27 int GLFontRenderer::m_screenWidth=640;
28 int GLFontRenderer::m_screenHeight=480;
29 float GLFontRenderer::m_color[4]={1.0f, 1.0f, 1.0f, 1.0f};
31 bool GLFontRenderer::init()
33 glGenTextures(1, &m_textureObject);
34 if(m_textureObject == 0) return false;
36 glBindTexture(GL_TEXTURE_2D, m_textureObject);
37 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
38 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
41 unsigned char* pNewSource = new unsigned char[OGL_FONT_TEXTURE_WIDTH*OGL_FONT_TEXTURE_HEIGHT*4];
42 for(int i=0;i<OGL_FONT_TEXTURE_WIDTH*OGL_FONT_TEXTURE_HEIGHT;i++)
44 pNewSource[i*4+0]=255;
45 pNewSource[i*4+1]=255;
46 pNewSource[i*4+2]=255;
47 pNewSource[i*4+3]=OGLFontData[i];
49 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, OGL_FONT_TEXTURE_WIDTH, OGL_FONT_TEXTURE_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, pNewSource);
55 void GLFontRenderer::print(float x, float y, float fontSize, const char* pString, bool forceMonoSpace, int monoSpaceWidth, bool doOrthoProj)
57 // x = x*m_screenWidth;
58 // y = y*m_screenHeight;
59 fontSize = fontSize*m_screenHeight;
66 unsigned int num = (unsigned int)strlen(pString);
67 if(m_isInit && num > 0)
69 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
70 glDisable(GL_DEPTH_TEST);
71 glDisable(GL_LIGHTING);
73 glEnable(GL_TEXTURE_2D);
74 glBindTexture(GL_TEXTURE_2D, m_textureObject);
79 glMatrixMode(GL_PROJECTION);
82 glOrtho(0, m_screenWidth, 0, m_screenHeight, -1, 1);
84 glMatrixMode(GL_MODELVIEW);
90 glColor4f(m_color[0], m_color[1], m_color[2], m_color[3]);
92 const float glyphHeightUV = ((float)OGL_FONT_CHARS_PER_COL)/OGL_FONT_TEXTURE_HEIGHT*2-0.01f;
94 float translate = 0.0f;
96 float* pVertList = new float[num*3*6];
97 float* pTextureCoordList = new float[num*2*6];
99 int textureCoordIndex = 0;
101 float translateDown = 0.0f;
102 unsigned int count = 0;
104 for(unsigned int i=0;i<num; i++)
106 const float glyphWidthUV = ((float)OGL_FONT_CHARS_PER_ROW)/OGL_FONT_TEXTURE_WIDTH;
108 if (pString[i] == '\n') {
109 translateDown-=0.005f*m_screenHeight+fontSize;
114 int c = pString[i]-OGL_FONT_CHAR_BASE;
115 if (c < OGL_FONT_CHARS_PER_ROW*OGL_FONT_CHARS_PER_COL) {
119 float glyphWidth = (float)GLFontGlyphWidth[c];
121 glyphWidth = (float)monoSpaceWidth;
124 glyphWidth = glyphWidth*(fontSize/(((float)OGL_FONT_TEXTURE_WIDTH)/OGL_FONT_CHARS_PER_ROW))-0.01f;
126 float cxUV = float((c)%OGL_FONT_CHARS_PER_ROW)/OGL_FONT_CHARS_PER_ROW+0.008f;
127 float cyUV = float((c)/OGL_FONT_CHARS_PER_ROW)/OGL_FONT_CHARS_PER_COL+0.008f;
129 pTextureCoordList[textureCoordIndex++] = cxUV;
130 pTextureCoordList[textureCoordIndex++] = cyUV+glyphHeightUV;
131 pVertList[vertIndex++] = x+0+translate;
132 pVertList[vertIndex++] = y+0+translateDown;
133 pVertList[vertIndex++] = 0;
135 pTextureCoordList[textureCoordIndex++] = cxUV+glyphWidthUV;
136 pTextureCoordList[textureCoordIndex++] = cyUV;
137 pVertList[vertIndex++] = x+fontSize+translate;
138 pVertList[vertIndex++] = y+fontSize+translateDown;
139 pVertList[vertIndex++] = 0;
141 pTextureCoordList[textureCoordIndex++] = cxUV;
142 pTextureCoordList[textureCoordIndex++] = cyUV;
143 pVertList[vertIndex++] = x+0+translate;
144 pVertList[vertIndex++] = y+fontSize+translateDown;
145 pVertList[vertIndex++] = 0;
147 pTextureCoordList[textureCoordIndex++] = cxUV;
148 pTextureCoordList[textureCoordIndex++] = cyUV+glyphHeightUV;
149 pVertList[vertIndex++] = x+0+translate;
150 pVertList[vertIndex++] = y+0+translateDown;
151 pVertList[vertIndex++] = 0;
153 pTextureCoordList[textureCoordIndex++] = cxUV+glyphWidthUV;
154 pTextureCoordList[textureCoordIndex++] = cyUV+glyphHeightUV;
155 pVertList[vertIndex++] = x+fontSize+translate;
156 pVertList[vertIndex++] = y+0+translateDown;
157 pVertList[vertIndex++] = 0;
159 pTextureCoordList[textureCoordIndex++] = cxUV+glyphWidthUV;
160 pTextureCoordList[textureCoordIndex++] = cyUV;
161 pVertList[vertIndex++] = x+fontSize+translate;
162 pVertList[vertIndex++] = y+fontSize+translateDown;
163 pVertList[vertIndex++] = 0;
165 translate+=glyphWidth;
169 glEnableClientState(GL_VERTEX_ARRAY);
170 glVertexPointer(3, GL_FLOAT, 0, pVertList);
171 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
172 glTexCoordPointer(2, GL_FLOAT, 0, pTextureCoordList);
173 glDrawArrays(GL_TRIANGLES, 0, count*6);
174 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
175 glDisableClientState(GL_VERTEX_ARRAY);
178 delete[] pTextureCoordList;
182 glMatrixMode(GL_PROJECTION);
185 glMatrixMode(GL_MODELVIEW);
188 glEnable(GL_DEPTH_TEST);
189 glEnable(GL_LIGHTING);
190 glDisable(GL_TEXTURE_2D);
195 void GLFontRenderer::setScreenResolution(int screenWidth, int screenHeight)
197 m_screenWidth = screenWidth;
198 m_screenHeight = screenHeight;
201 void GLFontRenderer::setColor(float r, float g, float b, float a)