00001
00002 #include <stdio.h>
00003 #include <stdarg.h>
00004
00005
00006 #include <GL/gl.h>
00007
00008 #include "DrawText.h"
00009 #include <assert.h>
00010
00011 #include <string.h>
00012
00013 #include "tga.h"
00014
00015
00016
00017
00018
00019 void Go2d()
00020 {
00021
00022 glMatrixMode(GL_PROJECTION);
00023 glPushMatrix();
00024 glLoadIdentity();
00025 glOrtho(0,1024,0,768,-1,1);
00026 glMatrixMode(GL_MODELVIEW);
00027 glPushMatrix();
00028 glLoadIdentity();
00029 }
00030
00031
00032
00033
00034 void Exit2d()
00035 {
00036 glMatrixMode(GL_PROJECTION);
00037 glPopMatrix();
00038 glMatrixMode(GL_MODELVIEW);
00039 glPopMatrix();
00040 }
00041
00042
00043
00044 CTextDrawer* CTextDrawer::mSingleton = NULL;
00045
00046
00047
00048 CTextDrawer &CTextDrawer::GetSingleton()
00049 {
00050 if (!mSingleton)
00051 {
00052 mSingleton = new CTextDrawer;
00053 Initialise();
00054 }
00055
00056 return *mSingleton;
00057 }
00058
00059
00060
00061 void CTextDrawer::Destroy()
00062 {
00063 if (mSingleton)
00064 {
00065
00066 glDeleteLists(mSingleton->mBaseListId, NUMCHARS);
00067
00068 glDeleteTextures(1,&(mSingleton->mFontTexId));
00069 delete mSingleton;
00070 mSingleton = NULL;
00071 }
00072 }
00073
00074
00075 CTextDrawer::CTextDrawer()
00076 {
00077 mBaseListId = 0;
00078 mR = mG = mB = mA = 1.0f;
00079 }
00080
00081
00082
00083
00084 void CTextDrawer::SetFontTextureId(GLuint aFontTextureId)
00085 {
00086 mSingleton->mFontTexId = aFontTextureId;
00087 }
00088
00089
00090 void CTextDrawer::Initialise()
00091 {
00092 assert(mSingleton->mFontTexId);
00093
00094 float cx;
00095 float cy;
00096 int loop;
00097
00098 mSingleton->mFontTexId = createTexture("font_arial.tga");
00099
00100 mSingleton->mBaseListId = glGenLists(NUMCHARS);
00101 glBindTexture(GL_TEXTURE_2D, mSingleton->mFontTexId);
00102 for (loop=0; loop<NUMCHARS; loop++)
00103 {
00104
00105
00106 cx=((float)(loop%16))/16.0f;
00107 cy=(((float)(loop/16))/16.0f);
00108
00109 glNewList(mSingleton->mBaseListId+loop, GL_COMPILE);
00110 glBegin(GL_QUADS);
00111 glTexCoord2f(cx,cy);
00112 glVertex2i(0,0);
00113 glTexCoord2f(cx+0.0625f,cy);
00114 glVertex2i(16,0);
00115 glTexCoord2f(cx+0.0625f,cy+0.0625f);
00116 glVertex2i(16,16);
00117 glTexCoord2f(cx,cy+0.0625f);
00118 glVertex2i(0,16);
00119 glEnd();
00120 glTranslatef(12.0f,0,0);
00121 glEndList();
00122 }
00123 }
00124
00125
00126
00127 void CTextDrawer::SetColor(float aR, float aG, float aB, float aA)
00128 {
00129 mR = aR;
00130 mG = aG;
00131 mB = aB;
00132 mA = aA;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 bool CTextDrawer::PrintText(bool bold, int maxLength, const char *fmt, ...)
00148 {
00149 assert(mBaseListId != 0);
00150
00151 char text[1024];
00152 va_list ap;
00153
00154 if (fmt == NULL)
00155 return true;
00156
00157
00158 va_start(ap, fmt);
00159 vsprintf(text, fmt, ap);
00160 va_end(ap);
00161
00162 glEnable(GL_TEXTURE_2D);
00163 glBindTexture(GL_TEXTURE_2D, mFontTexId);
00164
00165
00166
00167 glEnable(GL_BLEND);
00168 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00169
00170
00171
00172
00173
00174
00175
00176
00177 if (bold)
00178 glListBase(mBaseListId - ' ' + 0);
00179 else
00180 glListBase(mBaseListId - ' ' + 128);
00181
00182
00183
00184
00185 if (maxLength == -1)
00186 glCallLists(strlen(text), GL_BYTE, text);
00187 else
00188 glCallLists(maxLength, GL_BYTE, text);
00189
00190
00191 glDisable(GL_TEXTURE_2D);
00192
00193
00194 glDisable(GL_BLEND);
00195
00196
00197 return true;
00198 }
00199