【问题标题】:SFML Vertical Sync is not supported不支持 SFML 垂直同步
【发布时间】:2018-05-11 20:01:22
【问题描述】:

我试图实现一个 wator 模拟,如果鲨鱼吃鱼,我想随机生成鲨鱼,程序编译但我得到“不支持设置垂直同步”。

在 Ubuntu 16.04 上工作。在我从事其他工作之前,我遇到了同样的错误,但显示的窗口不是这样。有什么帮助吗?

编辑我已经修复了代码,我的循环中有太多{,但现在我收到“分段错误(核心转储)”错误我已将我的 png 更改为 8有点,但没有帮助。

#include <SFML/Graphics.hpp>



int main()
{
  int n;
  int x;
  int y;

  sf::RenderWindow window(sf::VideoMode(800, 800), "SFML works!");
 // Set Frame Rate to 60fps
    window.setFramerateLimit(60);

    srand(time(0));

    sf::Texture shark;
    shark.loadFromFile("image.png");
    std::vector<sf::Sprite> Fishes(n,sf::Sprite(shark));
    for (int n = 0; n < Fishes.size(); n++){
        Fishes[n].setOrigin(15, 15);
        Fishes[n].getPosition();
        Fishes[n].setPosition(x = rand() % 790 + 10, y = rand() % -10 - 50);
    }

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
      sf::Event event;
        while (window.pollEvent(event))
      {
            // "close requested" event: we close the window
      if (event.type == sf::Event::Closed)
                window.close();
        }

        Fishes[n].setPosition(x, y+=1);
            Fishes[n].rotate(1);

            // clear the window with black color
            window.clear(sf::Color::Black);

            // draw everything here...
            // window.draw(...);
            window.draw(Fishes[n]);

            // end the current frame
            window.display();
        }

        return 0;
}

【问题讨论】:

  • 您的循环变量是i,但您使用的是全局变量n。您在渲染中缺少一个完整的循环,只需再次使用此n。我想说你应该首先弄清楚你的逻辑(一个好主意可能是将全局变量限制为没有,这样你就可以看到你的范围)。垂直同步可能只是一个警告,一旦你修复它,你的程序应该没问题。
  • 我搞定了...我有一个{ 太多了
  • 我已经用新的错误更新了我的答案

标签: c++ c++11 sfml


【解决方案1】:

我会添加评论,但我缺乏声誉。段错误是由非法内存写入或读取引起的。在您的情况下,我会尝试检查您的图像是否正确加载。

我还要注意在正文中只有一行的 for 循环不要只使用一个大括号,要么都使用,要么都不使用。

【讨论】:

    【解决方案2】:

    在渲染循环中,您仍然缺少对鱼的循环。

    您的第一个循环将您的循环变量n 设置为您最后一条鱼之后的数字再次 使用相同的循环变量将导致未定义的行为。 修复它。可能是通过添加另一个for 循环,您在while 渲染循环中第二次使用n

    具体来说,当你来到这一行时:

    Fishes[n].setPosition(x, y+=1);
    

    你的变量n不是任何类型的循环变量。更糟糕的是,它完全是随机的,你没有设置任何值。在main() 之后的第一行是int n;。如果你删除那一行(main 之后的第一行),你会发现哪里出了问题。

    【讨论】:

    • 你能添加一个我不太明白你的意思的代码示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    相关资源
    最近更新 更多