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

Author Topic: I have a problem  (Read 413 times)

0 Members and 1 Guest are viewing this topic.

swthxbld3

  • Newbie
  • *
  • Posts: 3
    • View Profile
I have a problem
« on: May 19, 2024, 06:00:01 am »
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);


       

})


swthxbld3

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: I have a problem
« Reply #1 on: May 19, 2024, 06:17:40 am »
It turns out that this is because of the image itself. But now I have fps drawdowns. It is also unclear why. Could you look at the code and tell me what is better to redo.

kojack

  • Sr. Member
  • ****
  • Posts: 331
  • C++/C# game dev teacher.
    • View Profile
Re: I have a problem
« Reply #2 on: May 19, 2024, 09:08:51 am »
The biggest hit here is that you are loading the background jpg from disk every frame. It really should be loaded just once at the beginning then reused, like the hero sprite's texture.

 

anything