Code::Blocks 可执行文件没有纹理

Code::Blocks executable file doesn't have textures

我已经使用 Code::Blocks 中的 SFML 库制作了一个基本程序,现在我只想将它发送给朋友。我已经在调试和发布模式下构建了我的程序,但是每当我 运行 我的 bin 中的 .exe 文件时,它根本不会加载纹理。当然,当我在 Code::Blocks 客户端中 运行 它时,它工作得很好。我不确定我是否需要打包我的工作或单独发送文件或什么,但非常感谢任何帮助

这是代码,以防它有帮助(我知道它不漂亮):

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <iostream>
#include <Windows.h>
int main(){

    sf::RenderWindow Window;
    Window.create(sf::VideoMode(800, 600), "Enjoy :)");

    sf::Clock clock;

    int pixelFindX = 0, pixelFindY = 0;
    int oldLoc = 0, newLoc = 0;
    int changeVal = 1;
    int tripleCheck = 0;
    int xLoc = 0, yLoc = 100;

    double timeChecker = 0;

    bool execute = false, secondExecute = false;
    bool oldSide = false, newSide = false, changer = true;
    bool drawLeft = false, drawRight = false, drawCenter = false;

    sf::Texture pTexture;
    sf::Sprite playerImage;
    if(!pTexture.loadFromFile("Pixel Dude.png")){
        std::cout << "Could not load pTexture file" << std::endl;
    }

    sf::Texture bTexture;
    sf:: Sprite backgroundImage;
    if(!bTexture.loadFromFile("molester moon background IS DONE.jpg")){
        std::cout << "Could not load bTexture file" << std::endl;
    }

    sf::Texture bridgeTexture;
    sf::Sprite bridgeImageL;
    sf::Sprite bridgeImageR;
    if(!bridgeTexture.loadFromFile("pixel bridge.jpg")){
        std::cout << "Could not load bridgeTexture file" << std::endl;
    }

    sf::Texture trophyTexture;
    sf::Sprite trophyImage;
    if(trophyTexture.loadFromFile("trophy pixeled.png")){
        std::cout << "Could not load trophyTexture file" << std::endl;
    }

    sf::String stringL = "Move to the Left!", stringR = "Move to the Right!", stringC = "You are currently balanced.";
    sf::String stringGo = stringL;
    sf::Font font;

    if(!font.loadFromFile("arial.ttf")){
        std::cout << "Could not load font file" << std::endl;
    }

    sf::Text text(stringGo, font, 50);
    text.setPosition(xLoc, yLoc);
    text.setStyle(sf::Text::Bold);

    playerImage.setTexture(pTexture);
    playerImage.setPosition(336, 355);
    playerImage.setScale(4,4);
    playerImage.setTextureRect(sf::IntRect(pixelFindX*32, pixelFindY*32, 32, 32));

    backgroundImage.setTexture(bTexture);
    backgroundImage.setPosition(0,0);

    bridgeImageL.setTexture(bridgeTexture);
    bridgeImageL.setPosition(0, 450);
    bridgeImageL.setScale(4.17,4.68);

    bridgeImageR.setTexture(bridgeTexture);
    bridgeImageR.setPosition(400,450);
    bridgeImageR.setScale(4.17, 4.68);

    trophyImage.setTexture(trophyTexture);
    trophyImage.setPosition(600, 305);
    trophyImage.setScale(2,2);

    sf::View view;
    sf::View viewReg;

    view.reset(sf::FloatRect(0, 0, 800, 600));
    view.setViewport(sf::FloatRect(0, 0, 1, 1));

    viewReg.reset(sf::FloatRect(0, 0, 800, 600));
    viewReg.setViewport(sf::FloatRect(0, 0, 1, 1));

    std::cout << "Player Dimensioms: Width = " << playerImage.getGlobalBounds().width << ", Height = " << playerImage.getGlobalBounds().height << std::endl;

    while(Window.isOpen()){
        clock.restart();
        sf::Event Event;
        while(Window.pollEvent(Event)){

            switch(Event.type){
            case sf::Event::Closed:
                Window.close();
                break;
            case sf::Event::KeyPressed:
                if(Event.key.code == sf::Keyboard::Escape){
                    Window.close();
                }
                break;
            }
        }

        //std::cout << timeChecker << std::endl;

        timeChecker += clock.getElapsedTime().asSeconds();
        if(timeChecker >= .00025){
            execute = true;
            timeChecker = 0;
            //std::cout << "execute = true" << std::endl;
        }

        oldLoc = playerImage.getPosition().x;
        oldSide = changer;

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::D) && execute){
            playerImage.move(1, 0);
            pixelFindY = 2;
            tripleCheck++;
            changer = true;
            //std::cout << "d called" << std::endl;
        } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A) && execute){
            playerImage.move(-1, 0);
            pixelFindY = 1;
            tripleCheck++;
            changer = false;
            //std::cout << "a called" << std::endl;
        }
        if(playerImage.getPosition().x < -96){
            playerImage.setPosition(800, playerImage.getPosition().y);
        } else if(playerImage.getPosition().x > 800){
            playerImage.setPosition(-96, playerImage.getPosition().y);
        }

        newLoc = playerImage.getPosition().x;
        newSide = changer;

        pixelFindX += changeVal;
        if(pixelFindX > 2){
            pixelFindX -= 2;
            changeVal = -1;
        } else if(pixelFindX < 0){
            pixelFindX += 2;
            changeVal = 1;
        }

        if(oldLoc != newLoc && tripleCheck > 40 || oldSide != newSide){
            playerImage.setTextureRect(sf::IntRect(pixelFindX*32, pixelFindY*32, 32, 32));
            tripleCheck = 0;
            //std::cout << "Yes called, pixelFindX = " << pixelFindX << ", tripleCheck = " << tripleCheck << std::endl;
        } else {
            //std::cout << "-----Not called, pixelFindX = " << pixelFindX << ", tripleCheck = " << tripleCheck << std::endl;
        }
        execute = false;

/*        if(playerImage.getPosition().x + 128 > trophyImage.getPosition().x && playerImage.getPosition().x < trophyImage.getPosition().x + 192){
            view.rotate(.0001);
        }
*/
        if(playerImage.getPosition().x > 500 - 128){
            view.rotate(-.01);
            drawRight = true;
        } else if (playerImage.getPosition().x < 300 - 128){
            view.rotate(.01);
            drawLeft = true;
        } else{
            drawCenter = true;
        }
        //std::cout << "Player X: " << playerImage.getPosition().x + 128 << ", Trophy X: " << trophyImage.getPosition().x << std::endl;

        if(drawLeft){
            stringGo = stringR;
            xLoc = 0;
        } else if (drawRight){
            stringGo = stringL;
            xLoc = 800 - text.getGlobalBounds().width - 10;
        } else if(drawCenter){
            stringGo = stringC;
            xLoc = 400 - text.getGlobalBounds().width / 2;
        }

        text.setPosition(xLoc, yLoc);
        text.setString(stringGo);

        Window.setView(viewReg);
        Window.draw(backgroundImage);
        Window.draw(text);

        Window.setView(view);

        Window.draw(bridgeImageL);
        Window.draw(bridgeImageR);
       // Window.draw(trophyImage);
        Window.draw(playerImage);

        Window.display();
        Window.clear();

        drawLeft = false;
        drawRight = false;
        drawCenter = false;
        //Sleep(50);

    }
}

将编译后的可执行文件复制到一个新文件夹中,并将纹理也粘贴到那里。您可以将此文件夹发送给您的朋友(例如在 rar 存档中)。纹理不会加载,因为可能纹理不在 bin/debugbin/release 文件夹。在另一个地方,调试模式设置为工作 directory.If 你想改变它(我不推荐)你可以去 Project -> Properties -> Build targets -> [name of target] -> Execution working dir 并将其更改为编译后的可执行文件所在的目录。