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

Author Topic: Using sf::texture and sf::sprite  (Read 4839 times)

0 Members and 1 Guest are viewing this topic.

Abragon

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Using sf::texture and sf::sprite
« 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;
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Using sf::texture and sf::sprite
« Reply #1 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/
« Last Edit: October 02, 2017, 12:24:19 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Abragon

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Using sf::texture and sf::sprite
« Reply #2 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

Abragon

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Using sf::texture and sf::sprite
« Reply #3 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 .

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Using sf::texture and sf::sprite
« Reply #4 on: October 03, 2017, 12:07:14 am »
Use your debugger and track values.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Using sf::texture and sf::sprite
« Reply #5 on: October 03, 2017, 02:04:29 pm »
Take a closer look at this part:
                if (walkingCounter== 8)
                {
                        walkingCounter == 0;
                }
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Abragon

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Using sf::texture and sf::sprite
« Reply #6 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!