I have made a header file for SDL GUI applications. I was testing the SDLGame::Draw() function when I got a SigSegV fault. The fault occurs in different locations each time I run the program. Also sometimes instead of a SigSegV fault I get a SigTrap fault. Here is the code involved with the SigSegV.
SDLGame class constructor:
SDLGame::SDLGame(int w, int h, int bpp, Uint32 flags)
{
if (SDL_Init(SDL_INIT_EVERYTHING)==-1)
exit(-1);
Window=SDL_SetVideoMode(w, h, bpp, flags);
if (Window==NULL)
exit(-2);
}
SDLGame class Draw():
void SDLGame::Draw(Sprite img, int x, int y)
{
SDL_Rect offset;
offset.x=x;
offset.y=y;
SDL_BlitSurface((SDL_Surface*)img, NULL, Window, &offset);
}
Sprite class constructor (from file):
Sprite::Sprite(char *filename)
{
SDL_Surface *Temp=IMG_Load(filename);
if (Temp!=NULL)
{
SDL_Surface *Good=SDL_DisplayFormatAlpha(Temp);
pixels=new Pixel32[Good->w*Good->h];
w=Good->w;
h=Good->h;
pixels=(Pixel32*)Good->pixels;
SDL_FreeSurface(Temp);
//SDL_FreeSurface(Good);
}
else
{
pixels=NULL;
w=0;
h=0;
SDL_FreeSurface(Temp);
}
}
Sprite class SDL_Surface typecast operator:
Sprite::operator SDL_Surface*()const
{
SDL_Surface *ret=NULL;
ret=SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
Pixel32 *surface=(Pixel32*)ret->pixels;
for (int i=0; i<(w*h); i++)
{
if (!pixels[i]||!surface[i])
{
i=(w*h);
break;
}
surface[i]=pixels[i];
}
SDL_Surface *dup=ret;
ret->refcount++;
return dup;
}
Pixel32 is essentially typedef'd as Uint32. This is the basis of my main() function (but not it entirely, I think the problem lies in the fact that I draw a surface twice):
int main()
{
SDL_Game TTT(600, 600, 32, SDL_SWSURFACE);
Sprite Board("BOARD.png");
TTT.Draw(Board, 0, 0);
TTT.Step();
TTT.Draw(Board, 0, 0);
TTT.Step();
return 0;
}
If I need to post the whole code I can. I just don't wish to right now. Could anybody solve this problem without more code?