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

Author Topic: settexturerect seems offset.  (Read 1620 times)

0 Members and 1 Guest are viewing this topic.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
settexturerect seems offset.
« on: November 04, 2022, 09:25:52 pm »
I've been having this problem since I started using sfml and I still cant figure it out.I don't know if its offset or what but settexture rect just doesn't seem to display what is actually in the rect.left.I have been simply using Texture.loadfromfile for animations but its a waste of performance to keep loading it.Here is some script of an attempt at using settexturerect.
#include "testfunction.h"
#include <SFML/Graphics.hpp>
using namespace sf;
int x;
float preclock;
Clock clock2;
float animate(Sprite &sprite,float delay)
{
       
       
        if(clock2.getElapsedTime().asSeconds()>preclock+delay)
        {
               
               
                if(x>=13*40)
                {
                        x=0;
                }
                else
                {
                       
                        sprite.setTextureRect(IntRect(x,0,40,32)); //right here
                        x+=40;
                }
                preclock=clock2.getElapsedTime().asSeconds();
        }
}
 



This code works when I have texture.loadfromfile but with this it shows blank with a black line at the top.I'm sure that it has the right position and that simply selecting the next animation frame as a test still shows blank.I have tested this without the function.Any ideas on what's happening here? Thanks.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: settexturerect seems offset.
« Reply #1 on: November 07, 2022, 05:36:40 pm »
It's true that using texture.loadFromFile over and over is a bad idea as it is loading the texture in fully each time. That said, it's still needed to load it once before you can set a texture rectangle; it needs to know the texture from which to take the rectangle. (you don't show that enough to know if you are actually loading it or not).

You could also show us the texture used (or an example one) and the result you are getting.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: settexturerect seems offset.
« Reply #2 on: November 08, 2022, 05:47:54 pm »
Here's a picture of the problem.The mouth of this character is a separate sprite and texture and when I use settexturerect it doesn't display.


To make this less confusing I removed stuff out of this script and took out the function to test settexturerect.
int main()
{
       
        RenderWindow window(VideoMode(600, 480),"");

        Texture ffhead;
        ffhead.loadFromFile("ff-stat-head.png",IntRect(0,0,40,32));
        Sprite sprite2;
        sprite2.setTexture(ffhead);
        sprite2.setPosition(300,300);
        while (window.isOpen())
    {
       
       
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }
       
                if(Keyboard::isKeyPressed(Keyboard::Space))
                {
                        sprite2.setTextureRect(IntRect(40,0,40,32));
                }
               
        window.clear();
        window.draw(sprite2);
        window.display();
       
        }
        return 0;
}

Can you spot where my mistake is?

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: settexturerect seems offset.
« Reply #3 on: November 09, 2022, 04:07:59 am »
When you give loadFromFile an IntRect, it creates a texture only big enough to fit that rectangle.
So your ffhead texture is only 40x32, the rest was cropped away during loading. This means the setTextureRect can't go to 40,0,40,32. It seems SFML uses clamp texture addressing by default, so trying to go off the edge of a texture results is duplicating the edge pixels.

Try this instead:
ffhead.loadFromFile("ff-stat-head.png");
Sprite sprite2;
sprite2.setTexture(ffhead);
sprite2.setTextureRect(IntRect(0,0,40,32));
That should load the full texture, but set the region that renders to the top left 40x32.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: settexturerect seems offset.
« Reply #4 on: November 09, 2022, 08:06:06 pm »
 ;) Thanks so much for clearing that up for me.