简体   繁体   English

在C ++中获取Xcode项目的资源

[英]Getting an Xcode project's resources in c++

I'm coding a simple game using SFML in Xcode. 我正在Xcode中使用SFML编写一个简单的游戏。 I have a .png of a block I want to use in a sprite. 我有一个要在精灵中使用的块的.png文件。 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 my 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. 我知道Xcode项目可以具有“资源”,但是我以前从未使用过它,从我对Google的了解来看,它仅对使用Apple框架的项目很重要。 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). 就操作系统而言,Resources只是应用程序捆绑包中的一个文件夹,唯一与众不同的是,当应用程序启动时,Resources文件夹是默认文件路径(称为工作目录)。

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* 使用此调用时,操作系统会检查block.png是否在硬盘驱动器的根目录中,但找不到它,然后检查工作目录*

If you want to change the working directory, you can use the common unix function 如果要更改工作目录,可以使用通用的unix函数

chdir(const char *path);

Which in Mac OS X is included from: Mac OS X中的哪个包含来自:

#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. Arjit所指的是Xcode中的一项便捷功能,您可以在任何给定目标中进行复制文件的构建阶段,并使其将文件甚至文件夹复制到应用程序的Resources文件夹中。

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 我认为,当您右键单击该项目时,它可以选择添加资源并添加现有的ITem,并且您可以轻松地添加它

For adding folder http://forums.macrumors.com/showthread.php?t=458594 用于添加文件夹http://forums.macrumors.com/showthread.php?t=458594

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM