LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-22-2006, 03:20 AM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
Question/Problem with SDL_ttf program I made...


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:

Code:
george@georgescomp1:~/cfiles/George Lair/030506/032906/041506/041706TEST$ ./TTFTest042206_105
Fatal signal: Segmentation Fault (SDL Parachute Deployed)
george@georgescomp1:~/cfiles/George Lair/030506/032906/041506/041706TEST$
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.
 
Old 04-22-2006, 06:03 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
You should not free a surface which has been created via setvideomode. see docs
Quote:
Originally Posted by http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSetVideoMode
...The surface returned is freed by SDL_Quit and should not be freed by the caller...
You have set sdlquit to be called twice, once at the end of main and once atexit.

Your main function is incorrect, it should be
Code:
int main (int argc, char *argv[])
as this is the entry point for sdl into the program.
also check the return value when you intailise sdl and try compling with -pthread.

If these fixes don't get it running just post back.

Last edited by dmail; 04-22-2006 at 06:05 AM.
 
Old 04-22-2006, 03:48 PM   #3
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
I did what you said dmail and I'm not too sure where to go for all I get is the following as an error message...

Fatal signal: Segmentation Fault (SDL Parachute Deployed)


Here is my code now:

Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <SDL/SDL.h>
#include "SDL_ttf.h"
using namespace std;

int main(int argc, char *argv[])
{


	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);
	}

	SDL_Surface *screen;

	screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
	
	TTF_Font *font;
	font = TTF_OpenFont("couri.ttf", 16);
	if (!font)
	{
		printf("Something went fucked : %s\n", TTF_GetError());
		return 0;
	}

	int style;
	style = TTF_GetFontStyle(TTF_STYLE_NORMAL);

	SDL_Color color={0,0,0};
	SDL_Surface *text_surface;
	Uint16 text[] = {'H','e','l','l','o',' ','W','o','r','l','d'};

		if(!(text_surface = TTF_RenderUNICODE_Solid(font,text,color)))
		{
			cout << "\n\tFuck it... (check line 36-40)\n";
		}
		else
		{	
		SDL_BlitSurface(text_surface, NULL, screen, NULL);
		system("sleep 5s");
		SDL_FreeSurface(text_surface);
		}

	SDL_Quit();
	TTF_Quit();

	return 0;
}
 
Old 04-23-2006, 04:51 PM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
[/edit]

Last edited by dmail; 04-23-2006 at 05:02 PM.
 
Old 04-26-2006, 03:38 PM   #5
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
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.
 
Old 04-26-2006, 04:24 PM   #6
cupubboy
Member
 
Registered: May 2003
Location: Bucharest,Romania
Distribution: Fedora Core 7
Posts: 109

Rep: Reputation: 15
I'm not sure .. maybe I missed something .. But shouldn't you do a SDL_Flip() or SDL_UpdateRects() after blitting?

My SDL knowledge is a little old

Cheers
 
Old 04-26-2006, 10:38 PM   #7
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Your knowledge is not too old, or at all! It works now, here is the code so anyone try out:

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);
		SDL_UpdateRect(screen, 0, 0, 0, 0);
		system("sleep 2s");
		SDL_FreeSurface(text_surface);
		}

	return 0;
}
be sure to replace the .ttf with whatever you want. ($ locate *.ttf)

Thank you dmail and cupubboy
 
  


Reply

Tags
code, game, sample, sdl, ttf


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there something that can transform a program made with php to shell-script? stormrider_may Programming 4 01-31-2006 10:59 AM
SDL_ttf e6767593 Linux - Software 3 05-12-2005 03:48 PM
I wonder why a program I made with streams is taking so much memory eantoranz Programming 5 04-30-2004 07:37 PM
The best linux program ever made??? Mannyakatheman General 49 03-08-2004 04:27 PM
How to install SDL_ttf under slack9.1 preswang Linux - Software 0 10-28-2003 07:58 AM


All times are GMT -5. The time now is 04:24 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration