简体   繁体   English

Xcode 5 C ++ SDL应用程序未在XCode IDE外部加载

[英]Xcode 5 C++ SDL app not loading outside of XCode IDE

I've been coding a game for the past year, I'm in the process of porting to Mac. 在过去的一年中,我一直在编写游戏,目前正在移植到Mac。

I've set everything up according to: http://blog.davidecoppola.com/2013/08/22/how-to-set-up-a-sdl-2-project-for-os-x-in-xcode-4/ . 我已经根据以下内容设置了所有内容: http : //blog.davidecoppola.com/2013/08/22/how-to-set-up-a-sdl-2-project-for-os-x-in-xcode -4 / I have changed some of the steps to compensate for the version (5). 我已经更改了一些步骤以补偿版本(5)。

In my code I use tinyXML to load a .xml file within an assets folder. 在我的代码中,我使用tinyXML在资源文件夹中加载.xml文件。 This works perfectly on windows and linux, however, in Mac I experience some issues. 这在Windows和Linux上完美运行,但是在Mac中,我遇到了一些问题。 Here's the line: 这是一行:

Doc.LoadFile(DataPath);

Doc is a tinyxml2::XMLDocument type, and the DataPath is usually "assets/Data.xml", in Mac I hard-coded that line into the place of "DataPath". Doc是tinyxml2 :: XMLDocument类型,DataPath通常是“ assets / Data.xml”,在Mac中,我将该行硬编码到“ DataPath”的位置。

When running the code within the IDE I experience no issues and my game comes up and plays like it does on all other platforms, however, when running the .app file (Located in gameMac/Build/Products/Debug) it crashes right after starting up. 在IDE中运行代码时,我没有遇到任何问题,并且我的游戏可以像在所有其他平台上一样正常运行,但是,当运行.app文件(位于gameMac / Build / Products / Debug中)时,它在启动后立即崩溃起来。 The Assets folder is also located in this location. 资产文件夹也位于此位置。

Here's the error Mac throws at me after the crash: 这是Mac崩溃后向我抛出的错误:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   gameMac                        0x000c1525 tinyxml2::XMLNode::FirstChildElement(char const*) const + 21
1   gameMac                        0x000d1954 tinyxml2::XMLNode::FirstChildElement(char const*) + 36
2   gameMac                        0x000d04d5 DataLoader::load(char const*, ID_Data, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) + 197
3   gameMac                        0x000a5849 MapHandler::MapHandler(std::__1::shared_ptr<gameData>) + 1177
4   gameMac                        0x000a539e MapHandler::MapHandler(std::__1::shared_ptr<gameData>) + 30
5   gameMac                        0x000b7eb8 splay::splay(std::__1::shared_ptr<gameData>) + 1592
6   gameMac                        0x000b786e splay::splay(std::__1::shared_ptr<gameData>) + 30
7   gameMac                        0x000ee3f9 std::__1::shared_ptr<splay> std::__1::shared_ptr<splay>::make_shared<std::__1::shared_ptr<gameData>&>(std::__1::shared_ptr<gameData>&&&) + 1849
8   gameMac                        0x000ebc7c stateHandlerGen::stateHandlerGen(std::__1::shared_ptr<gameData>) + 236
9   gameMac                        0x000ebb7e stateHandlerGen::stateHandlerGen(std::__1::shared_ptr<gameData>) + 30
10  gameMac                        0x000e7a89 game::init(char const*, int, int, int, int, bool) + 1545
11  gameMac                        0x000be8c4 main + 516
12  libdyld.dylib                       0x91b3c725 start + 1

(Please note that gameMac is not the name of my project, I have changed that detail due to an NDA). (请注意,gameMac不是我的项目的名称,由于NDA,我已经更改了该细节)。

As I've noted here, the same method that loads my data DataLoader::load(params) is giving the crash. 正如我在这里指出的,加载我的数据DataLoader::load(params)的相同方法正在崩溃。 This leads me to believe that my assets folder is not being loaded into my .app file. 这使我相信我的资产文件夹未加载到我的.app文件中。

Through this I attempted to edit a Build Phase, Copy Bundle Resources this did not change the issue. 通过此操作,我尝试编辑“构建阶段”,“ Copy Bundle Resources并没有解决问题。 I then attempted to make a New Build Phase Copy Files and set it to Resources , this also failed. 然后,我尝试制作一个新的构建阶段Copy Files ,并将其设置为Resources ,这也失败了。 I then experimented by setting the Copy Files to Product Directory ; 然后,我通过设置“ Copy FilesProduct Directory进行了实验; this copied the assets folder to the build folder, but the .app file still won't load in the assets. 这会将资产文件夹复制到构建文件夹,但是.app文件仍然不会加载到资产中。

Did I miss something? 我错过了什么? Could some XCode vets help me out here, I've attempted to fix this for the past few days. 某些XCode兽医可以在这里帮助我吗,过去几天我一直在尝试解决此问题。 Thanks. 谢谢。

http://pastebin.com/Xq4Hxh4d (Here's the top of my DataLoader as mentioned above. EDIT: Added in my check for nullptr return, yes, it returns true if failed). http://pastebin.com/Xq4Hxh4d (如上所述,这是我的DataLoader的顶部。编辑:在我的检查中添加了nullptr返回,是的,如果失败,则返回true)。

PS I can only give out snippets of my code, I have given what I feel is most likely the problem, resource loading :) PS我只能给出我的代码片段,我给出了我认为最有可能的问题,资源加载:)

The full crash report is in the comments below. 完整的崩溃报告在下面的评论中。

The fix, thanks to the help from Brad Allred: 该修复程序要感谢Brad Allred的帮助:

The resources are located within the .app file, specifically {Application}/Contents/Resources ; 资源位于.app文件中,尤其是{Application} / Contents / Resources; in my case I wanted to access a file within assets. 就我而言,我想访问资产中的文件。 I did a build phase to copy the data to the resources folder, making my directory {Application}/Contents/Resources/Assets/../. 我执行了一个构建阶段,将数据复制到资源文件夹,将目录设置为{Application} / Contents / Resources / Assets /../。 Using SDL_GetBasePath I'm able to create a string which is the absolute path from your base directory up to the Resources folder. 使用SDL_GetBasePath,我可以创建一个字符串,该字符串是从基本目录到Resources文件夹的绝对路径。

Here's the code: 这是代码:

if(Doc.LoadFile("assets/Data.xml"))
{
    std::string path = (std::string)SDL_GetBasePath() + "assets/Data.xml";
    if(Doc.LoadFile(path.c_str()))
    {
        return false;
    }
}

I concat the base path + whatever data I need, and pass the .c_str() version to tinyXML. 我连接基本路径+我需要的任何数据,然后将.c_str()版本传递给tinyXML。 If I find a better/cleaner version I'll be sure to update it here. 如果找到更好/更干净的版本,请确保在此处进行更新。

your crash report tells you the line number in your code that is crashing ( tinyxml2.cpp:745 ). 您的崩溃报告会告诉您代码中崩溃的行号( tinyxml2.cpp:745 )。 at Probably your assets are not where the app expects them to be or doesnt have permission. 在可能您的资产不在应用程序期望的位置或没有权限。 examining your DataPath to see where its trying to load should get you started. 检查您的DataPath以了解尝试将其加载到何处。

It seems like maybe you are mistakenly assuming that you can use a relative path and get assets relative to the app bundle; 似乎您可能错误地假设您可以使用相对路径来获取相对于应用程序包的资产; you can't do that. 你不能那样做。

Since you are using SDL, why not use the SDL API (see SDL_GetBasePath ) to get resource paths so that you can store your assets properly inside your app bundle in the Resources directory? 由于您使用SDL,为什么不使用SDL API(见SDL_GetBasePath )来获取资源的路径,这样就可以正确地存储在资源目录中的应用程序包的资产?

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

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