Sign in to follow this  
The_Marlboro_Man

Initializing SDL outside the main function.

Recommended Posts

Hello!. Second thread... I managed to solve a lot of doubts myself by carefully dissecting the Lazy Foo tutorials on SDL but I´ve come across something that is a bit beyond me. Everything I´ve studied about C++ programming tells me that I should follow the structured programming method (one entry, one exit) and that I should try to achieve the clearest code possible. In order to do that I´m trying to put as many functions outside the main as I can... One thing I´m trying is to make a function that would initialize the SDL and a screen surface, returning 0 if there was a mistake... Something like: int init_sdl_thing(SDL_Surface *screensurface) { int result=1; if( (SDL_INIT(SDL_INIT_EVERYTHING)==-1)||(screen=NULL) ) { result=0; printf("Blahblaherrorblah"); } return result; } So I can do this in the main: { ... SDL_Surface *screen=NULL; if(init_sdl_thing(screen)==0) { return -1; //Go and break the "one entry, one exit rule :P" } ... } But it just won´t work: the program will compile but the SDL parachute message will appear in my log and I´m forced to declare screen as a global surface. I know I can just break the rules they´re teaching me but I´d like to know if there´s anyway I could pass the surface declared in the main to another function so I can initialize a video mode on it. Is something wrong with my syntax?. Is it just that this SDL_INIT function will only work in the main function (sure not)?. Maybe I am passing the screen surface badly?. Any help is appreciated. Thanks a lot.

Share this post


Link to post
Share on other sites
You *are* passing the screen surface badly [smile]

The way you have it set up, you are trying to assign the screen pointer inside the function. To do this, you must either pass the pointer by pointer, or, preferably, by reference:

int init_sdl_thing(SDL_Surface* & screensurface) // Note the &
{
int result=1;
if( (SDL_INIT(SDL_INIT_EVERYTHING)==-1)||(screen=NULL) )
{
result=0;
printf("Blahblaherrorblah");
}
return result;
}



Even better, you could return the surface from the function and use exceptions to report errors - since that should be an exceptional occurrence:


SDL_Surface* init_sdl_thing( )
{
if( (SDL_INIT(SDL_INIT_EVERYTHING)==-1)||(screen=NULL) )
{
throw Error( "Blahblaherrorblah" );
}

// I'm assuming you forgot this in your code
SDL_Surface* screen = SDL_SetVideoMode( ... );

if( !screen )
{
throw Error( "Blahblaherrorblah" );
}

return screen;
}



HTH

Share this post


Link to post
Share on other sites
Quote:
Everything I´ve studied about C++ programming tells me that I should follow the structured programming method (one entry, one exit)...

I break that "rule" all the time. It leads to needlessly convoluted code, in my experience. I prefer to have redundant return statements rather that unnecessary internal scoping logic just to maintain a single exit point.

int init_sdl_thing(SDL_Surface *screensurface)

should be:
int init_sdl_thing(SDL_Surface *&screensurface)

This ensures that your routine can modify the pointer you pass into it (in straight C, you'd have to introduce an additional level of indirection), rather than a copy of the pointer. Yes, it's confusing.

Share this post


Link to post
Share on other sites
if( (SDL_INIT(SDL_INIT_EVERYTHING)==-1)||(screen=NULL) )
This is probably your problem.
It should be:
if( (SDL_INIT(SDL_INIT_EVERYTHING)==-1)||(screen==NULL) )

Share this post


Link to post
Share on other sites
I see there´s a lot for me to study in this thread!. Though I think I understand the passing by references, values and such I´m not sure I fully understand what should really be going on in that function... Just a bit more of study and reading the documentation, I guess!!.

I´ll try understand the replies given before implementing them... I thint that the last pharagraph from Oluseyi might help me understand it. I´d like still to consider Simian Man´s second possibilit: I can read through it and can grasp the differences. It seems a bit clearer to me as I´d have no problem with something like:

SDL_Surface *screen=init_sdl_thing( )

But, isn´t the function missing a declaration for screen or am I missing something again?. Finally, thanks for the point CrazedBrit, I completely missed that one... So far it doesn´t cause any problems but I´ll correct it...

Oh, also I almost forgot, how do I use the code tags in the forums?. I´d like to help people reading my code.

Thanks a lot people, your replies are very helpful.

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