【问题标题】:os x app couldn't load file in Resources folderos x app 无法加载资源文件夹中的文件
【发布时间】:2014-01-23 04:20:54
【问题描述】:

我正在尝试在 osx(Lion 10.7.5) 上测试库 assimp。但是 Demo.app 在 Demo.app/Contents/Resources 文件夹中找不到模型文件。奇怪的是,我的旧演示工作正常。 主要代码如下:

bool ImportModel(const string& pFile)
{
    Assimp::Importer importer;
    const aiScene* scene = importer.ReadFile(pFile, ...);
    if(!scene)
    {
        cout<<importer.GetErrorString()<<endl;
        return false;
    }
    return true;
}

int main(void)
{
    while(ImportModel("skin.xml"));
    return 0;
}

一直返回:无法打开文件“skin.xml”。 Demo.app 的结构如下:

Contents
    Frameworks
    info.plist
    MacOS
        Demo
        libassimp.dylib
    Resources
        Demo.icns
        skin.xml -> the model file.

知道哪里可能出错了吗?谢谢大家。

【问题讨论】:

    标签: c++ macos


    【解决方案1】:

    在 assimp 的文档中,Assimp::Importer::ReadFile 将路径作为第一个参数。似乎没有(至少从文档中),对默认路径的任何特定支持 Resources 文件夹。

    很有可能您必须提供文件的完整路径才能从这些库中正确读取它,这意味着获取主包的资源路径、附加您的模型文件名并转换最终的ac 字符串的路径(将成为 std::string)。或者,您可以获取主包的资源路径,将其转换为 c 字符串并将skin.xml 附加到该路径。

    如果你使用的是objective-c++,这很简单:

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource: @"skin" ofType: @"xml"];
    // now get the c string from the path
    const char *cPath =[path cStringUsingEncoding: NSUTF8StringEncoding];
    

    如果你想在基础上保持 C++,由于手动内存管理和更广泛的错误检查,这有点棘手(对于上面,你可以只对 cPath 进行错误检查,因为 Objective-C代码将毫无问题地向nil发送消息)。

    【讨论】:

    • 是的,我刚刚发现,我之前的“工作”演示使用 GLFW3,它的 glfwInit() 完成了这个捆绑工作。有问题。谢谢你的详细解释:D
    猜你喜欢
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 2013-03-22
    相关资源
    最近更新 更多