Thread: Program can't locate resource when run outside IDE

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    Program can't locate resource when run outside IDE

    I'm really stumped. I'm using Code::Blocks and SDL.
    I'm trying to load some images, and it runs fine through the Code::Blocks, but when run through the explorer it fails every time.
    I've tried putting the images essentially everywhere, I even used to absolute path to them and it still couldn't find 'em.

    Here is the function that doesn't give any trouble in the IDE, but won't work outside:
    Code:
    bool loadResources()
    {
        /*Load boards*/
        surfaces[SURF_TTT1] = IMG_Load("ttt1.png");
        if (!surfaces[SURF_TTT1]) {
    		return false;
    	}
    	surfaces[SURF_TTT2] = IMG_Load("ttt2.png");
        if (!surfaces[SURF_TTT2]) {
    		return false;
    	}
    	surfaces[SURF_TTT3] = IMG_Load("ttt3.png");
        if (!surfaces[SURF_TTT3]) {
    		return false;
    	}
    	surfaces[SURF_TTT4] = IMG_Load("ttt4.png");
        if (!surfaces[SURF_TTT4]) {
    		return false;
    	}
    	surfaces[SURF_TTT5] = IMG_Load("ttt5.png");
        if (!surfaces[SURF_TTT5]) {
    		return false;
    	}
    
    	/*Load Xs*/
    	surfaces[SURF_X1] = IMG_Load("x1.png");
        if (!surfaces[SURF_X1]) {
    		return false;
    	}
    	surfaces[SURF_X2] = IMG_Load("x2.png");
        if (!surfaces[SURF_X2]) {
    		return false;
    	}
    	surfaces[SURF_X3] = IMG_Load("x3.png");
        if (!surfaces[SURF_X3]) {
    		return false;
    	}
    	surfaces[SURF_X4] = IMG_Load("x4.png");
        if (!surfaces[SURF_X4]) {
    		return false;
    	}
    	surfaces[SURF_X5] = IMG_Load("x5.png");
        if (!surfaces[SURF_X5]) {
    		return false;
    	}
    
    	/*Load Os*/
    	surfaces[SURF_O1] = IMG_Load("o1.png");
        if (!surfaces[SURF_O1]) {
    		return false;
    	}
    	surfaces[SURF_O2] = IMG_Load("o2.png");
        if (!surfaces[SURF_O2]) {
    		return false;
    	}
    	surfaces[SURF_O3] = IMG_Load("o3.png");
        if (!surfaces[SURF_O3]) {
    		return false;
    	}
    	surfaces[SURF_O4] = IMG_Load("o4.png");
        if (!surfaces[SURF_O4]) {
    		return false;
    	}
    	surfaces[SURF_O5] = IMG_Load("o5.png");
        if (!surfaces[SURF_O5]) {
    		return false;
    	}
    
    	/*Load Positions*/
    	imagePos[POS_TTT].x = (int)((0.5*xr)-(0.5*400));
        imagePos[POS_TTT].y = (int)((0.5*yr)-(0.5*400));
        imagePos[POS_1].x = imagePos[POS_TTT].x + 15;
        imagePos[POS_1].y = imagePos[POS_TTT].y + 15;
        imagePos[POS_2].x = imagePos[POS_TTT].x + 15 + 130;
        imagePos[POS_2].y = imagePos[POS_TTT].y + 15;
        imagePos[POS_3].x = imagePos[POS_TTT].x + 15 + 260;
        imagePos[POS_3].y = imagePos[POS_TTT].y + 15;
        imagePos[POS_4].x = imagePos[POS_TTT].x + 15;
        imagePos[POS_4].y = imagePos[POS_TTT].y + 15 + 130;
        imagePos[POS_5].x = imagePos[POS_TTT].x + 15 + 130;
        imagePos[POS_5].y = imagePos[POS_TTT].y + 15 + 130;
        imagePos[POS_6].x = imagePos[POS_TTT].x + 15 + 260;
        imagePos[POS_6].y = imagePos[POS_TTT].y + 15 + 130;
        imagePos[POS_7].x = imagePos[POS_TTT].x + 15;
        imagePos[POS_7].y = imagePos[POS_TTT].y + 15 + 260;
        imagePos[POS_8].x = imagePos[POS_TTT].x + 15 + 130;
        imagePos[POS_8].y = imagePos[POS_TTT].y + 15 + 260;
        imagePos[POS_9].x = imagePos[POS_TTT].x + 15 + 260;
        imagePos[POS_9].y = imagePos[POS_TTT].y + 15 + 260;
    
        return true;
    }
    It loads some static images, and does a few other things. It returns false if any of the images could not be loaded.

    Does anyone have suggestions?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Are you double clicking on the exe to run it? Are all those images in the same directory as the exe? Do you know which image is failing to load (or are they all failing)?

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    I am double clicking it, all if the images are in the same directory, and the first image that it attempts to load fails. I'm pretty sure they will all fail.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Does the capitalization of file names match? (to change capitalization of a file name you will have to rename it to something else and then rename it back, since Windows won't let you just change the capitalization for some reason that's beyond me...)

    I'm not sure if it matters on Windows but...

    If you are not using Windows, capitalization does matter.

  5. #5
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Yes, I just double checked, also I think if there was a problem with spelling/capitalization it wouldn't run through the IDE either.

    I took out the code that closes the program when it can't load the images, and everything runs just as it should - without the images. Really weird. What could Code::Blocks be doing to make it run properly?

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I'm not sure about Code::Blocks but this is a common problem in MSVC. The default place for files with no path is the make folder in MSVC. Normally if you specify a specific folder where your resources will be and do not rely on project settings for the location then it should work just fine.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    The working directory is obviously different when you run from inside the IDE. Try running from a command prompt in the directory. I'm guessing that will work, too.

    To fix, create a resource loader class that manages the loading of resources based on the working directory of the running program (which you might have to get using a platform specific call). I usually put them in subdirs like images, sounds, etc.

  8. #8
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    I haven't had a chance to work on this for a bit, but I am still stuck. I think you're right about this working directory problem. I ended up recreating the whole project, so I could get everything reset. The problem is still there but it's changed a little bit. Now the program won't run through codeblocks, or by double clicking the exe.

    I did what medievalelks suggested and put the images in a subdirectory named images.

    I put this images directory both in the main project folder and also in the folder containing the exe, the problem persists.

    I'm starting to wonder if it's really a matter of not being able to locate the images.

  9. #9
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    I just tested the code using SDL_LoadBMP() instead of the SDL_image IMG_Load() and it worked just fine. The problem I guess is with IMG_Load(). I'm going to do some Googling, please let me know if you know how to fix the problem.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Id suggest doing some simple debugging, trace your file locations. Display your file location that your trying to load from, before loading it, then compare it to the local destination of said file. If you can determine that your file location is absolutely correct based off where you have run the program (Within the executable folder, or the main.cpp folder). Then you know, you have an error not due with file location, which narrows it down pretty quick.
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  11. #11
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    I fixed it. I simply forgot to put the pnglib dll in the same directory. I don't know too much about how dll's work, but it seems like normally when you are missing a dll, the program informs you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Getting a C# program with a manifest file to run in the debugger
    By Xarzu Athanasop in forum C# Programming
    Replies: 0
    Last Post: 01-18-2008, 06:34 PM
  3. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  4. Replies: 3
    Last Post: 07-11-2005, 03:07 AM