I'm writing a program that renders font in SDL. I have a struct with the color and text of a 'pixel.' It causes a segmentation fault, and I can't make any sense of it.
struct pixInfo
{
SDL_Color bColor;
SDL_Color tColor;
Uint16 ch;
};
And this is the rendering code
fchar = TTF_RenderGlyph_Solid(com->font, im->ch, im->tColor); // Segmentation fault
com is a pointer to the main class, which stores the font. im is an iterator to a pixInfo struct fchar is a SDL_Surface* This will crash if im->ch is something like 9554 (ASCI characters work fine)
However, This code does work
fchar = TTF_RenderGlyph_Solid(com->font, 9554, im->tColor);
// OR
Uint16 num = 9554;
fchar = TTF_RenderGlyph_Solid(com->font, num, im->tColor);
This does not work:
Uint16 num = im->ch;
fchar = TTF_RenderGlyph_Solid(com->font, num, im->tColor); // Segmentation fault
If I print the 'num' variable to the output in both of those examples, they are 9554 (Keep in mind, it's not just 9554 doing this)
EDIT: I just tried another approach, it segfaults as well
Uint16 num;
memcpy(&num, &im->ch, sizeof(Uint16));
fchar = TTF_RenderGlyph_Solid(com->font, num, im->tColor); // Segmentation fault
EDIT 2: This code does not crash
Uint16 num;
memcpy(&num, &im->ch, sizeof(Uint16));
if (num == 9554)
{
std::cout << "9554" << std::endl;
num = 32;
}
fchar = TTF_RenderGlyph_Solid(com->font, num, im->tColor);
But this code does segfault
Uint16 num;
memcpy(&num, &im->ch, sizeof(Uint16));
if (num == 9554)
{
std::cout << "9554" << std::endl;
num = 32;
num = 9554;
}
fchar = TTF_RenderGlyph_Solid(com->font, num, im->tColor);