diff --git a/data/test_scene.json b/data/test_scene.json new file mode 100644 index 0000000..555f9e5 --- /dev/null +++ b/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 + } + } + ] +} diff --git a/src/hexgame.cpp b/src/hexgame.cpp index 04c5889..b505d0a 100644 --- a/src/hexgame.cpp +++ b/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 @@ -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; @@ -79,7 +83,7 @@ createHexes(vector *hxi_array, const Layout &layout, uint32 color) { 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; @@ -110,7 +114,7 @@ void resetHexes() { g_game_state->start_hex = g_game_state->current_hex = 0; - + for (hex_info &hxi : *g_game_state->hex_array) { hxi.selected = false; @@ -125,13 +129,13 @@ getSingleHex(int32 x, int32 y) 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->hex_layout, p)); - + for (hex_info &hxi : *g_game_state->hex_array) { if (hex_equal(h, hxi.hex)) return &hxi; } - + return 0; } @@ -161,7 +165,7 @@ updateHexFill(int32 x, int32 y) { g_game_state->current_hex = hxi; int l = hex_distance(g_game_state->start_hex->hex, g_game_state->current_hex->hex); - + for (hex_info &h : *g_game_state->hex_array) { if (hex_distance(g_game_state->start_hex->hex, h.hex) <= l) @@ -186,11 +190,11 @@ updateHexLineDraw(int32 x, int32 y) { g_game_state->current_hex = hxi; vector hexLine = hex_linedraw(g_game_state->start_hex->hex, hxi->hex); - + for (hex_info &h1 : *g_game_state->hex_array) { for (uint i = 0; i < hexLine.size(); i++) - { + { Hex h2 = hexLine[i]; if (hex_equal(h1.hex, h2)) { @@ -215,14 +219,14 @@ updateHexConeFill(int32 x, int32 y) if (hxi && (hxi != g_game_state->current_hex) && g_game_state->start_hex) { g_game_state->current_hex = hxi; - + // TODO: Remove debug code here and from gooey.h Point p1(g_game_state->start_hex->XPos, g_game_state->start_hex->YPos); Point p2(g_game_state->current_hex->XPos, g_game_state->current_hex->YPos); real64 angle = std::atan2(p2.y - p1.y, p2.x - p1.x); real64 len = std::hypot(p2.y - p1.y, p2.x - p1.x); real64 coneAngle = CONE_ANGLE * M_PI / 180; // M_PI is non-standard and may not be portable - + real64 x1 = len * std::cos(angle); real64 y1 = len * std::sin(angle); // top of cone @@ -271,18 +275,18 @@ handleMouseDown(SDL_MouseButtonEvent &e) case FILL: startDrawHelper(coords.x, coords.y); break; - + case LINE: startDrawHelper(coords.x, coords.y); break; - + case CONE_FILL: startDrawHelper(coords.x, coords.y); break; - + case PATHFINDING: break; - + case NONE: // fall through default: @@ -298,7 +302,7 @@ handleMouseDown(SDL_MouseButtonEvent &e) else { hex->current_color = hex->stored_color; - } + } } }break; } @@ -308,24 +312,24 @@ void handleMouseMove(SDL_MouseMotionEvent &e) { v2i coords = mapMouseToViewport(e.x, e.y); - + switch (g_game_state->draw_mode) { case FILL: updateHexFill(coords.x, coords.y); break; - + case LINE: updateHexLineDraw(coords.x, coords.y); break; - + case CONE_FILL: updateHexConeFill(coords.x, coords.y); break; - + case PATHFINDING: break; - + case NONE: // fall through default: @@ -337,24 +341,24 @@ void handleMouseUp(SDL_MouseButtonEvent &e) { //v2i coords = mapMouseToViewport(e.x, e.y); - + switch (g_game_state->draw_mode) { case FILL: g_game_state->is_selecting = false; break; - + case LINE: g_game_state->is_selecting = false; break; - + case CONE_FILL: g_game_state->is_selecting = false; break; - + case PATHFINDING: break; - + case NONE: // fall through default: @@ -446,7 +450,7 @@ processSDLEvents() break; } } - + return run; } @@ -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 @@ -513,7 +517,7 @@ int main(int argc, char* argv[]) createHexes(g_game_state->hex_array, g_game_state->hex_layout, g_render_state->fill_color); SDL_Handles handles; - + if (SDL_Init(SDL_INIT_VIDEO) != 0) { LOG(ERROR) << "Error, SDL_Init: " << SDL_GetError() << "\n"; return 1; @@ -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; @@ -611,18 +626,18 @@ int main(int argc, char* argv[]) #endif game_state* g = g_game_state; render_state* r = g_render_state; - + moveCamera(g->is_moveup, g->is_moveleft, g->is_movedown, g->is_moveright, g->is_moveforward, g->is_movebackward); - + renderFrame(g->hex_array, g->entities, g->entity_count); - + if (r->is_debug_draw && g->draw_mode == CONE_FILL) renderDebug(g_polygon_select_vertices); - + renderGooey(handles, g->draw_mode, r->is_debug_draw, g->start_hex, g->current_hex, g->is_selecting, getCameraPosition()); - + SDL_GL_SwapWindow(handles.window); platform_wait_for_vblank(VSYNC_ENABLED); diff --git a/src/renderer.cpp b/src/renderer.cpp index a69ac15..42d73d9 100644 --- a/src/renderer.cpp +++ b/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* hexes); void fillTriangleBufferFromHex(GLfloat buf[], int idx, const hex_info &hex); void fillColorBuffer(GLfloat buf[], int len, std::vector* 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* hexes) { diff --git a/src/scene_loader.cpp b/src/scene_loader.cpp new file mode 100644 index 0000000..6233d2b --- /dev/null +++ b/src/scene_loader.cpp @@ -0,0 +1,52 @@ + +#include +#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; +} diff --git a/src/scene_loader.h b/src/scene_loader.h new file mode 100644 index 0000000..d054bc0 --- /dev/null +++ b/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);