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

Author Topic: [Solved] Split image in multiple sprites  (Read 4411 times)

0 Members and 1 Guest are viewing this topic.

Sakurazaki

  • Newbie
  • *
  • Posts: 31
  • `お嬢様を守ります!
    • MSN Messenger - sakurazaki.setsunita@gmail.com
    • View Profile
[Solved] Split image in multiple sprites
« on: May 24, 2013, 09:17:17 pm »
Hi!

I've this code to split one image in multiple sprites.

http://ideone.com/Q7oXkm


I need to know if this is the best way, or if I'm doing it right.

Thanks!
« Last Edit: May 24, 2013, 11:53:22 pm by Sakurazaki »
こんな私も変われるのならもし変われるのなら白になる

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Split image in multiple sprites
« Reply #1 on: May 24, 2013, 09:57:43 pm »
You're doing it totally wrong ;)

1. The sprites refers to a sf::Texture which no longer exists after the function ends. It won't be able to show anything from this texture. Please read the tutorial, this issue is clearly described.

2. You don't need to create one texture for each area that you want to assign to a sprite. Just use the textureRect property of sf::Sprite, and use a single big texture for all the sprites.
Laurent Gomila - SFML developer

Sakurazaki

  • Newbie
  • *
  • Posts: 31
  • `お嬢様を守ります!
    • MSN Messenger - sakurazaki.setsunita@gmail.com
    • View Profile
Re: Split image in multiple sprites
« Reply #2 on: May 24, 2013, 10:15:22 pm »
Well, I just read this tutorial:

http://www.sfml-dev.org/tutorials/2.0/graphics-sprite.php

So what i basically have to do, is in these steps:

load texture from file.
to my sprite call sprite.settexture(texture)
and then call setTextureRect to cut the texture to only what i want to show.

Is this right?
こんな私も変われるのならもし変われるのなら白になる

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Split image in multiple sprites
« Reply #3 on: May 24, 2013, 10:44:12 pm »
Yes. And make sure to keep the sf::Texture instance alive as long as it is used by the sprites.
Laurent Gomila - SFML developer

Sakurazaki

  • Newbie
  • *
  • Posts: 31
  • `お嬢様を守ります!
    • MSN Messenger - sakurazaki.setsunita@gmail.com
    • View Profile
Re: Split image in multiple sprites
« Reply #4 on: May 24, 2013, 11:52:52 pm »
Okay, thank you very much for help :)

Lets change the topic to solved
こんな私も変われるのならもし変われるのなら白になる

 

anything