Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Create a double array of sfSprite  (Read 13167 times)

0 Members and 4 Guests are viewing this topic.

BIOS

  • Newbie
  • *
  • Posts: 7
    • View Profile
Create a double array of sfSprite
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Create a double array of sfSprite
« Reply #1 on: February 26, 2018, 10:41:35 pm »
What's a double array?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BIOS

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Create a double array of sfSprite
« Reply #2 on: February 27, 2018, 11:45:16 am »
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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Create a double array of sfSprite
« Reply #3 on: February 27, 2018, 12:04:46 pm »
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create a double array of sfSprite
« Reply #4 on: February 27, 2018, 12:44:10 pm »
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 ;)
Laurent Gomila - SFML developer

BIOS

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Create a double array of sfSprite
« Reply #5 on: February 27, 2018, 12:53:15 pm »
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create a double array of sfSprite
« Reply #6 on: February 27, 2018, 01:03:49 pm »
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.
Laurent Gomila - SFML developer

BIOS

  • Newbie
  • *
  • Posts: 7
    • View Profile
Create a double array of sfSprite
« Reply #7 on: February 27, 2018, 04:40:00 pm »
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.
« Last Edit: February 27, 2018, 04:42:11 pm by BIOS »

BIOS

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Create a double array of sfSprite
« Reply #8 on: February 27, 2018, 04:48:42 pm »
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create a double array of sfSprite
« Reply #9 on: February 27, 2018, 08:36:39 pm »
This is exactly equivalent to what I posted, which is not what you tried before. Please read the code carefully ;)
Laurent Gomila - SFML developer

 

anything