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

Author Topic: sf::Texture subRect loading bug?  (Read 2322 times)

0 Members and 1 Guest are viewing this topic.

Symphonym

  • Newbie
  • *
  • Posts: 32
    • View Profile
sf::Texture subRect loading bug?
« on: December 23, 2012, 11:59:23 pm »
So I'm experimenting around with my assetLoader, parsing asset data (If we take a Texture for example), such as subRect and filepath. As this allows be to change path and such of my assets without messing around with the code.

And now I implemented an additional little feature, to reparse the file and reload the asset during runtime, mainly for faster testing of different things. However, when changing data for my Textures, the subRect data more specifically(show below):

myTexture.loadFromFile("myPath.png", subRect);

If I change the Xand Y values of the subRect, another sprite on my spritesheet is drawn on my screen. But if I change the Width and Height values of the subRect, and reloading, nothing happens to the texture. Altough if I change the subRect Width/Height to something lower, I get this(See below):

INFO: The below pictures are rendered on my SFML sf::RenderWindow, and I have then taken screenshots of what it looks like, just to clear things up.

Texture taken from sprite sheet, sf::IntRect(0,0, 8,8)


Texture taken from sprite sheet, sf::IntRect(0,0, 4,4)


As you can see the picture with width and height of 4 pixels, is still 8 x 8 pixels. I have tried invoking the texture's default constructor right before reloading it to reset all values, but still the same result. I suspect this is a bug but I really have no idea what's going on :/

HOWEVER... When I have reloaded the texture as a 4x4 texture, atleast the 4x4 pixels around the top left corner are showing what they should, my guess is that the other pixels are somehow "left behind" and not getting cleared up or something :I
« Last Edit: December 24, 2012, 12:02:32 am by Symphonym »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: sf::Texture subRect loading bug?
« Reply #1 on: December 24, 2012, 12:11:41 am »
I'm not sure if you really want only to load an area of the image, because you can 'cut-out' some part of the texture easily with sprite.setTextureRect() and you won't have to reload any assets.
But if you want to reload the texture, you probably will have to first call create() with the new size and then load the texture, but I'm not too sure.

Providing a minimal example would be nice, so we could quickly test it and give you more detailed feedback. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Symphonym

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: sf::Texture subRect loading bug?
« Reply #2 on: December 24, 2012, 12:22:41 am »
I'm not sure if you really want only to load an area of the image, because you can 'cut-out' some part of the texture easily with sprite.setTextureRect() and you won't have to reload any assets.
But if you want to reload the texture, you probably will have to first call create() with the new size and then load the texture, but I'm not too sure.

Providing a minimal example would be nice, so we could quickly test it and give you more detailed feedback. ;)

Some minimalistic semi-pseudo code

On a side note, I know about the setSubRect method. Altough it won't work if I have things on decimal coordinates, and it allows me to keep control of textures in one place. Altough I might consider placing some data loading in my TileMap class, loading subRects for each tiletype and having a texture from which it snatches it's textures, might be a cleaner choice.


void gamestuff()
{

sf::Texture t;
t.loadFromFile("path.png", sf::IntRect(0,0,8,8);

    while(gameloop)
   {
         if(isKeyPressed(B))
         {
               t.create(4, 4);
               t.loadFromFile("path.png", sf::IntRect(0,0,4,4);
          }

           sf::Sprite(t);
           window.draw(t);
    }
}
 
« Last Edit: December 24, 2012, 12:29:09 am by Symphonym »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: sf::Texture subRect loading bug?
« Reply #3 on: December 24, 2012, 01:14:23 am »
Some minimalistic semi-pseudo code
Well that doesn't help me, since I still need to write my own testcase to test it... :-\

Anyways I quickly did your work (that's a minimal and complete example):
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");

    sf::Texture tex;
    tex.loadFromFile("img.png", sf::IntRect(0, 0, 8, 8));

    sf::Sprite spr(tex);

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }
            else if(event.type == sf::Event::KeyReleased)
            {
                tex.loadFromFile("img.png", sf::IntRect(0, 0, 4, 4));
                spr.setTextureRect(sf::IntRect(0,0,4,4));
            }
        }

        window.clear();
        window.draw(spr);
        window.display();
    }
}
 

So I was mistaken with calling create(), but you have to update the texture rect of the sprite, otherwise the texture rect would still be 8x8 and just scale the provided texture.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything