使用xcode的c++应用程序中文件的相对路径

Relative path for files in c++ app using xcode

本文关键字:文件 相对 中文 路径 应用程序 xcode c++ 使用      更新时间:2023-10-16

我试图加载一个文件与可可应用程序,但应用程序无法找到文件

我使用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中找到的一个文件。这是我代码的大多数用户想要的(也是作为用户我想要的)。