nicknicknick111
nicknicknick111

Reputation: 69

Code::Blocks executable file doesn't have textures

I've made a basic program using the SFML library in Code::Blocks, and now all I want to do is send it to a friend. I've built my program in both debug and release mode, yet whenever I run the .exe file from my bin, it simply will not load the textures. Of course when I run it within the Code::Blocks client, it works perfectly. I'm not sure if I need to package my work or send the files separately or what, but any help would be much appreciated

Here's the code in case it's helpful somehow (I know it's not pretty):

#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);

    }
}

Upvotes: 0

Views: 244

Answers (1)

Bruno Zell
Bruno Zell

Reputation: 8541

Copy the compiled executable into a new folder and paste there the textures too. This folder you can send your friend (e.g. in a rar-archive).The textures won't load because probably the textures weren't in the bin/debug or bin/release folder. There are in another place wich is in the debugging mode is set as the working directory.If you want to change that (what I would not recommend) you can go to Project -> Properties -> Build targets -> [name of target] -> Execution working dir and change it to the directory where the compiled executables went.

Upvotes: 1

Related Questions