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?
Understanding code
Moderator: Coders of Rage
- hurstshifter
- 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
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;
}
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
http://www.thenerdnight.com
- Bakkon
- Chaos Rift Junior
- Posts: 384
- Joined: Wed May 20, 2009 2:38 pm
- Programming Language of Choice: C++
- Location: Indiana
Re: Understanding code
People differ on how they write these, but I typically write them like this to avoid the same confusion you're having: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?
SDL_Surface* load_image(string filename)
This is a function named "load_image" that returns an SDL_Surface pointer. Hope that helps.
- dandymcgee
- 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
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?".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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Understanding code
Declarations of pointers to functions look like this:
returnType (*functionName) (argumentType1, argumentType2, ...)
Calling them looks like this:
(*functionName) (arg1, arg2, ...)
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.