From 8a81d5541bab030c0e6d9cd41f0edd57bd87eed6 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Fri, 23 Nov 2018 13:54:28 -0500 Subject: [PATCH] load hexgrid from scene file --- data/scene_schema.json | 23 +++++++++++++++--- data/test_scene.json | 22 +++++++++++------ src/hexgame.cpp | 55 +++++++++--------------------------------- src/hexgrid.cpp | 39 +++++++++++++++++++++++++++--- src/hexgrid.h | 21 +++++++++++++--- src/scene_loader.cpp | 34 +++++++++++++++++++++++--- src/scene_loader.h | 4 ++- 7 files changed, 132 insertions(+), 66 deletions(-) diff --git a/data/scene_schema.json b/data/scene_schema.json index a74bdc6..60b1f1a 100644 --- a/data/scene_schema.json +++ b/data/scene_schema.json @@ -49,10 +49,23 @@ "hex_grid" : { "type": "object", "properties": { - "layout_type" : { "type": "string" }, + "position": { "$ref": "#/definitions/vector3" }, + "fill_color" : { "type": "string" }, + "selected_fill_color" : { "type": "string" }, + "hexlib_orientation" : { "type": "string" }, "hex_size" : { "type": "number" }, + "grid_type" : { "type": "string" }, "hex_radius" : { "type": "number" } - } + }, + "required": [ + "position", + "fill_color", + "selected_fill_color", + "hexlib_orientation", + "hex_size", + "grid_type", + "hex_radius" + ] }, "entities": { @@ -63,7 +76,8 @@ "position": { "$ref": "#/definitions/vector3" }, "rotation": { "$ref": "#/definitions/vector4" }, "scale": { "$ref": "#/definitions/vector3" } - } + }, + "required" : ["name", "model_file", "position", "rotation", "scale"] }, "lights": { @@ -72,7 +86,8 @@ "intensity": { "type": "number" }, "color": { "type": "#/definitions/rgb" }, "position": { "$ref": "#/definitions/vector3" } - } + }, + "required" : ["intensity", "color", "position"] } } } diff --git a/data/test_scene.json b/data/test_scene.json index 339f59d..1e01f56 100644 --- a/data/test_scene.json +++ b/data/test_scene.json @@ -1,8 +1,8 @@ { "camera" : { "position" : { - "x" : 640, - "y" : 0, + "x" : 0, + "y" : -800, "z" : 100 }, "target" : { @@ -18,8 +18,16 @@ }, "hex_grid" : { - "layout_type" : "hexagon", + "position" : { + "x" : 0, + "y" : 0, + "z" : 0 + }, "hex_size" : 10, + "fill_color" : "0x565656FF", + "selected_fill_color": "0xF46000FF", + "hexlib_orientation" : "layout_flat", + "grid_type" : "hexagon", "hex_radius" : 20 }, @@ -28,8 +36,8 @@ "name" : "catepillar 1", "model_file" : "catepillar.dae", "position" : { - "x" : 740, - "y" : 500, + "x" : -140, + "y" : 50, "z" : 0 }, "rotation" : { @@ -48,8 +56,8 @@ "name" : "catepillar 2", "model_file" : "catepillar.dae", "position" : { - "x" : 600, - "y" : 400, + "x" : 140, + "y" : 0, "z" : 0 }, "rotation" : { diff --git a/src/hexgame.cpp b/src/hexgame.cpp index 6c9d470..5db2746 100644 --- a/src/hexgame.cpp +++ b/src/hexgame.cpp @@ -1,17 +1,14 @@ /******************************************************************************* * TODO: - * - add a color buffer to hex_line and debug render groups to re-use - * fragment shaders and simplify draw rgDraw() - * - need to add normal buffer too + * - add 3d orientation widget to gooey * - lighting - * - add light struct - * - pass all lights to render groups/shaders every frame + * - test adding more lights * - map generation * - pathfinding * - assimp animation * - replace aixlog with custom logging iostream? * - fix vector normalization in renderer.cpp when moving in 2 directions - * - may be fixed?? need to test by examining camera position between frames + * - may be fixed?? need to test by examining camera position between frames * and compare the distance * - add prefix to interface function names for eg) gooey.h, renderer.h * - move hex logic to new file (hexgrid.h) to leave only init glue in a main.cpp @@ -20,17 +17,20 @@ * - actually don't need to save the vertex/normal buffers after passing * to opengl * - use a storage pool for assimp meshes allowing reuse across entities - * - replace calls to malloc/free with safe(r) function in util.h + * - replace remaining calls to malloc/free with safe(r) function in util.h * - add cpu perforcmace counters in render loop * - check for memory leaks w/ valgrind ******************************************************************************/ // Some defaults for the game layout +#if 0 #define HEX_SIZE 10 #define HEX_RADIUS 19 #define HEX_ORIENTATION layout_flat #define FILL_COLOR 0x565656FF #define SELECTED_FILL_COLOR 0xF46000FF +#endif + #define DEBUG_DRAW true #define VIEWPORT_WIDTH 1920 #define VIEWPORT_HEIGHT 1080 @@ -77,32 +77,6 @@ static game_state* g_game_state; static render_state* g_render_state; static vector g_polygon_select_vertices = {Point(), Point(), Point(), Point()}; -void -createHexes(vector *hxi_array, const Layout &layout, uint32 color) -{ - // create a hexagonal grid of hexagons - int map_radius = HEX_RADIUS; - for (int q = -map_radius; q <= map_radius; q++) - { - int r1 = std::max(-map_radius, -q - map_radius); - int r2 = std::min(map_radius, -q + map_radius); - - for (int r = r1; r <= r2; r++) - { - hex_info hxi; - hxi.hexID = (int32) hxi_array->size(); - hxi.hex.q = q; hxi.hex.r = r; hxi.hex.s = -q-r; - Point p = hex_to_pixel(layout, hxi.hex); - hxi.XPos = p.x; - hxi.YPos = p.y; - hxi.current_color = color; - hxi.stored_color = color; - hxi.vertices = polygon_corners(layout, hxi.hex); - hxi.vertices.shrink_to_fit(); - hxi_array->push_back(hxi); - } - } -} v2i mapMouseToViewport(int32 x, int32 y) @@ -131,7 +105,7 @@ getSingleHex(int32 x, int32 y) v2i dims = g_render_state->viewport_dims; v2f v = getUnprojectedCoords(x, y, dims.x, dims.y); Point p(v.x, v.y); - Hex h = hex_round(pixel_to_hex(g_game_state->grid.hex_layout, p)); + Hex h = hex_round(pixel_to_hex(g_game_state->grid.hexlib_layout, p)); for (hex_info &hxi : *g_game_state->grid.hex_array) { @@ -502,9 +476,7 @@ int main(int argc, char* argv[]) LOG(INFO) << "Application started\n"; // init global game state - Layout layout(HEX_ORIENTATION, Point(HEX_SIZE, HEX_SIZE), Point(VIEWPORT_WIDTH / 2, VIEWPORT_HEIGHT / 2)); g_game_state = UTIL_ALLOC(1, game_state); - g_game_state->grid.hex_layout = layout; g_game_state->grid.hex_array = new vector; g_game_state->entities = UTIL_ALLOC(MAX_ENTITIES, Entity); @@ -512,11 +484,7 @@ int main(int argc, char* argv[]) g_render_state = UTIL_ALLOC(1, render_state); g_render_state->is_debug_draw = DEBUG_DRAW; g_render_state->viewport_dims = v2i(VIEWPORT_WIDTH, VIEWPORT_HEIGHT); - g_render_state->fill_color = FILL_COLOR; - g_render_state->selected_fill_color = SELECTED_FILL_COLOR; - - createHexes(g_game_state->grid.hex_array, g_game_state->grid.hex_layout, g_render_state->fill_color); SDL_Handles handles; if (SDL_Init(SDL_INIT_VIDEO) != 0) { @@ -559,10 +527,11 @@ int main(int argc, char* argv[]) slSceneDoc* sd = slLoadFile(DATA_DIR, DEFAULT_SCENE_FILE, SCENE_SCHEMA_FILE); if (sd != nullptr) { - slParseEntities(sd, g_game_state->entities, g_game_state->entity_count, MAX_ENTITIES, - DATA_DIR); + game_state* g = g_game_state; + render_state* r = g_render_state; + slParseEntities(sd, g->entities, g->entity_count, MAX_ENTITIES, DATA_DIR); slParseCamera(sd, renGetCamera()); - slParseHexGrid(sd, g_game_state->grid); + slParseHexGrid(sd, g->grid, r->fill_color, r->selected_fill_color); slFreeSceneDoc(sd); } else { LOG(ERROR) << "Error loading scene, exiting\n"; diff --git a/src/hexgrid.cpp b/src/hexgrid.cpp index 42573ce..e612884 100644 --- a/src/hexgrid.cpp +++ b/src/hexgrid.cpp @@ -1,13 +1,19 @@ +#include "aixlog.hpp" + #include "hexgrid.h" +// forward declarations +void createHexagonGrid(hexgrid hg); + // interface void -hgCreateHexes(vector *hxi_array, const Layout &layout, uint32 color) +hgCreateHexes(hexgrid hg) { - + if (hg.gridT == HEXAGON) + createHexagonGrid(hg); } hex_info* @@ -26,7 +32,34 @@ hgResetHexes() // internal void -createHexagonGrid(vector *hxi_array, const Layout &layout, uint32 color, uint radius) +createHexagonGrid(hexgrid hg) { + if (hg.hex_array == nullptr) { + LOG(ERROR) << "hg.hex_array is not initialized\n"; + return; + } + + int hr = hg.hex_radius; + int nhr = hg.hex_radius * -1; + + for (int q = nhr; q <= hr; q++) + { + int r1 = std::max(nhr, -q - hr); + int r2 = std::min(hr, -q + hr); + for (int r = r1; r <= r2; r++) + { + hex_info hxi; + hxi.hexID = (int32) hg.hex_array->size(); + hxi.hex.q = q; hxi.hex.r = r; hxi.hex.s = -q-r; + Point p = hex_to_pixel(hg.hexlib_layout, hxi.hex); + hxi.XPos = p.x; + hxi.YPos = p.y; + hxi.current_color = hg.fill_color; + hxi.stored_color = hg.fill_color; + hxi.vertices = polygon_corners(hg.hexlib_layout, hxi.hex); + hxi.vertices.shrink_to_fit(); + hg.hex_array->push_back(hxi); + } + } } diff --git a/src/hexgrid.h b/src/hexgrid.h index ba412c1..82803ee 100644 --- a/src/hexgrid.h +++ b/src/hexgrid.h @@ -7,6 +7,7 @@ #include "util.h" +// TODO: rename to "hex_draw_mode" to fit project style enum HexDrawMode { NONE, @@ -16,6 +17,15 @@ enum HexDrawMode PATHFINDING }; +// TODO: implement more hexgrid types +enum grid_type +{ + HEXAGON, + PARALLELOGRAM, + RHOMBUS, + HASH_MAP +}; + struct hex_info { int32 hexID = 0; @@ -30,13 +40,16 @@ struct hex_info struct hexgrid { - Layout hex_layout; + grid_type gridT = HEXAGON; + Layout hexlib_layout; + Orientation hexlib_orientation; + v3f position = v3f(0, 0, 0); uint hex_size = 10; uint hex_radius = 0; uint32 fill_color = 0x565656FF; - uint32 selected_color = 0xF46000FF; + uint32 selected_fill_color = 0xF46000FF; - std::vector *hex_array; + std::vector* hex_array = nullptr; hex_info* start_hex = nullptr; hex_info* current_hex = nullptr; vector selected_hexes; @@ -45,6 +58,6 @@ struct hexgrid }; -void hgCreateHexes(vector *hxi_array, const Layout &layout, uint32 color); +void hgCreateHexes(hexgrid hg); hex_info* hgGetSingleHex(int32 x, int32 y); void hgResetHexes(); diff --git a/src/scene_loader.cpp b/src/scene_loader.cpp index 9bc42f6..5aadfca 100644 --- a/src/scene_loader.cpp +++ b/src/scene_loader.cpp @@ -9,6 +9,7 @@ #include "entity.h" #include "hexgrid.h" +#include "hexlib.h" #include "mesh.h" #include "util.h" #include "scene_loader.h" @@ -24,6 +25,7 @@ struct slSceneDoc bool parseFile(rapidjson::Document* doc, const char* data_dir, const char* file_name); glm::vec3 parseVec3(const rapidjson::Value& node); glm::vec4 parseVec4(const rapidjson::Value& node); +uint32 parseHex32(std::string s); bool validateScene(rapidjson::Document* schema_doc, rapidjson::Document* scene_doc, const char* scene_file); @@ -108,15 +110,39 @@ slParseCamera(slSceneDoc* sd, camera& cam) } void -slParseHexGrid(slSceneDoc* sd, hexgrid& hg) +slParseHexGrid(slSceneDoc* sd, hexgrid& hg, uint32& fill_color, uint32&selected_fill_color) { const rapidjson::Value& json_grid = (*sd->doc)["hex_grid"]; - std::string layout_type = json_grid["layout_type"].GetString(); - if (layout_type == "hexagon") { - hg.hex_size = json_grid["hex_size"].GetInt(); + std::string grid_type_str = json_grid["grid_type"].GetString(); + if (grid_type_str == "hexagon") + hg.gridT = HEXAGON; + else if (grid_type_str == "parallelogram") + hg.gridT = PARALLELOGRAM; + else if (grid_type_str == "rhombus") + hg.gridT = RHOMBUS; + else if (grid_type_str == "hash_map") + hg.gridT = HASH_MAP; + + hg.hex_size = json_grid["hex_size"].GetInt(); + glm::vec3 pos = parseVec3(json_grid["position"]); + hg.position.x = pos.x; + hg.position.y = pos.y; + hg.position.z = pos.z; + std::string fill_color_str = json_grid["fill_color"].GetString(); + hg.fill_color = std::stoul(fill_color_str, nullptr, 16); + std::string selected_fill_color_str = json_grid["selected_fill_color"].GetString(); + hg.selected_fill_color = std::stoul(selected_fill_color_str, nullptr, 16); + std::string orientation_str = json_grid["hexlib_orientation"].GetString(); + hg.hexlib_orientation = (orientation_str == "layout_flat") ? layout_flat : layout_pointy; + Layout lo(hg.hexlib_orientation, Point(hg.hex_size, hg.hex_size), Point(hg.position.x, hg.position.y)); + hg.hexlib_layout = lo; + + if (hg.gridT == HEXAGON) { hg.hex_radius = json_grid["hex_radius"].GetInt(); } + + hgCreateHexes(hg); } diff --git a/src/scene_loader.h b/src/scene_loader.h index dad960a..7e0f2ee 100644 --- a/src/scene_loader.h +++ b/src/scene_loader.h @@ -18,4 +18,6 @@ bool slParseEntities( ); void slParseCamera(slSceneDoc* sd, camera& cam); -void slParseHexGrid(slSceneDoc* sd, hexgrid& hg); + +// TODO: remove fill_color,selected_fill_color refs here, and use hex_grid object in renderer +void slParseHexGrid(slSceneDoc* sd, hexgrid& hg, uint32& fill_color, uint32&selected_fill_color);