I have the following program (this is only one file of the program):
#include “draw.h”
Draw::Draw(void)
{
}
Draw::~Draw(void)
{
}
void Draw::Init()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf(“SDL failed to initialize: %s\n”, SDL_GetError());
exit(1);
}
screen = SDL_SetVideoMode(800, 600, 16, SDL_HWSURFACE ||
SDL_DOUBLEBUF);
if (screen == NULL)
{
printf(“SetVideo failed: %s\n”, SDL_GetError());
exit(1);
}
if (!TTF_Init())
{
printf("TTF_Init failed: %s\n", TTF_GetError());
//exit(1);
}
font = TTF_OpenFont("ARIAL.TTF", 12);
if (font == NULL)
{
printf("font is NULL\n");
//exit(1);
}
SDL_Color foregroundColor = { 255, 255, 255 };
SDL_Color backgroundColor = { 0, 0, 255 };
char buff[100];
for (int i = 0; i < 12; i++)
{
sprintf( buff, "%d", i);
enemyimage[i] = TTF_RenderText_Shaded(font, buff, foregroundColor,
backgroundColor);
}
SDL_WM_SetCaption("OurRPG", "OurRPG");
red = 0;
green = 0;
blue = 0;
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, red, green,
blue));
}
int Draw::getState() { return state; }
void Draw::setState(int i) { state = i; }
void Draw::setAllyParty(AllyParty p)
{
allyparty = p;
Character temp;
for (int i = 0; i < 4; i++)
{
temp = allyparty->getCharacter(i);
ally[i] = static_cast <Ally*> (temp);
}
}
void Draw::setEnemyParty(EnemyParty p)
{
enemyparty = p;
Character temp;
for (int i = 0; i < enemyparty->getCount(); i++)
{
temp = enemyparty->getCharacter(i);
enemy[i] = static_cast <Enemy*> (temp);
}
}
void Draw::draw(int state)
{
SDL_Rect allysource[4], allydest[4], enemysource[12], enemydest[12];
for (int i = 0; i < 12; i++)
{
// enemyimage[i] = IMG_Load(enemy[i]->getIMFileName().c_str());
enemysource[i].h = 50;
enemysource[i].w = 50;
enemysource[i].x = 0;
enemysource[i].y = 0;
enemydest[i].x = enemy[i]->getX();
enemydest[i].y = enemy[i]->getY();
enemydest[i].h = 50;
enemydest[i].w = 50;
}
for (int i = 0; i < 4; i++)
{
allyimage[i] = IMG_Load(ally[i]->getIMFileName().c_str());
allysource[i].h = 50;
allysource[i].w = 50;
allysource[i].x = 0;
allysource[i].y = 0;
allydest[i].x = ally[i]->getX();
allydest[i].y = ally[i]->getY();
allydest[i].w = 50;
allydest[i].h = 50;
SDL_BlitSurface(allyimage[i], &allysource[i], screen, &allydest[i]);
}
for (int i = 0; i < 12; i++)
{
SDL_BlitSurface(enemyimage[i], &enemysource[i], screen,
&enemydest[i]);
}
SDL_Flip(screen);
SDL_Delay(5000);
SDL_Quit();
}
When I build and run my program, it segfaults. When I run it in gdb, I
see that TTF_Init is failing, but TTF_GetError() is not telling me why.
Why is this? Am I using it wrong?