Page 1 of 1

SDL Segmentation fault (core dumped)

Posted: Fri May 10, 2013 8:09 pm
by Nislipk
I am learning SDL and I want to make a Player class. Right now I am trying to get sprite sheets seperated so that I can loop through the frames. It compiles without any errors but, when I try to run I get Segmentation fault (core dumped)

Sorry for the wrong subjects before renamed it

I am not possitive of why this is happening I read that it is because I am passing something that I shouldn't be but, I don't see the problem. If someone could help that would be great.

Code: Select all

#include <iostream>
#include <SDL/SDL.h>
#include <string>
#include <vector>

using namespace std;

const int SCREEN_WIDTH  = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BBP    = 032;
SDL_Surface *screen;

class C_Player
{
	public:
	SDL_Rect rect;
	SDL_Rect clip[5];
	SDL_Surface *spriteSheet;


	void get_image ( std :: string );
	void set_clips();
	void apply_sprite();

};

void C_Player :: set_clips()
{

	int u = 0;
	for (int i = 0; i < 5; i++){	
		clip[u].w = 50;
		clip[u].h = 50;
		clip[u].x = (i*50);
		clip[u].y = 00;
		u++;	
	};
}

//Load Surface
void C_Player :: get_image ( std::string filename)
{
	
	//Temporary storage for the image that's loaded
	SDL_Surface* loadedImage = NULL;
	

	//Load the image
	loadedImage = SDL_LoadBMP ( filename.c_str() );
	
	//If nothing went wrong in loading the image
	if( loadedImage != NULL)
	{

		//Create an optimized image
		spriteSheet = SDL_DisplayFormat( loadedImage );

		//Free the old image
		SDL_FreeSurface( loadedImage );
	}
	
}


void C_Player :: apply_sprite()
{
	SDL_BlitSurface(spriteSheet, &clip[1], screen, &rect);
}

int main( int argc, char* args[])
{
	C_Player player;
	player.rect.x = 50;
	player.rect.y = 50;
	player.rect.h = 50;
	player.rect.w = 50;

	player.get_image( "bmp.bmp" );
	player.set_clips();

	
	//Start SDL
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
	{
		return 1;
	}
	//Create Window
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BBP, SDL_SWSURFACE);
	
	if( screen == NULL )
	{
		return 1;		
	}
	
	
	//Set the Window Caption
	SDL_WM_SetCaption( "Sprite", NULL);

	SDL_Event event;

	
	bool running = true;
	while ( running )
	{
	while ( SDL_PollEvent( &event) )
	{
		switch(event.type)
		{
			case SDL_QUIT:
			running = false;
			break;
		}
		
		switch (event.key.keysym.sym)
		{
			case SDLK_ESCAPE:
			running = false;
			break;		
		}

	}
	
			player.apply_sprite();
			//Update screen
			SDL_Flip( screen );
		
	}
	//Free surfaces
	SDL_FreeSurface( player.spriteSheet );
		
	//Quit SDL
	SDL_Quit();
	


return 0;
}

Re: SDL Segmentation fault (core dumped)

Posted: Fri May 10, 2013 8:32 pm
by Ginto8
Segmentation faults are caused by dereferencing an invalid pointer (typically NULL). My guess in this case is that it fails to load "bmp.bmp", which would leave spriteSheet uninitialized (it could be anything, including NULL), then when you try to use it in apply_sprite() it crashes.

Re: SDL Segmentation fault (core dumped)

Posted: Fri May 10, 2013 9:19 pm
by Nislipk
ok thanks I figured it out. I didn't have the function in the right place. After I put it into the while loop it worked.

Re: SDL Segmentation fault (core dumped)

Posted: Fri May 24, 2013 8:04 pm
by MarauderIIC
Learn to use your debugger and step through code/watch variables. It really really helps in finding those kinds of problems.