iDevGames Forums
SDL/OpenGL fullscreen text rendering - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: SDL/OpenGL fullscreen text rendering (/thread-1565.html)

SDL/OpenGL fullscreen text rendering - StealthyCoin - Mar 23, 2009 02:22 PM

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?

SDL/OpenGL fullscreen text rendering - Volte - Mar 26, 2009 09:34 AM

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

SDL/OpenGL fullscreen text rendering - StealthyCoin - Mar 26, 2009 09:47 AM

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?