SFML community forums

Help => General => Topic started by: Abragon on October 01, 2017, 11:42:17 pm

Title: Using sf::texture and sf::sprite
Post by: Abragon on October 01, 2017, 11:42:17 pm
Hi all, Hoping to get some help with an issue I get while trying to follow a tutorial series on youtube. I'm trying to create an animation using a 576x256 .png in each direction but my sprite disappears when I do input. Any help on this would be greatly appreciated.Here is the following code:
#include <SFML\Graphics.hpp>

int main()
{
   sf::RenderWindow window(sf::VideoMode(1000, 800), "SFML works!");
//   sf::CircleShape shape(100.f);
//   shape.setFillColor(sf::Color::Green);
   float playerMovementSpeed = 1;
   int walkingCounter = 0;

   sf::Texture texturePlayer;
   texturePlayer.loadFromFile("walksprite.png");

   sf::Sprite spritePlayer(texturePlayer);
   spritePlayer.setPosition(window.getSize().x / 2, window.getSize().y / 2);
   spritePlayer.setTextureRect(sf::IntRect(0, 0, 64, 64));
   while (window.isOpen())
   {
      sf::Event event;
      while (window.pollEvent(event))
      {
         if (event.type == sf::Event::Closed)
            window.close();
      }

      window.clear();
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
      {
         spritePlayer.move(0, -playerMovementSpeed);
         spritePlayer.setTextureRect(sf::IntRect(walkingCounter*64, 0, 64, 64));
      }
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
      {
         spritePlayer.move(0, playerMovementSpeed);

         spritePlayer.setTextureRect(sf::IntRect(walkingCounter*64, 64*2, 64, 64));
      }
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
      {
         spritePlayer.move(-playerMovementSpeed, 0);
         spritePlayer.setTextureRect(sf::IntRect(walkingCounter*64, 64*1, 64, 64));
      }
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
      {
         spritePlayer.move(playerMovementSpeed, 0);
         spritePlayer.setTextureRect(sf::IntRect(walkingCounter*64, 64*3, 64, 64));
      }
      walkingCounter++;
      if (walkingCounter== 8)
      {
         walkingCounter == 0;
      }
   //   window.draw(shape);
      window.draw(spritePlayer);
      window.display();
   }

   return 0;
}
Title: Re: Using sf::texture and sf::sprite
Post by: eXpl0it3r on October 02, 2017, 12:22:29 am
Your movement and animations are not aware of your framerate, as such it will go as fast as possible. For example if you get 1000 fps, then your sprite will move out at 1000 pixels per second, thus moving out of the window.

Calculate the frame time with a clock and multiple that with your speed and animation.
Or fix your time step: https://gafferongames.com/post/fix_your_timestep/
Title: Re: Using sf::texture and sf::sprite
Post by: Abragon on October 02, 2017, 02:03:59 am
I tried adding a window.setFramerateLimit(60) but no change.If i sub the keyboard texturerects with 0 I get the familiar png movements but I fail to add the increment counter
Title: Re: Using sf::texture and sf::sprite
Post by: Abragon on October 02, 2017, 11:13:25 pm
Update: When I add only one of the counterWalking I get the dissapearing bug but if I keep the other directions starting at 0 then it reappears where the faulty input should have been. From this I believe that the dissapearing still keeps the position .
Title: Re: Using sf::texture and sf::sprite
Post by: eXpl0it3r on October 03, 2017, 12:07:14 am
Use your debugger and track values.
Title: Re: Using sf::texture and sf::sprite
Post by: Hapax on October 03, 2017, 02:04:29 pm
Take a closer look at this part:
                if (walkingCounter== 8)
                {
                        walkingCounter == 0;
                }
Title: Re: Using sf::texture and sf::sprite
Post by: Abragon on October 11, 2017, 11:17:22 pm
Thank you so much because that fixed it. It was suppose to be an assignment(= v ==). You're great!