Browse Source

splitting up slLoadFile into smaller functions

master
cinnaboot 8 years ago
parent
commit
8bcc551ca5
  1. 15
      src/hexgame.cpp
  2. 11
      src/hexgrid.h
  3. 98
      src/scene_loader.cpp
  4. 21
      src/scene_loader.h

15
src/hexgame.cpp

@ -14,7 +14,7 @@
* - 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 * and compare the distance
* - add prefix to interface function names for eg) gooey.h, renderer.h * - 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? * - move SDL input callbacks somewhere too?
* - think about combining mesh data and render_group data to save some memory * - 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 * - actually don't need to save the vertex/normal buffers after passing
@ -41,6 +41,7 @@
#define DATA_DIR "../data" #define DATA_DIR "../data"
#define DEFAULT_SCENE_FILE "test_scene.json" #define DEFAULT_SCENE_FILE "test_scene.json"
#define SCENE_SCHEMA_FILE "scene_schema.json" #define SCENE_SCHEMA_FILE "scene_schema.json"
#define MAX_ENTITIES 1000
#include <vector> #include <vector>
@ -511,7 +512,7 @@ int main(int argc, char* argv[])
g_game_state->draw_mode = CONE_FILL; g_game_state->draw_mode = CONE_FILL;
// TODO: better entity memory management // TODO: better entity memory management
// TODO: hard coded limit of 1000 entities here // 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 // init global render state
g_render_state = new render_state(); g_render_state = new render_state();
@ -562,10 +563,12 @@ int main(int argc, char* argv[])
// testing scene_loader // testing scene_loader
if (slLoadFile(DATA_DIR, DEFAULT_SCENE_FILE, SCENE_SCHEMA_FILE, slSceneDoc* sd = slLoadFile(DATA_DIR, DEFAULT_SCENE_FILE, SCENE_SCHEMA_FILE);
g_game_state->entities, g_game_state->entity_count, 1000, renGetCamera())) if (sd != nullptr) {
{ slParseEntities(sd, g_game_state->entities, g_game_state->entity_count, MAX_ENTITIES,
// ??? DATA_DIR);
slParseCamera(sd, renGetCamera());
slFreeSceneDoc(sd);
} else { } else {
LOG(ERROR) << "Error loading scene, exiting\n"; LOG(ERROR) << "Error loading scene, exiting\n";
return 1; return 1;

11
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<hex_info> *hxi_array, const Layout &layout, uint32 color);
hex_info* hgGetSingleHex(int32 x, int32 y);
void hgResetHexes();

98
src/scene_loader.cpp

@ -8,57 +8,67 @@
#include "aixlog.hpp" #include "aixlog.hpp"
#include "entity.h" #include "entity.h"
#include "hexgrid.h"
#include "mesh.h" #include "mesh.h"
#include "util.h" #include "util.h"
#include "scene_loader.h" #include "scene_loader.h"
struct slSceneDoc
{
rapidjson::Document* doc;
};
// forward declarations // 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::vec3 parseVec3(const rapidjson::Value& node);
glm::vec4 parseVec4(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); const char* scene_file);
// interface // interface
bool slSceneDoc*
slLoadFile( slLoadFile(const char* data_dir, const char* scene_file, const char* schema_file)
const char* data_dir,
const char* scene_file,
const char* schema_file,
Entity* entity_array,
uint& entity_count,
uint max_entities,
camera& cam)
{ {
if (entity_array == nullptr) {
LOG(ERROR) << "entity array not initialized\n";
return false;
}
LOG(INFO) << "Loading scene file: " << scene_file << "\n"; LOG(INFO) << "Loading scene file: " << scene_file << "\n";
rapidjson::Document doc, schema_doc; rapidjson::Document schema_doc;
bool retDoc = parseFile(doc, data_dir, scene_file); slSceneDoc* sd = UTIL_ALLOC(1, slSceneDoc);
bool retSchema = parseFile(schema_doc, data_dir, schema_file); sd->doc = new rapidjson::Document();
if (!retDoc || !retSchema || !validateScene(schema_doc, doc, scene_file)) bool retDoc = parseFile(sd->doc, data_dir, scene_file);
return false; bool retSchema = parseFile(&schema_doc, data_dir, schema_file);
// camera if (!retDoc || !retSchema || !validateScene(&schema_doc, sd->doc, scene_file)) {
const rapidjson::Value& json_cam = doc["camera"]; slFreeSceneDoc(sd);
cameraInitPerspective(cam, return nullptr;
parseVec3(json_cam["position"]), }
parseVec3(json_cam["target"]),
parseVec3(json_cam["up"]) return sd;
); }
void
slFreeSceneDoc(slSceneDoc* sd)
{
if (sd->doc != nullptr) {
delete sd->doc;
sd->doc = nullptr;
}
// hex grid utilSafeFree(sd);
}
// entities bool
const rapidjson::Value& entities = doc["entities"]; 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) { if (entities.Size() > max_entities) {
LOG(ERROR) << "entity count: " << entities.Size() << ", was more than max_entities: " LOG(ERROR) << "entity count: " << entities.Size() << ", was more than max_entities: "
<< max_entities << "\n"; << max_entities << "\n";
@ -83,13 +93,25 @@ slLoadFile(
entity_count++; entity_count++;
} }
// lights
return true; 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 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; bool ret = true;
std::string file_path; std::string file_path;
@ -101,7 +123,7 @@ parseFile(rapidjson::Document& doc, const char* data_dir, const char* file_name)
ret = false; 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"; LOG(ERROR) << "Error parrsing scene file: " << file_name << "\n";
ret = false; ret = false;
} }
@ -112,12 +134,12 @@ parseFile(rapidjson::Document& doc, const char* data_dir, const char* file_name)
} }
bool 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); rapidjson::SchemaValidator validator(schema);
if (!scene_doc.Accept(validator)) { if (!scene_doc->Accept(validator)) {
LOG(ERROR) << "Error validating scene file: " << scene_file << "\n"; LOG(ERROR) << "Error validating scene file: " << scene_file << "\n";
rapidjson::StringBuffer sb; rapidjson::StringBuffer sb;
validator.GetInvalidSchemaPointer().StringifyUriFragment(sb); validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);

21
src/scene_loader.h

@ -2,14 +2,19 @@
#include "camera.h" #include "camera.h"
#include "util.h" #include "util.h"
/*
* @param max_entities maximum size of array allocated struct slSceneDoc;
*/
bool slLoadFile( slSceneDoc* slLoadFile(const char* data_dir, const char* scene_file, const char* schema_file);
const char* data_dir, void slFreeSceneDoc(slSceneDoc* sd);
const char* scene_file,
const char* schema_file, bool slParseEntities(
slSceneDoc* sd,
Entity* entity_array, Entity* entity_array,
uint& entity_count, uint& entity_count,
uint max_entities, uint max_entities,
camera& cam); const char* data_dir
);
void slParseCamera(slSceneDoc* sd, camera& cam);
//bool slParseHexGrid(slSceneDoc* sd, std::vector<hex_info*>& hexes);

Loading…
Cancel
Save