【问题标题】:SFML image not loadingSFML 图像未加载
【发布时间】:2013-12-10 18:30:59
【问题描述】:

我正在使用带有 SFML 的 Xcode。我将文件放在 Xcode 项目中,该项目将其复制到目录中。但是,我仍然无法将其加载到程序中。有什么想法吗?

代码:

int main(int argc, const char * argv[])
{
    sf::RenderWindow(window);
    window.create(sf::VideoMode(800, 600), "My window");

    sf::Texture img;
    if(!img.loadFromFile("colorfull small.jpg")) return 0;

    sf::Sprite sprite;
    sprite.setTexture(img);

    window.draw(sprite);
    window.display();

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed) window.close();
        }
    }
    return 0;
}

编译器:

目录:

谢谢

【问题讨论】:

    标签: c++ xcode image load sfml


    【解决方案1】:

    引用the official tutorial:

    该项目在 main.cpp 中带有一个基本示例和一个辅助函数:std::string resourcePath(void);在 ResourcePath.hpp 和 ResourcePath.mm 中。如所提供的示例所示,此功能的用处在于方便地访问应用程序包的 Resources 文件夹。

    这意味着您必须编写如下内容:

    #include "ResourcePath.hpp"
    :
    :
    if (!icon.loadFromFile(resourcePath() + "colorfull small.jpg")) {
    :
    :
    

    (受项目模板生成的默认代码启发。)

    nvoigt 的回答也很重要。

    【讨论】:

      【解决方案2】:

      我有点惊讶“正确答案”有一条评论说它不起作用......无论如何:

      您的渲染(绘图)需要在循环的每次迭代中进行:

      int main(int argc, const char * argv[])
      {
          sf::RenderWindow(window);
          window.create(sf::VideoMode(800, 600), "My window");
      
          sf::Texture img;
          if(!img.loadFromFile("colorfull small.jpg")) return 0;
      
          sf::Sprite sprite;
          sprite.setTexture(img);
      
           while(window.isOpen())
          {
              sf::Event event;
              while(window.pollEvent(event))
              {
                  if(event.type == sf::Event::Closed) window.close();
              }
      
              // drawing needs to take place every loop right HERE 
              window.clear();
              window.draw(sprite);
              window.display();
          }
      
          return 0;
      }
      

      【讨论】:

      • 为什么每次调用事件时都需要绘制它?我只想绘制一次并让它显示出来,而不做任何更改。
      • 计算机不是这样工作的。如果你绘制一次,你的窗口不会重绘,它看起来就像你的程序崩溃了。运行程序意味着一直在绘制图形。如果您有业务程序,他们可能会在发生特殊事件时满足于重绘,例如大小更改或其他窗口覆盖您的窗口并被移除,但一般来说......您经常重绘。只看教程。他们甚至说您不必害怕每帧重绘 1000 个项目……这远低于当今显卡所能提供的能力。
      【解决方案3】:

      这个错误有几个原因:

      来自sfml documentation

      不支持某些格式选项,例如渐进式 jpeg。

      找不到文件,因为文件名中有空格(重命名文件,例如 colourll_small.jpg)

      程序的工作目录没有.jpg文件(可以使用getcwd(NULL)打印(#include <unistd.h>有))

      【讨论】:

      • 谢谢。我将名称更改为 colourful_small.jpg 但没有帮助。然后我下载了一个 PNG 文件来确定渐进式 JPEG 是否是问题所在。它给了我同样的错误。我不确定我应该如何做你解决方案的最后一部分。我包括了你告诉我的图书馆,我做了 cout
      猜你喜欢
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 2012-11-29
      • 2012-04-21
      • 2016-03-16
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多