gpt4 book ai didi

xml - 使用 NDK 和 SDL_RWops 无法从 android Assets 文件夹中读取 xml/tmx 文件

转载 作者:行者123 更新时间:2023-12-04 18:08:01 25 4
gpt4 key购买 nike

我在使用 SDL2 + NDK 框架 (r9c) 时遇到了一个非常烦人的问题。

我将我的 .xml 和 .tmx 文件保存在 Assets 文件夹中。现在我使用 SDL_RWops 从 Assets 目录中读取 xml 和 tmx 文件,并使用 tinyxml 库进行解析。

读取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>

最佳答案

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

可以将以下代码视为如何使用 NDK 框架使用 SDL_RWops 从 android 文件夹中的 Assets 中读取任何文件的示例。感谢您的帮助。

关于xml - 使用 NDK 和 SDL_RWops 无法从 android Assets 文件夹中读取 xml/tmx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21813019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com