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

Author Topic: how to create and manage 20 sprites  (Read 6330 times)

0 Members and 1 Guest are viewing this topic.

paulhaggo

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
how to create and manage 20 sprites
« on: November 10, 2014, 09:41:41 pm »
Hello simple newbie question here

If you were making the game pacman

There are say 20 badguys (ghosts)

Should i be creating 20 different sprites

    sf::Texture txBadPlayer;
    if (!txBadPlayer.loadFromFile("badplayer.png"))
    {
       return 0;
    }
    sf::Sprite spBadPlayer;
    spBadPlayer.setTexture(txBadPlayer);

 sf::Texture txBadPlayer2;
    if (!txBadPlayer2.loadFromFile("badplayer.png"))
    {
       return 0;
    }
    sf::Sprite spBadPlayer2;
    spBadPlayer2.setTexture(txBadPlayer2);
 

times 20

or should i use arrays or something ?

ps.. all bad guys have the same picture, but different movement

please could someone point me in the right direction
thanks
« Last Edit: November 10, 2014, 09:58:46 pm by Laurent »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: would you ?
« Reply #1 on: November 10, 2014, 09:53:56 pm »
There is definitely no reason whatsoever to load the same image into multiple textures.  Use one texture and let the sprites share it.

You will probably want 20 sprites if you are going to draw 20 enemies on the screen.  It's just easier that way.

There's also no real reason not to put them in a std::vector.  That also makes a lot of things easier when you want to manipulate that set of enemies.

    const int ENEMY_COUNT = 20;

    ...

    sf::Texture enemyTexture;
    if (!enemyTexture.loadFromFile("badplayer.png")) {
       std::cerr << "Failed to load badplayer.png" << std::endl;
       return 0;
    }

    std::vector<sf::Sprite> enemies;
    for(int i = 0; i < ENEMY_COUNT; ++i) {
        sf::Sprite enemy;
        enemy.setTexture(enemyTexture);
        enemies.push_back(enemy);
    }
 

But the part about whether you store your objects in an array or a vector or don't bother with containers at all is basic C++ stuff, not in any way unique to SFML.  If you aren't comfortable with C++ containers, you should read a good book about the C++ language itself before working with libraries written in C++.
« Last Edit: November 10, 2014, 10:01:12 pm by Ixrec »

paulhaggo

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: how to create and manage 20 sprites
« Reply #2 on: November 10, 2014, 10:03:02 pm »
i really had no idea a standard c++ std::vector could hold a sfml sprites object

thank you for your help

going off your code

sf::Sprite enemy;

how would i set the postion of sprite 4

say

enemy[3].setpostion(x,y);

??

thanks again




Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: how to create and manage 20 sprites
« Reply #3 on: November 10, 2014, 10:08:54 pm »
Nobody's going to teach you the C++ language here. I strongly advise you to read a good C++ book or tutorial. Guessing and asking here for every single thing that you don't know is not going to lead you anywhere... Do you a favor and learn C++ first ;)
Laurent Gomila - SFML developer

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: how to create and manage 20 sprites
« Reply #4 on: November 10, 2014, 10:09:36 pm »
Yes, that's exactly how you would do it.

A std::vector, like all the other containers, can hold any type, because it's a templated class.

Again, this is need-to-know C++ stuff.  You need to learn the rest of C++ before working with SFML.

paulhaggo

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: how to create and manage 20 sprites
« Reply #5 on: November 10, 2014, 10:21:34 pm »
ok thanks Ixrec

i come from vb6 and php so, some of these c++ things are a bit strange, getting a handle thou

i am reading books also, i am just having a play around with sfml


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: how to create and manage 20 sprites
« Reply #6 on: November 10, 2014, 10:35:54 pm »
i am reading books also, i am just having a play around with sfml
You should learn C++ before dealing with SFML.

See also:
Is using SFML a good way to learn to program (in C++)?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: how to create and manage 20 sprites
« Reply #7 on: November 12, 2014, 05:04:42 am »
I completely agree as I am new to sfml but understand much of it. It is greatly advised by me to learn it. Good luck

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: how to create and manage 20 sprites
« Reply #8 on: November 12, 2014, 02:23:48 pm »
@OP, don't worry, I've been told the same thing not too long ago. I recently solved a similar question of my own on a c++ forum. The 7th post on http://www.cplusplus.com/forum/beginner/147404/ will show an example of how to manage multiple objects of any type. Feel free to pm for clarification or make any suggestions if it's not a good model.