Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with SDL_ttf

Started by geekyware Sep 7, 2011 at 4:56 PM 3 replies 3.3k views
Original Post
geekyware
geekyware
I'm programming a game using SDL and SDL_ttf, I'm doing it in C++ using NetBeans in a Linux system (Ubuntu specifically). I've read over here that SDL have some problems when you use it in C++, but I haven't had any (until now). Well, the thing is that my game compiles and executes just perfectly on the IDE, the problem comes when I compile an executable and try to run it directly (the one it makes in dist/Release/...).

When I do it, it gives me a segment violation. I've been isolating the fail, and it's in this code of block:

void dibujarmenu(TTF_Font *font1, SDL_Surface **screen, controlador *contr) {
TTF_Font *font = TTF_OpenFont("FreeSans.ttf", 10);
dibujarseleccionado(screen, contr);
//Left menu first because of being smaller
//QUIT
color = (SDL_Color) {150,150,150,255};
if ((text = TTF_RenderText_Solid(font, "QUIT", color))==NULL)
cout << TTF_GetError() << endl;
rect = (SDL_Rect) {712,584,88,13};
SDL_BlitSurface(text, NULL, *screen, ▭);



The exact problem is in the function TTF_RenderTextSolid();

As you can see, I load the font directly in the function, I used to give it to the function in the arguments (the first one), but I thought that could be causing the problem so I changed it, but nothing changed in the result.

color is defined in the same file but outside the function, like this:

SDL_Color color;


Can you see any problems that may be causing this?

Thanks in advance.
Wooh
Wooh
I have never heard that SDL have problems with C++. Your problem here is that relative paths are relative the working directory. Probably your IDE use another working directory than you do when run it "directly". You should have better error checking because now you assume TTF_OpenFont will always succeed.

If you want the paths to be relative the executable file you can check the first argument to main (argv[0]). That often contains a relative path to the executable file. If you remove the executable file name from that path you can append that to all your paths when loading to get the correct path.
geekyware
geekyware
Oh sh*t, thanks, problem solved, just copied the ttf file into the executable directory.

Thanks again, don't know why I didn't realise.

About the problems with C+, I didn't knew it either, I registered at this community two days ago andhave been looking around, and I've read a los of times that SDL is very buggy, specially with C++, that Allegro is better, etc. But I haven't had any problems with it so far... thank you again.

By the way, do you know anyway in NetBeans (or where is it generally in IDEs) so I can make it to copy the resource files to the folder where the executable is so I don't have to copy them manually?
Wooh
Wooh
The SFML fans like to tell people SFML is much better than SDL. Maybe it's the same with Allegro. Don't listen to them too much. SDL is a C library, maybe they have a hard time using C style libraries or they just don't like them. If you use SDL correctly it should not be much problems. SDL is very stable in my opinion. The problem you had here has nothing to do with SDL.

If you have your resources in a directory maybe you can use a symlink instead but I have never tried that so don't know if it's any good.
geekyware
geekyware

The SFML fans like to tell people SFML is much better than SDL. Maybe it's the same with Allegro. Don't listen to them too much. SDL is a C library, maybe they have a hard time using C style libraries or they just don't like them. If you use SDL correctly it should not be much problems. SDL is very stable in my opinion. The problem you had here has nothing to do with SDL.

If you have your resources in a directory maybe you can use a symlink instead but I have never tried that so don't know if it's any good.


Ok, thank you. Will then stick to SDL for now, as it's the one I've learned.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.