Code: Select all
Texture temp;
/*
Draw simple white text to the screen
*/
void RenderText(TTF_Font* font, Vector2& position, const char *fmt, ...){
//Holds the string
char text[256];
int h,w;
//Color for the text
SDL_Color color;
color.r = 255;
color.g = 255;
color.b = 255;
// Pointer to list of arguments
va_list ap;
// If There's no text, do nothing
if (fmt == NULL)
return;
// Parses the string for variables
va_start(ap, fmt);
vsprintf(text, fmt, ap);
va_end(ap);
//Create the font texture
SDL_Surface *initial = TTF_RenderText_Blended(font, text, color);
h = initial->h;
w = initial->w;
Texture texture = createGLTexture(initial);
//Temporary vars
Rect dimensions = Rect(position.getX(), position.getY(), texture.getHeight(), texture.getWidth());
Rect source = Rect(0, 0, texture.getHeight(), texture.getWidth());
//Draw the text
drawQuad(texture, dimensions, source);
}
/*
Draw simple textured quad
*/
void drawQuad(Texture &texture, Rect &dimensions, Rect &source){
//Bind the texture (will be the texture used for this quad)
glBindTexture(GL_TEXTURE_2D, texture.getTexture());
//Calculate texture coords
float min_x = (float)(source.getX())/texture.getWidth();
float min_y = (float)(source.getY())/texture.getHeight();
float max_x = (float)(source.getX() + source.getWidth())/texture.getWidth();
float max_y = (float)(source.getY() + source.getHeight())/texture.getHeight();
//Draw the quad to the screen
glBegin(GL_QUADS);
glTexCoord2f(min_x, max_y); glVertex2i(dimensions.getX(), dimensions.getY() + dimensions.getHeight());
glTexCoord2f(min_x, min_y); glVertex2i(dimensions.getX(), dimensions.getY());
glTexCoord2f(max_x, min_y); glVertex2i(dimensions.getX() + dimensions.getWidth(), dimensions.getY());
glTexCoord2f(max_x, max_y); glVertex2i(dimensions.getX() + dimensions.getWidth(), dimensions.getY() + dimensions.getHeight());
glEnd();
glLoadIdentity();
}
/*
Create a texture from sdl_surface
*/
Texture& createGLTexture(SDL_Surface* tex){
//Declare a GL texture
GLuint texture = 0;
if(tex){
//Bind the texture
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
//Get the height and width of the texture and bpp
int width = tex->w;
int height = tex->h;
int bpp = tex->format->BitsPerPixel;
//Set the mode
int mode = GL_RGB;
//Check bpp
if(bpp == 32)
mode = GL_RGBA;
//Generate the texture
glTexImage2D( GL_TEXTURE_2D, 0, tex->format->BytesPerPixel,
width, height, 0, mode, GL_UNSIGNED_BYTE, tex->pixels );
//Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Free the SDL_surface
SDL_FreeSurface(tex);
//Create the new texture
temp = Texture(texture, height, width);
}
//Return the texture
return temp;
}