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

Author Topic: Problem with IntRect or setTextureRect  (Read 2973 times)

0 Members and 1 Guest are viewing this topic.

DarwinCar

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Problem with IntRect or setTextureRect
« on: October 24, 2018, 06:09:06 pm »
Hello,
So I've struggling to find if its a problem with my code or anything else on my part. (Also, tried different Visual Studio versions, different textures/sprites, also tried inside a VM and with different SFML versions 2.4/2.5.1).

I've attached the whole project.
The way its supposed to look : https://imgur.com/a/o8Wc2oZ
And how it looks : https://imgur.com/a/rzU6zM1

Also here is the code :
Code: [Select]
#include "SFML/Graphics.hpp"

int main(int argc, char ** argv) {
sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "Demo Game");

sf::Event event;
sf::Texture texture;
texture.loadFromFile("images/dragonFrames.png");

sf::IntRect rectSourceSprite(300, 0, 300, 400);
sf::Sprite sprite(texture, rectSourceSprite);
sf::Clock clock;

while (renderWindow.isOpen()) {
while (renderWindow.pollEvent(event)) {
if (event.type == sf::Event::EventType::Closed)
renderWindow.close();
}

if (clock.getElapsedTime().asSeconds() > 1.0f) {
if (rectSourceSprite.left == 600)
rectSourceSprite.left = 0;
else
rectSourceSprite.left += 300;

sprite.setTextureRect(rectSourceSprite);
clock.restart();
}


renderWindow.clear();
renderWindow.draw(sprite);
renderWindow.display();
}
}

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Problem with IntRect or setTextureRect
« Reply #1 on: October 24, 2018, 07:05:31 pm »
What is the width and height of dragonFrames.png? Is it only 300x400?

DarwinCar

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Problem with IntRect or setTextureRect
« Reply #2 on: October 24, 2018, 07:13:23 pm »
« Last Edit: October 24, 2018, 07:16:25 pm by DarwinCar »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Problem with IntRect or setTextureRect
« Reply #3 on: October 24, 2018, 07:18:56 pm »
Ok, yeah your texture looks to be 319x424.

sf::IntRect rectSourceSprite(300, 0, 300, 400);
sf::Sprite sprite(texture, rectSourceSprite);
 

You're setting your sprite to be almost the same size as the whole texture.

Hapax

  • Hero Member
  • *****
  • Posts: 3349
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with IntRect or setTextureRect
« Reply #4 on: October 30, 2018, 10:18:35 pm »
The initial x position (offset of the rectangle from the left side) is causing the texture to almost entirely miss the significant part of the image.

You most likely want the IntRect to start with zero.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything