I got it! I put a breakpoint on line 1 of my init function, and found the bug.
In the original code, I was calling TTF_Init() at the end of my Init function. Well, I found a performance issue. My DisplayText() function was loading the font. Since DisplayText() is called 60 times/second, it lagged.
To fix this, I made the font pointer global, move the initialization of it to my Init() function, and had DisplayText just use the font. I forgot to move the TTF_Init() function up in my code! That's all it was!
Old code:
void Init(){ // Initialize SDL components SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); // Setup the video mode sfScreen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_ANYFORMAT | SDL_DOUBLEBUF | SDL_HWPALETTE |SDL_HWSURFACE); // Set the title of the window SDL_WM_SetCaption(WINDOW_CAPTION, 0); // Get the number of ticks since SDL was initialized gTimer = SDL_GetTicks(); // Fill the BMP surface sfBG = SDL_LoadBMP("data/img/bg/bg1.bmp"); // Fmod stuff FSOUND_Init(32000, 64, 0); Player.LoadSong("data/music/cor.mp3", "Champion of RON", "vuxnut"); // Init font ftForgot = TTF_OpenFont("data/fonts/forgot.ttf", 14); // At this point the compiler is thinking WTF? Font? // Initialize the stack with the exit state StateStruct state; state.StatePointer = gsExit; gStateStack.push(state); // Add a pointer to the menu state state.StatePointer = gsMenu; gStateStack.push(state); // Initialize the TTF library TTF_Init(); // And at this point the compiler is thinking OMGWTFLOL! }
Fixed code:
void Init(){ // Initialize SDL components SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); // Setup the video mode sfScreen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_ANYFORMAT | SDL_DOUBLEBUF | SDL_HWPALETTE |SDL_HWSURFACE); // Set the title of the window SDL_WM_SetCaption(WINDOW_CAPTION, 0); // Get the number of ticks since SDL was initialized gTimer = SDL_GetTicks(); // Fill the BMP surface sfBG = SDL_LoadBMP("data/img/bg/bg1.bmp"); // Fmod stuff FSOUND_Init(32000, 64, 0); Player.LoadSong("data/music/cor.mp3", "Champion of RON", "vuxnut"); // Initialize the TTF library TTF_Init(); // ok // Init font ftForgot = TTF_OpenFont("data/fonts/forgot.ttf", 14); // ah! teh font! // Initialize the stack with the exit state StateStruct state; state.StatePointer = gsExit; gStateStack.push(state); // Add a pointer to the menu state state.StatePointer = gsMenu; gStateStack.push(state); }
Your skill in debugging has increased to 2.