diff --git a/data/hashmap_scene_hexmap.json b/data/hashmap_scene_hexmap.json index e69de29..39017d8 100644 --- a/data/hashmap_scene_hexmap.json +++ b/data/hashmap_scene_hexmap.json @@ -0,0 +1 @@ +{"hex_size":10,"layout_mode":"layout_flat","world_position":{"x":0.0,"y":0.0,"z":0.0},"world_normal":{"x":0.0,"y":0.0,"z":1.0},"hexes":[{"XPos":195.0,"YPos":25.98076211353316,"q":13,"r":-5,"s":-8,"vertices":[{"x":205.0,"y":25.980762481689454,"z":0.0},{"x":200.0,"y":17.32050895690918,"z":0.0},{"x":190.0,"y":17.32050895690918,"z":0.0},{"x":185.0,"y":25.980762481689454,"z":0.0},{"x":190.0,"y":34.64101791381836,"z":0.0},{"x":200.0,"y":34.64101791381836,"z":0.0}]},{"XPos":210.0,"YPos":17.320508075688769,"q":14,"r":-6,"s":-8,"vertices":[{"x":220.0,"y":17.32050895690918,"z":0.0},{"x":215.0,"y":8.66025447845459,"z":0.0},{"x":205.0,"y":8.66025447845459,"z":0.0},{"x":200.0,"y":17.32050895690918,"z":0.0},{"x":205.0,"y":25.980762481689454,"z":0.0},{"x":215.0,"y":25.980762481689454,"z":0.0}]},{"XPos":210.0,"YPos":0.0,"q":14,"r":-7,"s":-7,"vertices":[{"x":220.0,"y":0.0,"z":0.0},{"x":215.0,"y":-8.66025447845459,"z":0.0},{"x":205.0,"y":-8.66025447845459,"z":0.0},{"x":200.0,"y":-1.2246467996456087e-15,"z":0.0},{"x":205.0,"y":8.66025447845459,"z":0.0},{"x":215.0,"y":8.66025447845459,"z":0.0}]},{"XPos":210.0,"YPos":-34.641016151377538,"q":14,"r":-9,"s":-5,"vertices":[{"x":220.0,"y":-34.64101791381836,"z":0.0},{"x":215.0,"y":-43.30126953125,"z":0.0},{"x":205.0,"y":-43.30126953125,"z":0.0},{"x":200.0,"y":-34.64101791381836,"z":0.0},{"x":205.0,"y":-25.980762481689454,"z":0.0},{"x":215.0,"y":-25.980762481689454,"z":0.0}]},{"XPos":195.0,"YPos":43.301270189221927,"q":13,"r":-4,"s":-9,"vertices":[{"x":205.0,"y":43.30126953125,"z":0.0},{"x":200.0,"y":34.64101791381836,"z":0.0},{"x":190.0,"y":34.64101791381836,"z":0.0},{"x":185.0,"y":43.30126953125,"z":0.0},{"x":190.0,"y":51.961524963378909,"z":0.0},{"x":200.0,"y":51.961524963378909,"z":0.0}]},{"XPos":210.0,"YPos":-69.28203230275509,"q":14,"r":-11,"s":-3,"vertices":[{"x":220.0,"y":-69.28203582763672,"z":0.0},{"x":215.0,"y":-77.9422836303711,"z":0.0},{"x":205.0,"y":-77.9422836303711,"z":0.0},{"x":200.0,"y":-69.28203582763672,"z":0.0},{"x":205.0,"y":-60.62177658081055,"z":0.0},{"x":215.0,"y":-60.62177658081055,"z":0.0}]},{"XPos":195.0,"YPos":60.621778264910698,"q":13,"r":-3,"s":-10,"vertices":[{"x":205.0,"y":60.62177658081055,"z":0.0},{"x":200.0,"y":51.961524963378909,"z":0.0},{"x":190.0,"y":51.961524963378909,"z":0.0},{"x":185.0,"y":60.62177658081055,"z":0.0},{"x":190.0,"y":69.28203582763672,"z":0.0},{"x":200.0,"y":69.28203582763672,"z":0.0}]},{"XPos":210.0,"YPos":-86.60254037844388,"q":14,"r":-12,"s":-2,"vertices":[{"x":220.0,"y":-86.6025390625,"z":0.0},{"x":215.0,"y":-95.2627944946289,"z":0.0},{"x":205.0,"y":-95.2627944946289,"z":0.0},{"x":200.0,"y":-86.6025390625,"z":0.0},{"x":205.0,"y":-77.9422836303711,"z":0.0},{"x":215.0,"y":-77.9422836303711,"z":0.0}]},{"XPos":105.0,"YPos":-112.58330249197701,"q":7,"r":-10,"s":3,"vertices":[{"x":115.0,"y":-112.58330535888672,"z":0.0},{"x":110.0,"y":-121.2435531616211,"z":0.0},{"x":100.0,"y":-121.2435531616211,"z":0.0},{"x":95.0,"y":-112.58330535888672,"z":0.0},{"x":100.0,"y":-103.92304992675781,"z":0.0},{"x":110.0,"y":-103.92304992675781,"z":0.0}]},{"XPos":60.0,"YPos":-155.88457268119897,"q":4,"r":-11,"s":7,"vertices":[{"x":70.0,"y":-155.8845672607422,"z":0.0},{"x":65.0,"y":-164.54483032226563,"z":0.0},{"x":55.0,"y":-164.54483032226563,"z":0.0},{"x":50.0,"y":-155.8845672607422,"z":0.0},{"x":55.0,"y":-147.2243194580078,"z":0.0},{"x":65.0,"y":-147.2243194580078,"z":0.0}]},{"XPos":75.0,"YPos":-129.90381056766578,"q":5,"r":-10,"s":5,"vertices":[{"x":85.0,"y":-129.90380859375,"z":0.0},{"x":80.0,"y":-138.56407165527345,"z":0.0},{"x":70.0,"y":-138.56407165527345,"z":0.0},{"x":65.0,"y":-129.90380859375,"z":0.0},{"x":70.0,"y":-121.2435531616211,"z":0.0},{"x":80.0,"y":-121.2435531616211,"z":0.0}]},{"XPos":210.0,"YPos":-17.320508075688769,"q":14,"r":-8,"s":-6,"vertices":[{"x":220.0,"y":-17.32050895690918,"z":0.0},{"x":215.0,"y":-25.980762481689454,"z":0.0},{"x":205.0,"y":-25.980762481689454,"z":0.0},{"x":200.0,"y":-17.32050895690918,"z":0.0},{"x":205.0,"y":-8.66025447845459,"z":0.0},{"x":215.0,"y":-8.66025447845459,"z":0.0}]},{"XPos":75.0,"YPos":-147.22431864335457,"q":5,"r":-11,"s":6,"vertices":[{"x":85.0,"y":-147.2243194580078,"z":0.0},{"x":80.0,"y":-155.8845672607422,"z":0.0},{"x":70.0,"y":-155.8845672607422,"z":0.0},{"x":65.0,"y":-147.2243194580078,"z":0.0},{"x":70.0,"y":-138.56407165527345,"z":0.0},{"x":80.0,"y":-138.56407165527345,"z":0.0}]},{"XPos":45.0,"YPos":-129.90381056766578,"q":3,"r":-9,"s":6,"vertices":[{"x":55.0,"y":-129.90380859375,"z":0.0},{"x":50.0,"y":-138.56407165527345,"z":0.0},{"x":40.0,"y":-138.56407165527345,"z":0.0},{"x":35.0,"y":-129.90380859375,"z":0.0},{"x":40.0,"y":-121.2435531616211,"z":0.0},{"x":50.0,"y":-121.2435531616211,"z":0.0}]},{"XPos":60.0,"YPos":-138.56406460551018,"q":4,"r":-10,"s":6,"vertices":[{"x":70.0,"y":-138.56407165527345,"z":0.0},{"x":65.0,"y":-147.2243194580078,"z":0.0},{"x":55.0,"y":-147.2243194580078,"z":0.0},{"x":50.0,"y":-138.56407165527345,"z":0.0},{"x":55.0,"y":-129.90380859375,"z":0.0},{"x":65.0,"y":-129.90380859375,"z":0.0}]},{"XPos":210.0,"YPos":-51.961524227066309,"q":14,"r":-10,"s":-4,"vertices":[{"x":220.0,"y":-51.961524963378909,"z":0.0},{"x":215.0,"y":-60.62177658081055,"z":0.0},{"x":205.0,"y":-60.62177658081055,"z":0.0},{"x":200.0,"y":-51.961524963378909,"z":0.0},{"x":205.0,"y":-43.30126953125,"z":0.0},{"x":215.0,"y":-43.30126953125,"z":0.0}]},{"XPos":90.0,"YPos":-121.24355652982139,"q":6,"r":-10,"s":4,"vertices":[{"x":100.0,"y":-121.2435531616211,"z":0.0},{"x":95.0,"y":-129.90380859375,"z":0.0},{"x":85.0,"y":-129.90380859375,"z":0.0},{"x":80.0,"y":-121.2435531616211,"z":0.0},{"x":85.0,"y":-112.58330535888672,"z":0.0},{"x":95.0,"y":-112.58330535888672,"z":0.0}]},{"XPos":45.0,"YPos":-147.22431864335455,"q":3,"r":-10,"s":7,"vertices":[{"x":55.0,"y":-147.2243194580078,"z":0.0},{"x":50.0,"y":-155.8845672607422,"z":0.0},{"x":40.0,"y":-155.8845672607422,"z":0.0},{"x":35.0,"y":-147.2243194580078,"z":0.0},{"x":40.0,"y":-138.56407165527345,"z":0.0},{"x":50.0,"y":-138.56407165527345,"z":0.0}]},{"XPos":180.0,"YPos":-103.92304845413264,"q":12,"r":-12,"s":0,"vertices":[{"x":190.0,"y":-103.92304992675781,"z":0.0},{"x":185.0,"y":-112.58330535888672,"z":0.0},{"x":175.0,"y":-112.58330535888672,"z":0.0},{"x":170.0,"y":-103.92304992675781,"z":0.0},{"x":175.0,"y":-95.2627944946289,"z":0.0},{"x":185.0,"y":-95.2627944946289,"z":0.0}]},{"XPos":60.0,"YPos":-121.24355652982139,"q":4,"r":-9,"s":5,"vertices":[{"x":70.0,"y":-121.2435531616211,"z":0.0},{"x":65.0,"y":-129.90380859375,"z":0.0},{"x":55.0,"y":-129.90380859375,"z":0.0},{"x":50.0,"y":-121.2435531616211,"z":0.0},{"x":55.0,"y":-112.58330535888672,"z":0.0},{"x":65.0,"y":-112.58330535888672,"z":0.0}]},{"XPos":120.0,"YPos":-121.2435565298214,"q":8,"r":-11,"s":3,"vertices":[{"x":130.0,"y":-121.2435531616211,"z":0.0},{"x":125.0,"y":-129.90380859375,"z":0.0},{"x":115.0,"y":-129.90380859375,"z":0.0},{"x":110.0,"y":-121.2435531616211,"z":0.0},{"x":115.0,"y":-112.58330535888672,"z":0.0},{"x":125.0,"y":-112.58330535888672,"z":0.0}]},{"XPos":135.0,"YPos":-112.58330249197701,"q":9,"r":-11,"s":2,"vertices":[{"x":145.0,"y":-112.58330535888672,"z":0.0},{"x":140.0,"y":-121.2435531616211,"z":0.0},{"x":130.0,"y":-121.2435531616211,"z":0.0},{"x":125.0,"y":-112.58330535888672,"z":0.0},{"x":130.0,"y":-103.92304992675781,"z":0.0},{"x":140.0,"y":-103.92304992675781,"z":0.0}]},{"XPos":150.0,"YPos":-121.24355652982143,"q":10,"r":-12,"s":2,"vertices":[{"x":160.0,"y":-121.2435531616211,"z":0.0},{"x":155.0,"y":-129.90380859375,"z":0.0},{"x":145.0,"y":-129.90380859375,"z":0.0},{"x":140.0,"y":-121.2435531616211,"z":0.0},{"x":145.0,"y":-112.58330535888672,"z":0.0},{"x":155.0,"y":-112.58330535888672,"z":0.0}]},{"XPos":165.0,"YPos":-112.58330249197704,"q":11,"r":-12,"s":1,"vertices":[{"x":175.0,"y":-112.58330535888672,"z":0.0},{"x":170.0,"y":-121.2435531616211,"z":0.0},{"x":160.0,"y":-121.2435531616211,"z":0.0},{"x":155.0,"y":-112.58330535888672,"z":0.0},{"x":160.0,"y":-103.92304992675781,"z":0.0},{"x":170.0,"y":-103.92304992675781,"z":0.0}]},{"XPos":195.0,"YPos":-95.26279441628827,"q":13,"r":-12,"s":-1,"vertices":[{"x":205.0,"y":-95.2627944946289,"z":0.0},{"x":200.0,"y":-103.92304992675781,"z":0.0},{"x":190.0,"y":-103.92304992675781,"z":0.0},{"x":185.0,"y":-95.2627944946289,"z":0.0},{"x":190.0,"y":-86.6025390625,"z":0.0},{"x":200.0,"y":-86.6025390625,"z":0.0}]}]} \ No newline at end of file diff --git a/src/hexgame.cpp b/src/hexgame.cpp index 24b4d79..12d3b1a 100644 --- a/src/hexgame.cpp +++ b/src/hexgame.cpp @@ -95,7 +95,7 @@ init() // which contains the hashtable. g_game_state->grid.hex_map = std::unordered_map(); // 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); diff --git a/src/scene_loader.cpp b/src/scene_loader.cpp index 06b9b57..27c266f 100644 --- a/src/scene_loader.cpp +++ b/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& 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 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& 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(); +} diff --git a/src/util.cpp b/src/util.cpp index d10598e..0f3569f 100644 --- a/src/util.cpp +++ b/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; }