Thread: Trouble with TTFs

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428

    Trouble with TTFs

    This line of code is crashing my game. If I comment it everything else runs smooth, but of course no text is displayed when needed.

    Code:
    SDL_Surface *linea = NULL;
    ....
    ....
    int main()
    {
    linea = TTF_RenderText_Solid( font, "Welcome to my shop we have 6 weapons", textColor );
    }
    any ideas?
    Last edited by Lesshardtofind; 07-23-2009 at 02:55 AM.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    any ideas?
    Are you sure the font is valid ?
    Also, you are supposed to free the surface after blitting it.

    SDL_ttf 2.0.7: TTF_RenderText_Solid

  3. #3
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by Spidey View Post
    Also, you are supposed to free the surface after blitting it.
    Yup

    Code:
    SDL_FreeSurface(linea);
    linea = TTF_RenderText_Solid( font, "Welcome to my shop we have 6 weapons", textColor );

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Yes the font is valid and in the same directory as the project. And I already free all the surfaces in the program at the end with a freeallsurfaces(); function.
    The program doesn't get that far. Let me post a little more code maybe someone will know the problem then.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <SDL/SDL.h>
    #include <windows.h>
    #include <string>
    #include <SDL/SDL_image.h>
    #include <SDL/SDL_ttf.h>
    
    SDL_Surface *screen = NULL;
    SDL_Surface *background = NULL;
    SDL_Surface *character = NULL;
    SDL_Surface *town = NULL;
    SDL_Surface *weapshoptemp = NULL;
    SDL_Surface *rustysword = NULL;
    SDL_Surface *bronzesword = NULL;
    SDL_Surface *plasmasword = NULL;
    SDL_Surface *platinumsword = NULL;
    SDL_Surface *silversword = NULL;
    SDL_Surface *bdiamondsword = NULL;
    SDL_Surface *linea = NULL;
    SDL_Color textColor = { 255, 255, 255 };
    SDL_Event event;
    TTF_Font *font = NULL;
    
    int location =2;
    
    int initsdl()
    {
        /* Initialize SDL */
        if (SDL_Init (SDL_INIT_EVERYTHING) == -1)
        {
            return 1;
        }
        
    
        /* Set 640x480 16-bits video mode */
        screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE |     
        SDL_DOUBLEBUF);
    
        if (screen == NULL)
        {
            return 1;
        }
        if( TTF_Init() == -1 )
        {
            return false;    
        }
        SDL_WM_SetCaption ("Starting with physics", NULL);
    }
    
    int freesurfaces()
    {
        SDL_FreeSurface(background);
        SDL_FreeSurface(screen);
        SDL_FreeSurface(character);
        SDL_FreeSurface(rustysword);
        SDL_FreeSurface(bronzesword);
        SDL_FreeSurface(silversword);
        SDL_FreeSurface(plasmasword);
        SDL_FreeSurface(platinumsword);
        SDL_FreeSurface(bdiamondsword);
        SDL_FreeSurface(linea);
    }
    
    void loadimages()
    {
         character = loadimage("ani.gif");
         rustysword = loadimage("rustysword.png");
         bronzesword = loadimage("bronzesword.png");
         silversword = loadimage("silversword.png");
         plasmasword = loadimage("plasmasword.png");
         platinumsword = loadimage("platinumsword.png");
         bdiamondsword = loadimage("bluediamondsword.png");
         font = TTF_OpenFont( "achafsex.ttf", 28 );
    }
    
    void displayarea()
    {
         //1 is town
         if(location == 1)
         {
           background = loadimage("town.png");          
           applysurface(0,0, background, screen, NULL);
           applysurface(x, y, character, screen, &animation[1]);
         }
         else if(location == 2)
         {
           background = loadimage("shop.png");          
           applysurface(0,0, background, screen, NULL);
           applysurface(10,70, rustysword, screen, NULL);
           applysurface(310,70, bronzesword, screen, NULL);
           applysurface(10, 140, silversword, screen, NULL);
           applysurface(310, 140, platinumsword, screen, NULL);
           applysurface(10, 210, bdiamondsword, screen, NULL);
           applysurface(310, 210, plasmasword, screen, NULL);
         }
    }
    
    void play()
    {
      int MouseCoordX, MouseCoordY;
      displayarea();
      bool quit = false;
      while(quit == false)
      {
        while(SDL_PollEvent(&event))
        {
          displayarea();                          
          if(event.type == SDL_QUIT)
          {
            quit = true;
          }
          else if(location == 1)
          {
            if( event.type == SDL_KEYDOWN )
            {
              switch( event.key.keysym.sym ) 
              { 
                case SDLK_UP: moveup();
                break;  
                case SDLK_LEFT: moveleft(); 
                break; 
                case SDLK_RIGHT: moveright();
                break;
                case SDLK_DOWN: movedown(); 
              } 
            }
          }
          else if(location == 2)
          {
             if(event.type == SDL_MOUSEBUTTONDOWN)
             {
               if(event.button.button == SDL_BUTTON_LEFT)
               {
                 MouseCoordX = event.button.x;
                 MouseCoordY = event.button.y;
                 SDL_FreeSurface(linea);
                 //if I comment the following line the program runs else it stops and     
                 does not even apply the line applysurface that follows it.
                 linea = TTF_RenderText_Solid( font, "Welcome to my shop we have 6             weapons", textColor );
                 applysurface(MouseCoordX, MouseCoordY, linea, screen, NULL);
               }
             }
          }     
          SDL_Flip(screen);
        }
      }  
    }
    
    int main (int argc, char *argv[])
    {
      initsdl();
      loadimages(); 
      play();
      freesurfaces();
      return 0;
    }
    sorry I know its long, but it was the shortest version of the real code I could copy paste and still give you an idea of whats goin on

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    UPDATE: I made the smallest program possible to print ttfs and it still crashed at the same line of code. I found this error message in a notepad file outside the program. Fatal signal: Segmentation Fault (SDL Parachute Deployed)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM