Public Group

Passing SDL_Surface pointer to a function ?

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

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)
{
}
AFAIK that should work, but it doesnt seem to do ANYTHING :( Is there a different way I'm surposed to go about this?

Share on other sites
I'd do it like this:

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

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 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 on other sites
Quote:
 Original post by xyuriWhat 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 on other sites
Maybe you need to allocate memory to Selection.
In C++: Selection = new SDL_Surface;
Also, don't forget to free(Selection) (C) or delete Selection (C++) when you finish using it.

pex.

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 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 on other sites
None of these ways seem to work :( I just get a blank window.

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.

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 14
• Forum Statistics

• Total Topics
632961
• Total Posts
3009491
• Who's Online (See full list)

There are no registered users currently online

×