SDL TFF problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Jokeboxproductions
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Nov 03, 2008 6:46 am

SDL TFF problem

Post by Jokeboxproductions »

I recently got back in the mood to program so I'm running through Lazyfoo's Tuts and I got stuck at Lesson 7&8 Using TTF files.
When I run my program with out TTF it builds and show the screen until I click X to exit out, but lately when I used TTF functions I get the program build and run and the screen just comes on go right off even do it's post to be event driven.

Here is my code. Little messy so :P I'm just thinking about skipping lesson 7&8 but its going to come back and bite me I know it.

Code: Select all

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

    SDL_Surface *white = NULL;
    SDL_Surface *screen = NULL;
    SDL_Surface *stick = NULL;
    SDL_Surface *sheet = NULL;
    SDL_Surface *message = NULL;
    SDL_Surface *upMessage = NULL;
    SDL_Surface *downMessage = NULL;
    SDL_Surface *leftMessage = NULL;
    SDL_Surface *rightMessage = NULL;
    SDL_Event event;
    TTF_Font *font = NULL;
    SDL_Color textColor = {255, 255, 255};





    SDL_Surface *load_image(std::string filename)

{

    SDL_Surface* loadedImage = NULL;

    SDL_Surface* optimizedImage = NULL;

    loadedImage = SDL_LoadBMP(filename.c_str());

    if( loadedImage != NULL)
    {
        optimizedImage = SDL_DisplayFormat(loadedImage);

        SDL_FreeSurface(loadedImage);
        if(optimizedImage != NULL);
    }
    {

Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
 SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
    }

    return optimizedImage;

}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    SDL_BlitSurface(source, NULL, destination, &offset);

}
bool init()
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return false;
    }

    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

    if(screen == NULL)
    {
        return false;
    }

    if(TTF_Init() == -1)
    {
        return false;
    }

    SDL_WM_SetCaption("Project",NULL);

return true;
}
bool load_files()
{
    sheet = load_image("result.bmp");
    stick = load_image("stickblit.bmp");
    white = load_image("white.bmp");
    font = TTF_OpenFont( "lasy.ttf",28);
    upMessage = TTF_RenderText_Solid(font,"Up", textColor);




    if(white == NULL)
    {
        return false;
    }
    if (font == NULL)
    {
    return false;
    }


    return true;
}
void clean_up()
{

    SDL_FreeSurface(white);
    SDL_FreeSurface(sheet);
    TTF_CloseFont(font);

    TTF_Quit();


    SDL_Quit();
    }

int main(int argc, char* args[])
{
    bool quit = false;
    if( init() == false )
    {
        return 1;
    }

    if( load_files() == false)

    upMessage = TTF_RenderText_Solid(font, "UP",textColor);
    downMessage = TTF_RenderText_Solid(font, "Down", textColor);
    leftMessage = TTF_RenderText_Solid(font ,"Left", textColor);
    rightMessage = TTF_RenderText_Solid(font, "right", textColor);

    if( upMessage == NULL)
    if( downMessage == NULL)
    if( leftMessage == NULL)
    if( rightMessage == NULL)

    {
        return 1;
    }

    apply_surface(0,0, white, screen);
    apply_surface(150,0, sheet, screen);



    if(SDL_Flip( screen ) == -1)
    {
        return 1;
    }

while( quit == false)
    {
        while(SDL_PollEvent( &event))
        {
            if(event.type == SDL_QUIT)
            {
                quit = true;
            }
        }
    }
    clean_up();
    return 1;



}



-Premium
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: SDL TFF problem

Post by Bakkon »

Usually when SDL crashes like that, it's because it's failing to load something and using a null reference. Make sure all the bitmaps and the font are in the same directory as your project.
User avatar
Jokeboxproductions
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Nov 03, 2008 6:46 am

Re: SDL TFF problem

Post by Jokeboxproductions »

Thank you kindly :D
Protimus
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Mon Jun 01, 2009 9:27 pm
Current Project: Game Engine
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Jackson, AL
Contact:

Re: SDL TFF problem

Post by Protimus »

In your load_files() method, you're loading the font "lasy.ttf". You probably want "lazy.ttf". Assuming you pasted your code here directly from your file, we can start working on the other issues after you fix that. :)
Judge not a man by his language but by his use of the language.
Post Reply