3

I am facing a very annoying issue with SDL2 + NDK framework (r9c).

I have kept my .xml and .tmx file in assets folder. Now i am using SDL_RWops to read the xml and tmx file from assets directory and parsing using tinyxml lib.

Source code for reading the xml/tmx file:-

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;
}

Parsing function using 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;
 ..................
..............

}

i am able to access & parse xml file but the same function fails to access .tmx file with strange error. The NDK fails with following error:-

Error Log:-

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)

You can see that there is some issue with line marked with **.

I am not able to resolve the issue and stuck on the same from last two days. Please help.

map1.tmx file output:-

<?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 Answers1

1

I have traced down the problem. The problem was that i was passing the wrong file xmlDoc.Parse(stateFile). It is resolved by passing the file_contents buffer to the parse function. Now i am able to access and parse xml/tmx file from assets directory.

The following code can be considered as example how to read any file from assets in android folder using SDL_RWops using NDK framework. Thanks for help.