Barcio77
Barcio77

Reputation: 44

SFML C++ not rendering

I got a problem, I'm writing a small game, and few times when I added new variable such as float or sf::Vector2f, sfml is not rendering other elements or not moving (or other odd problems).

This is my main function:

const int resW=800, resH=600;
sf::RenderWindow app(sf::VideoMode(resW, resH), "Jump");//, sf::Style::Fullscreen);

int main()
{
    //FPS
    app.setFramerateLimit(60);
    sf::Font font;
    float jumpStrength = 0.0;

    //when i uncomment it, my player is not rendered.
    //When it's commented everything is rendered
    //some time ago I had declared float number in player class,
    //and then player couldn't move (problem with sfml setPosition()???
    //sf::RectangleShape strengthRect(sf::Vector2f(100,100));
    while (app.isOpen())
    {
        // Process events
        sf::Event event;
        while (app.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed ||
                sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            {
                app.close();
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
            {
                jumpStrength+=0.2;
            }
            else if(jumpStrength>0.0)
            {
                player.Jump(jumpStrength);
            }
        }

        // Clear screen
        app.clear();

        ChandleInput();
        ChandleCollisions();

        player.Update();
        player.Draw(&app);
        app.draw(strengthRect);
    }
}

class CPlayer
{
    private:

    sf::Texture playerTex[3];
    sf::Sprite player;

    float g;
    float height;
    float jumpVel;

    void ResetJump() {jumpVel = 11;
            g = 0.2;
            height = 0;}
            void Move(float x, float y)
            {
                sf::Vector2f vec = player.getPosition();
                vec.x += x;
                vec.y += y;
                player.setPosition(vec.x,vec.y);
            }
    public:
        CPlayer()
        {
        if (!playerTex[0].loadFromFile("Images/frame_1.png"))
            return;// EXIT_FAILURE;
        if (!playerTex[1].loadFromFile("Images/frame_2.png"))
            return;// EXIT_FAILURE;
        if (!playerTex[2].loadFromFile("Images/frame_3.png"))
            return;// EXIT_FAILURE;
            player.setTexture(playerTex[0]);
                int x,y;
    int w,h;
            w = player.getLocalBounds().width;
            h = player.getLocalBounds().height;
            x = resW/2;y=resH-h/2;
            x-=w/2;
            y-=h/2;
            player.setPosition(x,y);
        }
        int GetX(){return player.getPosition().x;}
        int GetY(){return player.getPosition().y;}
        float GetHeight(){return height;}
        void Update()
        {
            if(height>0)//skacze
            {
                jumpVel -= g;
                height += jumpVel;
            }
            else
            {
                if(height<0)
                {
                    height = 0;
                }
                if(jumped)
                {
                    landed = true;
                    player.setTexture(playerTex[0]);
                }
                //Move(0,-1);
            }
        }
        void Jump(float strength)
        {
            if(!jumped)
            if(height<=0)//nie skacze
            {
                ResetJump();
                height = jumpVel;
                jumped = true;
                player.setTexture(playerTex[1]);
                jumpVel = strength;
            }
        }
        void Draw(sf::RenderWindow *app)
        {
                Move(0.0,-height);
                app->draw(player);
                Move(0.0,height);

        }
};

EDIT

I noticed that when I call (in my player's class) player.setPosition(...); Player is not rendered. when I don't call it, it is rendered. On the other hand when I don't declare RectangleShape, player is moved and rendered correctly.

EDIT 2 In class CPlayer is Move() method in which is setPosition().

Upvotes: 0

Views: 310

Answers (2)

Alex Kiecker
Alex Kiecker

Reputation: 61

unfortunately when you have a window context you have to define it in main()

so

int main()
{
   sf::RenderWindow window(sf::VideoMode(800, 600), "MY GAME WINDOW");
}

and not

sf::RenderWindow window(sf::VideoMode(800, 600), "MY GAME WINDOW");

int main()
{
   //window stuff...
}

and if you want multithreading you will have to look into the thread documentation but you can do

void Thread(sf::RenderWindow &window)
{
    // Draw stuff...
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "MY GAME WINDOW");

    sf::Thread exampleThread(Thread, window);

    window.setActive(false);

    while (window.isOpen()) { /* preferably do events here still 
         something I struggle with */ }
}

but again the documentation is great for sfml.

Upvotes: 0

Leo
Leo

Reputation: 1254

When getting this kind of errors/glitches, where the behaviour of your game change depending on the number of variables, or items inside an array, its more likely caused by variables not being properly initializated.

Give all attributes in all your classes a value in all of their constructors.

In your case, g, height and jumpVel are not initializated. Give them a default value in the constructor, and also check your other classes have their attributes initializated in the contructor.

Upvotes: 1

Related Questions