C ++发布程序无法在visual studio 2015中加载图像和字体

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C ++发布程序无法在visual studio 2015中加载图像和字体相关的知识,希望对你有一定的参考价值。

我为我的项目制作了一个程序,但由于某些原因没有加载任何图像或字体

程序运行得很好,编译器根本没有说任何错误,但是当程序运行时,它是一个黑盒子

我的代码片段:

VideoMode vm(1600, 1200);
    RenderWindow window(vm, "francais project", Style::Resize);

    Texture texturebackground;
    texturebackground.loadFromFile("graphics/white.png");
    Sprite background;
    background.setTexture(texturebackground);

    Font font;
    font.loadFromFile("fonts/cour.ttf");

    Text question;
    question.setFont(font);
    question.setFillColor(Color::Black);
    question.setCharacterSize(60);
    question.setPosition(50, 50);

    Text answer1;
    answer1.setFont(font);
    answer1.setFillColor(Color::Black);
    answer1.setCharacterSize(50);
    answer1.setPosition(200, 200);

    Text answer2;
    answer2.setFont(font);
    answer2.setFillColor(Color::Black);
    answer2.setString("2.voiture");
    answer2.setCharacterSize(50);
    answer2.setPosition(200, 300);

    Text answer3;
    answer3.setFont(font);
    answer3.setFillColor(Color::Black);
    answer3.setCharacterSize(50);
    answer3.setPosition(200, 400);

    Text answer4;
    answer4.setFont(font);
    answer4.setFillColor(Color::Black);
    answer4.setString("2.voiture");
    answer4.setCharacterSize(50);
    answer4.setPosition(200, 500);

    //for showing the score
    Text textScore;
    textScore.setFont(font);
    textScore.setFillColor(Color::Black);
    textScore.setCharacterSize(100);
    textScore.setPosition(100, 100);

    int intScore = 0;

    //bread textures

    Texture textureBread1;
    textureBread1.loadFromFile("graphics/bread1.png");

    Texture textureBread2;
    textureBread2.loadFromFile("graphics/bread2.png");

    Texture textureBaguette;
    textureBaguette.loadFromFile("graphics/baguette.png");

    Texture textureBread4;
    textureBread4.loadFromFile("graphics/bread4.png");


    //tower textures
    Texture textureTower1;
    textureTower1.loadFromFile("graphics/tower1.png");

    Texture textureEiffelTower;
    textureEiffelTower.loadFromFile("graphics/eiffel_tower.png");

    Texture textureTower3;
    textureTower3.loadFromFile("graphics/tower3.png");

    Texture textureTower4;
    textureTower4.loadFromFile("graphics/tower4.png");

    //tower sprites
    Sprite spriteTower1;
    Sprite spriteEiffelTower;
    Sprite spriteTower3;
    Sprite spriteTower4;

    spriteTower1.setTexture(textureTower1);
    spriteEiffelTower.setTexture(textureEiffelTower);
    spriteTower3.setTexture(textureTower3);
    spriteTower4.setTexture(textureTower4);

    spriteTower1.setPosition(300, 200);
    spriteEiffelTower.setPosition(1000, 200);
    spriteTower3.setPosition(300, 600);
    spriteTower4.setPosition(1000, 600);

    //brand textures
    Texture textureChanel;
    textureChanel.loadFromFile("graphics/chanel.png");

    Texture texturePerfume2;
    texturePerfume2.loadFromFile("graphics/perfume2.png");

    Texture texturePerfume3;
    texturePerfume3.loadFromFile("graphics/perfume3.png");

    Texture texturePerfume4;
    texturePerfume4.loadFromFile("graphics/perfume4.png");

    //brand sprites
    Sprite spriteChanel;
    Sprite spritePerfume2;
    Sprite spritePerfume3;
    Sprite spritePerfume4;

    //sets texture of perfumes
    spriteChanel.setTexture(textureChanel);
    spritePerfume2.setTexture(texturePerfume2);
    spritePerfume3.setTexture(texturePerfume3);
    spritePerfume4.setTexture(texturePerfume4);

    spriteChanel.setPosition(280, 200);
    spritePerfume2.setPosition(1000, 200);
    spritePerfume3.setPosition(300, 700);
    spritePerfume4.setPosition(1000, 650);

    //textures of brands

    Texture textureBrand1;
    textureBrand1.loadFromFile("graphics/brand1.png");

    Texture textureBrand2;
    textureBrand2.loadFromFile("graphics/brand2.png");

    Texture textureBrand3;
    textureBrand3.loadFromFile("graphics/brand3.png");

    Texture texturePaul;
    texturePaul.loadFromFile("graphics/paul.png");

    //brands sprites
    Sprite spriteBrand1;
    Sprite spriteBrand2;
    Sprite spriteBrand3;
    Sprite spritePaul;

    //sets texture of sprites
    spriteBrand1.setTexture(textureBrand1);
    spriteBrand2.setTexture(textureBrand2);
    spriteBrand3.setTexture(textureBrand3);
    spritePaul.setTexture(texturePaul);

    spriteBrand1.setPosition(280, 200);
    spriteBrand2.setPosition(1000, 200);
    spriteBrand3.setPosition(300, 700);
    spritePaul.setPosition(1000, 650);

    //texture flags
    Texture textureFlagFrance;
    textureFlagFrance.loadFromFile("graphics/flag_france.png");

    Texture textureFlag2;
    textureFlag2.loadFromFile("graphics/flag2.png");

    Texture textureFlag3;
    textureFlag3.loadFromFile("graphics/flag3.png");

    Texture textureFlag4;
    textureFlag4.loadFromFile("graphics/flag4.png");

    //flag sprites
    Sprite spriteFlagFrance;
    Sprite spriteFlag2;
    Sprite spriteFlag3;
    Sprite spriteFlag4;

    spriteFlagFrance.setTexture(textureFlagFrance);
    spriteFlag2.setTexture(textureFlag2);
    spriteFlag3.setTexture(textureFlag3);
    spriteFlag4.setTexture(textureFlag4);

    spriteFlagFrance.setPosition(300, 200);
    spriteFlag2.setPosition(300, 425);
    spriteFlag3.setPosition(300, 625);
    spriteFlag4.setPosition(300, 850);

    //bread sprites

    Sprite spriteBread1;
    Sprite spriteBread2;
    Sprite spriteBread3;
    Sprite spriteBread4;

    spriteBread1.setTexture(textureBread1);
    spriteBread2.setTexture(textureBread2);
    spriteBread3.setTexture(textureBaguette);
    spriteBread4.setTexture(textureBread4);

    spriteBread1.setPosition(300, 200);
    spriteBread2.setPosition(300, 400);
    spriteBread3.setPosition(300, 600);
    spriteBread4.setPosition(300, 800);

    Texture textureArrow;
    textureArrow.loadFromFile("graphics/Arrow.png");
    Sprite spriteArrow1;
    Sprite spriteArrow2;
    Sprite spriteArrow3;
    Sprite spriteArrow4;

    spriteArrow1.setTexture(textureArrow);
    spriteArrow2.setTexture(textureArrow);
    spriteArrow3.setTexture(textureArrow);
    spriteArrow4.setTexture(textureArrow);

    spriteArrow1.setPosition(100, 200);
    spriteArrow2.setPosition(100, 400);
    spriteArrow3.setPosition(100, 600);
    spriteArrow4.setPosition(100, 800);

我一直在程序的终端而不是编译器中收到此错误消息:

Failed to load image "graphics/white.png". Reason: Unable to open file
Failed to load font "fonts/cour.ttf" (failed to create the font face)
Failed to load image "graphics/bread1.png". Reason: Unable to open file
Failed to load image "graphics/bread2.png". Reason: Unable to open file
Failed to load image "graphics/baguette.png". Reason: Unable to open file
Failed to load image "graphics/bread4.png". Reason: Unable to open file
Failed to load image "graphics/tower1.png". Reason: Unable to open file
Failed to load image "graphics/eiffel_tower.png". Reason: Unable to open file
Failed to load image "graphics/tower3.png". Reason: Unable to open file
Failed to load image "graphics/tower4.png". Reason: Unable to open file
Failed to load image "graphics/chanel.png". Reason: Unable to open file
Failed to load image "graphics/perfume2.png". Reason: Unable to open file
Failed to load image "graphics/perfume3.png". Reason: Unable to open file
Failed to load image "graphics/perfume4.png". Reason: Unable to open file
Failed to load image "graphics/brand1.png". Reason: Unable to open file
Failed to load image "graphics/brand2.png". Reason: Unable to open file
Failed to load image "graphics/brand3.png". Reason: Unable to open file
Failed to load image "graphics/paul.png". Reason: Unable to open file
Failed to load image "graphics/flag_france.png". Reason: Unable to open file
Failed to load image "graphics/flag2.png". Reason: Unable to open file
Failed to load image "graphics/flag3.png". Reason: Unable to open file
Failed to load image "graphics/flag4.png". Reason: Unable to open file
Failed to load image "graphics/Arrow.png". Reason: Unable to open file
答案

这是我第一次使用SFML时遇到的问题。因此,摆脱这些错误并将文件加载到程序中的最简单方法是在.exe或.cpp文件所在的位置添加要加载的每个文件。这是一开始的解决方案,但我仍然没有达到需要更换目录的程度。 Here is a picture of how it should look

您可以看到我有不同文件的文件夹,就像你一样,它的方式相同,不仅仅是相同的地方(如果是这种情况)。

但是,如果它仍然无效,您可能需要检查是否已正确链接.dll文件

另一答案

请确保图形文件夹和字体文件夹与生成的可执行文件具有相同的路径。

以上是关于C ++发布程序无法在visual studio 2015中加载图像和字体的主要内容,如果未能解决你的问题,请参考以下文章

C 文件无法在 c++ Visual Studio 中编译

无法在 Visual Studio 中发布 ASP.Net 应用程序

无法在 Windows XP 上运行 Visual Studio 2012 程序

为啥在 Visual Studio 中构建失败,因为它无法从引用的程序集中解析命名空间?

C++ Visual Studio Express 2010 无法启动程序错误

(Visual Studio 2015) 无法创建 Win32 C++ 应用程序