LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Question/Problem with SDL_ttf program I made... (https://www.linuxquestions.org/questions/programming-9/question-problem-with-sdl_ttf-program-i-made-437657/)

RHLinuxGUY 04-22-2006 03:20 AM

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

dmail 04-22-2006 06:03 AM

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.

RHLinuxGUY 04-22-2006 03:48 PM

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


dmail 04-23-2006 04:51 PM

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]

RHLinuxGUY 04-26-2006 03:38 PM

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.

cupubboy 04-26-2006 04:24 PM

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

RHLinuxGUY 04-26-2006 10:38 PM

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


All times are GMT -5. The time now is 07:17 PM.