2014-09-27 109 views
1

我是初學者,所以請裸露在我身邊。我正嘗試使用SFML 2.1,C++和MS Visual Studio Professional 2013將圖像輸出到屏幕上。嘗試將文件加載到紋理時出現意外錯誤。它輸出一大堆隨機字符。我確定它是如何使用Visual Studio配置SFML庫的,或者是否存在代碼問題。有誰能解決這個問題嗎?謝謝。無法使用C++和SFML 2.1加載圖像文件作爲紋理

這裏是什麼樣子,當我運行程序(http://i.stack.imgur.com/uMdLT.png)像截圖:

這是我的代碼:

#include <SFML/Graphics.hpp> 
#include <iostream> 
#include <string> 
using namespace std; 

int main() { 

    sf::RenderWindow window; 
    window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing 

    sf::Texture jetTexture; 
    sf::Sprite jetImage; 

    // Getting Error here! 
    if (!jetTexture.loadFromFile("fighter jet.png")) 
     throw std::runtime_error("Could not load fighter jet.png"); 

    jetImage.setTexture(jetTexture); 

    while (window.isOpen()) 
    { 
     sf::Event event; 
     while (window.pollEvent(event)) 
     { 
      if (event.type == sf::Event::Closed) 
       window.close(); 
     } 
     window.draw(jetImage); 
     window.display(); 
    } 

    return 0; 
} 

對於所有的配置屬性,它們看起來就像這樣:

鏈接器 - >常規(http://i.stack.imgur.com/NZg7P.png):

鏈接器 - >輸入(http://i.stack.imgur.com/1tPaB.png):

**請注意,如果有一天,我也沒有配置SFML庫,然後我會從我的系統說msvcr110d.dll is missing得到一個錯誤。

編輯:請參閱更新代碼。

+0

我剛剛使用VS Express 2013與你確切的源代碼,我得到了同樣的錯誤!所以它不只是你的機器。我試圖找出它爲什麼會發生! – 2014-10-06 01:28:02

+0

將'fighter jet.png'重命名爲'fighterjet.png',並將其放入Project文件夾和Debug文件夾中,然後嘗試再次運行它。 – Romeo 2014-10-07 10:56:08

回答

5

好,我設法解決這個問題,以下是你需要做的:

  1. 設置你的子系統WINDOWS:
  2. 添加 「SFML-main.lib」 你的庫:
  3. 將您的運行時庫更改爲/ MD。這是因爲您沒有使用SFML 2.1庫的調試版本(並且可能無法在VS2013中使用)。
  4. 確保您的「fighter jet.png」圖像位於正確的位置。默認情況下,Visual Studio使用Project Directory作爲工作目錄。因此,將png放在與vcxproj文件相同的文件夾中。
+0

非常感謝。 +1,我給你賞金! – Alias 2014-10-07 15:22:51

+0

解決了我與繪製精靈的問題;)+1 – c0ntrol 2015-09-08 18:20:45

1

你確定圖片在你程序的執行目錄下嗎? 您應該避免使用文件名稱中的空格(fighter_jet.png)。

如果您不確定執行目錄,請嘗試使用絕對路徑(以確保它是路徑問題,而不是圖片的問題)。

我希望它有幫助。

我試過這個代碼在我的系統(Xcode - OSX)上,我的圖片之一,它的工作原理。 你用另一張照片試過了嗎?

#include <SFML/Graphics.hpp> 
#include <iostream> 
#include <string> 
using namespace std; 

int main() { 

    sf::RenderWindow window; 
    window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing 

    sf::Texture jetTexture; 
    sf::Sprite jetImage; 

    // Getting Error here! 
    if (!jetTexture.loadFromFile("fighter jet.png")) 
     throw std::runtime_error("Could not load fighter jet.png"); 

    jetImage.setTexture(jetTexture); 

    while (window.isOpen()) 
    { 
     sf::Event event; 
     while (window.pollEvent(event)) 
     { 
      if (event.type == sf::Event::Closed) 
       window.close(); 
     } 
     window.draw(jetImage); 
     window.display(); 
    } 

    return 0; 
} 
+0

我試過你提出的兩種情況(絕對路徑和刪除文件名中的空格),我仍然得到相同的錯誤 – Alias 2014-09-29 12:39:05

+0

好吧,我已經添加了一個遊戲循環,你可以試試嗎? – 2014-09-29 12:57:21

+0

仍然收到相同的錯誤。 – Alias 2014-09-29 13:05:27