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