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

Author Topic: [thor] Particle System in class  (Read 2294 times)

0 Members and 1 Guest are viewing this topic.

emayo

  • Newbie
  • *
  • Posts: 3
    • View Profile
[thor] Particle System in class
« on: January 06, 2013, 12:37:59 am »
Hello
I tried to refactor simple code in order to use classes. When the all code was in main function, everything worked. Now it does comile, but it doesn't show particle. I don't know where's a problem but, I think it's related to texture loading. Can any one help me?

Here's code: http://pastebin.com/xs9NCRwB
I'm using sfml2 and thor2.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [thor] Particle System in class
« Reply #1 on: January 06, 2013, 02:20:46 am »
You never check the return value of sf::Texture::loadFromFile(), are you sure the texture is loaded correctly? Apart from that, I don't see an issue immediately. It sounds stupid, since you already did it, but try to reduce your code a little bit (or expand the one with a single main() step-wise). The debugger may also help you.

By the way, you don't need to keep the shared pointers (texture and emitter) as member variables, if you don't access them after passing them to the particle system.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

emayo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [thor] Particle System in class
« Reply #2 on: January 06, 2013, 02:04:49 pm »
Here's code in main: http://pastebin.com/FiCU6iQG. Working without a problem.

Texture is loading correctly, Player.texture and Player.System.mTexture have the same address, but after debuging in class-code I found out that System.mTextureRect has width and height set to 0 unlike in main-code.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [thor] Particle System in class
« Reply #3 on: January 06, 2013, 05:24:57 pm »
Thanks for debugging. Now I see the problem.

The constructor
thor::ParticleSystem::ParticleSystem(std::shared_ptr<const sf::Texture> texture)
looks at the size of the texture and stores it. If the texture is changed later, thor::ParticleSystem won't recognize the modification and keeps the old size -- in your case (0,0).

Maybe I should document this. To change the behavior, I could poll the texture size (or rect) each frame. But this conflicts with the second constructor
thor::ParticleSystem::ParticleSystem(std::shared_ptr<const sf::Texture> texture, const sf::IntRect& textureRect)
which explicitly stores the specified texture rect. The alternative would be to provide a setTexture() option, but I actually wanted to set the texture in stone at initialization and leave it for the rest of the lifetime. It looks like the way to go, but I'm sure if I don't enforce to pass it in the constructor, people will forget to pass a texture again and again. However, they should already know from sf::Sprite ;)

shared_ptr is misleading at this place in Thor, it suggests that you can alter the texture after passing it to the particle system. I don't know whether it's a good idea to use it here anyway. The user needn't care about ownership anymore, but he is forced to use also shared_ptr in his code (even if it's only a function that sets the shared pointer to an automatic variable). And since he must own the textures for SFML's sprite anyway, I could handle particle systems similarly, i.e. the constructor may in future take:
thor::ParticleSystem::ParticleSystem(const sf::Texture& texture);
I still have to think about this, it's also a point on my progress list.

For the moment, you have to initialize the texture before passing it to the particle system. You can do it like this:
std::shared_ptr<sf::Texture> createTexture()
{
    // instead of
    // std::shared_ptr<sf::Texture> texture(new sf::Texture());
    auto texture = std::make_shared<sf::Texture>();
    if (!texture->loadFromFile("particle.png"))
    {
        // error
    }

    return texture;
}

Particle::Particle()
: emitter(thor::UniversalEmitter::create())
, texture(createTexture())
, system(texture)
, fader(0.1f, 0.1f)
{
    ...
}

But I have to do something in the long term. Thanks for pointing this out!
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

emayo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [thor] Particle System in class
« Reply #4 on: January 06, 2013, 07:21:41 pm »
Thanks for help. Now it's working  :)