Loading:
1) Generate font path and attempt to load the font using SDL_TTF
2) Render 128 characters to individual SDL_Surfaces, from character 32 to 160
3) Keep track of the size of the largest characters
4) Create a new surface to render all 128 characters onto based on the largest width and height (also make sure it is a power of 2 to prevent scaling)
5) Iterate over all 128 of the characters, rendering to them to the full surface (16x8)
6) Create an OpenGL texture from the data contained in the full surface
7) Create an OpenGL list of all of the characters for ease of use when rendering text later
Rendering:
1) Generate output text data based on format string and arguments passed in
2) Use glCallLists to render the string using the previously generated font data
It's been somewhat successful so far, but I have hit two major snags:
1) SDL_Surfaces store pixel data upside down. I've read about this issue online, but have not found any simple solutions to this. I ultimately did find some code to flip the texture upside-down, but it seemed like an unnecessary amount of code to do so - and it still didn't fix the problem. In short, if I blit to an SDL_Surface, then attempt to make an OpenGL texture out of it, the image is upside-down when rendered. And since I'm generating the image on runtime, having an upside-down input file is not an option.
2) The text shows up yellow instead of white, as specified by the SDL_Color struct. I also found others online who had this same issue, but have not managed to find a solution. When the temp SDL_Surface is allocated, I had to change the RGBA masks to BGRA instead of the usual RGBA / ABGR depending on what endian SDL is using in order to have anything show up at all. I'm not sure if I horribly messed this up or not, but it's the only thing that produces any results thus far. I also tried SDL_DisplayFormat on the surface I was generating, but no matter what I change the RGBA masks to after that, there is no output.
I have attached a quad displaying the full image output at the bottom as well as relevant source code. If anyone has any advice / tips / hints / suggestions / solutions / etc. to this problem, they would be tremendously appreciated. I'll gladly surf around the forums and help other people out as well afterwards, seeing as how this is my first post. Thanks so much!
Edit: I just realized the texture co-ordinates are semi wrong when generating the lists, they're for a 16x16 grid, but the one I'm using is 16x8. They also don't account for non-perfect fit textures (ie. whitespace on the "top" and "right" to make the texture a power of 2, to prevent automatic scaling).
Font Loading:
bool Font::load() {
if(m_loaded) { return true; }
QString fontPath = QString(Game::settings->dataDirectory);
fontPath.append("/Fonts/");
fontPath.append(m_filename);
QFileInfo fontFile(fontPath);
if(!fontFile.exists()) { return false; }
QByteArray bytes = fontPath.toLocal8Bit();
TTF_Font * fontData;
if(!(fontData = TTF_OpenFont(bytes.data(), m_size))) {
return false;
}
SDL_Color colour = {255, 255, 255, 255};
SDL_Surface * character;
QVector<SDL_Surface *> characters;
int maxWidth = -1;
int maxHeight = -1;
char data[2];
data[1] = '\0';
for(int i=0;i<128;i++ ) {
data[0] = (char) (i + 32);
character = TTF_RenderText_Blended(fontData, data, colour);
characters.push_back(character);
if(maxWidth < 0 || maxWidth < character->w) { maxWidth = character->w; }
if(maxHeight < 0 || maxHeight < character->h) { maxHeight = character->h; }
}
int width = 1, tempWidth = maxWidth * 16;
int height = 1, tempHeight = maxHeight * 8;
while(width < tempWidth) { width *= 2; }
while(height < tempHeight) { height *= 2; }
SDL_Surface * temp = SDL_CreateRGBSurface(0, width, height, 32, 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff);
SDL_Rect sourceArea, destArea;
int x = 0;
int y = 0;
for(int i=0;i<128;i++) {
sourceArea.x = 0;
sourceArea.y = 0;
sourceArea.w = characters->w;
sourceArea.h = characters->h;
destArea.x = maxWidth * x;
destArea.y = maxHeight * y;
destArea.w = characters->w;
destArea.h = characters->h;
SDL_BlitSurface(characters, &sourceArea, temp, &destArea);
x++;
if(x == 16) {
x = 0;
y++;
}
}
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, temp->w, temp->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, temp->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
m_font = glGenLists(128);
float cx, cy;
for(int i=0;i<128;i++) {
cx = (float) (i % 16) / 16.0f;
cy = (float) (i / 16) / 16.0f;
glNewList(m_font + i, GL_COMPILE);
glBegin(GL_QUADS);
glTexCoord2f(cx, 1 - cy - 0.0625f); glVertex2i(0, m_size);
glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f); glVertex2i(m_size, m_size);
glTexCoord2f(cx + 0.0625f, 1 - cy ); glVertex2i(m_size, 0 );
glTexCoord2f(cx, 1 - cy ); glVertex2i(0, 0 );
glEnd();
glTranslated(10, 0, 0);
glEndList();
}
for(int i=0;i<128;i++) {
SDL_FreeSurface(characters);
}
SDL_FreeSurface(temp);
m_loaded = true;
return true;
}
Text Drawing:
void Font::drawText(int x, int y, const char * format, ...) const {
if(!m_loaded) { return; }
char text[256];
va_list args;
va_start(args, format);
vsprintf_s(text, 256, format, args);
va_end(args);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glBindTexture(GL_TEXTURE_2D, m_texture);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslated(x, y, 0);
glListBase(m_font - 32);
glCallLists(strlen(text), GL_BYTE, text);
glPopMatrix();
}
Output:
[attachment=4116:font_texture.png]