0

Sorry to bother you! Right now I'm having a "bug" in my game right now and if someone could point out what I'm doing wrong it would be very helpful. What is happening is when I run my code through Xcode the TileMap loads the file fine.

However when I get the executable generated, copy and paste it into a different place, and then paste all the needed data files into the folder (.png files, .txt files), the game works fine except for the fact that the tilemap won't load.

I'm not sure if this bug is due to the file locations changing when I run it in IDE and in stand alone.

Images: (Sorry I don't have enough reputation so here's links)

Game working run from Xcode: enter image description here Game not run from Xcode: enter image description here

This is the part of the code in TileMap.cpp that deals with loading the data from a text file into an array (just to show you it works):

int t_map[20][15] {};

TileMap::TileMap(std::string fileName, std::string picFile)
{
    std::ifstream inFS;
    inFS.open(fileName.c_str());
    if(!(inFS.is_open()))
    {
        std::cout << "Could not open file.\n";
        return;
    }

    //controlls row
    int tt = 0;

    while(!inFS.eof())
    {
        char buff[100];

        //move to buffer thing
        inFS.getline(buff, 100);

        const char * token[20] = {};
        int j = 0;

        token[0] = strtok(buff, ",");

        if(token[0])
        {
            for(j=1;j<COLMAX;j++)
            {
                token[j] = strtok(0, ",");
                if(!token[j])
                    break;
            }
        }

        for(int i=0;i<COLMAX;i++)
        {
            t_map[i][tt] = atoi(token[i]);
        }
        tt++;
    }

    inFS.close();
    //ends the loading the file and starts the image stuff
    TextureManager::Instance()->load(picFile, "tileMap", Game::Instance()->getRenderer());
}

Contents of text file I pass:

2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2

Thank you very much for reading and helping.

KojinKuro
  • 1
  • 3
  • 4
    possible duplicate of [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Wintermute Apr 02 '15 at 08:09
  • How do you build the `fileName` and `picFile` parameters? Have you checked the paths are actually correct? In other words, does it fail to read from the files or does it not find the files at all? – Ionut Apr 02 '15 at 08:34
  • I assume the file path is correct because of the fact that it works when run in xCode. – KojinKuro Apr 02 '15 at 08:46
  • 2
    You shouldn't assume anything, especially if you're using relative or hardcoded paths. When running through xCode, the IDE might change the current folder for your app so relative paths that work through the IDE might resolve to completely different paths when the application is run standalone. – Ionut Apr 02 '15 at 09:02
  • Can you tell me how to go about fixing that then please? – KojinKuro Apr 02 '15 at 09:03
  • First check that it's actually a problem, from my part it was only a suggestion based on the code you shared. If it turns out it really is a problem, then supply or compute the correct paths to those files in a way that would work or make sense for your particular case. You haven't shown how you do this in code so I can't guess. – Ionut Apr 02 '15 at 09:23
  • @lonut is correct. Under OSX and iOS the resources are put into the app bundle and the process *current working directory* won't be the parent of this resource folder. When developing/debugging the app under Xcode you can arrange for the process *current working directory* to be the parent, hence the potential difference. You need to solve it by asking the framework (whatever that is) to return the root app bundle folder and create the fullpath to the file using that. – trojanfoe Apr 02 '15 at 09:28

1 Answers1

0

I found the answer to my own problem. I needed to get the base path and then make the systems search for the file from there.

KojinKuro
  • 1
  • 3