Sign in to follow this  
kaktusas2598

SDL runtime error

Recommended Posts

kaktusas2598    953
I looked in Lazzy Foo tutorials in SDL,I'm currently on 7th tutorial "True Type Fonts". I made program using things I learned there and then I run my program I got runtime error. Here's the code:
#include "SDL.h"
#include "SDL_ttf.h"
#include <string>
#include "SDL_image.h"

SDL_Surface* background = NULL;
SDL_Surface* screen = NULL;
SDL_Surface* message = NULL;

SDL_Event event;
//the font what's going to be used
TTF_Font* font = NULL;

//color of the font
SDL_Color textColor = {255,255,255};
bool init()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
		return false;
	screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
	if(screen = NULL)
		return false;
	//Initialize SDL_ttf 
	if( TTF_Init() == -1 ) 
		return false; 
	SDL_WM_SetCaption( "TTF Test", NULL ); 
	return true; 
}
SDL_Surface *load_image( std::string filename )
{ 
	SDL_Surface* loadedImage = NULL; 
	SDL_Surface* optimizedImage = NULL; 
	loadedImage = IMG_Load( filename.c_str() ); 
	if( loadedImage != NULL ) 
	{ 
		optimizedImage = SDL_DisplayFormat( loadedImage ); 
		SDL_FreeSurface( loadedImage ); 
	} 
	if(optimizedImage != NULL)
	{
		//map the color key
		Uint32 colorkey = SDL_MapRGB(optimizedImage->format,0,0xFF,0xFF);
		//set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
		SDL_SetColorKey(optimizedImage,SDL_SRCCOLORKEY,colorkey);
	}
	return optimizedImage;
}
bool load_files()
{ 
	background = load_image( "background.png" ); 
	//Open the font 
	font = TTF_OpenFont( "lazy.ttf", 28 ); 
	if( background == NULL ) 
		return false; 
	//If there was an error in loading the font 
	if( font == NULL )
		return false;
	
	return true;
} 
void apply_surface(int x, int y, SDL_Surface* source,SDL_Surface* destination)
{
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source,0,destination,&offset);
}
void clean_up() 
{ 
	SDL_FreeSurface( background ); 
	SDL_FreeSurface( message ); 
	//Close the font that was used 
	TTF_CloseFont( font ); 
	//Quit SDL_ttf 
	TTF_Quit(); 
	
	SDL_Quit(); } 
int main(int argc, char* args[])
{
	init();
	load_files();
	//Render the text 
	message = TTF_RenderText_Solid( font, "The quick brown fox jumps over the lazy dog", textColor ); 
	//If there was an error in rendering the text 
	if( message == NULL ) 
		return 1; 
	
	apply_surface( 0, 0, background, screen ); 
	apply_surface( 0, 150, message, screen ); 
	if( SDL_Flip(screen) == -1 ) 
		return 1;  
	bool quit = false;
	while(quit == false)
	{
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
				quit = true;
		}
	}
	clean_up();
	return 0;
}
[Edited by - kaktusas2598 on March 30, 2010 9:22:33 AM]

Share this post


Link to post
Share on other sites
Gage64    1235
Use the debugger to find out what line causes the crash.

Also, please edit your post to use source tags (see the FAQ for instructions).

Share this post


Link to post
Share on other sites
Ezbez    1164
Classic C or C++ mistake: using = instead of == in an if test or similar. You have "if(screen = NULL)" shortly after the call to SDL_SetVideoMode. Since you use =, it will assign NULL to screen, rather than check if screen equals NULL. Watch out for this error; it's easy to make and makes some funny results. Turn on higher warning levels and most compilers will warn you about it, though.

Share this post


Link to post
Share on other sites
ZenicReverie    100
I used to know someone who would code like this in order to avoid those mistakes:

if(NULL == screen)

Doesn't work when comparing variables to each other, but this will always cause an error when comparing a value if you accidentally type:

if(NULL = screen)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this