使用xcode的c++应用程序中文件的相对路径
Relative path for files in c++ app using xcode
我试图加载一个文件与可可应用程序,但应用程序无法找到文件
我使用SDL_image,代码是:
SDL_Surface* surface = IMG_Load(file);
文件为:"/Resources/cursor.png"
我试过在Objective-C中使用一些行,但应用程序不会运行,因为它是用c++编写的
谢谢,丹尼尔
更新:纹理加载代码:
GLuint CGLTexture::load_texture(const char* file, bool wrap)
{
SDL_Surface* surface = IMG_Load(file);
if (!surface) {
printf("Error, probably the file isn't there :S");
return 0;
}
GLuint texture;
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
SDL_PixelFormat *format = surface->format;
// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
wrap ? GL_REPEAT : GL_CLAMP );
if (format->Amask)
{
gluBuild2DMipmaps(GL_TEXTURE_2D, 4,
surface->w, surface->h, GL_RGBA,GL_UNSIGNED_BYTE, surface->pixels);
}
else
{
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
surface->w, surface->h, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
}
SDL_FreeSurface(surface);
return texture;
}
我这样调用这个函数:
cursorTexture = CGLTexture::load_texture("/Resources/cursor.png",false);
文件从未找到,我认为这是在应用程序的bundle的某个地方,但是,我怎么能用c++访问它?
你可以使用objective-c++,那么你就有了c++和objective-c的特性。
要在可移植环境中使用objective c,请使用以下命令:
header file:
std::string GetTextureFilename(const char *Name);
implementation file that ends with .mm and included only in mac:
std::string GetTextureFilename(const char *Name)
{
return [[[NSBundle mainBundle]
pathForResource:
[NSString stringWithUTF8String: Name
ofType: nil] toUTF8String];
}
source file that ends with .cpp and included only on other platforms:
std::string GetTextureFilename(const char *Name)
{
return Name;
}
如果您的代码是从主应用程序包运行的,那么您可以使用以下命令:
NSString *cursorImagePath = [[NSBundle mainBundle]
pathForResource:@"cursor" ofType:@"png"];
const char *cCursorImagePath = [cursorImagePath fileSystemRepresentation];
cursorTexture = CGLTexture::load_texture(cCursorImagePath, false);
(注意:将你想使用Objective-C的任何文件的扩展名从.cpp
更改为.mm
)
NSBundle
(或CFBundle
)可用于定位到应用程序包内资源的(完整)路径。参见Bundle编程指南:获取资源的路径。
另外,我不确定你的应用程序有多"可可"。具体来说,我不确定您是否已经有了NSAutoreleasePool
。在控制台中观察输出。app或Xcode的调试控制台的错误信息沿__NSAutoreleaseNoPool() - just leaking
行。如果您确实看到了这些,则需要用以下内容包围上述代码:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// above code
[pool release];
关于"我必须包含一些东西来使用这个代码吗?"这是一个c++应用程序":
是的,你需要导入(和链接,如果你还没有这样做):
#import <Foundation/Foundation.h>
(#import
是Objective-C的智能#include
:它确保它只包含一次)。
你说你的应用程序是c++,但两个主要的GUI框架要么是Carbon(基于C的HIToolbox),要么是Cocoa(基于Objective-C)。您知道自己使用的是哪一个框架吗?还是使用了另一个框架(看起来像是SDL)来隐藏这些细节?
你是说"./Resources/cursor.png"
吗?
"/Resources/cursor.png"
在根文件系统
"./Resources/cursor.png"
是相对于工作目录的。
或者相对于用户的主路径
#include <string>
#include <stdlib.h>
std::string filename = getenv("HOME");
filename += "/.ecf/employee.db";
在不填充环境变量的基于Unix的操作系统上不起作用。我被告知OS X不能。不管它为什么失败,你都可以在那个时候使用getpwuid()
。
为完整性添加。您可以使用在这里找到的代码以可执行文件的路径为基础。在所有的现实中,我几乎所有的Unix代码都以主目录为基础,并退回到/etc中找到的一个文件。这是我代码的大多数用户想要的(也是作为用户我想要的)。
- cmake find_package CMakeLists.txt 子文件夹中的相对路径
- cmake:包括vs vs add_subDirectory:相对标头文件路径
- stat() 相对于文件系统是原子的
- 在可执行文件中使用带有符号链接的相对路径
- 如何将COM .tlb文件导入具有相对路径C++文件中?
- Boost Filesystem flowly_caronical不会在没有父文件夹的情况下解决相对路径
- 如何获取与 objdump 输出的标签相对应的 ELF 二进制文件中的文件偏移量
- 如何获取存储在 hpp 头文件中的相对或绝对文件路径,可以在任何系统中使用?
- C 能够以其完整路径打开代码::块中的文件,但不能以相对路径作为命令行参数打开文件
- Visual Studio单元使用具有相对路径的文件测试
- 如何使用cmake使相对文件路径在visualstudio中工作
- 如何配置MSVC以使用__FILE__显示头文件的相对路径
- (罗)如何使用相对路径保存日志文件
- 如何添加相对于当前文件的头文件路径
- 我如何确保Fopen()相对于可执行的目录打开一个文件,而不是我当前的目录
- 使用Boost程序选项读取相对文件路径
- C++:使用相对路径将内容写入文件
- 服务器应用程序看不到具有相对路径的文件
- 如何在C++中打开具有相对路径的文件
- 我们可以在c++中使用带有文件相对路径的stat吗