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.