Jump to content
  • Advertisement
Sign in to follow this  
Wabsta

[SDL] Access Violation error - SDL_ttf

This topic is 2586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!