Skip to main content
GameDev.net gamedev.net
🔒 Locked

help calling this function

Started by russt17 Mar 27, 2008 at 8:08 PM 3 replies 550+ views
Original Post
russt17
russt17
HI I have this function and I cant figure out how to call it because it isn't working, does anyone know how? SDL_Surface *load_image(const char * filename) { SDL_Surface *image; image = SDL_LoadBMP (filename); image = SDL_DisplayFormat (image);return image; } if the image's name was player1 and the file name was "graphics/playerpic.bmp", how would i use the function?
Amrazek
Amrazek
What kind of errors does the compiler give you? Have you set up SDL properly? Have you included the header and added the SDL library to the linker dependencies?

Your function looks alright, but don't forget to free the surface you've loaded in after converting it to the correct display format. Currently, you load an image A, create a new image B using image A's data which is optimized for the video card, and image A gets lost in memory somewhere.

If you're not already, you can check out http://lazyfoo.net/SDL_tutorials/index.php for lots of great tutorials.
If I put my 2 cents in and get a penny for my thoughts, where does my other penny go?
russt17
russt17
My only problem is that I dont actually know how you use a function. Am I supposed to write

load_image ("graphics/playerpic.bmp");

or

load_player1 ("graphics/playerpic.bmp");

or

SDL_Surface * load_player1 ("graphics/playerpic.bmp");

or

SDL_Surface * load_image ("graphics/playerpic.bmp");

or

player1 = load_image ("graphics/playerpic.bmp");


or something like that I just need to know the correct formatting for it.
Amrazek
Amrazek
Well, it returns a pointer to an SDL_Surface struct so you're going to need a variable to keep track of that, or it just gets lost in memory.
SDL_Surface* mySurface = load_image("graphics//playerpic.bmp");

Don't forget to clean up at some point afterwards using SDL_FreeSurface(mySurface).
If I put my 2 cents in and get a penny for my thoughts, where does my other penny go?
eedok
eedok
well you use it to replace code like this in your program:
	SDL_Surface *spacemanleft1 = SDL_LoadBMP("graphics/spacemanleft1.bmp");	SDL_Surface *spacemanleft2 = SDL_LoadBMP("graphics/spacemanleft2.bmp");

so you make it like this:
	SDL_Surface *spacemanleft1 = load_image("graphics/spacemanleft1.bmp");	SDL_Surface *spacemanleft2 = load_image("graphics/spacemanleft2.bmp");


Which then makes this part of your code unnecessary:
	spacemanleft1 = SDL_DisplayFormat (spacemanleft1);	spacemanleft2 = SDL_DisplayFormat (spacemanleft2);

So you can get rid of it, making it so you have less code to work with.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.