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

Author Topic: C++ SFML - Sprite sheet animation problem (not working)  (Read 1267 times)

0 Members and 1 Guest are viewing this topic.

MarbleXeno

  • Guest
C++ SFML - Sprite sheet animation problem (not working)
« on: July 15, 2020, 08:41:09 pm »
First of all - thank you for your answer!

My problem is small - my animation "system" does not work, there should be an idle animation on the screen, but there is only a static frame. I'm still learning, C ++ is new to me (I've worked in C # before) and I wrote it for fun, I don't know why it doesn't work. The code in Animator.hpp works (when it's not in Animator.hpp), so the problem is probably in Player.hpp - thanks for your help!

Here's how it looks:

main.cpp
#include <iostream>

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include "Player.hpp"


using namespace sf;

RenderWindow window(VideoMode(800, 600), "Simple platformer in CPP with SFML", Style::Close | Style::Titlebar);
float deltaTime = 0.016f;
Player player;

void Update() {

    player.update(deltaTime);
    window.clear(Color::Color({ 86 , 131 , 63 }));
    window.draw(player);
    window.display();

}

int main() {
    window.setFramerateLimit(60);
    Event evnt;
    Clock clock;
    float elapsedTime;

    while (window.isOpen()) {
        elapsedTime = clock.getElapsedTime().asSeconds();
        while (window.pollEvent(evnt)) {
            if (evnt.type == Event::Closed)
                window.close();
        }

        Update();

        std::cout << "DELTA TIME: " << deltaTime << std::endl;
        deltaTime = clock.getElapsedTime().asSeconds() - elapsedTime;
    }



    return 0;

}
 

animator.hpp
#pragma once
#include <SFML/Graphics.hpp>

class Animator {
private:
        sf::Clock clock;
        sf::Sprite entitySprite;
        sf::Texture Texture;

public:
        Animator(sf::Sprite sprite, sf::Texture texture) {
                entitySprite = sprite;
                Texture = texture;
        }

        void play(sf::IntRect txtIntRect, int IntRectLeftVal, int ResetIntRectLeftVal, int IntRectResetToVal, float AnimationSpeed) {
                if (clock.getElapsedTime().asSeconds() > AnimationSpeed) {
                        if (txtIntRect.left == ResetIntRectLeftVal) {
                                txtIntRect.left = IntRectResetToVal;
                        }
                        else {
                                txtIntRect.left += IntRectLeftVal;
                                entitySprite.setTexture(Texture);
                                entitySprite.setTextureRect(txtIntRect);
                                clock.restart();
                        }
                }
        }

private:

};
 

player.hpp
#pragma once
#include <SFML/Graphics.hpp>
#include "Animator.hpp"

class Player : public sf::Drawable
{
private:
        sf::Texture playerTextureSheet;
        sf::Sprite playerSprite;
        sf::IntRect idleRect;
        sf::Clock clock;
        Animator idleAnimation = Animator(playerSprite, playerTextureSheet);


public:
        Player() {
                playerTextureSheet.loadFromFile("sprites/playerSpriteSheet.png");
                idleRect = sf::IntRect(4, 0, 27, 33);
                playerSprite.setTexture(playerTextureSheet);
                playerSprite.setTextureRect(idleRect);
                playerSprite.setScale({ 4.f, 4.f });
        }

        void update(float deltaTime) {
                animation();
        }

        void move() {

        }
private:
        void draw(sf::RenderTarget& target, sf::RenderStates states)const override {
                target.draw(playerSprite);
        }

        void animation() {
                idleAnimation.play(idleRect, 32, 288, 4, 0.5f);

                //WORKS
                /*if (clock.getElapsedTime().asSeconds() > 0.5f) {
                        if (idleRect.left == 292) {
                                idleRect.left = 4;
                        }
                        else {
                                idleRect.left += 32;
                                playerSprite.setTextureRect(idleRect);
                                clock.restart();
                        }
                }*/

        }
};

 



Yes - i know, this code is a trash, but i'm just starting with c++ and sfml.
« Last Edit: July 15, 2020, 08:46:00 pm by MarbleXeno »

MarbleXeno

  • Guest
Re: C++ SFML - Sprite sheet animation problem (not working)
« Reply #1 on: July 16, 2020, 05:01:16 pm »
Ok, after one day of thinking and learning a finally did it! I'm so happy, the problem was with animation.hpp, new code looks like this:

#pragma once
#include <SFML/Graphics.hpp>

class Animator
{
private:
        sf::Sprite *EntitySprite;
        sf::IntRect EntityRect;
        sf::Texture EntityTexture;

        sf::Clock clock;

public:
        Animator(sf::Sprite& sprite, sf::IntRect entityRect, std::string pathToSpriteSheet) {
                EntityTexture.loadFromFile(pathToSpriteSheet);
                EntitySprite = &sprite;
                EntityRect = entityRect;

                EntitySprite->setTexture(EntityTexture);
                EntitySprite->setTextureRect(EntityRect);
                //SpriteScale = spriteScale;

                //EntitySprite->setTexture(EntityTexture);
                //EntitySprite->setScale(SpriteScale);
        }


        void update(float deltaTime)
        {

        }

        void applyAnimation(int intRectLeftAdd, float animationSpeed, int intRectResetAt, int intRectResetTo) {
               
                if (clock.getElapsedTime().asSeconds() > animationSpeed) {
                        if (EntityRect.left == intRectResetAt) {
                                EntityRect.left = intRectResetTo;
                        }
                        else {
                                EntityRect.left += intRectLeftAdd;
                                EntitySprite->setTextureRect(EntityRect);
                                clock.restart();
                        }
                }
        }




};

 

So the problem is fixed! C++ is new to me so i didn't knew about ampersand and pointer.
« Last Edit: July 16, 2020, 05:03:25 pm by MarbleXeno »