Understanding code

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
h0ts0up
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Fri Jun 05, 2009 6:17 pm

Understanding code

Post 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?
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Understanding code

Post 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
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Understanding code

Post 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. :)
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Understanding code

Post 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:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Understanding code

Post by MarauderIIC »

Declarations of pointers to functions look like this:

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

Calling them looks like this:

(*functionName) (arg1, arg2, ...)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply