Jump to content
  • Advertisement
Sign in to follow this  
xyuri

Passing SDL_Surface pointer to a function ?

This topic is 4889 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

firstly ... I am aware of the "Alternate Libraries" forum but I think this thread belongs in the Beginners area because of what I am asking ;) anyways ... I have a bunch of SDL_Surface pointer which hold a surface to be drawn, here is one of them:
SDL_Surface* Selection = SDL_LoadBMP("Data\\Selection.bmp");
Now, I want to have the initialisation of all of them in a seperate function, so I modified the code like so:
SDL_Surface* Selection = 0;

void InitSurfaces (SDL_Surface* Selection)
{
  Selection = SDL_LoadBMP("Data\\Selection.bmp");
}
AFAIK that should work, but it doesnt seem to do ANYTHING :( Is there a different way I'm surposed to go about this?

Share this post


Link to post
Share on other sites
Advertisement
I'd do it like this:



SDL_Surface* LoadBitmaps( std::string filename)
{
SDL_Surface* temp = SDL_LoadBMP(filename.c_str());
return temp;
}


Share this post


Link to post
Share on other sites
I have functions similar to that that work perfectly...

so what happens when you run it..the surface is still null? you should check and see if everything went alright...because if they work like the first method..then they should also work with the second..

also you should make the function like ontheheap says..

and then..to check if they went ok..just do a simple



if (LoadBitmaps("Data\\Selection.bmp"))
{
//success
}
else
{
//something bad happened
}

Share this post


Link to post
Share on other sites
What can I say except, errrr, that function works quite well ...

Is there a reason why my original (disfunctional) method doesnt work?

EDIT:

That still doesnt solve my problem though ... I want to pass a surface to a function and have it set the graphics and whatnot :(

Share this post


Link to post
Share on other sites
Quote:
Original post by xyuri
What can I say except, errrr, that function works quite well ...

Is there a reason why my original (disfunctional) method doesnt work?

EDIT:

That still doesnt solve my problem though ... I want to pass a surface to a function and have it set the graphics and whatnot :(


Hrm. Well, I'm not entirely sure what you're trying to accomplish. I'm guessing you want to pass a null SDL_Surface pointer into a function, and have the function load the bmp file? I don't see any reason to do it like that, though. First off, you'de still need to pass a filename to the function so you can pass something to SDL_LoadBMP. My brain seems to be quite broken lately though, so its quite possible that I'm missing your point!

Share this post


Link to post
Share on other sites
Maybe you need to allocate memory to Selection.
In C++: Selection = new SDL_Surface;
In C: Selection = (SDL_Surface*)malloc(sizeof(SDL_Surface)); <- I'm not sure about this.
Also, don't forget to free(Selection) (C) or delete Selection (C++) when you finish using it.

pex.

Share this post


Link to post
Share on other sites
well..with that code you could basically make it into your old function..


void Init_Graphics(SDL_Surface * surf)
{
surf = LoadBitmap("Data\\Selection.bmp");
}


although i don't really see the point in doing that

Share this post


Link to post
Share on other sites
Questions as to why you're doing it this way aside, your problem, if I understand you correctly, is that you are assigning the loaded bmp to the local copy of the pointer, which means you have a leak when the code exits. You need either a reference to the pointer (C++-style):


void InitSurfaces (SDL_Surface*& Selection)
{
Selection = SDL_LoadBMP("Data\\Selection.bmp");
}




or a pointer to the pointer (C-style):


void InitSurfaces (SDL_Surface** Selection)
{
*Selection = SDL_LoadBMP("Data\\Selection.bmp");
}


Share this post


Link to post
Share on other sites
Are you sure it doesn't load the bitmap?
Be sure by if (Selection) (After InitSurfaces) or check for errors (i dont know SDL so i dont know how to check, but i'm sure there is a way).
Edit: Or you can load the bitmap without InitSurfaces and see if now you can see the image in the window.
(And I hope you didn't missed my reply!)

pex.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!