2013-03-21 69 views
0

当我试图在SFML 2.0中显示图像时,它无法找到图像。我正在使用Visual Studio Express C++ 2010.这是我的代码。未找到图像

#include "stdafx.h" 

using namespace std; 

int main() 
{ 
    // Create the main window 
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window"); 

    // Load a sprite to display 
    sf::Texture texture; 
    if (!texture.loadFromFile("image.png")) 
    { 
     cout << "Could not find file image.png" << endl; 
     system("PAUSE"); 
    } 
    sf::Sprite sprite(texture); 

    // Create a graphical text to display 


    // Load a music to play 

    // Start the game loop 
    while (window.isOpen()) 
    { 
     // Process events 
     sf::Event event; 
     while (window.pollEvent(event)) 
     { 
      // Close window : exit 
      if (event.type == sf::Event::Closed) 
       window.close(); 
     } 

     // Clear screen 
     window.clear(); 

     // Draw the sprite 
     window.draw(sprite); 

     // Draw the string 

     // Update the window 
     window.display(); 
    } 

    return EXIT_SUCCESS; 
} 

但是,当我改变图像目录到具体的 “C:\用户\大通\文档\ Visual Studio 2010的\项目\ Boonce \ image.png” 它的工作原理。我需要将image.png放在哪里才能显示,而不显示具体内容。我听到有人说工作目录,我不知道它是什么。如果它是th项目文件夹(Projects \ Boonce \),那么我已经尝试过了,并且它不起作用。

回答

1

它应该放置在与.exe文件相同的目录中,或者取决于您在某个其他目录(可能是项目目录)中的项目设置。

+0

好吧,只有当我启动.exe时才工作,但是当我从VS运行我的项目时,找不到图像。 – freemann098 2013-03-21 04:07:15

+0

我认为这是因为exe可视化工作室使用的是在不同的目录中(如果您使用的是不同的构建设置)。尝试在Debug或Release目录中。 – 2013-03-21 04:07:56

+0

我目前正在通过发行版运行它,并将映像放入发布目录中。 – freemann098 2013-03-21 04:09:30

0

在Visual Studio中,你需要设置你的工作目录

转到:

  • 项目
  • PROJECTNAME)性能
  • 调试
  • 工作目录

并设置您的资产目录所在的路径。

默认为$(PROJECTDIR),在你的的.vcproj文件所在的目录。