ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to integrate SDL_ttf into my little game of mine, but am having difficutly having a test program work so it displays onto the screen.. here is the code that I have...
Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <SDL/SDL.h>
#include "SDL_ttf.h"
using namespace std;
int main()
{
/*Initialize SDL_Init() with video enabled and TTF_Init()*/
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
SDL_Surface *screen;
/*Create a surface of 640x480x8 using software rendering*/
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
/*Make sure to exit/dump all SDL code within the program at
exit*/
atexit(SDL_Quit);
/*Use a font of your choice, just do a "locate *.ttf" in
console and use any font at random.*/
TTF_Font *font;
font = TTF_OpenFont("couri.ttf", 16);
if (!font)
{
printf("Something went fuged : %s\n", TTF_GetError());
return 0;
}
int style;
/*This just makes the font non-bold/italisized/underlined*/
style = TTF_GetFontStyle(TTF_STYLE_NORMAL);
/*Color of the font*/
SDL_Color color={0,0,0};
Uint16 text[] = {'H','e','l','l','o',' ','W','o','r','l','d'};
bool exit = 0;
/*As long as exit is 0, keep looping, if not break.*/
while(exit == 0)
{
if(!(screen = TTF_RenderUNICODE_Solid(font,text,color)))
{ //Just an error message that does not pop up.
cout << "\n\tFug it... (check line 36-40)\n";
}
else
{
/*Here is where I believe my problem might be. Am I
calling to the wrong SDL_Surface object?*/
SDL_BlitSurface(screen, NULL, screen, NULL);
SDL_FreeSurface(screen);
}
}
/*Quit all SDL, though, should atexit(SDL_Quit); do this for
me?*/
SDL_Quit();
TTF_Quit();
return 0;
}
... I changed a part where the SDL_ttf documentation says to put text_surface as a pointer object to SDL_Surface (so "SDL_Surface *text_surface, but I still could not have the object show up on my screen. Can someone give me a hint of what I could do?
When I run this program all I get is the following:
BTW, compile with the following (assuming you have g++, other then that I have no idea.): [code] g++ TheFileName.cpp `sdl-config --cflags --libs` -Wall -lSDL -lSDL_ttf -o SomeExecutable
Last edited by RHLinuxGUY; 04-22-2006 at 03:22 AM.
I've never used SDL_ttf, but I now see your problem. Have a look at this page http://jcatki.no-ip.org/SDL_ttf/SDL_ttf.html#SEC21
the func takes a TTF_Font* as a param and your passing it a "0x00"(TTF_STYLE_NORMAL) which the func assumes is an address and this is the seg fault.
[edit]
in fact the above in not true, you are infact passing null
Quote:
NOTE: Passing a NULL font into this function will cause a segfault.
I'm back, it took sometime to get everything working without any immediatly apparent problems, (I mean compile errors) here is my problem now. I can compile my code fine and I can execute without a segfault, but I can't see my text no matter what color, or position I put them in. I'm at a loss, what do I do?
Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <SDL/SDL.h>
#include "SDL_ttf.h"
using namespace std;
int main(int argc, char *argv[])
{
/* check args */
/*if(argc!=2)
{
fprintf(stderr,"%s file.ttf\n",argv[0]);
return 1;
}*/
if((SDL_Init(SDL_INIT_VIDEO)==-1))
{
printf("\n\tCould not initialize SDL: %s", SDL_GetError());
exit(-1);
}
if((TTF_Init())==-1)
{
printf("\n\tCould initialize SDL_ttf: %s", TTF_GetError());
exit(-1);
}
atexit(SDL_Quit);
atexit(TTF_Quit); /* remember to quit SDL_ttf */
//atexit(free_font); /* remember to free any loaded font and glyph cache */
SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
TTF_Font *font;
font = TTF_OpenFont("couri.ttf", 24);
if (!font)
{
printf("Something went fucked : %s\n", TTF_GetError());
return 0;
}
int style;
style = TTF_GetFontStyle(font);
SDL_Color color={0,255,0};
SDL_Surface *text_surface;
Uint16 text[] = {'H','e','l','l','o',' ','W','o','r','l','d'};
/*Unless I absolutely have to have destination and source rects then uncomment these and put whatever value you want.*/
//SDL_Rect sRect, eRect;
//sRect.x = 4;
//sRect.y = 4;
//eRect.x = 24;
//eRect.y = 16;
if(!(text_surface = TTF_RenderUNICODE_Solid(font,text,color)))
{
printf("\n\nSomething went fuged : %s\n", TTF_GetError());
return 0;
}
else
{
/*If both srcrect and dstrect are NULL then the ENTIRE surface should be copied.*/
SDL_BlitSurface(text_surface, NULL, screen, NULL);
system("sleep 2s");
SDL_FreeSurface(text_surface);
}
return 0;
}
Can someone give me a idea of what could be the problem? Since I have to go work in about 10 minutes (have to get ready), I won't be able to fill the code with comments on how it should work. Once I get back if someone wants me to comment the code up I will. Thanks dmail for your help so far I appreciate it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.