gpt4 book ai didi

c++ - 没有键盘输入的 SFML 动画

转载 作者:行者123 更新时间:2023-11-30 05:34:58 25 4
gpt4 key购买 nike

我目前正在从事一个项目,该项目基本上是排序算法的可视化,以解释它们的工作原理(而不是概述)。我是 SFML(甚至 OpenGL)的新手,对库的使用经验有限,但我想做的是将绘制的 Sprite 移动到不同的位置以显示排序。我查看了教程和示例,但它们都采用键盘输入来移动 Sprite ——本项目中未使用的东西。有谁知道如何实现这一目标?

这是当前代码:

画杯.h

class DrawCups
{
public:
DrawCups(sf::RenderWindow& window);
~DrawCups();

void loadImage(const char* pathname, sf::Texture& texture, sf::Sprite& sprite);

void drawCup1();

private:
sf::RenderWindow& _window;
};

DrawCups.cpp(选定函数)

void DrawCups::drawCup1()
{
// load our image
sf::Texture cup1; // the texture which will contain our pixel data
sf::Sprite cup1Sprite; // the sprite which will actually draw it
loadImage("./images/InsertionSort/red_cup_1.png", cup1, cup1Sprite);
cup1Sprite.setPosition(sf::Vector2f(150, 230));
_window.draw(cup1Sprite);
}

main.cpp

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

显然可以根据您的需要更改机芯。如果它移动得太快或太慢,则进行更小/更大的更新。

关于c++ - 没有键盘输入的 SFML 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34031012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com