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

时间:2015-07-30 18:08:26

标签: c++ export codeblocks sfml

我在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);

    }
}

1 个答案:

答案 0 :(得分:1)

将已编译的可执行文件复制到新文件夹中,并将纹理粘贴到那里。您可以将此文件夹发送给您的朋友(例如,在rar-archive中)。纹理不会加载,因为纹理可能不在 bin / debug bin中/ release 文件夹。另一个位于调试模式的地方被设置为工作目录。如果你想改变它(我不推荐)你可以去项目 - &gt;属性 - &gt;构建目标 - &gt; [目标名称] - &gt;执行工作目录并将其更改为已编译的可执行文件所在的目录。