在c++中从js文件中读取和使用数据



我有一个使用SDL的c++游戏项目。此外,我有一个.js文件平铺地图(我附加了一个pastebin链接更详细:https://pastebin.com/GKqs3km5)。我如何在我的项目中使用这个来创建地图?我尝试ifstream读取这个文件,但它似乎不正确。

//from lesson 39 Lazyfoo
for (int i = 0; i < TOTAL_TILES; ++i)
{
//Determines what kind of tile will be made
int tileType = -1;
//Read tile from file
js >> tileType;
//If the was a problem in reading the map
if (js.fail())
{
//Stop loading map
printf("Error loading map: Unexpected end of file!n");
tilesLoaded = false;
break;
}
//If the number is a valid tile number
if ((tileType >= 0) && (tileType < TOTAL_TILE_SPRITES))
{
tiles[i] = new Tile(x, y, tileType);
}

删除.js格式。它是从Tiled导出的,所以在Tiled中打开它并重新导出为JSON(或XML)。然后使用任何JSON(或XML)解析库。

可能有专门的库来解析JSON/XML平铺映射,但由于格式如此简单,您不妨使用通用库。

最新更新