Hello!
I’m making a game in SDL, I’m trying to load a font using SDL_ttf so I can render text. Upon running the code to render said text, I get hit with an error. In the IDE itself on a line where I defined the rect, I got an error “Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)”. This also had a red underline on the 3rd perimeter which is for the width of the rect. I’m assuming the same could go for the 4th perimeter for height.
In the terminal itself, I got an SDL_ttf error.
“‘SDL_CreateTextureFromSurface(): surface’ is invalid.”
This appears twice, as I tried loading text twice.
Codewise, this is a snippet of the class I had the code for loading an image and rendering text
class diaText {
public:
SDL_Surface* diaName1;
SDL_Surface* mainDiaText1;
SDL_Texture* diaNameTexture1;
SDL_Texture* mainDiaTextTexture1;
diaText() {
diaName1 = TTF_RenderText_Solid(mainFont50, "Moses", cWHITE);
mainDiaText1 = TTF_RenderText_Solid(mainFont50, "Testing Testing 1, 2 ,3", cWHITE);
diaNameTexture1 = SDL_CreateTextureFromSurface(gRenderer, diaName1);
if (!diaName1) {
std::cout << "Error rendering text SDL_ttf:" << TTF_GetError() << std::endl;
}
mainDiaTextTexture1 = SDL_CreateTextureFromSurface(gRenderer, mainDiaText1);
if (!mainDiaText1) {
std::cout << "Error rendering text SDL_ttf" << TTF_GetError() << std::endl;
}
}
void render() {
SDL_Rect diaName1Rect = {100, 100, diaName1->w, diaName1->h}; // error on this line
SDL_Rect mainDiaText1Rect = {200, 200, mainDiaText1->w, mainDiaText1->h};
SDL_RenderCopy(gRenderer, diaNameTexture1, NULL, &diaName1Rect);
SDL_RenderCopy(gRenderer, mainDiaTextTexture1, NULL, &mainDiaText1Rect);
}
};
In the main function, I call all the functions in the classes.
dialouge dia;
dialouge::diaText diaText;
(Insert code in between here)
SDL_RenderClear(gRenderer);
bg.drawBackground();
moses.render();
dia.render();
diaText.~diaText();
diaText.render();
SDL_RenderPresent(gRenderer);
Yes, my renderer is working, it’s in a header file separate from this main cpp file.
For any more information, reply please!
Xcode 16.2
SDL2
SDL_ttf 2
(unrelated)
SDL_image 2
Thank you for reading!