SFML C++ 不渲染

SFML C++ not rendering

我遇到了一个问题,我正在写一个小游戏,有几次当我添加新的变量如 float 或 sf::Vector2f 时,sfml 没有渲染其他元素或不移动(或其他奇怪的问题).

这是我的主要功能:

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);

        }
};

编辑

我注意到当我打电话时(在我的播放器 class 中) player.setPosition(...); 播放器未呈现。当我不调用它时,它会被渲染。 另一方面,当我不声明 RectangleShape 时,播放器会被正确移动和渲染。

编辑 2 在class CPlayer中是Move()方法,其中是setPosition()。

当出现这种 errors/glitches 时,您的游戏行为会根据变量的数量或数组中的项目而改变,这很可能是由于变量未正确初始化引起的。

在所有 类 的所有构造函数中为所有属性赋值

在你的例子中,g、height 和 jumpVel 没有被初始化。在构造函数中给它们一个默认值,并检查你的其他 类 是否在构造函数中初始化了它们的属性。

不幸的是,当您有一个 window 上下文时,您必须在 main()

中定义它

所以

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

而不是

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

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

如果你想要多线程,你将不得不查看线程文档,但你可以这样做

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 */ }
}

但是文档再次对 sfml 非常有用。