Advertisement

SDL - runtime segmentation fault?

Started by January 26, 2005 04:44 AM
3 comments, last by sand_man 19 years, 7 months ago
Alright, so I have my project working fine, my code compiles flawlessly under MSVC++ .NET 2003. The code is as follows:
//Built-in Libraries
#include <stdlib.h>

//SDL API
#include <SDL.h>

//Include our SDL libs
#pragma comment( lib, "SDL.lib" )
#pragma comment( lib, "SDLmain.lib" )

//=========================================================================
SDL_Surface *g_pDisplaySurface = NULL;
SDL_Surface *g_pBitmapSurface = NULL;
SDL_Event g_Event;
SDL_Rect g_SrcRect, g_DstRect;

int main(int argc, char* argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	//atexit(SDL_Quit);

	g_pDisplaySurface = SDL_SetVideoMode(800,600,0,SDL_ANYFORMAT);
	g_pBitmapSurface = SDL_LoadBMP("cirlce.bmp");
	g_SrcRect.w = g_DstRect.w = g_pBitmapSurface->w;
	g_SrcRect.h = g_DstRect.h = g_pBitmapSurface->h;
	g_SrcRect.x = g_SrcRect.y = 0;

	for(;;)
	{
		if(SDL_PollEvent(&g_Event)==0)
		{
			g_DstRect.x = rand()%(800-g_DstRect.w);
			g_DstRect.y = rand()%(600-g_DstRect.h);
			SDL_BlitSurface(g_pBitmapSurface, &g_SrcRect, g_pDisplaySurface, &g_DstRect);
			SDL_UpdateRect(g_pDisplaySurface, 0 ,0 ,0 ,0);
		}
		else
		{
			if(g_Event.type==SDL_QUIT) break;
		}
	}

	return 0;
}
And when I execute it from MSVC++, I get this error: Fatal Signal: Segmentation Fault(SDL Parachute Deployed) press any key to continue... When I execute the .exe independently it doesn't have a pause statement, so it just closes. Anyway, I'm not sure what my problem is, I'm trying out some example code from the first chapter of Focus on SDL by Ernest Pazera. I saved a 64x64px .bmp called circle.bmp w/ a circle drawn in it saved in 24-bit format, although I don't think that's the problem. I really wanted to get this working, any ideas? =(
-Conrad
Well a segmentation fault means you're accessing memory you shouldn't be. In your program you've got two pointers g_pDisplaySurface and g_pBitmapSurface. If either of these are null you'll be getting a crash like you describe. Thus either SDL_SetVideoMode failed for some reason (not really that likely) or SDL_LoadBMP couldn't find or load cirlce.bmp which is more likely and I'm guessing you've simply spelt circle.bmp wrong here [smile].
Advertisement
Quote: Original post by Monder
SDL_LoadBMP couldn't find or load cirlce.bmp which is more likely and I'm guessing you've simply spelt circle.bmp wrong here [smile].


I think you are 100% right - just a hapless mispelling to ruin the day [lol]. That code gave me a seg fault as well but as soon as I made the .bmp file with the right name it worked.
Oh goodness. =/ How careless of me. Sorry guys! >_< Thanks!
-Conrad
A debugger is good for these problems. Narrow the problem down to a smaller area of code and THEN hopefully you will see tiny errors just like that.

This topic is closed to new replies.

Advertisement