From 4064b410148fc0ebdf7d7707ecb98aa99d68821e Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Thu, 24 Jan 2019 02:12:12 -0500 Subject: [PATCH] move hexgrid render_group init out of scene_loader --- TODO.md | 5 ++--- src/hexgame.cpp | 33 +++++++++++++++++---------------- src/hexgrid.cpp | 21 ++++++++++++++++++++- src/hexgrid.h | 10 +++++----- src/scene_loader.cpp | 25 ++----------------------- src/scene_loader.h | 2 -- 6 files changed, 46 insertions(+), 50 deletions(-) diff --git a/TODO.md b/TODO.md index 3b4b39d..1a21dc7 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,6 @@ ## TODO: -- init HexGrid render groups in hgInit and remove slCreateHexRenderGroups +- allocate meMeshGroup->meshes as single chunk of mem instead of p2p - position entities on grid hexes - pathfinding - assimp animation @@ -35,9 +35,8 @@ - think about combining mesh data and render_group data to save some memory - 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 - - also cache textures from loaded meshes for re-use - remove v2i/v3f... etc from util.h and either use glm everywhere, or write a smaller linear math.h -- find a better alternative to g_data_dir in scene_loader.cpp +- add an application config file and structure to replace various defines ## LATER TODO: - add initial opengl constant for INT_MAX, and checks in render_group functions diff --git a/src/hexgame.cpp b/src/hexgame.cpp index 937a8ba..7e7eed1 100644 --- a/src/hexgame.cpp +++ b/src/hexgame.cpp @@ -54,6 +54,23 @@ loadSceneFromJson(game_state* s, render_state* rs) game_state* gs = g_game_state; render_state* rs = g_render_state; + slParseCamera(sd, rs->cam); + slParseHexGrid(sd, gs->grid, rs->palette_image); + + if (gs->grid.gridT == HASH_MAP + && !slParseGridHashMap(gs->grid, DATA_DIR, HEXMAP_SCHEMA_FILE)) + { + ret = false; + } + + if (hgInit(gs->grid, rs->filled_hex_render_group, rs->hex_line_render_group, rs->default_shader)) { + // TODO: can remove these once we're using the global color palatte for all textures + rs->filled_hex_render_group->render_objects[0]->tex_id = rs->palette_id; + rs->hex_line_render_group->render_objects[0]->tex_id = rs->palette_id; + } else { + ret = false; + } + if (slParseEntities(sd, gs->entities, gs->entity_count, MAX_ENTITIES)) { for (uint i = 0; i < gs->entity_count; i++) { if (!entInit(gs->entities[i], DATA_DIR, rs->default_shader)) { @@ -66,22 +83,6 @@ loadSceneFromJson(game_state* s, render_state* rs) ret = false; } - slParseCamera(sd, rs->cam); - slParseHexGrid(sd, gs->grid, rs->palette_image); - - if (slCreateHexRenderGroups(gs->grid, rs)) { - if (gs->grid.gridT == HASH_MAP) { - if (!slParseGridHashMap(gs->grid, DATA_DIR, HEXMAP_SCHEMA_FILE)) - ret = false; - } - - if (!hgInit(gs->grid, rs->filled_hex_render_group, rs->hex_line_render_group)) - ret = false; - } else { - LOG(ERROR) << "Error creating hexgrid render groups\n"; - ret = false; - } - if (!slParseLights(sd, rs->lights, rs->num_lights, rs->max_lights)) { LOG(ERROR) << "Error loading lights, exiting\n"; ret = false; diff --git a/src/hexgrid.cpp b/src/hexgrid.cpp index 4644f23..e16eae0 100644 --- a/src/hexgrid.cpp +++ b/src/hexgrid.cpp @@ -3,6 +3,10 @@ #include "hexgrid.h" +#define MAX_HEX_COUNT 8192 // NOTE: padding some extra space for hex buffers to grow +#define IDX_PER_HEX_FILLED 54 // NOTE: 6 triangles * 3 vertices per triangle * 3 floats per vertex +#define IDX_PER_HEX_LINES 36 // NOTE: 6 lines * 2 vertices per line * 3 floats per vertex + // forward declarations hex_info createHexInfo(hexgrid& hg, int q, int r); @@ -14,7 +18,11 @@ void populateLineGLBuffers(hexgrid& hg, hex_info& hxi, render_group* rg_lines); // interface bool -hgInit(hexgrid& hg, render_group* rg_filled, render_group* rg_lines) +hgInit( + hexgrid& hg, + render_group*& rg_filled, + render_group*& rg_lines, + rg_shader_program& shader) { Orientation o = (hg.layout_mode == LAYOUT_FLAT) ? layout_flat : layout_pointy; hg.hexlib_layout = Layout(o, Point(hg.hex_size, hg.hex_size), Point(hg.position.x, hg.position.y)); @@ -33,6 +41,17 @@ hgInit(hexgrid& hg, render_group* rg_filled, render_group* rg_lines) uint hex_count = hg.hex_map.size(); assert(hex_count < MAX_HEX_COUNT); + // init render_groups + uint buf_len = MAX_HEX_COUNT * IDX_PER_HEX_FILLED; + rg_filled = rgInitSingle(shader, buf_len, true); + buf_len = MAX_HEX_COUNT * IDX_PER_HEX_LINES; + rg_lines = rgInitSingle(shader, buf_len, true, 0, GL_LINES); + + if ((rg_filled == nullptr) || (rg_lines == nullptr)) { + LOG(ERROR) << "Error allocating render_group, exiting\n"; + return false; + } + render_object* ro_f = rg_filled->render_objects[0]; render_object* ro_l = rg_lines->render_objects[0]; ro_f->vertex_buffer.count = hex_count * IDX_PER_HEX_FILLED; diff --git a/src/hexgrid.h b/src/hexgrid.h index 53e60c6..89e8b73 100644 --- a/src/hexgrid.h +++ b/src/hexgrid.h @@ -8,10 +8,6 @@ #include "render_group.h" #include "util.h" -#define MAX_HEX_COUNT 8192 // NOTE: padding some extra space for hex buffers to grow -#define IDX_PER_HEX_FILLED 54 // NOTE: 6 triangles * 3 vertices per triangle * 3 floats per vertex -#define IDX_PER_HEX_LINES 36 // NOTE: 6 lines * 2 vertices per line * 3 floats per vertex - // TODO: rename to "hex_draw_mode" to fit project style enum HexDrawMode @@ -87,7 +83,11 @@ struct hexgrid }; -bool hgInit(hexgrid& hg, render_group* rg_filled, render_group* rg_lines); +bool hgInit( + hexgrid& hg, + render_group*& rg_filled, + render_group*& rg_lines, + rg_shader_program& shader); bool hgAddHex(hexgrid& hg, v3f hex_coords, render_group* rg_filled, render_group* rg_lines); diff --git a/src/scene_loader.cpp b/src/scene_loader.cpp index 35a29c9..2bab987 100644 --- a/src/scene_loader.cpp +++ b/src/scene_loader.cpp @@ -20,8 +20,7 @@ struct slSceneDoc rapidjson::Document* doc; }; -// TODO: find a better way to avoid threading 'data_dir' through gooey -// this would be a good candidate to go in some global application config structure +// TODO: put in global application config structure with other defines static const char* g_data_dir; @@ -213,30 +212,10 @@ slParseGridHashMap(hexgrid& hg, const char* data_dir, const char* hexmap_schema_ return true; } + LOG(ERROR) << "Error validation JSON\n"; return false; } -bool -slCreateHexRenderGroups(hexgrid& hg, render_state* rs) -{ - uint buf_len = MAX_HEX_COUNT * IDX_PER_HEX_FILLED; - rs->filled_hex_render_group = rgInitSingle(rs->default_shader, buf_len, true); - - buf_len = MAX_HEX_COUNT * IDX_PER_HEX_LINES; - rs->hex_line_render_group = rgInitSingle(rs->default_shader, buf_len, true, 0, GL_LINES); - - if ((rs->filled_hex_render_group == nullptr) || (rs->hex_line_render_group == nullptr)) { - LOG(ERROR) << "Error allocating render_group, exiting\n"; - return false; - } - - // TODO: can remove these once we're using the global color palatte for all textures - rs->filled_hex_render_group->render_objects[0]->tex_id = rs->palette_id; - rs->hex_line_render_group->render_objects[0]->tex_id = rs->palette_id; - - return true; -} - bool slParseLights(slSceneDoc* sd, rg_point_light* lights, uint& num_lights, uint max_lights) { diff --git a/src/scene_loader.h b/src/scene_loader.h index 7442d6c..ff83a5a 100644 --- a/src/scene_loader.h +++ b/src/scene_loader.h @@ -23,8 +23,6 @@ void slParseHexGrid(slSceneDoc* sd, hexgrid& hg, util_image& palette_image); bool slParseGridHashMap(hexgrid& hg, const char* data_dir, const char* hexmap_schema_file); -bool slCreateHexRenderGroups(hexgrid& hg, render_state* rs); - bool slParseLights(slSceneDoc* sd, rg_point_light* lights, uint& num_lights, uint max_lights); // TODO: this is pretty hacky atm just for testing