Sign in to follow this  
Mizipzor

Problems with SDL init

Recommended Posts

I run this:
	if(!SDL_Init(SDL_INIT_VIDEO)) {
		printf("Unable to init SDL: %s", SDL_GetError());
		getch();
		return -1;
	}

Program compiles fine but when I run it I get that textline, unable to init sdl... but no error. Whats wrong? Why doesnt SDL_GetError() return anything? And what do I do about it? Thanks in advance Mizii

Share this post


Link to post
Share on other sites
Ummmmm. My C is rusty and my brain is nonfunctional at the moment (so I could be making an embaressing mistake), but the docs say SDL_Init() returns 0 on success, -1 on failure. You should probably ditch the ! in the if statement.

Share this post


Link to post
Share on other sites
No youre right. Sorry, that was just something I took for granted :P negative values on failure. Next time ill read the documentation better... heh

Share this post


Link to post
Share on other sites

if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}


Works fine for me. :)

Share this post


Link to post
Share on other sites
As Icefox said, you should drop the '!' in the if statement. The reason: You could use JWindebank's method, but (SDL_Init() < 0) would do the computation of the 'less than' function (not overly expensive, but not necessary) and compare the result to zero (what C/C++ if statements do), where as Icefox's version just does the comparison.

[EDIT]
There is a downside to this: the code looks like it'd be complaining if the function succeeded. ;)

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