Hi!
Have a really annoying problem with SDL_TTF. I have tried a lot of different things and this is starting to drive me mad!
Anyway the problem is as follows, I want to print a simple string of characters to the screen via SDL_TTF. The problem arises when I try to blit something to the text_surface. In other words it fails right here:
text_surface = TTF_RenderText_Solid(font,"Hello World!", fg);
if(!text_surface){
cout << "Error rendering font to surface: " << TTF_GetError() << endl;
return -1;
}
Well here is the whole code:
#include <string.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
SDL_Surface *screen = 0;
SDL_Surface *text_surface = 0;
SDL_Rect dst;
TTF_Font *font;
SDL_Color fg={0, 0, 255, 255};
if(SDL_Init(SDL_INIT_VIDEO) != 0){
cout << "Error, SDL_Init();" << endl;
return -1;
}
screen = SDL_SetVideoMode(640, 480, 0, 0);
if(screen == NULL){
cout << "Error, SDL_SetVideMode();" << endl;
return -1;
}
if(TTF_Init() == -1){
cout << "Error, TTF_Init" << endl;
}
font = TTF_OpenFont("Vera.ttf", 14);
if(!font){
cout << "Error, TTF_Font" << endl;
}
printf("TTF_FontHeight : %d\n",TTF_FontHeight(font));
printf("TTF_FontAscent : %d\n",TTF_FontAscent(font));
printf("TTF_FontDescent : %d\n",TTF_FontDescent(font));
printf("TTF_FontLineSkip: %d\n",TTF_FontLineSkip(font));
TTF_SetFontStyle(font, TTF_STYLE_NORMAL);
text_surface = TTF_RenderText_Solid(font,"Hello World!", fg);
if(!text_surface){
cout << "Error rendering font to surface: " << TTF_GetError() << endl;
return -1;
}
dst.x = 0;
dst.y = 0;
dst.w = text_surface->w;
dst.h = text_surface->h;
SDL_BlitSurface(text_surface, NULL, screen, &dst);
SDL_Delay(1000);
return 0;
}
I am running it on a Linux-box with freetype 2 installed and I compiled it with the following line:
g++ font.cc -o font `sdl-config --libs` -lSDL_ttf
[edited by - dipp on May 17, 2004 6:33:55 PM]