Skip to content

Instantly share code, notes, and snippets.

@alecbz
Created April 2, 2013 22:50
Show Gist options
  • Save alecbz/5296913 to your computer and use it in GitHub Desktop.
Save alecbz/5296913 to your computer and use it in GitHub Desktop.
void renderText(TTF_Font* font, string text) {
SDL_Color color = {255, 0, 0};
SDL_Surface* rendered = TTF_RenderText_Solid(font, text.c_str(), color);
// slightly modified from http://stackoverflow.com/questions/5289447/using-sdl-ttf-with-opengl
GLint colors = rendered->format->BytesPerPixel;
GLenum format;
if (colors == 4) { // alpha
if (rendered->format->Rmask == 0x000000ff) {
format = GL_RGBA;
} else {
format = GL_BGRA;
}
} else { // no alpha
if (rendered->format->Rmask == 0x000000ff) {
format = GL_RGB;
} else {
format = GL_BGR;
}
}
GLuint texture;
glGenTextures(1, &texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, colors, rendered->w, rendered->h, 0, format, GL_UNSIGNED_BYTE, rendered->pixels);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texture); // TODO: redundant?
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex3f(0, 0, 0);
glTexCoord2i(1, 0);
glVertex3f(rendered->w, 0, 0);
glTexCoord2i(1, 1);
glVertex3f(rendered->w, rendered->h, 0);
glTexCoord2i(0, 1);
glVertex3f(0, rendered->h, 0);
glEnd();
glLoadIdentity();
SDL_GL_SwapBuffers();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment