Browse Source

serialize hexgrid and output to test file

master
cinnaboot 8 years ago
parent
commit
97ee8b1acc
  1. 1
      data/hashmap_scene_hexmap.json
  2. 2
      src/hexgame.cpp
  3. 73
      src/scene_loader.cpp
  4. 22
      src/util.cpp

1
data/hashmap_scene_hexmap.json

File diff suppressed because one or more lines are too long

2
src/hexgame.cpp

@ -95,7 +95,7 @@ init()
// which contains the hashtable.
g_game_state->grid.hex_map = std::unordered_map<Hex, hex_info, hex_hashfunc>();
// NOTE: testing hex_add
g_game_state->grid.draw_mode = NONE;
g_game_state->grid.draw_mode = ADD_HEXES;
// TODO: maybe add this as an option to scene json?
g_game_state->grid.normal = v3f(0.f, 0.f, 1.f);

73
src/scene_loader.cpp

@ -31,6 +31,7 @@ glm::vec3 parseVec3(const rapidjson::Value& node);
glm::vec4 parseVec4(const rapidjson::Value& node);
bool validateScene(rapidjson::Document* schema_doc, rapidjson::Document* scene_doc,
const char* scene_file);
void addV3f(rapidjson::Writer<rapidjson::StringBuffer>& writer, float x, float y, float z);
// interface
@ -198,23 +199,58 @@ slSaveGridFile(hexgrid& hg)
if (utilConcatPath(full_path, g_data_dir, hg.map_file, max_len)) {
LOG(INFO) << "saving grid file: " << full_path << "\n";
// assemble json from hexgrid
rapidjson::Document doc;
// need to store grid properties: hex_size, world origin, world normal?, layout_mode?
// hex properties: hex space coordinates, world XPos/YPos, world hex vertices
// write document to buffer
rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
doc.Accept(writer);
// write buffer to file
if (utilWriteTextFile(full_path, sb.GetString())) {
LOG(INFO) << "save successful\n";
return true;
writer.StartObject();
writer.Key("hex_size"); writer.Uint(hg.hex_size);
writer.Key("layout_mode");
writer.String((hg.layout_mode == LAYOUT_FLAT) ? "layout_flat" : "layout_pointy");
writer.Key("world_position");
addV3f(writer, hg.position.x, hg.position.y, hg.position.z);
writer.Key("world_normal");
addV3f(writer, hg.normal.x, hg.normal.y, hg.normal.z);
writer.Key("hexes");
writer.StartArray();
for (auto& it : hg.hex_map) {
hex_info& hxi = it.second;
writer.StartObject();
writer.Key("XPos"); writer.Double(hxi.XPos);
writer.Key("YPos"); writer.Double(hxi.YPos);
writer.Key("q"); writer.Int(hxi.hex.q);
writer.Key("r"); writer.Int(hxi.hex.r);
writer.Key("s"); writer.Int(hxi.hex.s);
writer.Key("vertices");
writer.StartArray();
for (uint i = 0; i < hxi.vertices.size(); i++) {
// TODO: hard coded z value for hex vertices
Point p = hxi.vertices[i];
addV3f(writer, p.x, p.y, 0.f);
}
writer.EndArray();
writer.EndObject();
}
LOG(ERROR) << "error saving file: " << full_path << "\n";
writer.EndArray();
writer.EndObject();
if (writer.IsComplete()) {
if (utilWriteTextFile(full_path, sb.GetString())) {
LOG(INFO) << "save successful\n";
return true;
}
LOG(ERROR) << "error saving file: " << full_path << "\n";
return false;
}
LOG(ERROR) << "malformed json\n";
return false;
}
@ -296,3 +332,16 @@ parseVec4(const rapidjson::Value& node)
return v4;
}
void
addV3f(rapidjson::Writer<rapidjson::StringBuffer>& writer, float x, float y, float z)
{
writer.StartObject();
writer.Key("x");
writer.Double(x);
writer.Key("y");
writer.Double(y);
writer.Key("z");
writer.Double(z);
writer.EndObject();
}

22
src/util.cpp

@ -83,10 +83,32 @@ utilDumpTextFile(const char* filename)
return buf;
}
// TODO: might want to do the base_dir concat in this function to prevent clobbering
// user files on accident
bool
utilWriteTextFile(const char* filename, const char* text)
{
size_t text_len = std::strlen(text);
if (text_len >= MAX_FILESIZE) {
LOG(ERROR) << "that string is too big\n";
return false;
}
std::FILE* fp = fopen(filename, "wt");
if (fp) {
size_t written = fwrite(text, sizeof(char), text_len, fp);
fclose(fp);
if (written == text_len) {
LOG(DEBUG) << "successfuly wrote " << written << " bytes\n";
return true;
} else {
LOG(ERROR) << "error writing to file: " << filename << "\n";
return false;
}
}
LOG(DEBUG) << text << "\n";
return false;
}

Loading…
Cancel
Save