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

Author Topic: [solved]Hi Iam following an tutorial for sfml but Iam kinda stuck in Animation  (Read 1887 times)

0 Members and 2 Guests are viewing this topic.

ecarusb

  • Newbie
  • *
  • Posts: 4
    • View Profile
Hi

I need help with animations,
the Issue is the animations are way to jittery and inconsistant I feel iam using the deltaTime wrong,
as it is almost always lower than my switchTime (time to switch the animation) and if I lower the switchTime the animations becomes too fast.
Is it just because iam on a fast machine and there is no way around it ?

main.cpp
#include <iostream>
#include <SFML\Graphics.hpp>
#include "Animations.h"

int main()
{
    sf::RenderWindow window(sf::VideoMode(512, 512), "SFML", sf::Style::Close | sf::Style::Resize);
        sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
        player.setPosition(206.0f, 206.0f);
        sf::Texture playerTexture;
        playerTexture.loadFromFile("bat-sprite.png");
        player.setTexture(&playerTexture);
       
        Animations animation(&playerTexture, sf::Vector2u(4, 4), 0.005f);

        float deltaTime = 0.0f;
        sf::Clock clock;       
       
        while (window.isOpen())
        {
                deltaTime = clock.restart().asSeconds();

                sf::Event evnt;
                while (window.pollEvent(evnt))
                {
                        switch (evnt.type)
                        {
                        case sf::Event::Closed :
                                window.close();
                                break;
                        }
                }

                animation.update(1, deltaTime);
                player.setTextureRect(animation.uvRect);
                window.clear(sf::Color(150,150,150));
                window.draw(player);
                window.display();
        }

    return 0;
}

Animation.h
#include <SFML\Graphics.hpp>

class Animations
{
public:
        Animations(sf::Texture* texture, sf::Vector2u imageCount, float swtichTime);
        void update(int row, float deltaTime);
public:
        sf::IntRect uvRect;
private:
        sf::Vector2u imagecount;
        sf::Vector2u currentImage;

        float totalTime;
        float switchTime;

       
};

 

Animations.cpp
#include "Animations.h"
#include <iostream>

Animations::Animations(sf::Texture* texture, sf::Vector2u imageCount, float swtichTime)
{
        this->imagecount = imageCount;
        this->switchTime = swtichTime;
        totalTime = 0.0f;
        currentImage.x = 0;

        uvRect.width = texture->getSize().x / float(imageCount.x);
        uvRect.height = texture->getSize().y / float(imageCount.y);
}

void Animations::update(int row, float deltaTime)
{
        currentImage.y = row;
        totalTime = deltaTime;


        //std::cout << "Total Time : " << totalTime << " SwitchTime: " << switchTime << " result :" << (totalTime >= switchTime) << std::endl;
        if (totalTime >= switchTime)
        {
                //std::cout << currentImage.x << std::endl;
                totalTime -= switchTime;
                currentImage.x++;

                if (currentImage.x >= imagecount.x) {
                        currentImage.x = 0;
                }
        }
       
       
        uvRect.left = currentImage.x * uvRect.width;
        uvRect.top = currentImage.y * uvRect.height;

}
 

Any help is appreciated
« Last Edit: August 01, 2024, 07:31:57 pm by ecarusb »

ecarusb

  • Newbie
  • *
  • Posts: 4
    • View Profile
Hi I fixed it,

I realized I'm just resetting the total time with deltatime in my code instead of adding delta time to total time.
it works as expected now thanks anyways