|
|
|
|
@ -26,7 +26,8 @@ static const char* g_data_dir;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// forward declarations
|
|
|
|
|
bool parseFile(rapidjson::Document* doc, const char* data_dir, const char* file_name); |
|
|
|
|
bool loadTextFile(char*& output, const char* data_dir, const char* file_name); |
|
|
|
|
bool parseFile(rapidjson::Document* doc, const char* file_contents, const char* file_name); |
|
|
|
|
glm::vec3 parseVec3(const rapidjson::Value& node); |
|
|
|
|
glm::vec4 parseVec4(const rapidjson::Value& node); |
|
|
|
|
bool validateScene(rapidjson::Document* schema_doc, rapidjson::Document* scene_doc, |
|
|
|
|
@ -44,8 +45,18 @@ slLoadFile(const char* data_dir, const char* scene_file, const char* schema_file
|
|
|
|
|
slSceneDoc* sd = UTIL_ALLOC(1, slSceneDoc); |
|
|
|
|
sd->doc = new rapidjson::Document(); |
|
|
|
|
|
|
|
|
|
bool retDoc = parseFile(sd->doc, data_dir, scene_file); |
|
|
|
|
bool retSchema = parseFile(&schema_doc, data_dir, schema_file); |
|
|
|
|
bool retDoc, retSchema = false; |
|
|
|
|
|
|
|
|
|
char* scene_contents; |
|
|
|
|
if (loadTextFile(scene_contents, data_dir, scene_file)) |
|
|
|
|
retDoc = parseFile(sd->doc, scene_contents, scene_file); |
|
|
|
|
|
|
|
|
|
char* schema_contents; |
|
|
|
|
if (loadTextFile(schema_contents, data_dir, schema_file)) |
|
|
|
|
retSchema = parseFile(&schema_doc, schema_contents, schema_file); |
|
|
|
|
|
|
|
|
|
utilSafeFree(scene_contents); |
|
|
|
|
utilSafeFree(schema_contents); |
|
|
|
|
|
|
|
|
|
if (!retDoc || !retSchema || !validateScene(&schema_doc, sd->doc, scene_file)) { |
|
|
|
|
slFreeSceneDoc(sd); |
|
|
|
|
@ -151,6 +162,65 @@ slParseHexGrid(slSceneDoc* sd, hexgrid& hg, util_image& palette_image)
|
|
|
|
|
hg.layout_mode = (orientation_str == "layout_flat") ? LAYOUT_FLAT : LAYOUT_POINTY; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
slParseGridHashMap(hexgrid& hg, const char* data_dir) |
|
|
|
|
{ |
|
|
|
|
LOG(DEBUG) << "loading map_file: " << hg.map_file << "\n"; |
|
|
|
|
char* contents; |
|
|
|
|
|
|
|
|
|
if (!loadTextFile(contents, data_dir, hg.map_file)) |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
// NOTE: allow creating/saving over blank files
|
|
|
|
|
if (contents[0] == 0 || contents[0] == '\n') { |
|
|
|
|
utilSafeFree(contents); |
|
|
|
|
LOG(INFO) << "empty map file\n"; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: still need a schema for the hashmap
|
|
|
|
|
rapidjson::Document doc; |
|
|
|
|
bool ret = false; |
|
|
|
|
|
|
|
|
|
if (parseFile(&doc, contents, hg.map_file)) { |
|
|
|
|
hg.hex_size = doc["hex_size"].GetInt(); |
|
|
|
|
std::string orientation_str = doc["layout_mode"].GetString(); |
|
|
|
|
hg.layout_mode = (orientation_str == "layout_flat") ? LAYOUT_FLAT : LAYOUT_POINTY; |
|
|
|
|
glm::vec3 v = parseVec3(doc["world_position"]); |
|
|
|
|
hg.position.x = v.x; hg.position.y = v.y; hg.position.z = v.z; |
|
|
|
|
glm::vec3 n = parseVec3(doc["world_normal"]); |
|
|
|
|
hg.normal.x = n.x; hg.normal.y = n.y; hg.normal.z = n.z; |
|
|
|
|
rapidjson::Value& hexes = doc["hexes"]; |
|
|
|
|
|
|
|
|
|
for (uint i = 0; i < doc["hexes"].Size(); i++) { |
|
|
|
|
// TODO: could re-use hexgrid.createHexInfo() here
|
|
|
|
|
hex_info hxi; |
|
|
|
|
hxi.hexID = i; |
|
|
|
|
hxi.selected = false; |
|
|
|
|
hxi.XPos = hexes[i]["XPos"].GetFloat(); |
|
|
|
|
hxi.YPos = hexes[i]["YPos"].GetFloat(); |
|
|
|
|
hxi.hex.q = hexes[i]["q"].GetInt(); |
|
|
|
|
hxi.hex.r = hexes[i]["r"].GetInt(); |
|
|
|
|
hxi.hex.s = hexes[i]["s"].GetInt(); |
|
|
|
|
|
|
|
|
|
for (uint j = 0; j < hexes[i]["vertices"].Size(); j++) { |
|
|
|
|
rapidjson::Value& v = hexes[i]["vertices"][j]; |
|
|
|
|
glm::vec3 v3 = parseVec3(v); |
|
|
|
|
Point p(v3.x, v3.y); |
|
|
|
|
hxi.vertices.push_back(p); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
hxi.vertices.shrink_to_fit(); |
|
|
|
|
hg.hex_map.insert({hxi.hex, hxi}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ret = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
utilSafeFree(contents); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
slCreateHexRenderGroups(hexgrid& hg, render_state* rs) |
|
|
|
|
{ |
|
|
|
|
@ -262,29 +332,33 @@ slSaveGridFile(hexgrid& hg)
|
|
|
|
|
// internal
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
parseFile(rapidjson::Document* doc, const char* data_dir, const char* file_name) |
|
|
|
|
loadTextFile(char*& output, const char* data_dir, const char* file_name) |
|
|
|
|
{ |
|
|
|
|
bool ret = true; |
|
|
|
|
std::string file_path; |
|
|
|
|
file_path.append(data_dir).append("/").append(file_name); |
|
|
|
|
char* contents = utilDumpTextFile(file_path.c_str()); |
|
|
|
|
output = utilDumpTextFile(file_path.c_str()); |
|
|
|
|
|
|
|
|
|
if (contents == nullptr) { |
|
|
|
|
if (output == nullptr) { |
|
|
|
|
LOG(ERROR) << "Error reading file: " << file_name << "\n"; |
|
|
|
|
ret = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
rapidjson::ParseResult pr = doc->Parse(contents/*, rapidjson::kParseStopWhenDoneFlag*/); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
parseFile(rapidjson::Document* doc, const char* file_contents, const char* file_name) |
|
|
|
|
{ |
|
|
|
|
rapidjson::ParseResult pr = doc->Parse(file_contents); |
|
|
|
|
if (!pr || !doc->IsObject()) { |
|
|
|
|
LOG(ERROR) << "Error parrsing scene file: " << file_name << "\n"; |
|
|
|
|
LOG(ERROR) << "Error parrsing json file: " << file_name << "\n"; |
|
|
|
|
LOG(ERROR) << "Error desscription: " << rapidjson::GetParseError_En(pr.Code()) |
|
|
|
|
<< " (" << pr.Offset() << ")\n"; |
|
|
|
|
ret = false; |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
utilSafeFree(contents); |
|
|
|
|
|
|
|
|
|
return ret; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
|