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

Author Topic: Arbitrarily split an image?  (Read 3168 times)

0 Members and 1 Guest are viewing this topic.

stoikheion

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Arbitrarily split an image?
« on: July 01, 2010, 09:22:07 am »
Hello everyone,

I'm trying to "split" a sprite into two separate entities, as if I were to cut across one with a knife. This would be a fairly easy problem if I could allocate new images for each split, but since I want this to happen for several sprites at once, all using the same texture, allocating several pairs of new images seems very silly and inefficient. This is a visual example of what I'm trying to accomplish:

The red line across which the sprite is split could be at any angle (and hopefully in the future multiple lines, like a jagged line), and the two images in the third panel would be separate and independent.

I feel like the use of texture coordinates would simplify what I'm doing, but I haven't gone source-diving into SFML enough to know what modifications I would make, and I wanted to see if there's an easy way to do this that I'm overlooking. I can't create a pixel mask, since that would allocate a new image, but maybe I could make some sort of simple vector mask using sf::Shape?

If anyone could provide any general suggestions, references, or hints, that would be fantastic. Thanks in advance!

Recruit0

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Arbitrarily split an image?
« Reply #1 on: July 02, 2010, 04:53:17 pm »
You could create a temp image that's blank, then copy whatever the sliced parts are separately to their appropriate locations. Then set the image equal to the temp. I'm not sure SFML has this capability built in though.

I ran through ideas of doing it without extra images but they're more likely to be less efficient (e.g. store (x,y) coordinates for each pixel, then you could pixel plot them) At best, you'd have to use temp images.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Arbitrarily split an image?
« Reply #2 on: July 02, 2010, 04:56:35 pm »
I think using OpenGL is the best solution. With custom geometry and texture coordinates, you can easily achieve this kind of effects.
Laurent Gomila - SFML developer

stoikheion

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Arbitrarily split an image?
« Reply #3 on: July 03, 2010, 12:45:45 am »
Thanks for the tip! Do you think it makes the most sense to look at implementation for sf::Sprite::SetSubRect, and modify the sf::Sprite class directly? This would require recompiling SFML, but it might be easier to access the underlying OpenGL texture data used in sf::Sprite.
Alternatively, should I create a subclass from sf::Sprite in my own code, and manipulate the texture coordinates used in sf::Sprite from there? I don't know if I should consider the possibility of committing this change, if it works well and is usable in a lot of scenarios.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Arbitrarily split an image?
« Reply #4 on: July 03, 2010, 09:07:04 am »
It depends how much you're familiar with OpenGL, and how you want to integrate this feature inside your application.

I would personally write a new class from scratch, but inheriting from/modifying sf::Sprite can also be a good solution for you.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Arbitrarily split an image?
« Reply #5 on: July 03, 2010, 01:02:34 pm »
I wouldn't modify sf::Sprite directly, since that makes it impossible to give your code to others (if you once plan to do so). I would try to use sf::Sprite as a member/base class of an own class, and if that's too complicated, you can still copy the sf::Sprite code (consider the license, if you don't need the code only for yourself).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything