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

时间:2019-03-14 11:22:42

标签: c++ sfml

我为我的项目制作了一个笔迹图,但是由于某种原因,没有图像或字体加载

程序运行正常,编译器根本没有说任何错误,但是当程序运行以供发布时,它是一个黑框

我的代码段:

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

您会发现我像以前一样拥有用于存放不同文件的文件夹,这是相同的方式,而不仅是在同一位置(如果是这种情况)。

但是,如果仍然无法正常工作,则可能要检查是否已正确链接.dll文件