diff --git a/src/hexgame.cpp b/src/hexgame.cpp index 6a109d0..a6ed90f 100644 --- a/src/hexgame.cpp +++ b/src/hexgame.cpp @@ -14,7 +14,7 @@ * - 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 to leave only init glue in a main.cpp + * - move hex logic to new file (hexgrid.h) to leave only init glue in a main.cpp * - move SDL input callbacks somewhere too? * - 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 @@ -41,6 +41,7 @@ #define DATA_DIR "../data" #define DEFAULT_SCENE_FILE "test_scene.json" #define SCENE_SCHEMA_FILE "scene_schema.json" +#define MAX_ENTITIES 1000 #include @@ -511,7 +512,7 @@ int main(int argc, char* argv[]) g_game_state->draw_mode = CONE_FILL; // TODO: better entity memory management // TODO: hard coded limit of 1000 entities here - g_game_state->entities = UTIL_ALLOC(1000, Entity); + g_game_state->entities = UTIL_ALLOC(MAX_ENTITIES, Entity); // init global render state g_render_state = new render_state(); @@ -562,10 +563,12 @@ int main(int argc, char* argv[]) // testing scene_loader - if (slLoadFile(DATA_DIR, DEFAULT_SCENE_FILE, SCENE_SCHEMA_FILE, - g_game_state->entities, g_game_state->entity_count, 1000, renGetCamera())) - { - // ??? + 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); + slParseCamera(sd, renGetCamera()); + slFreeSceneDoc(sd); } else { LOG(ERROR) << "Error loading scene, exiting\n"; return 1; diff --git a/src/hexgrid.h b/src/hexgrid.h new file mode 100644 index 0000000..f80e172 --- /dev/null +++ b/src/hexgrid.h @@ -0,0 +1,11 @@ + +#include "hexlib.h" +#include "util.h" + + +// TODO: move hex_info definition here and update references +struct hex_info; + +void hgCreateHexes(vector *hxi_array, const Layout &layout, uint32 color); +hex_info* hgGetSingleHex(int32 x, int32 y); +void hgResetHexes(); diff --git a/src/scene_loader.cpp b/src/scene_loader.cpp index 9679e36..cb52c07 100644 --- a/src/scene_loader.cpp +++ b/src/scene_loader.cpp @@ -8,57 +8,67 @@ #include "aixlog.hpp" #include "entity.h" +#include "hexgrid.h" #include "mesh.h" #include "util.h" #include "scene_loader.h" +struct slSceneDoc +{ + rapidjson::Document* doc; +}; + + // forward declarations -bool parseFile(rapidjson::Document& doc, const char* data_dir, const char* file_name); +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); -bool validateScene(rapidjson::Document& schema_doc, rapidjson::Document& scene_doc, +bool validateScene(rapidjson::Document* schema_doc, rapidjson::Document* scene_doc, const char* scene_file); // interface -bool -slLoadFile( - const char* data_dir, - const char* scene_file, - const char* schema_file, - Entity* entity_array, - uint& entity_count, - uint max_entities, - camera& cam) +slSceneDoc* +slLoadFile(const char* data_dir, const char* scene_file, const char* schema_file) { - if (entity_array == nullptr) { - LOG(ERROR) << "entity array not initialized\n"; - return false; - } - LOG(INFO) << "Loading scene file: " << scene_file << "\n"; - rapidjson::Document doc, schema_doc; - bool retDoc = parseFile(doc, data_dir, scene_file); - bool retSchema = parseFile(schema_doc, data_dir, schema_file); + rapidjson::Document schema_doc; + slSceneDoc* sd = UTIL_ALLOC(1, slSceneDoc); + sd->doc = new rapidjson::Document(); - if (!retDoc || !retSchema || !validateScene(schema_doc, doc, scene_file)) - return false; + bool retDoc = parseFile(sd->doc, data_dir, scene_file); + bool retSchema = parseFile(&schema_doc, data_dir, schema_file); - // camera - const rapidjson::Value& json_cam = doc["camera"]; - cameraInitPerspective(cam, - parseVec3(json_cam["position"]), - parseVec3(json_cam["target"]), - parseVec3(json_cam["up"]) - ); + if (!retDoc || !retSchema || !validateScene(&schema_doc, sd->doc, scene_file)) { + slFreeSceneDoc(sd); + return nullptr; + } + + return sd; +} +void +slFreeSceneDoc(slSceneDoc* sd) +{ + if (sd->doc != nullptr) { + delete sd->doc; + sd->doc = nullptr; + } - // hex grid + utilSafeFree(sd); +} - // entities - const rapidjson::Value& entities = doc["entities"]; +bool +slParseEntities( + slSceneDoc* sd, + Entity* entity_array, + uint& entity_count, + uint max_entities, + const char* data_dir) +{ + const rapidjson::Value& entities = (*sd->doc)["entities"]; if (entities.Size() > max_entities) { LOG(ERROR) << "entity count: " << entities.Size() << ", was more than max_entities: " << max_entities << "\n"; @@ -83,13 +93,25 @@ slLoadFile( entity_count++; } - // lights - return true; } +void +slParseCamera(slSceneDoc* sd, camera& cam) +{ + const rapidjson::Value& json_cam = (*sd->doc)["camera"]; + cameraInitPerspective(cam, + parseVec3(json_cam["position"]), + parseVec3(json_cam["target"]), + parseVec3(json_cam["up"]) + ); +} + + +// internal + bool -parseFile(rapidjson::Document& doc, const char* data_dir, const char* file_name) +parseFile(rapidjson::Document* doc, const char* data_dir, const char* file_name) { bool ret = true; std::string file_path; @@ -101,7 +123,7 @@ parseFile(rapidjson::Document& doc, const char* data_dir, const char* file_name) ret = false; } - if (doc.Parse(contents).HasParseError() || !doc.IsObject()) { + if (doc->Parse(contents).HasParseError() || !doc->IsObject()) { LOG(ERROR) << "Error parrsing scene file: " << file_name << "\n"; ret = false; } @@ -112,12 +134,12 @@ parseFile(rapidjson::Document& doc, const char* data_dir, const char* file_name) } bool -validateScene(rapidjson::Document& schema_doc, rapidjson::Document& scene_doc, const char* scene_file) +validateScene(rapidjson::Document* schema_doc, rapidjson::Document* scene_doc, const char* scene_file) { - rapidjson::SchemaDocument schema(schema_doc); + rapidjson::SchemaDocument schema(*schema_doc); rapidjson::SchemaValidator validator(schema); - if (!scene_doc.Accept(validator)) { + if (!scene_doc->Accept(validator)) { LOG(ERROR) << "Error validating scene file: " << scene_file << "\n"; rapidjson::StringBuffer sb; validator.GetInvalidSchemaPointer().StringifyUriFragment(sb); diff --git a/src/scene_loader.h b/src/scene_loader.h index 3c5f878..b0cfa7f 100644 --- a/src/scene_loader.h +++ b/src/scene_loader.h @@ -2,14 +2,19 @@ #include "camera.h" #include "util.h" -/* - * @param max_entities maximum size of array allocated - */ -bool slLoadFile( - const char* data_dir, - const char* scene_file, - const char* schema_file, + +struct slSceneDoc; + +slSceneDoc* slLoadFile(const char* data_dir, const char* scene_file, const char* schema_file); +void slFreeSceneDoc(slSceneDoc* sd); + +bool slParseEntities( + slSceneDoc* sd, Entity* entity_array, uint& entity_count, uint max_entities, - camera& cam); + const char* data_dir + ); + +void slParseCamera(slSceneDoc* sd, camera& cam); +//bool slParseHexGrid(slSceneDoc* sd, std::vector& hexes);