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

Author Topic: Sprites into an array  (Read 4090 times)

0 Members and 1 Guest are viewing this topic.

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Sprites into an array
« 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???
« Last Edit: December 28, 2012, 02:39:56 pm by makerimages »
Makerimages-It`s in the pixel

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Sprites into an array
« Reply #1 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. 
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Sprites into an array
« Reply #2 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Sprites into an array
« Reply #3 on: December 29, 2012, 03:21:45 am »
If your compiler supports it, you can use std::array

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

  // Draw all your sprites
  for (unsigned int i = 0; i < mySprites.size(); ++i)
      window.draw (mySprites[i]);
 
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Re: Sprites into an array
« Reply #4 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...)
Makerimages-It`s in the pixel

 

anything