Annoying problem with SDL_TTF

Started by
7 comments, last by Dipp 19 years, 11 months ago
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]
Advertisement
And the error oyu get is.....
Try using,

TTF_RenderText_Shaded

Instead.
Parallel RealitiesProject: Starfighter
quote:Original post by Scarfy
Try using,

TTF_RenderText_Shaded

Instead.


I tried that now and it worked sort of. The program doesn''t die anymore but I can''t see any characters being written to the screen surface

Well here is the current code with changes:
#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;  SDL_Rect dst;  TTF_Font *font;  SDL_Color fg={255, 255, 255, 255};  SDL_Color bg={0, 0, 0, 255};    if(SDL_Init(SDL_INIT_VIDEO) != 0){    cout << "Error, SDL_Init();" << endl;    return -1;  }  screen = SDL_SetVideoMode(640, 480, 16, 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_Shaded(font,"Hello World!", fg, bg);    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;} 

You still haven''t told us the error that you''re getting...

[ CodeDread ]
quote:Original post by rypyr
You still haven''t told us the error that you''re getting...

<HR><SMALL> [ <a href="http://www.codedread.com/">CodeDread</a> ]</SMALL>



I have told you what "error" I get. I can''t see anything beeing written on the screen, the program runs fine, no failures there but I can''t see any text on the screen, I get a black screen that is all.
Do you have to call SDL_UpdateRect after a SDL_BlitSurface?

[ CodeDread ]
quote:Original post by Dipp
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;}   



And you never told me what is the error you get here...you said later that when you try Shaded it doesn''t display anything.

Regards,
Jeff

[ CodeDread ]
quote:Original post by rypyr
Do you have to call SDL_UpdateRect after a SDL_BlitSurface?

[ CodeDread ]


It worked! I am used to SDL_Flip(); that''s why I forgot it :/

Oh and sorry for that rypyr :D

Thanks once again for the help!

This topic is closed to new replies.

Advertisement