Advertisement

[SDL] Access Violation error - SDL_ttf

Started by November 07, 2011 05:52 PM
5 comments, last by OrangeeZ 13 years, 3 months ago
So, I'm busy making a (small) game for school.
And now I'm trying to display text, which should be easy.. This is my code:

if(TTF_Init() == -1)
{
std::cout << "TTF_Init() Failed: " << TTF_GetError() << endl;
}
const char* fontPath = "..\\..\\data\\font\\font2.ttf";
TTF_Font* font = TTF_OpenFont(fontPath, 18);
if(font == NULL)
{
std::cout << "Error while loading font: " << TTF_GetError() << endl;
}
SDL_Color c = { 255,255,255 };
const char* testtext = "This is a test text";
SDL_Surface* text = TTF_RenderText_Solid(font,testtext,c); // Access Violation here
if(text == NULL) // So it's not even able to come here
{
std::cout << "Error while rendering: " << TTF_GetError() << endl;
}

Should work right? Well, I get an Access Violation, the 005 one, which basically tells me something I'm pointing to with a pointer isn't really there right?
Oh, the font does exist. It doesn't give me an error in that error check, and if I fill in some gibberish as path ("ASDSAD.ttf"), it does give me the error.

I've tried searching the web, but after a day, I've given up, and decided to post here..
Could anyone help me / point me in the right direction?
Have you initialized SDL?
Advertisement
Yes, everything else I've done so far works, no need to ask if I initialized SDL itself or anything, it's just that part that doesn't work.
Have you pinpointed the exact line using your debugger?
I plugged your code in my own program and it worked for me. The only thing that I can not verify is your font location.
SDBradley
CGP
"A person who won't read has no advantage over one who can't read." ~Mark Twain
I discovered it was a wrong version.. I'm using SDL 1.3, with SDL_ttf 2.0.10.
I've already tried compiling the latest version (got them from the hg repo's) of both, but now it just won't open the file..

Is there any easy way to get SDL1.3 working with SDL_ttf?

Advertisement
You might try recompiling the lib, but here's what works for me:
1. Use TTF_RenderText_Blended
2. Before freeing the surface increase refcount at it's format
Issue is, when freeing surface it leaves dead pointer at format stack.

Example:



// load font, create text and colour here

SDL_Surface* sptr = TTF_RenderText_Blended(font, text, colour);

// make a texture, blit it, whatever

sptr->format->refcount ++; // or assign it a 2, this is a safe pointer, we're telling not to clear the format, for it's used later by SDL
SDL_FreeSurface(sptr); // no crashes should occur

This topic is closed to new replies.

Advertisement