SDL Surface Array

David Olofson wrote:

It is because I want an integer to decide which image is
shown. And
I can’t really find any other way to do this. I’ve tried
SDL_Surface array[8]; but I get a lot of errors.

There are many ways of doing that actually, but the simplest
one is
indeed to use arrays. However, since SDL_Surface structs are
created
(ie malloc()ed and filled in) by SDL, you shouldn’t use
arrays of
them. That would require copying structs around, and you’d
have to
free() the SDL_Surface struct you get from SDL. Even though
that part
might actually work, it may well screw up SDL’s internals.
Not sure
about that, but generally, you should never second-guess
libraries
that want to manage their own objects…

So, use arrays of pointers to SDL_Surfaces instead.

Okay, I’ve read about pointers and understand them.
But I still don’t understand how I should use them here.
Because, the pointers needs a type, don’t they?
Please, could I get a short example?
Do you mean that I should have an array like this:
SDL_Surface *surface1;
surface1 = IMG_Load(“picture1.png”);
?type? *pointer1;
pointer1 = &surface1;
array[0] = *pointer1;

I really hope you don’t consider this as off-topic because I really need this help and don’t know where I’m supposed to get it, if not here.

Christian> On Friday 20 February 2004 18.52, Br?nnum-Hansen wrote:

//David Olofson - Programmer, Composer, Open Source
Advocate

.- Audiality
-----------------------------------------------.
| Free/Open Source audio engine for games and multimedia.
|
| MIDI, modular synthesis, real time effects, scripting,…
|
`-----------------------------------> http://audiality.org
-’
http://olofson.nethttp://www.reologica.se


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

[…]

So, use arrays of pointers to SDL_Surfaces instead.

Okay, I’ve read about pointers and understand them.
But I still don’t understand how I should use them here.
Because, the pointers needs a type, don’t they?

A pointer has a type; it’s a pointer. Actually, it’s also a pointer
to something; in this case a pointer to an SDL_Surface struct - or
"SDL_Surface *", expressed in C/C++.

Please, could I get a short example?

//The surfaces
SDL_Surface *surfaces[8];

//Load graphics
surfaces[0] = IMG_Load(“picture1.png”);
surfaces[1] = IMG_Load(“picture2.png”);
surfaces[2] = IMG_Load(“picture3.png”);

surfaces[7] = IMG_Load(“picture8.png”);
//(Add error checking!)

//Do kewl stuff

//Free surfaces
for(int i = 0; i < 8; ++i)
SDL_FreeSurface(surfaces[i]);

[…]

I really hope you don’t consider this as off-topic because I really
need this help and don’t know where I’m supposed to get it, if not
here.

It is off-topic, as it’s not specific to SDL in any way whatsoever.
We’re talking about very basic C/C++ stuff. If you’ve read something
that actually explains pointers in a comprehensible way, this stuff
should be quite obvious.

Google comes up with countless hits on books and stuff about C and C++
for beginners, and I guess there are a few on-line resources as well,
though I didn’t find one right away. I’d recommend getting one of
those books anyway, though I don’t know which ones are best. (The one
I learnt C from was in swedish, and has no translations AFAIK. Not
that far form danish, but you have to be pretty much fluent in the
language for this kind of litterature.)

//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Saturday 21 February 2004 19.18, Br?nnum-Hansen wrote:

You can skip the pointer1 stuff and do something like this instead:

SDL_Surface *pSurfaces[MAX_SURFS];

pSurfaces[0] = IMG_Load(“picture1.png”);
pSurfaces[1] = IMG_Load(“picture2.png”);

That would load picture1.png into the first element of the array, and
picture2.png into the second element of the array, etc.

If you wanted to load a whole bunch of images, then you’d need a for
loop or a while loop. Now, if all of your pictures all start with
"picture" and are all PNGs, you can do something like this:

SDL_Surface *pSurfaces[MAX_SURFS];
Char szImgFile[255];

// this goes in the loading function, or wherever you choose to put it
for (int i = 0; i < MAX_SURFS; ++i)
{
// clear out the file name for the current image
memcpy(szImgFile, ‘\0’, 255); // this fills all 255 characters
in the // file name with the
null-terminating
// character.

// build the file name to the current image
strcpy(szImgFile, "picture");
strcat(szImgFile, atof(i+1)); // concatenate the character

returned by // atof()
strcat(szImgFile, “.png”); // add the .png to the end of
the name

// load the image into the current surface
pSurfaces[i] = IMG_Load(szImgFile);

}

And when it comes time to free the surfaces from memory (ie. When you’re
program is shutting down), you can use a for loop again:

for (int i = 0; i < MAX_SURFS; ++i)
{
SDL_FreeSurface(pSurfaces[i]);
}

That’s one possible way of doing it.
Hope that helps a bit.

Kevin> ----- Original Message -----

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org] On Behalf Of
Br?nnum-Hansen
Sent: Saturday, February 21, 2004 2:18 PM
To: sdl at libsdl.org
Subject: Re: [SDL] SDL Surface Array

Okay, I’ve read about pointers and understand them.
But I still don’t understand how I should use them here.
Because, the pointers needs a type, don’t they?
Please, could I get a short example?
Do you mean that I should have an array like this:
SDL_Surface *surface1;
surface1 = IMG_Load(“picture1.png”);
?type? *pointer1;
pointer1 = &surface1;
array[0] = *pointer1;

I really hope you don’t consider this as off-topic because I really need
this help and don’t know where I’m supposed to get it, if not here.

Christian

//David Olofson - Programmer, Composer, Open Source
Advocate

.- Audiality
-----------------------------------------------.
| Free/Open Source audio engine for games and multimedia.
|
| MIDI, modular synthesis, real time effects, scripting,…
|
`-----------------------------------> http://audiality.org
-’
http://olofson.nethttp://www.reologica.se


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

David Olofson wrote:

[…]

So, use arrays of pointers to SDL_Surfaces instead.

Okay, I’ve read about pointers and understand them.
But I still don’t understand how I should use them here.
Because, the pointers needs a type, don’t they?

A pointer has a type; it’s a pointer. Actually, it’s also
a pointer
to something; in this case a pointer to an SDL_Surface
struct - or
"SDL_Surface *", expressed in C/C++.

Please, could I get a short example?

//The surfaces
SDL_Surface *surfaces[8];

//Load graphics
surfaces[0] = IMG_Load(“picture1.png”);
surfaces[1] = IMG_Load(“picture2.png”);
surfaces[2] = IMG_Load(“picture3.png”);

surfaces[7] = IMG_Load(“picture8.png”);
//(Add error checking!)

//Do kewl stuff

//Free surfaces
for(int i = 0; i < 8; ++i)
SDL_FreeSurface(surfaces[i]);

[…]

I really hope you don’t consider this as off-topic because
I really
need this help and don’t know where I’m supposed to get
it, if not
here.

It is off-topic, as it’s not specific to SDL in any way
whatsoever.
We’re talking about very basic C/C++ stuff. If you’ve read
something
that actually explains pointers in a comprehensible way,
this stuff
should be quite obvious.

Google comes up with countless hits on books and stuff about
C and C++
for beginners, and I guess there are a few on-line resources
as well,
though I didn’t find one right away. I’d recommend getting
one of
those books anyway, though I don’t know which ones are best.
(The one
I learnt C from was in swedish, and has no translations
AFAIK. Not
that far form danish, but you have to be pretty much
fluent in the
language for this kind of litterature.)

Yes it is. And it was actually what I did before asking here. But I made an error which gave me a lot of errors when compiling which made me think that I had done something wrong. It was just a stupid error.
I apologize…

Thank you for your help.> On Saturday 21 February 2004 19.18, Br?nnum-Hansen wrote:

//David Olofson - Programmer, Composer, Open Source
Advocate

.- Audiality
-----------------------------------------------.
| Free/Open Source audio engine for games and multimedia.
|
| MIDI, modular synthesis, real time effects, scripting,…
|
`-----------------------------------> http://audiality.org
-’
http://olofson.nethttp://www.reologica.se


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

“Kevin” wrote:

memcpy(szImgFile, ‘\0’, 255);
strcpy(szImgFile, “picture”);
strcat(szImgFile, atof(i+1));
strcat(szImgFile, “.png”);

sprintf(szImgFile, “picture%i.png”, i);

That works too. :)> ----- Original Message -----

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org] On Behalf Of
Clemens Kirchgatterer
Sent: Sunday, February 22, 2004 5:32 AM
To: sdl at libsdl.org
Subject: Re: [SDL] SDL Surface Array

“Kevin” wrote:

memcpy(szImgFile, '\0', 255);
  strcpy(szImgFile, "picture");
strcat(szImgFile, atof(i+1));
strcat(szImgFile, ".png");

sprintf(szImgFile, “picture%i.png”, i);


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl