Well I finally got somewhere, and it's given me an UNBELIEVABLE speed increase!
I can't believe rendering "Hello" with my old code was so damn slow.
Problem is, the text looks pretty ugly, and very inconsistent. Here's a screenshot rendering Arial 28pt:
And some source:
namespace SYM{ SYM_FONT::SYM_FONT() { //Set default colour Colour.r = 255; Colour.g = 255; Colour.b = 255; Colour.unused = 255; } bool SYM_FONT::LoadFont(string TTF_Filename, int Size) { //Load the font Font = TTF_OpenFont(TTF_Filename.c_str(), Size); //Check font loaded if (Font == NULL) { //Return return false; } //Get font attributes Ascent = TTF_FontAscent(Font); Descent = TTF_FontDescent(Font); Height = TTF_FontHeight(Font); LineSkip = TTF_FontLineSkip(Font); //Loop through all chars for (int i = 0; i <= 255; i++) { //Clear the surface TempSurface = NULL; //Render the glyph onto the SDL surface TempSurface = TTF_RenderGlyph_Blended(Font, i, Colour); //Check the glyph rendered if ( TempSurface == NULL ) { //Close the font TTF_CloseFont(Font); //Return return false; } //New temp glyph TempGlyph = new SYM_GLYPH; //Get the glyph attributes TTF_GlyphMetrics(Font, i, &TempGlyph->MinX, &TempGlyph->MaxX, &TempGlyph->MinY, &TempGlyph->MaxY, &TempGlyph->Advance); //Convert SDL surface into OpenGL GLuint texture TempGlyph->Texture = SDL_Surface_To_GL_Tex(TempSurface); //Push back to glyphs vector Glyphs.push_back(*TempGlyph); //Delete temp glyph delete TempGlyph; //Free surface SDL_FreeSurface(TempSurface); } //Close the font TTF_CloseFont(Font); return true; } void SYM_FONT::RenderText(string Text, float x, float y) { int TexMinX, TexMaxX, TexMinY, TexMaxY, Width, Height, CharID; //Enable OpenGL texture mapping glEnable(GL_TEXTURE_2D); //Enable blending glEnable(GL_BLEND); //Loop through characters for (int i = 0; i < Text.size(); i++) { //Get ID of current char CharID = (int)Text; Width = Glyphs[CharID].MaxX; Height = Glyphs[CharID].MaxY; //Bind the glyph texture glBindTexture(GL_TEXTURE_2D, Glyphs[CharID].Texture); //Set the colour to white (for correct blending) glColor3f(1.0f, 1.0f, 1.0f); //Draw the glyph glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2f(x, y ); glTexCoord2f(1, 0); glVertex2f(x + Width, y ); glTexCoord2f(1, 1); glVertex2f(x + Width, y + Height); glTexCoord2f(0, 1); glVertex2f(x, y + Height); glEnd(); x += Glyphs[CharID].Advance; } //Disable blending glDisable(GL_BLEND); //Disable texture mapping glDisable(GL_TEXTURE_2D); } GLuint SDL_Surface_To_GL_Tex(SDL_Surface *Surface) { GLuint Texture; int Width, Height; SDL_Surface *Image; SDL_Rect Area; //Get the dimentions of the surface Area.x = 0; Area.y = 0; Area.w = Surface->w; Area.h = Surface->h; //Make sure the width and height are powers of two Width = NextPowerOfTwo(Surface->w); Height = NextPowerOfTwo(Surface->h); //Create an OpenGL compliant surface Image = SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); //Copy the surface to the OpenGL compatible one SDL_BlitSurface(Surface, &Area, Image, &Area); //Generate an OpenGL texture glGenTextures(1, &Texture); //Bind the texture ready for use glBindTexture(GL_TEXTURE_2D, Texture); //Copy data from the SDL surface to the OpenGL texture glTexImage2D(GL_TEXTURE_2D, 0, 4, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image->pixels); //Set texture filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //Free the SDL surface SDL_FreeSurface(Image); //Return the texture return Texture; }} //Namespace
I can't pick out the fault. I've tried other texture filters to take off the slight 'blur' too, but to no avail.