I'm coding a simple game using SFML in Xcode. I have a .png of a block I want to use in a sprite. At the moment, I have to type the full path to the image in the code snippet below:
sf::Image blockImage;
if (!blockImage.LoadFromFile("/Users/me/Development/Tetris/images/block.png")) {
cerr << "Could not load block image." << endl;
App.Close();
}
I'd rather not have to hard code the location of every image in m开发者_如何学Goy game like this. I know Xcode projects can have 'Resources', but I've never used this before, and from what I've been able to Google it only matters for projects that use Apple's frameworks. Can I add my image as a resource? How would I actually get its location and use it in my code?
Thanks!
Your question isn't exactly clear, but I'll try to explain as best I can:
As far as the OS is concerned, Resources is just a folder inside a application bundle, the only thing special about it is that when an application starts, the Resources folder is the default file path (called the working directory).
This means if you have an image in your apps resources file, you can load it with:
blockImage.LoadFromFile("/Developer/MyApp/build/Development/Tetris.app/Contents/Resources/block.png");
or
blockImage.LoadFromFile("block.png");
When you use this call, the OS checks if block.png is in the root of your hard drive, but not finding it then checks the Working Directory*
If you want to change the working directory, you can use the common unix function
chdir(const char *path);
Which in Mac OS X is included from:
#include <unistd.h>
What Arjit was refering to is a handy feature in Xcode whereby you can make a copy file's build phase in any given target and have it copy files or even folders into the Resources folder of an app.
Hope this clears things up!
*It might actually check the working directory first, not sure.
I think when you right click on the project it has an option to add a resource add existing ITem and you can easily add it
For adding folder http://forums.macrumors.com/showthread.php?t=458594
精彩评论