SDL/OpenGL fullscreen text rendering

Posts: 67
Joined: 2006.06
Post: #1
So I have this blob of code here:
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <SDL_ttf/SDL_ttf.h>

#define wWidth 1024
#define wHeight 800
#define wDepth 32

void kill() {

void shouldExit(SDL_Event event) {
    if (event.type == SDL_QUIT) {
    else if (event.type == SDL_KEYDOWN) {
        if (event.key.keysym.sym == SDLK_ESCAPE) {

void renderText(TTF_Font *font, GLubyte R, GLubyte G, GLubyte B, double X,
                double Y, double Z, std::string text) {
    SDL_Color color = {R, G, B};
    SDL_Surface *message = TTF_RenderText_Blended(font, text.c_str(), color);
    unsigned Texture = 0;
    glGenTextures(1, &Texture);
    glBindTexture(GL_TEXTURE_2D, Texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, message->w, message->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, message->pixels);
    glTexCoord2d(0, 0); glVertex3d(X, Y, Z);
    glTexCoord2d(1, 0); glVertex3d(X+message->w, Y, Z);
    glTexCoord2d(1, 1); glVertex3d(X+message->w, Y+message->h, Z);
    glTexCoord2d(0, 1); glVertex3d(X, Y+message->h, Z);
    glDeleteTextures(1, &Texture);

int initGL(int width, int height, int depth, bool fullscreen) {
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    int flags;
    if (fullscreen) {
        flags = SDL_OPENGL | SDL_FULLSCREEN;
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE,   8);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,  8);
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,  8);
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    else flags = SDL_OPENGL;
    SDL_WM_SetCaption("Window", NULL);
    SDL_Surface* screen = SDL_SetVideoMode(width, height, depth, flags);
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glViewport(0, 0, width, height);
    glOrtho(0.0f, (float)width, (float)height, 0.0f, -1.0f, 1.0f);
    return 0;

int main(int argc, char* args[]) {
    initGL(wWidth, wHeight, wDepth, true);
    TTF_Font *font = TTF_OpenFont("FreeSerifItalic.ttf", 24);
    renderText(font, 0, 255, 0, 100, 100, 0, "draw a thingamabbo");
    bool running = true;
    SDL_Event event;
    while (running) {
    return 0;

When I run it windowed (send false to initGL) it renders happy green text. WHen I send true, and it goes to fullscreen its just a white screen and a mouse.

I tried drawing a yellow square, works fine in both windowed and fullscreen. Have not tried loading a texture via some other method yet, have to go to work.

So, why does my renderText method fail in fullscreen mode?

The machine does not run without the coin.
Quote this message in a reply
Posts: 89
Joined: 2005.06
Post: #2
My first instinct is:

Try rendering your font inside your while loop, and don't forget SDL_GL_SwapBuffers(); to get your stuff on the screen.

It doesn't look like you have a legit render function set up. And the way you're handling your events is kinda... scattered. Take a look at my sample code (not that its the best way)

I know, you didn't ask for your code to be critiqued, but I took the liberty anyway Rasp

So yea, try placing your render stuff inside your loop (or better yet make a new render function that you can call) and don't forget to swap buffers. I'm guessing SDL does some fancy footwork when its setting fullscreen which ends up killing whatever you're trying to draw because you're not drawing it continuously and or swapping the buffers.

Other than that, I have no clue, and I'm not about to try to get that small piece of code to run Rasp

Good luck

edit: typo

Well since everyone else is doing it:
Twilight Coders
Quote this message in a reply
Posts: 67
Joined: 2006.06
Post: #3
The code was a quick dumb test to see if it worked, apparently not. I'll setup a render loop and check out your code in the intro c++ class I have to endure in 30 minutes.

Thanks for the stuff to think about! I'll edit when I try some stuff out.

I got it to work by using a function that converts a general purpose surface into a texture. No mater how I reorganized my code though I couldn't get the old one to work in fullscreen. Id like to find out why but I don't really know how so... Im going to go ahead and switch that without understanding why. Thanks for the suggestions and code.

By the way, why waitEvent instead of pollEvent?

The machine does not run without the coin.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Fast rendering with OpenGL 1.1 dotbianry 4 3,676 Dec 18, 2012 03:58 AM
Last Post: dotbianry
  Opengl/Cocoa text rendering tesil 15 17,446 Mar 20, 2012 11:16 AM
Last Post: OneSadCookie
  OpenGL Text Rendering (in Cocoa) daveh84 5 7,903 Feb 19, 2009 12:44 PM
Last Post: TomorrowPlusX
  opengl text/font drawing from CGContext fretmunky 11 13,071 Dec 7, 2008 11:29 AM
Last Post: fretmunky
  OpenGL text and other things. Talyn 7 4,777 Jul 22, 2008 02:09 AM
Last Post: OneSadCookie