Browse Source

breaking scene loading for new branch

master
cinnaboot 8 years ago
parent
commit
66d73199d8
  1. 58
      data/test_scene.json
  2. 19
      src/hexgame.cpp
  3. 2
      src/renderer.cpp
  4. 52
      src/scene_loader.cpp
  5. 7
      src/scene_loader.h

58
data/test_scene.json

@ -0,0 +1,58 @@
{
"camera" : {
"position" : {
"x" : 0,
"y" : 0,
"z" : 0
},
"rotation" : {
"x" : 0,
"y" : 0,
"z" : 0,
"w" : 0
}
},
"hex_grid" : {
"layout_type" : "hexagon",
"hex_size" : 10,
"hex_radius" : 20
},
"entities" : [
{
"name" : "catepillar 1",
"model_file" : "catepillar.dae",
"position" : {
"x" : 0,
"y" : 0,
"z" : 0
},
"rotation" : {
"x" : 0,
"y" : 0,
"z" : 0,
"w" : 0
}
}
],
"lights" : [
{
"intensity" : 1.0,
"color" : {
"r" : 0,
"g" : 0,
"b" : 0
},
"position" : {
"x" : 0,
"y" : 0,
"z" : 0
}
}
]
}

19
src/hexgame.cpp

@ -36,6 +36,9 @@
#define CONE_ANGLE 30
#define VSYNC_ENABLED true
//testing
#define DEFAULT_SCENE_FILE "../data/test_scene.json"
#include <vector>
@ -62,6 +65,7 @@
#include "mesh.h"
#include "platform_wait_for_vblank.h"
#include "renderer.h"
#include "scene_loader.h"
#include "util.h"
using std::vector;
@ -482,7 +486,7 @@ cleanUp(SDL_Handles &handles)
#if defined(_WIN32)
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
LPSTR lpCmdLine, int nShowCmd)
#else
int main(int argc, char* argv[])
#endif
@ -550,6 +554,7 @@ int main(int argc, char* argv[])
game_state* g = g_game_state;
g->entities = (Entity*) std::calloc(1000, sizeof(Entity));
#if 0
meMeshGroup mg1, mg2;
// TODO: store these meMeshGroups serperately from entities to re-use them,
@ -577,12 +582,22 @@ int main(int argc, char* argv[])
LOG(ERROR) << "Error loading file, exiting\n";
return 1;
}
#endif
} else {
LOG(ERROR) << "Error initializing assimp\n";
return 1;
}
// testing scene_loader
if (slLoadFile(DEFAULT_SCENE_FILE, g_game_state->entities, g_game_state->entity_count, 1000)) {
// ???
} else {
LOG(ERROR) << "Error reading scene file, exiting\n";
return 1;
}
if (!createScene(g_game_state->hex_array, g_game_state->entities, g_game_state->entity_count)) {
LOG(ERROR) << "Error in vertex data, exiting\n";
return 1;

2
src/renderer.cpp

@ -71,6 +71,7 @@ struct camera
void initMatrices(projection_type p);
void openglDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity,
GLsizei length, const GLchar* message, const void* userParam);
// TODO: move hex logic to new file
bool initHexGridBuffers(std::vector<hex_info>* hexes);
void fillTriangleBufferFromHex(GLfloat buf[], int idx, const hex_info &hex);
void fillColorBuffer(GLfloat buf[], int len, std::vector<hex_info>* hexes);
@ -484,6 +485,7 @@ openglDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity,
<< ", message: " << message << "\n";
}
// TODO: move hex logic to new file
bool
initHexGridBuffers(std::vector<hex_info>* hexes)
{

52
src/scene_loader.cpp

@ -0,0 +1,52 @@
#include <glm/glm.hpp>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "aixlog.hpp"
#include "entity.h"
#include "mesh.h"
#include "util.h"
#include "scene_loader.h"
bool
slLoadFile(const char* file_name, Entity* entity_array, uint& entity_count, uint max_entities)
{
if (entity_array == nullptr) {
LOG(ERROR) << "entity array not initialized\n";
return false;
}
rapidjson::Document doc;
char* contents = utilDumpTextFile(file_name);
if (contents == nullptr) {
LOG(ERROR) << "Error reading file: " << file_name << "\n";
return false;
}
if (doc.Parse(contents).HasParseError() || !doc.IsObject()) {
LOG(ERROR) << "Error parrsing scene file: " << file_name << "\n";
return false;
}
// entities
if (doc.HasMember("entities")) {
const rapidjson::Value& e = doc["entities"];
assert (e.IsArray());
for (uint i = 0; i < e.Size(); i++) {
assert(e[i].HasMember("name"));
assert(e[i]["name"].IsString());
assert(i < max_entities);
std::string s = e[i]["name"].GetString();
LOG(INFO) << "e[" << i << "][\"name\"]: " << s << "\n";
}
}
return true;
}

7
src/scene_loader.h

@ -0,0 +1,7 @@
#include "util.h"
/*
* @param max_entities maximum size of array allocated
*/
bool slLoadFile(const char* file_name, Entity* entity_array, uint& entity_count, uint max_entities);
Loading…
Cancel
Save