Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dipp

Annoying problem with SDL_TTF

This topic is 5237 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!