Hi,
I am using a bunch of code I found in a different thread on these forums (that I seem to have lost track of) attempting to render text with openGL and SDL. I've been staring at it for who knows how long and I can't figure out what I'm missing. Nothing at all is drawn. Thanks very much for any pointers.
The code to load a font.
void openFont()
{
string fontPath = "Fonts/" + m_sFontName + ".ttf";
m_font = TTF_OpenFont(fontPath.c_str(), m_nPtSize);
if(!m_font)
{
cerr<<"Could not open font '"<<m_sFontName<<"': "<<TTF_GetError()<<endl;
// Exit? Log?
}
}//END openFont
The code to load the texture.
GLuint loadTexture(SDL_Surface* surface)
{
GLuint texture = NULL;
if(surface)
{
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0,
GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
// Again using linear filtered textures for now
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SDL_FreeSurface(surface);
}
else // Handle null surface* as param
cerr<<"The surface pointer passed into loadTexture is null.\n";
return texture;
}//END loadTexture (SDL_Surface*)
The code to render the text.
void render()
{
if(m_txtImg)
SDL_FreeSurface(m_txtImg);
SDL_Surface* initialTxtImg = TTF_RenderText_Blended(m_font, m_sText.c_str(), m_fg);
if(!initialTxtImg)
{
cerr<<"Could not render: "<<m_sText<<" because: "<<TTF_GetError()<<endl;
}
m_nWidth = nextPowerOfTwo(initialTxtImg->w);
m_nHeight = nextPowerOfTwo(initialTxtImg->h);
m_txtImg = SDL_CreateRGBSurface(0, m_nWidth, m_nHeight, 32,
0x00ff0000, 0x0000ff00,
0x000000ff, 0xff000000);
if(!m_txtImg)
{
cerr<<"Could not render intermediate text image: "<<TTF_GetError()<<endl;
}
SDL_BlitSurface(initialTxtImg, 0, m_txtImg, 0);
m_nTexture = loadTexture(m_txtImg);
}//END render
The code to draw the rendered image.
void draw()
{
glBindTexture(GL_TEXTURE_2D, m_nTexture);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);// Normal facing us
// Loop through verts.
for(int i = 0; i < 4; i++)
{
glTexCoord2f(m_texCoords.x, m_texCoords.y);
glVertex3f(m_verts.x, m_verts.y, m_verts.z);
}
glEnd();
}//END draw
The loop actually calling the draw function.
void execGameLoop()
{
SDL_Event e;
Sprite glass(75, 75, 200, 300, "Graphics/Glass.bmp");
SDL_Color fg = {150, 150, 150};
DrawableString testString("Hello", "AbductionIII", 12, fg, 500, 500);
while(!m_done)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glass.draw();
testString.draw();
SDL_PollEvent(&e);
handleEvents(e);
SDL_GL_SwapBuffers();
}
}//END drawScene