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

Author Topic: Sizable Sprite Help!  (Read 1873 times)

0 Members and 1 Guest are viewing this topic.

coreymu

  • Guest
Sizable Sprite Help!
« on: October 21, 2015, 05:31:44 pm »
Hi, I'm using JSFML a 2.2 SFML port for Java.

I've been trying to make a class that extends BasicTransformable and implements the Drawable interface.
I am trying to make something similar as a VB.net Panel control.

This class named "Panel" has a Sprite, a RectangleShape, a float for Opacity and many other. So far, I've been able to make a setOpacity method that changes the opacity of the background image (the sprite's texture), change the color of the background color (the RectangleShape's Color) as well as many other features.

I wasn't able to change the size of the Panel since the sprite does not have a setSize() method. (Yes, I know I can use the setScale(target_size/current_size)) but this will not function correctly when the current size is (0,0) because it will be dividing a number by 0.

When I set a texture to a sprite and then use sprite.getGlobalBound.width (or height), I get a value of 0 even when I can visually see the sprite on the screen.

public class Panel() extends... {
        private Vector2f size;
        private Texture texture;
        public Panel() {
                ...
        }

        public void setSize(float x, float y) {
                this.size = new Vector2f(x,y);
                sprite.setScale(this.size.x / sprite.getGlobalBounds().width,  this.size.y / sprite.getGlobalBounds().height);
               
                rectangleShape.setSize(getSize());
        }

        public void setImage(ConstTexture texture) {
                this.texture = (Texture) texture;
                this.texture.setSmooth(true);
                //Not Important.
                //if(optionalShader.isPresent()) {
                        //optionalShader.get().setParameter("texture", this.texture);
                //}
                //Sets a texture to sprite and resizes the sprite. The
                sprite.setTexture(texture, true);
               
                //Stretches the resized sprite to the Panel's size.
                sprite.setScale((float)getSize().x / this.texture.getSize().x,  (float)getSize().y / this.texture.getSize().y);
        }
}

So how should I correctly implement a proper setSize() method on sfml?
I want the texture to always stretch to the size of the panel. so that is why I have a sprite.setScale() at the end of the setImage() method.
PS.I can read c++ code as well.
« Last Edit: October 22, 2015, 04:44:53 am by coreymu »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sizable Sprite Help!
« Reply #1 on: October 21, 2015, 06:17:14 pm »
Yes, I know I can use the setScale(target_size/current_size)) but this will not function correctly when the current size is (0,0) because it will be dividing a number by 0.
Yes, and that makes perfect sense: if the sprite has size (0, 0), you cannot resize it in any way to get a positive size. It will always be empty. However, sprites that are loaded correctly shouldn't be empty.

When I set a texture to a sprite and then use sprite.getGlobalBound.width (or height), I get a value of 0 even when I can visually see the sprite on the screen.
Either you're doing something wrong, or there's a bug in JSFML. Can you provide a minimal complete example? Without the Panel and other unrelated stuff.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

coreymu

  • Guest
Re: Sizable Sprite Help!
« Reply #2 on: October 22, 2015, 04:44:31 am »
Thanks Nexus, after reading your clarification regarding the usage of setScale(), I was able to fix the issue.

While initializing my class, I have set my size to (0,0) and used the setScale with a size of (0,0) so I got (0,0) all the time.

I added a isSizeless boolean and forced the size to change to the texture's size whenever isSizeless was true and made the sprite to stretch when isSizeless was false.

Oh, regarding the issue about getGlobalBound returning a null value, I made a mistake on it. Its not a bug in JSFML.

Lastly, I found out using texture.getSize() correctly changes the size instead of using sprite.getGlobalBounds().
I assume this is probably because getGlobalBounds returns the actual width and height after the scale, but setScale changes the size of the scale based on the original/texture size.
sprite.setScale(getSize().x / this.texture.getSize().x, getSize().y / this.texture.getSize().y);

Thanks for the help :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Sizable Sprite Help!
« Reply #3 on: October 22, 2015, 06:19:08 am »
You can also use getLocalBounds(), so you don't have to figure out how the sprite calculates its base size.
Laurent Gomila - SFML developer