SDL Segmentation fault (core dumped)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Nislipk
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Sun Jan 10, 2010 8:03 pm

SDL Segmentation fault (core dumped)

Post 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;
}
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL Segmentation fault (core dumped)

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Nislipk
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Sun Jan 10, 2010 8:03 pm

Re: SDL Segmentation fault (core dumped)

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: SDL Segmentation fault (core dumped)

Post by MarauderIIC »

Learn to use your debugger and step through code/watch variables. It really really helps in finding those kinds of problems.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply