Hi out there,
I’m trying to render a text with SDL_ttf but I don’t get a text displayed.
Here is my Code:
bool Win::init()
{
if ( SDL_Init( SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, “SDL konnte nicht initialisiert werden: %s\n”,
SDL_GetError());
return false;
}
if(TTF_Init()==-1)
{
printf("TTF_Init: %s\n", TTF_GetError());
return false;
}
atexit(SDL_Quit);
// init screen
display = SDL_SetVideoMode( 1280, 400, 16, SDL_HWSURFACE );
if ( display == NULL )
{
fprintf(stderr, "Konnte kein Fenster 1280x400x16 oeffnen: %s\n",
SDL_GetError());
return false;
}
font=TTF_OpenFont("/usr/share/fonts/truetype/arial.ttf", 48);
if(!font)
{
printf("TTF_OpenFont: %s\n", TTF_GetError());
// handle error
}
image = IMG_Load("circle.jpg");
if(!image)
printf("could not load image %s", SDL_GetError());
SDL_WM_SetCaption("Simple step sequencer",NULL);
return true;
}
int Win::mainLoop()
{
while(true)
{
SDL_Color color={255,255,0,255};
SDL_Surface *text_surface;
SDL_Rect rect;
rect.w=400;
rect.h=400;
rect.x=0;
rect.y=0;
if(!(text_surface=TTF_RenderText_Blended(font,“Hello World!”,color))) {
//handle error here, perhaps print TTF_GetError at least
printf(“render failed”);
} else {
SDL_BlitSurface(text_surface,NULL,display,NULL);
//perhaps we can reuse it, but I assume not for simplicity.
SDL_FreeSurface(text_surface);
}
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT: return 0;
case SDL_ACTIVEEVENT:
{
if( event.active.gain == 1 )
bFocus=true;
else
bFocus=false;
std::cout << "Focus is " << bFocus << std::endl;
break;
}
case SDL_KEYDOWN: if(handleKeys(&event.key.keysym) == -1) return 0;
}
}
}
}
Does anyone see the error?–
Dinosaurs aren’t extinct. They’ve just learned to hide in the trees.