Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无键盘输入的SFML动画_C++_Opengl_Sfml - Fatal编程技术网

C++ 无键盘输入的SFML动画

C++ 无键盘输入的SFML动画,c++,opengl,sfml,C++,Opengl,Sfml,我现在正在做一个项目,它基本上是一个排序算法的可视化,用来解释它们是如何工作的(而不是概述)。我对使用SFML(甚至OpenGL)还不熟悉,对该库的使用经验有限,但我尝试将绘制的精灵移动到不同的位置以显示排序。我已经看过了教程和示例,但它们都采用键盘输入来移动精灵,这在本项目中没有使用。有人知道如何做到这一点吗 以下是当前代码: 水杯 DrawCups.cpp(选定功能) main.cpp 在循环之前创建图像,并在绘制之前进行更新 DrawCups drawToWindow(window); /

我现在正在做一个项目,它基本上是一个排序算法的可视化,用来解释它们是如何工作的(而不是概述)。我对使用SFML(甚至OpenGL)还不熟悉,对该库的使用经验有限,但我尝试将绘制的精灵移动到不同的位置以显示排序。我已经看过了教程和示例,但它们都采用键盘输入来移动精灵,这在本项目中没有使用。有人知道如何做到这一点吗

以下是当前代码:

水杯 DrawCups.cpp(选定功能) main.cpp
在循环之前创建图像,并在绘制之前进行更新

DrawCups drawToWindow(window); //Constructor creates the sprite

while (window.isOpen())
{
    ...
    drawToWindow.update(); //Update the position

    //Redraw
    window.clear(sf::Color::White);
    drawToWindow.drawCup1();
    window.display();
}
我不确定您想要什么类型的移动,但更新功能可以是这样的:

void DrawCups::update()
{
    sf::Vector2f pos = this->cup1Sprite.getPosition();
    pos.x++; //Move 1 pixel to the left
    this->cup1Sprite.setPosition(pos);
}

显然,改变运动以满足您的需要。如果图像移动太快或太慢,则进行较小/较大的更新。

每次需要重新绘制图像时更新图像有什么问题?它需要从原始位置平滑移动到排序区域。重新绘制精灵会使动画起伏。必须重新绘制精灵才能使其移动。如果动画过于起伏,你要么没有足够快地重新绘制它,要么改变它的位置太快。你能举个例子吗?太棒了。谢谢你的例子。:)嗯,您提供的代码只是让屏幕闪烁,而不是移动元素本身。我使用了比1更大的像素范围来测试它。我现在无法测试我的代码,所以可能会有错误。你确定你正确地更新了职位吗?确保你的绘图功能不会像以前那样重置位置。也许我当时使用的位置错误,因为我已经尝试过。旋转,它工作正常。我目前正在使用pos.x+10;-这不正确吗?您可能是指
pos.x+=10
pos.x+10
对结果没有任何影响。
int main()
{
    sf::RenderWindow window(sf::VideoMode(1366, 768), "Sorting Algorithm Visualisation: SFML");
    window.setFramerateLimit(60);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color::White);
        DrawCups drawToWindow(window);;
        drawToWindow.drawCup1();
        window.display();
    }

    return 0;
}
DrawCups drawToWindow(window); //Constructor creates the sprite

while (window.isOpen())
{
    ...
    drawToWindow.update(); //Update the position

    //Redraw
    window.clear(sf::Color::White);
    drawToWindow.drawCup1();
    window.display();
}
void DrawCups::update()
{
    sf::Vector2f pos = this->cup1Sprite.getPosition();
    pos.x++; //Move 1 pixel to the left
    this->cup1Sprite.setPosition(pos);
}