Jump to content
  • Advertisement
Sign in to follow this  
NUCLEAR RABBIT

SDL/C++ Question

This topic is 4093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey! I'm goin thru the lazy foo SDL tuts and I was not quite sure what this code means. Its a really simple couple lines of code, but i'd like to make sure I know what it means before I start using it.
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
    return 1;
}
My questions: 1) if it == -1, is that another way of saying == false? 2) if so, why would you return 1 if the function failed? 3) return 1 means return true, correct? Thanx for any help [smile]

Share this post


Link to post
Share on other sites
Advertisement
Uh well, the SDL_Init() function returns -1 on failure and 0 on success.
And to answer your questions; one does usually use -1 for failure and anything over -1 as success.
Although, if you cast it to a boolean, 0 means false and anything else true.

Share this post


Link to post
Share on other sites
Quote:
Original post by NUCLEAR RABBIT
1) if it == -1, is that another way of saying == false?


No. That function returns 0 on success and -1 on error, as explained in the documentation. SDL is a C89 API, so it has no boolean type. Because of that, 0 usually is used for success, while any other value will indicate an error. The rationale is simple: there's usually only one way to succeed, but there are many reasons why a function can fail.


Quote:
3) return 1 means return true, correct?


In C, IIRC, any non-zero value is "true". However, on UN*X-like systems, a process may return 0 (for success) or non-zero (an error code). C having its roots in UNIX, and C++ being built on top of C89, returning 0 from main() means everything went OK, while any non-zero value is an error code. The calling process may or may not care about it, as it's totally dependent on the environment, which C and C++ don't know about.

If all you want to do is indicate success or failure to the calling process (the return statement in main()), you can return EXIT_SUCCESS and EXIT_FAILURE, respectively. They're usually (always?) defined in stdlib.h.


Hope this helps.

Share this post


Link to post
Share on other sites
I guess you already got your answer ,but you might find define(s) useful.

Example:


#define FALSE -1
#define TRUE 0

if(SDL_Init(SDL_INIT_EVERYTHING) == FALSE)
{
return 1;
}




....Good luck!

Share this post


Link to post
Share on other sites
Quote:
Original post by 3Dgonewild
I guess you already got your answer ,but you might find define(s) useful.

Example:

*** Source Snippet Removed ***

....Good luck!


Thats horrible (IMO).

Imagine if(FALSE) or while(TRUE).

Anyway, SDL is pretty consistent. If the function returns a pointers, then NULL is failure. In general, otherwise any value below zero is a failure. I always write:

if( SDL_SomeFunction() < 0 ) {
// handle failure
}



I believe the decision for not using 0 as failure stems from functions like SDL_BlitSurface() which return 2 distinct errors.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!