I have been reading over the past posts and am wondering my my code isn't working as well, I'll just post it and hope for something to jump out at somebody
This is my texttotexture function:
GLuint RenderTextureFromText(char *text, int r, int g, int b, int a){
GLuint texture;
int fontWidth, fontHeight, fontBPP;
SDL_Color clr = {r, g, b, a};
SDL_Surface *Text = TTF_RenderText_Blended(font, text, clr);
//SDL_Surface *Text = TTF_RenderText_Solid(font, text, clr);
if (Text == 0){
cout << "TTF_RenderText_Sold() failed: " << TTF_GetError() << endl;
}
SDL_SaveBMP(Text, "TTFOutTest.bmp");
//Slock(Text);
fontWidth = Text->w;
fontHeight = Text->h;
fontBPP = Text->format->BitsPerPixel;
//Sulock(Text);
//Next power of two.
int width = 1;
while(width < fontWidth){
width*=2;
}
int height = 1;
while(height < fontHeight){
height*=2;
}
// Make sure we have the correct endianess.
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
Uint32 rmask = 0xff000000;
Uint32 gmask = 0x00ff0000;
Uint32 bmask = 0x0000ff00;
Uint32 amask = 0x000000ff;
#else
Uint32 rmask = 0x000000ff;
Uint32 gmask = 0x0000ff00;
Uint32 bmask = 0x00ff0000;
Uint32 amask = 0xff000000;
#endif
SDL_Surface *TextSurface = SDL_CreateRGBSurface(SDL_HWSURFACE,
width, height,
32,
rmask, gmask, bmask, amask);
SDL_SetAlpha(Text, 0, 0);
if (SDL_BlitSurface(Text, 0, TextSurface, 0)!=0){
cerr << "Font Blit Failed!" << SDL_GetError() << endl;
}
SDL_SaveBMP(Text, "TTFOutTest2.bmp");
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TextSurface->w, TextSurface->h,
0, GL_RGBA, GL_UNSIGNED_BYTE, TextSurface->pixels);
SDL_FreeSurface(TextSurface);
SDL_FreeSurface(Text); //Might need to NULL these pointers to surfaces!
return texture;
}
This is my code where I render it:
sprintf(strFrameRate, "fps: %5d", lastFramesPerSecond);
cout << strFrameRate << endl;
fontTexture = RenderTextureFromText(strFrameRate, 255, 0, 0, 128);
glBindTexture(GL_TEXTURE_2D, fontTexture);
cout << RenderTextureFromText(strFrameRate, 255, 0, 0, 0) << endl;
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(0.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 0.0f);
glEnd();
glPopMatrix();