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

Author Topic: Better way to draw same sprite multiple times  (Read 16708 times)

0 Members and 1 Guest are viewing this topic.

joaosausen

  • Newbie
  • *
  • Posts: 10
    • View Profile
Better way to draw same sprite multiple times
« on: March 14, 2012, 07:54:26 pm »
Pretty simple.
Like in terraria, the same image is printed multiple times to build the map.
Do i need to Draw the sprite, change position, draw again multiple times them App.Display?

Thanks in advance

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Better way to draw same sprite multiple times
« Reply #1 on: March 14, 2012, 08:04:04 pm »
No, you do not need to. You can have multiple sf::Sprites constructed from the same sf::Image, and draw all of them.

Sprites are very light, don't worry about performance when using them :)


However, if it's actually simpler for you to move the sprite you can draw it multiple times, but I don't think that's what you want to do in your case.

joaosausen

  • Newbie
  • *
  • Posts: 10
    • View Profile
Better way to draw same sprite multiple times
« Reply #2 on: March 14, 2012, 09:52:46 pm »
I will use multiple Sprites.

Thank you for your help.

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Better way to draw same sprite multiple times
« Reply #3 on: March 14, 2012, 11:08:59 pm »
I know from my own experience and testing that using the same sprite to draw multiple objects is less effecient because you're using more calls to say,
 set Position, set Scale, set Rotation, and 'set' many other properties prior to the actual Draw. It's all extra crap that isn't entirely necessary. It looks like an OK thing to do until you're drawing thousands of objects to the screen. At this point the CPU cycles start to build up.

Using multiple sf::Sprites in a container might use a bit more memory, but all of their properties are contained right there in the sf::Sprite instance. Less work, particularly in a tight loop.

It doesn't always matter though. Sometimes if a class is using the same image/texture and isn't drawing that much then using the same sprite is ok.

It's a bit of a trade-off.
{much better code}

joaosausen

  • Newbie
  • *
  • Posts: 10
    • View Profile
Better way to draw same sprite multiple times
« Reply #4 on: March 14, 2012, 11:51:29 pm »
I'm in troubles right now..
i want to create a pacman game, so i want to create a lot of sprites, on sprite for every 20x20 pixels square.
I want to something like this

Code: [Select]

int x,y;
for (x=0;x<10;x++) {
    for (y=0;y<10;y++) {
      sf::Sprite NAMEUSINGXANDY(image);
    }
}


lets say i want to name "NAMEUSINGXANDY" has x0y0, since x will be 0 and y will be 0 too in thr first loop.

How can I do that? Or there is a better way to do that? (i believe there is a better way)


Thank you in advance.

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Better way to draw same sprite multiple times
« Reply #5 on: March 15, 2012, 12:00:41 am »
Don't do that :o

What you want to do is make an array/matrix of sf::Sprite.

Use either a
Code: [Select]
std::vector<sf::Sprite>
and map them with id = x+LINE_SIZE*y or a
Code: [Select]
std::vector< std::vector<sf::Sprite> >

This way to can do:

Code: [Select]
sf::Sprite s = myMatrix[x][y];