SFML community forums
Bindings - other languages => C => Topic started by: BIOS on February 26, 2018, 09:51:15 pm
-
Hello, Im wondering if we could create a double array of sprite.
I tried something like this:
sfSprite *sprite[3];
sprite = malloc(sizeof (sfSprite *) * 3);
sprite[0] = my_sprite create("Document/image/image.png");
sprite[1] = my_sprite create("Document/image/image.png");
sprite[2] = my_sprite create("Document/image/image.png");
sfRenderWindow_drawSprite(window, sprite[0], NULL);
I tried a lot of otherways but it don't work. I would like to know if it's possible.
-
What's a double array?
-
What's a double array?
I mean by a double array a sfSprite **
Same as char **
Inside a char ** you can store char *
I want to do the same store multiple sfSprite * in a sfSprite **. I would like to know if their is a way to store multiple sprite so i can access them more easly.
Envoyé de mon iPhone en utilisant Tapatalk
-
So a two dimensional array. It's not different to any other 2D array, so basic C. I have no idea how that is usually done in C, so maybe look it up somewhere.
-
A sfSprite* is usually one sprite (because CSFML internally allocates instances dynamically, and therefore returns/takes pointers). So sfSprite** would be a 1-dimensional array. A 2-dimensional array would be sfSprite***. Figure out this stuff an you'll have half of the problem solved. To solve the other half, you'll have to tell us what you want to do, not with C types but with real words, so that it is not ambiguous ;)
-
A sfSprite* is usually one sprite (because CSFML internally allocates instances dynamically, and therefore returns/takes pointers). So sfSprite** would be a 1-dimensional array. A 2-dimensional array would be sfSprite***. Figure out this stuff an you'll have half of the problem solved. To solve the other half, you'll have to tell us what you want to do, not with C types but with real words, so that it is not ambiguous ;)
Thank you for replying me first. What i want to do is a burger maker gamer. Basically when the player press a button for example a steak appears on the screen. So when the player press the button i create a new sprite give it a texture and display it. But if he press the button again a new sprite is create on the top of the old one. So to store all those sprite I need to store them somewhere in a array of sprites. So i can access to my sprites
Envoyé de mon iPhone en utilisant Tapatalk
-
So you just want a classic 1D array.
sfSprite
** sprites
;sprites
= malloc(3 * sizeof(sfSprite
*));sprites
[0] = ...
;sprites
[1] = ...
;sprites
[2] = ...
;
Note that this is basic C stuff, so maybe you should spend more time learning C before trying to do more complicated things.
-
So you just want a classic 1D array.
sfSprite
** sprites
;sprites
= malloc(3 * sizeof(sfSprite
*));sprites
[0] = ...
;sprites
[1] = ...
;sprites
[2] = ...
;
Note that this is basic C stuff, so maybe you should spend more time learning C before trying to do more complicated things.
I know it just basic stuff in C. But in basic C we just use pointer of char, void and int and the sfSprite type do not exist. I know that the sfSprite is a structure create for sfml and I thought It would be different. And I already try that. Check my first message I show you what I have tried. I have try exactly the same thing but it don't work.
Sorry it's been a year that im learning c.
-
So you just want a classic 1D array.
sfSprite
** sprites
;sprites
= malloc(3 * sizeof(sfSprite
*));sprites
[0] = ...
;sprites
[1] = ...
;sprites
[2] = ...
;
Note that this is basic C stuff, so maybe you should spend more time learning C before trying to do more complicated things.
I know it just basic stuff in C. But in basic C we just use pointer of char, void and int and the sfSprite type do not exist. I know that the sfSprite is a structure create for sfml and I thought It would be different. And I already try that. Check my first message I show you what I have tried. I have try exactly the same thing but it don't work.
Sorry it's been a year that im learning c.
Found the solution thank you everyone. I just changed my malloc from:
malloc(sizeof(sfSprite) * n); to
malloc(sizeof(*sprite) * n)
And it work now
Envoyé de mon iPhone en utilisant Tapatalk
-
This is exactly equivalent to what I posted, which is not what you tried before. Please read the code carefully ;)