Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


SDL_ttf runtime error


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Wraithe   Members   -  Reputation: 121

Like
0Likes
Like

Posted 15 July 2013 - 07:43 PM

Hello all,

 

Looking for a little help. I have used SDL with TTF quite a bit and I am fairly comfortable with it at this point. This error has me dumbfounded however, and I have no more ideas of how to proceed. I have tested the Font loading and TTF_RenderText_Solid. The surface returned from the rendertext function is not a null pointer, but it must be the issue, as my program crashes anytime I try to blit a text surface. I have used the same blitting function for standard images, and it works just fine.

 

I have also checked all my compiler linking (I am using code::blocks), it is fine. I have loaded up old programs, recompiled them, and they work just fine. I have no idea where to go from here. My button class is where I am trying to utilize text, so that is what I will be showing.

 

Edit: In case anyone ends up asking, I did call TTF_Init();

 
#include "text.h"
 
std::map <std::string, SDL_Color> Text::mColors;

std::map <std::string, TTF_Font *> Text::mFonts;



SDL_Surface * Text::applyText( std::string strText, std::string strFont, std::string strColor, int * w, int * h )

{

    SDL_Surface * surface = NULL;



    surface = TTF_RenderText_Solid( mFonts[ strFont ], strText.c_str(), mColors[ strColor ] );



    TTF_SizeText( mFonts[ strFont ], strText.c_str(), w, h );



    return surface;

}
 
 
 
#ifndef TEXT_H

#define TEXT_H



#include <map>

#include <string>



#include "SDL/sdl_ttf.h"



class Text

{

    public:

        static SDL_Surface * applyText( std::string strText, std::string strFont, std::string strColor, int * w, int * h );



        static std::map <std::string, SDL_Color> mColors;

        static std::map <std::string, TTF_Font *> mFonts;

};



#endif // TEXT_H
 
 
 
// inside of Display.cpp
 
void Display::loadFonts()

{

    initInput( "Resources/ResourceListFonts.txt" );



    while( !isFile.eof() )

    {

        readLine();



        if( !strData.empty() )

        {

            std::string strFont = strData;



            readLine();



            std::string strFontPath = strPath + strData;



            int fontSize = readValue();



            TTF_Font * newFont = NULL;



            newFont = TTF_OpenFont( strFontPath.c_str(), fontSize );



            mFonts.insert( std::pair <std::string, TTF_Font *> ( strFont, newFont ) );

        }

    }



    freeInput();

}
 
 
// in button.cpp
 
Button::Button( int iX, int iY, std::string strSurf, std::string strFunc, std::string strText )

{

    x = iX;

    y = iY;

    w = Display::getImageWidth( strSurf );

    h = Display::getImageHeight( strSurf );



    iTextW = 0;

    iTextH = 0;



    strFunction = strFunc;

    strSurface = strSurf;



    surfaceText = NULL;



    if( !strText.empty() )

    {

        strSurfaceText = strText;



        surfaceText = Display::applyText( strSurfaceText, "monospaceTypewriter12", "black", &iTextW, &iTextH );

    }



    sClipX = 0;

    sClipY = 0;

}
 
 
void Button::render()

{

    Display::applySurface( x, y, strSurface, sClipX, sClipY );



    if( surfaceText != NULL )

    {

        Display::applySurface( ( x + ( iTextW / 2 ) ), ( y + ( iTextH / 2 ) ), surfaceText );

    }

}
 

Edited by Wraithe, 15 July 2013 - 07:54 PM.


Sponsor:

#2 Wooh   Members   -  Reputation: 636

Like
0Likes
Like

Posted 16 July 2013 - 04:10 AM

Are you sure TTF_OpenFont does not return a null pointer?

Are you sure mFonts[strFont] does not return a null pointer?

#3 rip-off   Moderators   -  Reputation: 8515

Like
0Likes
Like

Posted 16 July 2013 - 04:20 AM

You need to have error checking around all calls. Almost all SDL calls can return status codes. The convention in SDL is that functions which return pointers can return nullptr on failure, and functions which return integer values return negative values on failure. You can use SDL_GetError() (or for TTF functions,  TTF_GetError()) to get a reasonably human readable description of the error.



#4 Wraithe   Members   -  Reputation: 121

Like
0Likes
Like

Posted 16 July 2013 - 04:31 AM

Thanks for the replies! As far as I can tell (with error checking) the pointers are valid. I set up error checking and not a single error occurs. The program just crashes when a text rendered surface hits the blit function.

 

 

Edit:


Are you sure mFonts[strFont] does not return a null pointer?

 

 

Yes, I have even tried filling in all the render text solid parameters with solid values, generated IN that function, and the crash still occured at the blit function.


Edited by Wraithe, 16 July 2013 - 04:33 AM.


#5 Wraithe   Members   -  Reputation: 121

Like
0Likes
Like

Posted 16 July 2013 - 06:05 AM

I have also tried freeing the rendered text surface immediately after creation, and the program crashes, yet the pointer is not NULL. Again, my other programs work just fine with TTF, what on earth could the issue be?

 

Edit: Finally got a crash report (Only does it once out of every 10 runs )

 

Problem signature:
  Problem Event Name:    APPCRASH
  Application Name:    Space Invaders.exe
  Application Version:    0.0.0.0
  Application Timestamp:    51e53cc5
  Fault Module Name:    ntdll.dll
  Fault Module Version:    6.1.7600.16385
  Fault Module Timestamp:    4a5bdb3b
  Exception Code:    c0000005
  Exception Offset:    000335f2
  OS Version:    6.1.7600.2.0.0.256.1
  Locale ID:    1033
  Additional Information 1:    e3cc
  Additional Information 2:    e3cc0ee538989a7b66ab90686b3c00a6
  Additional Information 3:    d22a
  Additional Information 4:    d22a3993b2d48ddd96705e026b50390c

 

 

  Fault Module Name:    ntdll.dll  <-- What is this?


Edited by Wraithe, 16 July 2013 - 06:32 AM.


#6 Wraithe   Members   -  Reputation: 121

Like
0Likes
Like

Posted 16 July 2013 - 06:40 AM

Sorry to post so many times in a row, but I found a solution.

 

The program only crashes when I use a vector of my button class. If I make it a vector of pointers, it works. Anyone know why this is?  (Not a big deal, pointers are fine, I simply don't understand why that would cause a crash).



#7 Wooh   Members   -  Reputation: 636

Like
0Likes
Like

Posted 16 July 2013 - 07:01 AM

In certain situations the vector will copy objects stored in the vector so if you have not implemented the Button class to handle copying correctly that is probably the problem.

Also see the rule of three.

#8 rip-off   Moderators   -  Reputation: 8515

Like
0Likes
Like

Posted 16 July 2013 - 07:25 AM

Were you not running your program in a debugger? It would be a lot easier to diagnose the cause of the crash using a debugger.



#9 Wraithe   Members   -  Reputation: 121

Like
0Likes
Like

Posted 16 July 2013 - 07:40 AM

Thank you both again for the replies!

 

I do not ever think to use the debugger =(, will do my best to remember.

 

There was some error about std::mem_copy or something similar, so it must have been vector copying errors.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS