在 c 中获取 Xcode 项目的资源;
我正在 Xcode 中使用 SFML 编写一个简单的游戏。我有一个 .png 的块,我想在精灵中使用。目前,我必须在下面的代码片段中输入图像的完整路径:
sf::Image blockImage;
if (!blockImage.LoadFromFile("/Users/me/Development/Tetris/images/block.png")) {
cerr << "Could not load block image." << endl;
App.Close();
}
我不想像这样对游戏中每个图像的位置进行硬编码。我知道 Xcode 项目可以有“资源”,但我以前从未使用过这个,而且从我在 Google 上搜索到的信息来看,它只对使用 Apple 框架的项目有用。我可以将我的图像添加为资源吗?我如何实际获取它的位置并在我的代码中使用它?
谢谢!
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 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. Can I add my image as a resource? How would I actually get its location and use it in my code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题不太清楚,但我会尽力解释:
就操作系统而言,资源只是应用程序包中的一个文件夹,唯一特别的是当应用程序启动时,Resources文件夹是默认文件路径(称为工作目录)。
这意味着如果您的应用程序资源文件中有图像,则可以使用以下命令加载它:
或者
当您使用此调用时,操作系统会检查 block.png 是否位于硬盘驱动器的根目录中,但如果没有找到它,则检查工作目录*
如果您想更改工作目录,您可以使用常见的 unix 函数
,该函数在 Mac OS X 中包含在:
Arjit 所指的是 Xcode 中的一个方便的功能,您可以在任何给定的位置复制文件的构建阶段目标并将文件甚至文件夹复制到应用程序的资源文件夹中。
希望这能解决问题!
*它实际上可能首先检查工作目录,不确定。
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:
or
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
Which in Mac OS X is included from:
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.
我认为当您右键单击该项目时,它有一个选项可以添加资源添加现有的 ITem,您可以轻松添加它
以添加文件夹
http://forums.macrumors.com/showthread.php?t=458594
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