3

我在使用 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;
 ..................
..............

}

我能够访问和解析 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>
4

1 回答 1

1

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

下面的代码可以看作是如何使用 NDK 框架使用 SDL_RWops 从 android 文件夹中的资产中读取任何文件的示例。感谢帮助。

于 2014-02-17T10:34:21.797 回答