Results 1 to 4 of 4

Thread: TTF_OpenFont

  1. #1
    Join Date
    Jun 2011
    Beans
    63

    TTF_OpenFont

    I have a problem with SDL. Here is the code
    Code:
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    #include "SDL/SDL_ttf.h"
    #include <string>
    using namespace std;
    
    SDL_Surface* screen = NULL;
    SDL_Surface* img = NULL;
    TTF_Font* font = NULL;
    SDL_Surface* messageup = NULL;
    SDL_Surface* messagedown = NULL;
    SDL_Surface* messageleft = NULL;
    SDL_Surface* messageright = NULL;
    SDL_Surface* message = NULL;
    SDL_Color textColor = {0,0,0};
    
    SDL_Event event;
    
    SDL_Surface* load_image(string filename)
    {
        SDL_Surface* loadedImage = IMG_Load(filename.c_str());
        SDL_Surface* optimizedImage = SDL_DisplayFormat(loadedImage);
        SDL_FreeSurface(loadedImage);
        return optimizedImage;
    }
    
    void apply_surface(int x ,int y,SDL_Surface* source,SDL_Surface* dest)
    {
        SDL_Rect offset;
        offset.x = x;
        offset.y = y;
        SDL_BlitSurface(source,NULL,dest,&offset);
    }
    
    void init(string caption = "",int height = 640,int width = 480,int colorbits = 32)
    {
        SDL_Init(SDL_INIT_EVERYTHING);
        screen = SDL_SetVideoMode(height,width,colorbits,SDL_SWSURFACE);
        TTF_Init();
        SDL_WM_SetCaption(caption.c_str(),NULL);
    }
    
    int main( int argc, char* args[] )
    {
        init("butthole");
        img = load_image("background.png");
        font = TTF_OpenFont("lazy.tff",28);
        if(!font) {
        printf("TTF_OpenFont: %s\n", TTF_GetError());
            return 1;
        }
    
        messageup = TTF_RenderText_Solid(font,"Up pressed",textColor);
        messagedown = TTF_RenderText_Solid(font,"Down pressed",textColor);
        messageleft = TTF_RenderText_Solid(font,"Left pressed",textColor);
        messageright = TTF_RenderText_Solid(font,"Right pressed",textColor);
    
    
        apply_surface(0,0,img,screen);
        SDL_Flip(screen);
        bool quit = true;
        while(quit)
        {
            while(SDL_PollEvent(&event))
            {
                if(event.type == SDL_QUIT)
                {
                    quit = false;
                }
                switch(event.key.keysym.sym)
                {
                    case SDLK_UP: message = messageup; break;
                    case SDLK_DOWN: message = messagedown; break;
                    case SDLK_LEFT: message = messageleft; break;
                    case SDLK_RIGHT: message = messageright; break;
                }
                apply_surface(320,240,message,screen);
                SDL_Flip(screen);
                SDL_Delay(20);
            }
        }
        SDL_FreeSurface(img);
        SDL_FreeSurface(message);
        SDL_FreeSurface(messagedown);
        SDL_FreeSurface(messageup);
        SDL_FreeSurface(messageleft);
        SDL_FreeSurface(messageright);
        SDL_Quit();
    }
    It compiles fine(using the command: g++ -o a.out main.cpp -lSDL -lSDL_image -lSDL_ttf)
    but gives the error: TTF_OpenFont: Couldn't open lazy.tff

    Any idea what is wrong?

    lazy.tff is in the same directory

  2. #2
    Join Date
    Feb 2008
    Beans
    1,145
    Distro
    Xubuntu

    Re: TTF_OpenFont

    TTF_OpenFont: Couldn't open lazy.tff

    Any idea what is wrong?

    lazy.tff is in the same directory
    I think I see your problem. You sure that's how that extension is spelled?
    Help yourself: Ubuntu Docs - Ubuntu Packages

  3. #3
    Join Date
    Jun 2011
    Beans
    63

    Re: TTF_OpenFont

    Quote Originally Posted by gsmanners View Post
    I think I see your problem. You sure that's how that extension is spelled?
    Oh. My. God. WHY MUST I BE SUCH AN IDIOT. Thanks a lot man.

  4. #4
    Join Date
    Mar 2011
    Location
    A Snowy Wintery Land
    Beans
    18

    Re: TTF_OpenFont

    Quote Originally Posted by 7cardcha View Post
    Oh. My. God. WHY MUST I BE SUCH AN IDIOT. Thanks a lot man.
    No way. You are not an idiot. That is the exact sort of mistake anyone makes.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •