When I run the program, the character moves in a small fps. I looked at how it could be solved and came across Clock and time. But the problem has not been solved. Please help me, I have to submit the project soon. The problem is that when I add the background, the character starts lagging
main.cpp
(#include <SFML/Graphics.hpp>
#include "hero.h"
#include "game.h"
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(1920, 1080), "WayofDarkness");
game(window);
return 0;
})
hero.h
(#ifndef HERO_H
#define HERO_H
#include <SFML/Graphics.hpp>
class Hero {
private:
sf::Sprite sprite;
sf::Texture texture;
float x, y;
public:
Hero(std::string spritePath, float posX, float posY) {
if (!texture.loadFromFile(spritePath)) {
}
sprite.setTexture(texture);
x = posX;
y = posY;
sprite.setPosition(x, y);
}
void draw(sf::RenderWindow& window);
void move(sf::RenderWindow& window, float&, float&, float);
};
#endif HERO_H)
hero.cpp
(#include <iostream>
#include "hero.h"
#include "update.h"
void Hero::draw(sf::RenderWindow& window)
{
window.draw(sprite);
}
void Hero::move(sf::RenderWindow& window, float& playerX, float& playerY, float time)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
playerX -= 0.6 * time;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
playerX += 0.6 * time;
sprite.setPosition(playerX, playerY);
}
)
game.h
(#pragma once
#include <SFML/Graphics.hpp>
#include "hero.h"
#include "scence.h"
#include <iostream>
float playerX = 250;
float playerY = 151;
void game(sf::RenderWindow& window) {
//window.setFramerateLimit(60);
Hero hero("HeroSprite/doodle.png", 100, 100);
sf::Clock clock;
while (window.isOpen()) {
float time = clock.restart().asSeconds();
clock.restart();
std::cout << time<< std::endl;
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
hero.move(window, playerX, playerY, time);
scence(window);
hero.draw(window);
window.display();
}
}
)
scene.h
(#pragma once
#include <SFML/Graphics.hpp>
void scence(sf::RenderWindow& window) {
sf::Texture backgroundTexture;
backgroundTexture.loadFromFile("ALL/images/animationBG.jpg");
sf::Sprite background(backgroundTexture);
window.draw(background);
})