从android资源文件夹中读取xml / tmx文件,NDK和SDL_RWops无法正常工作

时间:2014-02-16 15:13:26

标签: xml android-ndk assets sdl-2 tmx

我面临着SDL2 + NDK框架(r9c)的一个非常烦人的问题。

我将.xml和.tmx文件保存在assets文件夹中。现在我 使用SDL_RWops从assets目录中读取xml和tmx文件 使用tinyxml lib解析。

读取xml / tmx文件的源代码: -

bool StateParser::read_text(const char* source_file, char** destination)
{
    // Open the file
    SDL_RWops *file;
    file = SDL_RWFromFile(source_file, "r");

#ifdef DEBUG
Logger::Instance()->Log(MODNAME, __func__,"Opening file using SDL_RWops
%s",
source_file);
#endif

    size_t file_length = SDL_RWseek(file, 0, SEEK_END);
    (*destination) = new char[file_length+1]; // allow an extra character
for '\0'

    // Reset seek to beginning of file and read text
    SDL_RWseek(file, 0, SEEK_SET);
    int n_blocks = SDL_RWread(file, (*destination), 1, file_length);
    if (n_blocks <= 0)
    {

#ifdef DEBUG
Logger::Instance()->Log(MODNAME, __func__,"SDL_RWread failed Read Block
[%d]", n_blocks);
#endif

    }
    SDL_RWclose(file);

    (*destination)[file_length] = '\0';

#ifdef DEBUG
Logger::Instance()->Log(MODNAME, __func__,"Blocks [%d], file lingth[%d] xml
file [%s]", n_blocks, file_length, *destination);
#endif

    return true;
}

使用Tinyxmllib进行解析功能: -

bool StateParser::parseState(const char *stateFile, std::string stateID,
std::vector<GameObject *> *pObjects,
                             std::vector<std::string> *pTextureIDs)
{

    char* file_contents = NULL;

    if (read_text(stateFile, &file_contents) != true)
    {

#ifdef DEBUG
Logger::Instance()->Log(MODNAME, __func__,"read_text failed");
#endif
        return false;
    }

     // create the XML document
     TiXmlDocument xmlDoc;
     // load the state file


     //if(!xmlDoc.LoadFile(file_contents))
     if(!xmlDoc.Parse(stateFile))
     {

#ifdef DEBUG
Logger::Instance()->Log(MODNAME, __func__,"Loadfile failed %s",
xmlDoc.ErrorDesc());
#endif
         return false;
     }

     delete[] file_contents;
 ..................
..............

}

我可以访问&amp;解析xml文件,但同一个函数无法访问 .tmx文件有奇怪的错误。 NDK失败并出现以下错误: -

错误日志: -

2-16 19:25:16.478: I/Log(27964): sdlGameObject:void
TextureManager::clearFromTextureMap(std::string),Erasing id:exitbutton from
map
02-16 19:25:16.478: I/Log(27964): menuState:virtual bool
MainMenuState::onExit(),removing texture [exitbutton]
02-16 19:25:16.478: I/Log(27964): GameStateMachine:void
GameStateMachine::ChangeState(GameState*),deleting MENU
02-16 19:25:16.478: I/Log(27964): GameStateMachine:void
GameStateMachine::ChangeState(GameState*),push_back (PLAY)
02-16 19:25:16.478: I/Log(27964): playerState:virtual bool
PlayState::onEnter(),Level Parser (map1.tmx)
02-16 19:25:16.478: I/Log(27964): LevelParser:bool
LevelParser::read_text(char const*, char**),Opening file using SDL_RWops
map1.tmx
02-16 19:25:16.479: I/Log(27964): LevelParser:bool
LevelParser::read_text(char const*, char**),Blocks [1312], file
lingth[1312] xml file [<?xml version="1.0" encoding="UTF-8"?>
02-16 19:25:16.479: I/Log(27964): <map version="1.0"
orientation="orthogonal" width="60" height="15" tilewidth="32"
tileheight="32">
02-16 19:25:16.479: I/Log(27964):  <properties>
02-16 19:25:16.479: I/Log(27964):   <property name="helicopter"
value="heli.png"/>
02-16 19:25:16.479: I/Log(27964):  </properties>
02-16 19:25:16.479: I/Log(27964):  <tileset firstgid="1" name="blocks1"
tilewidth="32" tileheight="32" spacing="2" margin="2">
02-16 19:25:16.479: I/Log(27964):  <image source="blocks1.png" width="614"
height="376"/>
02-16 19:25:16.479: I/Log(27964):  </tileset>
02-16 19:25:16.479: I/Log(27964):  <tileset firstgid="199" name="blocks2"
tilewidth="32" tileheight="32" spacing="2" margin="2">
02-16 19:25:16.479: I/Log(27964):  <image source="blocks2.png" width="614"
height="376"/>
02-16 19:25:16.479: I/Log(27964):  </tileset>
02-16 19:25:16.479: I/Log(27964):  <layer name="Tile Layer 1" width="60"
height="15">
02-16 19:25:16.479: I/Log(27964):   <data encoding="base64"
compression="zlib">
02-16 19:25:16.479: I/Log(27964):
 eJxjYBgFcUAcD8QJVDIvGYhTgDiVSuYNFbuHMhjIcKN2+hsoMFz8MQoGF7gPxA+A+CERah8B8WMgfkKmPLF2EmMOKe5GBh+B+BMQfyZC7Rcg/grE38iUJ9ZOYswhxd3ogIWReLXsBNQSkifWTmLMIdbdT9H4YlB9bGj6f2HRJ4nDDphemDyyHb+wqBPDog7dHGLMwKUfBuqBuAGIG4G4aZjiViT/egJx9TDHXqP+HdYY5l8AuJOg6A==
02-16 19:25:16.479: I/Log(27964):   </data>
02-16 19:25:16.479: I/Log(27964):  </layer>
*****02-16 19:25:16.479: I/Log(27964):  <objec*****
02-16 19:25:16.479: A//system/bin/app_process(27964): stack corruption
detected: aborted
02-16 19:25:16.479: A/libc(27964): Fatal signal 11 (SIGSEGV) at 0xdead2aed
(code=1), thread 27980 (SDLThread)

您可以看到标有**的行存在问题。

我无法解决这个问题,并且在过去的两天里仍然坚持不懈。 请帮忙。

map1.tmx文件输出: -

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="60" height="15" tilewidth="32" tileheight="32">
 <properties>
  <property name="helicopter" value="heli.png"/>
 </properties>
 <tileset firstgid="1" name="blocks1" tilewidth="32" tileheight="32" spacing="2" margin="2">
     <image source="blocks1.png" width="614" height="376"/>
 </tileset>
 <tileset firstgid="199" name="blocks2" tilewidth="32" tileheight="32" spacing="2" margin="2">
     <image source="blocks2.png" width="614" height="376"/>
 </tileset>
 <layer name="Tile Layer 1" width="60" height="15">
  <data encoding="base64" compression="zlib">
   eJxjYBgFcUAcD8QJVDIvGYhTgDiVSuYNFbuHMhjIcKN2+hsoMFz8MQoGF7gPxA+A+CERah8B8WMgfkKmPLF2EmMOKe5GBh+B+BMQfyZC7Rcg/grE38iUJ9ZOYswhxd3ogIWReLXsBNQSkifWTmLMIdbdT9H4YlB9bGj6f2HRJ4nDDphemDyyHb+wqBPDog7dHGLMwKUfBuqBuAGIG4G4aZjiViT/egJx9TDHXqP+HdYY5l8AuJOg6A==
  </data>
 </layer>
 <objectgroup name="Object Layer 1" width="60" height="15">
  <object name="Helicopter1" type="Player" x="32" y="32" width="32" height="32">
   <properties>
    <property name="numFrames" value="4"/>
    <property name="textureHeight" value="55"/>
    <property name="textureID" value="helicopter"/>
    <property name="textureWidth" value="120"/>
   </properties>
  </object>
 </objectgroup>
</map>

1 个答案:

答案 0 :(得分:0)

我已经追查到了这个问题。问题是我传递了错误的文件 xmlDoc.Parse(状态文件)。它通过将file_contents缓冲区传递给parse函数来解决。现在我能够从assets目录访问和解析xml / tmx文件。

以下代码可以被视为如何使用NDK框架使用SDL_RWops从android文件夹中的资源读取任何文件的示例。谢谢你的帮助。