SFML community forums

Help => Graphics => Topic started by: makerimages on December 28, 2012, 12:59:07 pm

Title: Sprites into an array
Post by: makerimages on December 28, 2012, 12:59:07 pm
Hello, how can I load a bunch of sprites into a array in a separate file (should it be .h or .cpp), so that I can later display them  (and set as a variable) in a different file by using the index??? 


Edit: could I make the sprites before making an array and then load either a pointer to those or those directly to an array???
Title: Re: Sprites into an array
Post by: masskiller on December 28, 2012, 09:36:19 pm
What I do is use a vector (if I am sure I won't be changing it's size many times) with it's reserve function and push_back a copy of a clean sf::Sprite in the constructor. Then I just access each sprite with a load function (when all sprites use the same texture) or you could just program the operator[] in order to have access to each sprite, but not the vector itself. It's pretty simple.

That's my approach until I finish writing and testing my high performance sprite for the usage of a single texture per sprite container. 
Title: Re: Sprites into an array
Post by: eXpl0it3r on December 28, 2012, 10:14:03 pm
Hello, how can I load a bunch of sprites into a array in a separate file (should it be .h or .cpp), so that I can later display them  (and set as a variable) in a different file by using the index???
It seems you know very little about C++, it's not a bad thing, but it's not a good prerequisite for using SFML and programming a game in C++.
What you should do is get a book or some freely available resource and start learning, what header and source files are and what they are used for, how arrays do work and why one should mostly prefer a std::vector over pure array, etc.
Those are some very basic topics and are essential for programming with C++.

Edit: could I make the sprites before making an array and then load either a pointer to those or those directly to an array???
Don't use raw pointers and for your case they are quite useless, the way masskiller described is way better.
Title: Re: Sprites into an array
Post by: mateandmetal on December 29, 2012, 03:21:45 am
If your compiler supports it, you can use std::array (http://en.cppreference.com/w/cpp/container/array)

  std::array <sf::Sprite, 10> mySprites;

  // Draw all your sprites
  for (unsigned int i = 0; i < mySprites.size(); ++i)
      window.draw (mySprites[i]);
 
Title: Re: Sprites into an array
Post by: makerimages on December 29, 2012, 09:45:24 am
Nevermind, Ill ditch the array approach, I dont even know anymore why I wanted it... (Ill have a project topic created soon for a game I`m working on...)