SDL - I want to use Windows fonts!

Started by
3 comments, last by Splinter of Chaos 15 years, 10 months ago
How to use windows fonts? The Windows font are still in the Directory C:/Windows/Fonts/.....? But it does not work! There's always the error, process treminated with status 1, what means, that he couldn't load the font. What shall i do now? Isn't it possible to use the windows fonts with the SDL. Of course, I've got the "SDL/SDL_ttf.h" installed.

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
using namespace std;

SDL_Surface * LoadImage(const string &filename);
void ApplySurface(short x, short y, SDL_Surface *source, SDL_Surface *destination);

int main(int argc, char *args[])
{
    //SDL structs
    SDL_Surface *screen     = NULL;
    SDL_Surface *background = NULL;
    SDL_Surface *message    = NULL;
    TTF_Font *font = NULL;
    SDL_Event event;
    
    //used in loop
    bool quit = true;
    
    SDL_Color textcolor = {0,0,0};

    //Initializing SDL + ttf 
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return 1;
    }
    if(TTF_Init() == -1)
    {
        return 1;
    }
    screen = SDL_SetVideoMode(1000,1200,32,SDL_SWSURFACE);

    //Load files
    background = LoadImage("w1.jpg");
    if(background == NULL)
    {
        return 1;
    }
    font = TTF_OpenFont("C:/Windows/Fonts/OpenSymbol.ttf",24);
    if(font == NULL)
    {
        return 1; //PROCESS TERMINATED HERE!!!
    }
    message = TTF_RenderText_Solid(font , "HELLO WORLD", textcolor);

    //paint on the screen
    ApplySurface(0,0,background,screen);
    ApplySurface(200,200,message,screen);


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

    //Event looü
    while(quit)
    {
        while(SDL_PollEvent(&event));
        {
            if(event.type == SDL_QUIT)
            {
                quit = false;
            }
        }
    }

    SDL_Quit();

    return 0;
}

SDL_Surface * LoadImage(const string &filename)
{
    SDL_Surface *loadedImage    = NULL;
    SDL_Surface *optimizedImage = NULL;
    loadedImage = IMG_Load(filename.c_str());
    if(loadedImage != NULL)
    {
        optimizedImage = SDL_DisplayFormat(loadedImage);
        SDL_FreeSurface(loadedImage);
    }
    return optimizedImage;
}

void ApplySurface(short x, short y, SDL_Surface *source, SDL_Surface *destination)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(source, NULL, destination, &offset);
}

Stromberg
Advertisement
It would probably help to check what error occurred, such as using TTF_GetError().
SiCrane's right on how to find the problem.

I believe that most of the time, when you're asked for a file name, it means one relative to where you are. In fact, I know of no occasion where this has not been the case. I'd suggest copying the font you want into your project's folder...except that's illegal thanks to copyright laws. Or, at least to distribute, it would be.

Google free fonts.
I agree with SiCrane check TTF_GetError() in this if
if(font == NULL)
{
in your code.

I disagree with Splinter of Chaos about the path having to be relative.
The path can be relative or absolute.
In my graphics engine (which uses SDL_TTF) I us this as the defalut path for windows fonts
#define True_Type_Font_Location "C:\\WINDOWS\\Fonts\\" //windows


and here is how I load the font for example
void textBox::LoadFont()	{         std::string FontToLoad=m_sTrue_Type_Font_Location+ m_sFont;         if(m_pLoadedFont)         {         TTF_CloseFont(m_pLoadedFont);         m_pLoadedFont=TTF_OpenFont(FontToLoad.c_str(),m_iFontPoint);         }         else         {            m_pLoadedFont=TTF_OpenFont(FontToLoad.c_str(),m_iFontPoint);         }        if(!m_pLoadedFont)        {             std::cerr<<"could not load font "<<FontToLoad.c_str()<<" for Sprocket "<<GetName()<<std::endl;        }        else        {        std::cout<<"loaded  font "<<FontToLoad.c_str()<<" for Sprocket "<<GetName()<<std::endl;        }         SetBlit(true);     }
Black CloakEpee Engine.
Quote:Original post by blackcloak
I agree with SiCrane check TTF_GetError() in this if
if(font == NULL)
{
in your code.

I disagree with Splinter of Chaos about the path having to be relative.
The path can be relative or absolute.
In my graphics engine (which uses SDL_TTF) I us this as the defalut path for windows fonts
#define True_Type_Font_Location "C:\\WINDOWS\\Fonts\\" //windows



Thanks for clearing that up.

This topic is closed to new replies.

Advertisement