[580] | 1 | #include <R2/R2Fonts.h> |
---|
| 2 | #include <R2/R2FilePath.h> |
---|
| 3 | #include <fstream> |
---|
| 4 | #include <stdarg.h> |
---|
| 5 | |
---|
| 6 | // constants |
---|
| 7 | const int c_nFileMagicHeader = 6666; |
---|
| 8 | |
---|
[953] | 9 | R2Fonts::R2Fonts() : |
---|
| 10 | _fontIndex(-1), |
---|
| 11 | _screenWidth(512), |
---|
| 12 | _screenHeight(512) |
---|
[580] | 13 | { |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | R2Fonts::~R2Fonts() |
---|
| 17 | { |
---|
[929] | 18 | for (unsigned index = 0; index < _fonts.size(); ++index) { |
---|
[580] | 19 | glDeleteLists(_fonts[index]._displayLists, 256); |
---|
| 20 | glDeleteTextures(1, &(_fonts[_fontIndex]. _fontTextureID)); |
---|
[835] | 21 | } |
---|
[580] | 22 | } |
---|
| 23 | |
---|
[835] | 24 | void |
---|
| 25 | R2Fonts::setFont(const char* fontName) |
---|
[580] | 26 | { |
---|
[1111] | 27 | if (fontName != NULL) { |
---|
| 28 | unsigned int i; |
---|
| 29 | for (i = 0; i < _fonts.size(); ++i) { |
---|
| 30 | if (strcmp(_fonts[i]._fontName, fontName) == 0) { |
---|
| 31 | _fontIndex = i; |
---|
| 32 | break; |
---|
| 33 | } |
---|
| 34 | } |
---|
[835] | 35 | } |
---|
[580] | 36 | } |
---|
| 37 | |
---|
| 38 | |
---|
[835] | 39 | void |
---|
| 40 | R2Fonts::addFont(const char* fontName, const char* fontFileName) |
---|
[580] | 41 | { |
---|
[835] | 42 | R2FontAttributes sFont; |
---|
| 43 | |
---|
| 44 | loadFont(fontName, fontFileName, sFont); |
---|
| 45 | initializeFont(sFont); |
---|
| 46 | _fonts.push_back(sFont); |
---|
[580] | 47 | } |
---|
| 48 | |
---|
[835] | 49 | void |
---|
| 50 | R2Fonts::draw(const char* pString, ...) const |
---|
[580] | 51 | { |
---|
[835] | 52 | va_list vlArgs; |
---|
[580] | 53 | char szVargsBuffer[1024]; |
---|
| 54 | |
---|
| 55 | va_start(vlArgs, pString); |
---|
| 56 | vsprintf(szVargsBuffer, pString, vlArgs); |
---|
[835] | 57 | |
---|
| 58 | if (_fontIndex != -1) { |
---|
[580] | 59 | int length = strlen(szVargsBuffer); |
---|
[953] | 60 | |
---|
[580] | 61 | glListBase(_fonts[_fontIndex]._displayLists); |
---|
[835] | 62 | glCallLists(length, GL_UNSIGNED_BYTE, |
---|
[953] | 63 | reinterpret_cast<const GLvoid*>(szVargsBuffer)); |
---|
[580] | 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
[953] | 67 | void |
---|
| 68 | R2Fonts::begin() |
---|
[580] | 69 | { |
---|
[835] | 70 | glEnable(GL_TEXTURE_2D); |
---|
| 71 | glBindTexture(GL_TEXTURE_2D, _fonts[_fontIndex]. _fontTextureID); |
---|
| 72 | |
---|
[580] | 73 | glPushAttrib(GL_TRANSFORM_BIT | GL_ENABLE_BIT); |
---|
| 74 | glMatrixMode(GL_PROJECTION); |
---|
| 75 | glPushMatrix(); |
---|
| 76 | |
---|
| 77 | glLoadIdentity( ); |
---|
| 78 | gluOrtho2D(0.0f, _screenWidth, _screenHeight, 0.0f); |
---|
| 79 | |
---|
| 80 | glMatrixMode(GL_MODELVIEW); |
---|
| 81 | glPushMatrix( ); |
---|
| 82 | glLoadIdentity( ); |
---|
| 83 | |
---|
| 84 | glEnable(GL_TEXTURE_2D); |
---|
| 85 | glEnable(GL_BLEND); |
---|
| 86 | glDisable(GL_DEPTH_TEST); |
---|
| 87 | } |
---|
| 88 | |
---|
[953] | 89 | void |
---|
| 90 | R2Fonts::end() |
---|
[580] | 91 | { |
---|
[835] | 92 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
[580] | 93 | |
---|
[835] | 94 | glMatrixMode(GL_PROJECTION); |
---|
[580] | 95 | glPopMatrix( ); |
---|
| 96 | |
---|
| 97 | glMatrixMode(GL_MODELVIEW); |
---|
| 98 | glPopMatrix( ); |
---|
| 99 | |
---|
| 100 | glPopAttrib( ); |
---|
| 101 | } |
---|
| 102 | |
---|
[953] | 103 | void |
---|
| 104 | R2Fonts::initializeFont(R2FontAttributes& attr) |
---|
[580] | 105 | { |
---|
[835] | 106 | attr._displayLists = glGenLists(256); |
---|
[580] | 107 | |
---|
[835] | 108 | R2int32 index; |
---|
| 109 | for (index = 0; index < 256; ++index) { |
---|
[953] | 110 | R2FontAttributes::R2CharInfo& charInfo = attr._chars[index]; |
---|
| 111 | glNewList(attr._displayLists + index, GL_COMPILE); |
---|
| 112 | if (charInfo._valid) { |
---|
| 113 | glBegin(GL_TRIANGLE_STRIP); |
---|
| 114 | |
---|
| 115 | glTexCoord2f(charInfo._left, charInfo._top ); |
---|
[1113] | 116 | glVertex2i(0, 0); |
---|
[953] | 117 | |
---|
| 118 | glTexCoord2f(charInfo._left, charInfo._bottom); |
---|
[1113] | 119 | glVertex2i(0, attr._fontHeight); |
---|
[953] | 120 | |
---|
| 121 | glTexCoord2f(charInfo._right, charInfo._top); |
---|
[1113] | 122 | glVertex2i(charInfo._width, 0); |
---|
[953] | 123 | |
---|
| 124 | glTexCoord2f(charInfo._right, charInfo._bottom); |
---|
[1113] | 125 | glVertex2i((int)charInfo._width, (int)attr._fontHeight); |
---|
[953] | 126 | |
---|
| 127 | glEnd( ); |
---|
[580] | 128 | glTranslatef(charInfo._width, 0.0f, 0.0f); |
---|
[953] | 129 | } |
---|
| 130 | glEndList(); |
---|
[835] | 131 | } |
---|
[580] | 132 | } |
---|
| 133 | |
---|
[835] | 134 | R2bool |
---|
| 135 | R2Fonts::loadFont(const char* fontName, const char* fontFileName, |
---|
[953] | 136 | R2FontAttributes& sFont) |
---|
[580] | 137 | { |
---|
[835] | 138 | R2bool bSuccess = false; |
---|
[1111] | 139 | |
---|
| 140 | const char *path = R2FilePath::getInstance()->getPath(fontFileName); |
---|
| 141 | if (path == NULL) { |
---|
| 142 | return false; |
---|
| 143 | } |
---|
| 144 | std::ifstream fsInput(path, std::ios::binary); |
---|
[835] | 145 | if (fsInput) { |
---|
[1111] | 146 | sFont._fontName = new char [strlen(fontName)+1]; |
---|
| 147 | strcpy(sFont._fontName, fontName); |
---|
| 148 | |
---|
[580] | 149 | // make sure this file is the correct type by checking the header |
---|
| 150 | unsigned int uiFileId = 0; |
---|
| 151 | fsInput.read(reinterpret_cast<char*>(&uiFileId), sizeof(unsigned int)); |
---|
[929] | 152 | if (uiFileId == (unsigned int)c_nFileMagicHeader) { |
---|
[580] | 153 | // read general font/texture dimensions |
---|
[835] | 154 | unsigned int uiTextureWidth, uiTextureHeight, uiFontHeight; |
---|
| 155 | uiTextureWidth = uiTextureHeight = uiFontHeight = 0; |
---|
| 156 | fsInput.read(reinterpret_cast<char*>(&sFont._textureWidth), |
---|
[953] | 157 | sizeof(unsigned int)); |
---|
[835] | 158 | fsInput.read(reinterpret_cast<char*>(&sFont._textureHeight), |
---|
[953] | 159 | sizeof(unsigned int)); |
---|
[835] | 160 | fsInput.read(reinterpret_cast<char*>(&sFont._fontHeight), |
---|
[953] | 161 | sizeof(unsigned int)); |
---|
[580] | 162 | |
---|
| 163 | // read dimensions for each charactor in 256-char ASCII chart |
---|
[835] | 164 | for (int i = 0; i < 256; ++i) { |
---|
[580] | 165 | unsigned int uiSize = 0; |
---|
| 166 | |
---|
| 167 | // top |
---|
[835] | 168 | fsInput.read(reinterpret_cast<char*>(&uiSize), |
---|
[953] | 169 | sizeof(unsigned int)); |
---|
[580] | 170 | sFont._chars[i]._top = static_cast<float>(uiSize) / sFont._textureHeight; |
---|
| 171 | // left |
---|
| 172 | fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int)); |
---|
| 173 | sFont._chars[i]._left = static_cast<float>(uiSize) / sFont._textureWidth; |
---|
| 174 | // bottom |
---|
| 175 | fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int)); |
---|
| 176 | sFont._chars[i]._bottom = static_cast<float>(uiSize) / sFont._textureHeight; |
---|
| 177 | // right |
---|
| 178 | fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int)); |
---|
| 179 | sFont._chars[i]._right = static_cast<float>(uiSize) / sFont._textureWidth; |
---|
| 180 | // enabled |
---|
| 181 | fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int)); |
---|
| 182 | sFont._chars[i]._valid = (uiSize != 0); |
---|
| 183 | // width factor |
---|
| 184 | float fWidthFactor = 1.0f; |
---|
| 185 | fsInput.read(reinterpret_cast<char*>(&fWidthFactor), sizeof(float)); |
---|
| 186 | sFont._chars[i]._width = fWidthFactor * sFont._fontHeight; |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | // allocate and read the texture map |
---|
[835] | 191 | if (!fsInput.eof() && !fsInput.fail()) { |
---|
[580] | 192 | unsigned int uiArea = sFont._textureWidth * sFont._textureHeight; |
---|
| 193 | unsigned char* pRawMap = new unsigned char[uiArea]; |
---|
| 194 | fsInput.read(reinterpret_cast<char*>(pRawMap), uiArea); |
---|
| 195 | |
---|
[835] | 196 | // we've only read the luminance values, but we need a luminance + |
---|
| 197 | // alpha buffer, so we make a new buffer and duplicate the |
---|
| 198 | // luminance values |
---|
[580] | 199 | unsigned char* pTexMap = new unsigned char[2 * uiArea]; |
---|
| 200 | unsigned char* pMap = pTexMap; |
---|
[835] | 201 | for (unsigned int i = 0; i < uiArea; ++i) { |
---|
[580] | 202 | *pMap++ = pRawMap[i]; |
---|
| 203 | *pMap++ = pRawMap[i]; |
---|
| 204 | } |
---|
| 205 | delete[] pRawMap; |
---|
| 206 | pRawMap = NULL; |
---|
| 207 | |
---|
| 208 | // make texture map out of new buffer |
---|
| 209 | glGenTextures(1, &sFont._fontTextureID); |
---|
| 210 | glBindTexture(GL_TEXTURE_2D, sFont._fontTextureID); |
---|
[835] | 211 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, |
---|
[953] | 212 | sFont._textureWidth, sFont._textureHeight, 0, |
---|
| 213 | GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, pTexMap); |
---|
[580] | 214 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
---|
| 215 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
| 216 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); |
---|
| 217 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); |
---|
| 218 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
| 219 | |
---|
| 220 | delete[] pTexMap; |
---|
| 221 | pTexMap = NULL; |
---|
| 222 | |
---|
| 223 | bSuccess = true; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | fsInput.close( ); |
---|
| 227 | } |
---|
[1111] | 228 | delete [] path; |
---|
[580] | 229 | return bSuccess; |
---|
| 230 | } |
---|
| 231 | |
---|
[835] | 232 | void |
---|
| 233 | R2Fonts::resize(R2int32 width, R2int32 height) |
---|
[580] | 234 | { |
---|
[835] | 235 | _screenWidth = width; |
---|
| 236 | _screenHeight = height; |
---|
| 237 | } |
---|
| 238 | |
---|