0
\$\begingroup\$

I've recently bought an SFML course on Udemy.The course is teaching me to make a game where you are chopping a tree while avoiding falling branches.

Right now i'm learning to animate moving clouds and bees in the background,since i'm using my own resources the name of the sprite will be different.

Basically the clouds wil be hunters : a spaceship in my game.

The bees will be stings : a type of insect in game.

So the bees will be flying from the right,everytime it reaches the left corner it'll reset on the right side at a random height.Same thing for the hunters which'll be moving from the left.

Here's the code:

    #include "pch.h"

using namespace sf;

int main()
{   //Set VM and window
    VideoMode vm(1920, 1080);
    RenderWindow window(vm, "Star Fire", Style::Default);

    //Timer
    Clock Timer;

    //Tex to hold graphics in GPU
    Texture BckGrnd;
    BckGrnd.loadFromFile("Graphics/M2.png");
    //Sprite
    Sprite SBck;
    SBck.setTexture(BckGrnd);
    //SBck cover the screen
    SBck.setPosition(0, 0);

    //Wall
    Texture Tree;
    Tree.loadFromFile("Graphics/sprwall.png");
    Sprite STree;
    STree.setTexture(Tree);
    STree.setPosition(800, 300);

    //Sting
    Texture Sting;
    Sting.loadFromFile("Graphics/sprsting.png");
    Sprite SSting;
    SSting.setTexture(Sting);
    SSting.setPosition(0, 600);
    //MovChk
    bool SActive = false;
    //Spd
    float SSpd = 0.0f;

    //Hunter
    Texture Hunt;
    Hunt.loadFromFile("Graphics/sprhunter.png");
    Sprite H1;
    Sprite H2;
    Sprite H3;
    H1.setTexture(Hunt);
    H2.setTexture(Hunt);
    H3.setTexture(Hunt);
    //Offscr pos
    H1.setPosition(0, 0);
    H2.setPosition(0, 250);
    H3.setPosition(0, 500);
    //MovChk
    bool H1Active = false;
    bool H2Active = false;
    bool H3Active = false;
    //Spd
    bool H1Spd = 0.0f;
    bool H2Spd = 0.0f;
    bool H3Spd = 0.0f;

    while (window.isOpen()) {
        if (Keyboard::isKeyPressed(Keyboard::Escape)) {
            window.close();
        }
        //Update
        Time dt = Timer.restart();

        //Setup Sting
        if (!SActive) {
            //Spd
            srand((int)time(0) * 10);
            SSpd = (rand() % 200) + 200;
            //HighChk
            srand((int)time(0) * 10);
            float H = (float)((rand() % 500) + 500);
            SSting.setPosition(2000, H);
            SActive = true;
        }
        else {
            //Mov
            SSting.setPosition(
                SSting.getPosition().x - (SSpd * dt.asSeconds()),
                SSting.getPosition().y
            );
            //Sting reaches screen edge
            if (SSting.getPosition().x < -100) {
                SActive = false;
            }
        }

        //Hunters
        if (!H1Active) {
            //Spd
            srand((int)time(0) * 10);
            H1Spd = (rand() % 200);
            //H
            srand((int)time(0) * 10);
            float H = (float)((rand() % 150));
            H1.setPosition(0, H);
            H1Active = true;
        }
        else {
            H1.setPosition(
                H1.getPosition().x + (H1Spd * dt.asSeconds()),
                H1.getPosition().y);
            //Rgt hand chk
            if (H1.getPosition().x > 1920) {
                //Set up new H
                H1Active = false;
            }
        }
        //2
        if (!H2Active) {
            //Spd
            srand((int)time(0) * 20);
            H1Spd = (rand() % 200);
            //H
            srand((int)time(0) * 20);
            float H = (float)((rand() % 300) - 150);
            H2.setPosition(0, H);
            H2Active = true;
        }
        else {
            H2.setPosition(
                H2.getPosition().x + (H2Spd * dt.asSeconds()),
                H2.getPosition().y);
            //Rgt hand chk
            if (H2.getPosition().x > 1920) {
                //Set up new H
                H2Active = false;
            }
        }
        //3
        if (!H3Active) {
            //Spd
            srand((int)time(0) * 30);
            H3Spd = (rand() % 200);
            //H
            srand((int)time(0) * 30);
            float H = (float)((rand() % 450) - 150);
            H3.setPosition(0, H);
            H3Active = true;
        }
        else {
            H3.setPosition(
                H3.getPosition().x + (H3Spd * dt.asSeconds()),
                H3.getPosition().y);
            //Rgt hand chk
            if (H3.getPosition().x > 1920) {
                //Set up new H
                H3Active = false;
            }
        }

        window.clear();
        //Bck
        window.draw(SBck);
        //Hunters
        window.draw(H1);
        window.draw(H2);
        window.draw(H3);
        //Tree
        window.draw(STree);
        //Sting
        window.draw(SSting);

        window.display();

    }
    std::cout << "Hello World!\n";
    return 0;
}

The stings was working completely fine,but the hunters was moving extremely slow no matter the speed.I've tried to change the speed,add something to it and it does nothing.

But when i disable the other 2 hunters,that one hunter flies slightly faster.I'm not sure if this is a bug or a performance problem.Can someone help me ?(I'm using visual studio 2019 community)

\$\endgroup\$
  • \$\begingroup\$ Have you tried compiling in Release to see if the problem goes away? \$\endgroup\$ – Casey Nov 1 at 3:28
  • \$\begingroup\$ For the second hunter you initialize H1Spd but use H2Spd, which will presumably always be 0. Have you tried removing your rand() functions for speed and just use static speeds? For example, how does it behave if you just give them all 200 in speed? \$\endgroup\$ – Mikael Nov 1 at 6:53
  • \$\begingroup\$ I am not very well-versed in c++, but I thought it had some type-safety mechanisms. But if the example compiles, then bool H1Spd = 0.0f; is the problem: the random function with mod is going to be a binary move/don't move. \$\endgroup\$ – Yorik Nov 1 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.