Page 1 of 1

Understanding code

Posted: Mon Jun 08, 2009 8:55 pm
by h0ts0up
I'm nub at coding and I thought it would be better to understand what it is I'm writing rather than just memorizing steps and taking steps from tutorials. As most of you already know Lazy Foo's popular SDL gaming tutorial. I've understood most of code except for a function that I just can't seem to wrap my head around. He calls a function:
SDL_Surface *load_image(string filename)
From what I know, I'm guessing this is a pointer to another function that takes arg filename and returns a surface. I did some research and also found out that a candidate for the function it's actually pointing to is SDL_LoadObject. What I don't seem to understand is, how does the compiler know it's actually pointing to that function without that function ever being called in the code?

Re: Understanding code

Posted: Mon Jun 08, 2009 9:32 pm
by hurstshifter
h0ts0up wrote:I'm nub at coding and I thought it would be better to understand what it is I'm writing rather than just memorizing steps and taking steps from tutorials. As most of you already know Lazy Foo's popular SDL gaming tutorial. I've understood most of code except for a function that I just can't seem to wrap my head around. He calls a function:
SDL_Surface *load_image(string filename)
From what I know, I'm guessing this is a pointer to another function that takes arg filename and returns a surface. I did some research and also found out that a candidate for the function it's actually pointing to is SDL_LoadObject. What I don't seem to understand is, how does the compiler know it's actually pointing to that function without that function ever being called in the code?

LazyFoo tutorials sometimes (and often) do not show the full context of the source right in the tutorials. Makes sure you download the code and read it through before implementing it (this helps me a lot). Here is the full source of that function...

Code: Select all

SDL_Surface *load_image( std::string filename ) 
{
 //The image that's loaded 
SDL_Surface* loadedImage = NULL; 

//The optimized image that will be used 
SDL_Surface* optimizedImage = NULL; 

//Load the image 
loadedImage = IMG_Load( filename.c_str() );

 //If the image loaded 
if( loadedImage != NULL )
 { 
//Create an optimized image
 optimizedImage = SDL_DisplayFormat( loadedImage ); 

//Free the old image 
SDL_FreeSurface( loadedImage ); 
} 

//Return the optimized image
 return optimizedImage;
 } 
So basically, this function takes the std::string filename of the image you want to load and uses it as an argument for IMG_Load (found in the SDL_Image library). Hope this helps

Re: Understanding code

Posted: Tue Jun 09, 2009 2:42 am
by Bakkon
h0ts0up wrote: SDL_Surface *load_image(string filename)
From what I know, I'm guessing this is a pointer to another function that takes arg filename and returns a surface. I did some research and also found out that a candidate for the function it's actually pointing to is SDL_LoadObject. What I don't seem to understand is, how does the compiler know it's actually pointing to that function without that function ever being called in the code?
People differ on how they write these, but I typically write them like this to avoid the same confusion you're having:
SDL_Surface* load_image(string filename)

This is a function named "load_image" that returns an SDL_Surface pointer. Hope that helps. :)

Re: Understanding code

Posted: Tue Jun 09, 2009 3:20 pm
by dandymcgee
Bakkon wrote: People differ on how they write these, but I typically write them like this to avoid the same confusion you're having:
SDL_Surface* load_image(string filename)

This is a function named "load_image" that returns an SDL_Surface pointer. Hope that helps. :)
I've been writing it that way so long, that I had forgotten you could do it any other way.. I actually read his post and went "Wtf? A pointer to a function that returns a surface?". :lol:

Re: Understanding code

Posted: Tue Jun 09, 2009 8:06 pm
by MarauderIIC
Declarations of pointers to functions look like this:

returnType (*functionName) (argumentType1, argumentType2, ...)

Calling them looks like this:

(*functionName) (arg1, arg2, ...)