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?
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?
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.
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