Browse Source

load hexgrid from scene file

master
cinnaboot 8 years ago
parent
commit
8a81d5541b
  1. 23
      data/scene_schema.json
  2. 22
      data/test_scene.json
  3. 53
      src/hexgame.cpp
  4. 39
      src/hexgrid.cpp
  5. 21
      src/hexgrid.h
  6. 32
      src/scene_loader.cpp
  7. 4
      src/scene_loader.h

23
data/scene_schema.json

@ -49,10 +49,23 @@
"hex_grid" : { "hex_grid" : {
"type": "object", "type": "object",
"properties": { "properties": {
"layout_type" : { "type": "string" }, "position": { "$ref": "#/definitions/vector3" },
"fill_color" : { "type": "string" },
"selected_fill_color" : { "type": "string" },
"hexlib_orientation" : { "type": "string" },
"hex_size" : { "type": "number" }, "hex_size" : { "type": "number" },
"grid_type" : { "type": "string" },
"hex_radius" : { "type": "number" } "hex_radius" : { "type": "number" }
} },
"required": [
"position",
"fill_color",
"selected_fill_color",
"hexlib_orientation",
"hex_size",
"grid_type",
"hex_radius"
]
}, },
"entities": { "entities": {
@ -63,7 +76,8 @@
"position": { "$ref": "#/definitions/vector3" }, "position": { "$ref": "#/definitions/vector3" },
"rotation": { "$ref": "#/definitions/vector4" }, "rotation": { "$ref": "#/definitions/vector4" },
"scale": { "$ref": "#/definitions/vector3" } "scale": { "$ref": "#/definitions/vector3" }
} },
"required" : ["name", "model_file", "position", "rotation", "scale"]
}, },
"lights": { "lights": {
@ -72,7 +86,8 @@
"intensity": { "type": "number" }, "intensity": { "type": "number" },
"color": { "type": "#/definitions/rgb" }, "color": { "type": "#/definitions/rgb" },
"position": { "$ref": "#/definitions/vector3" } "position": { "$ref": "#/definitions/vector3" }
} },
"required" : ["intensity", "color", "position"]
} }
} }
} }

22
data/test_scene.json

@ -1,8 +1,8 @@
{ {
"camera" : { "camera" : {
"position" : { "position" : {
"x" : 640, "x" : 0,
"y" : 0, "y" : -800,
"z" : 100 "z" : 100
}, },
"target" : { "target" : {
@ -18,8 +18,16 @@
}, },
"hex_grid" : { "hex_grid" : {
"layout_type" : "hexagon", "position" : {
"x" : 0,
"y" : 0,
"z" : 0
},
"hex_size" : 10, "hex_size" : 10,
"fill_color" : "0x565656FF",
"selected_fill_color": "0xF46000FF",
"hexlib_orientation" : "layout_flat",
"grid_type" : "hexagon",
"hex_radius" : 20 "hex_radius" : 20
}, },
@ -28,8 +36,8 @@
"name" : "catepillar 1", "name" : "catepillar 1",
"model_file" : "catepillar.dae", "model_file" : "catepillar.dae",
"position" : { "position" : {
"x" : 740, "x" : -140,
"y" : 500, "y" : 50,
"z" : 0 "z" : 0
}, },
"rotation" : { "rotation" : {
@ -48,8 +56,8 @@
"name" : "catepillar 2", "name" : "catepillar 2",
"model_file" : "catepillar.dae", "model_file" : "catepillar.dae",
"position" : { "position" : {
"x" : 600, "x" : 140,
"y" : 400, "y" : 0,
"z" : 0 "z" : 0
}, },
"rotation" : { "rotation" : {

53
src/hexgame.cpp

@ -1,11 +1,8 @@
/******************************************************************************* /*******************************************************************************
* TODO: * TODO:
* - add a color buffer to hex_line and debug render groups to re-use * - add 3d orientation widget to gooey
* fragment shaders and simplify draw rgDraw()
* - need to add normal buffer too
* - lighting * - lighting
* - add light struct * - test adding more lights
* - pass all lights to render groups/shaders every frame
* - map generation * - map generation
* - pathfinding * - pathfinding
* - assimp animation * - assimp animation
@ -20,17 +17,20 @@
* - actually don't need to save the vertex/normal buffers after passing * - actually don't need to save the vertex/normal buffers after passing
* to opengl * to opengl
* - use a storage pool for assimp meshes allowing reuse across entities * - use a storage pool for assimp meshes allowing reuse across entities
* - replace calls to malloc/free with safe(r) function in util.h * - replace remaining calls to malloc/free with safe(r) function in util.h
* - add cpu perforcmace counters in render loop * - add cpu perforcmace counters in render loop
* - check for memory leaks w/ valgrind * - check for memory leaks w/ valgrind
******************************************************************************/ ******************************************************************************/
// Some defaults for the game layout // Some defaults for the game layout
#if 0
#define HEX_SIZE 10 #define HEX_SIZE 10
#define HEX_RADIUS 19 #define HEX_RADIUS 19
#define HEX_ORIENTATION layout_flat #define HEX_ORIENTATION layout_flat
#define FILL_COLOR 0x565656FF #define FILL_COLOR 0x565656FF
#define SELECTED_FILL_COLOR 0xF46000FF #define SELECTED_FILL_COLOR 0xF46000FF
#endif
#define DEBUG_DRAW true #define DEBUG_DRAW true
#define VIEWPORT_WIDTH 1920 #define VIEWPORT_WIDTH 1920
#define VIEWPORT_HEIGHT 1080 #define VIEWPORT_HEIGHT 1080
@ -77,32 +77,6 @@ static game_state* g_game_state;
static render_state* g_render_state; static render_state* g_render_state;
static vector<Point> g_polygon_select_vertices = {Point(), Point(), Point(), Point()}; static vector<Point> g_polygon_select_vertices = {Point(), Point(), Point(), Point()};
void
createHexes(vector<hex_info> *hxi_array, const Layout &layout, uint32 color)
{
// create a hexagonal grid of hexagons
int map_radius = HEX_RADIUS;
for (int q = -map_radius; q <= map_radius; q++)
{
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;
hxi.hexID = (int32) hxi_array->size();
hxi.hex.q = q; hxi.hex.r = r; hxi.hex.s = -q-r;
Point p = hex_to_pixel(layout, hxi.hex);
hxi.XPos = p.x;
hxi.YPos = p.y;
hxi.current_color = color;
hxi.stored_color = color;
hxi.vertices = polygon_corners(layout, hxi.hex);
hxi.vertices.shrink_to_fit();
hxi_array->push_back(hxi);
}
}
}
v2i v2i
mapMouseToViewport(int32 x, int32 y) mapMouseToViewport(int32 x, int32 y)
@ -131,7 +105,7 @@ getSingleHex(int32 x, int32 y)
v2i dims = g_render_state->viewport_dims; v2i dims = g_render_state->viewport_dims;
v2f v = getUnprojectedCoords(x, y, dims.x, dims.y); v2f v = getUnprojectedCoords(x, y, dims.x, dims.y);
Point p(v.x, v.y); Point p(v.x, v.y);
Hex h = hex_round(pixel_to_hex(g_game_state->grid.hex_layout, p)); Hex h = hex_round(pixel_to_hex(g_game_state->grid.hexlib_layout, p));
for (hex_info &hxi : *g_game_state->grid.hex_array) for (hex_info &hxi : *g_game_state->grid.hex_array)
{ {
@ -502,9 +476,7 @@ int main(int argc, char* argv[])
LOG(INFO) << "Application started\n"; LOG(INFO) << "Application started\n";
// init global game state // init global game state
Layout layout(HEX_ORIENTATION, Point(HEX_SIZE, HEX_SIZE), Point(VIEWPORT_WIDTH / 2, VIEWPORT_HEIGHT / 2));
g_game_state = UTIL_ALLOC(1, game_state); g_game_state = UTIL_ALLOC(1, game_state);
g_game_state->grid.hex_layout = layout;
g_game_state->grid.hex_array = new vector<hex_info>; g_game_state->grid.hex_array = new vector<hex_info>;
g_game_state->entities = UTIL_ALLOC(MAX_ENTITIES, Entity); g_game_state->entities = UTIL_ALLOC(MAX_ENTITIES, Entity);
@ -512,11 +484,7 @@ int main(int argc, char* argv[])
g_render_state = UTIL_ALLOC(1, render_state); g_render_state = UTIL_ALLOC(1, render_state);
g_render_state->is_debug_draw = DEBUG_DRAW; g_render_state->is_debug_draw = DEBUG_DRAW;
g_render_state->viewport_dims = v2i(VIEWPORT_WIDTH, VIEWPORT_HEIGHT); g_render_state->viewport_dims = v2i(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
g_render_state->fill_color = FILL_COLOR;
g_render_state->selected_fill_color = SELECTED_FILL_COLOR;
createHexes(g_game_state->grid.hex_array, g_game_state->grid.hex_layout, g_render_state->fill_color);
SDL_Handles handles; SDL_Handles handles;
if (SDL_Init(SDL_INIT_VIDEO) != 0) { if (SDL_Init(SDL_INIT_VIDEO) != 0) {
@ -559,10 +527,11 @@ int main(int argc, char* argv[])
slSceneDoc* sd = slLoadFile(DATA_DIR, DEFAULT_SCENE_FILE, SCENE_SCHEMA_FILE); slSceneDoc* sd = slLoadFile(DATA_DIR, DEFAULT_SCENE_FILE, SCENE_SCHEMA_FILE);
if (sd != nullptr) { if (sd != nullptr) {
slParseEntities(sd, g_game_state->entities, g_game_state->entity_count, MAX_ENTITIES, game_state* g = g_game_state;
DATA_DIR); render_state* r = g_render_state;
slParseEntities(sd, g->entities, g->entity_count, MAX_ENTITIES, DATA_DIR);
slParseCamera(sd, renGetCamera()); slParseCamera(sd, renGetCamera());
slParseHexGrid(sd, g_game_state->grid); slParseHexGrid(sd, g->grid, r->fill_color, r->selected_fill_color);
slFreeSceneDoc(sd); slFreeSceneDoc(sd);
} else { } else {
LOG(ERROR) << "Error loading scene, exiting\n"; LOG(ERROR) << "Error loading scene, exiting\n";

39
src/hexgrid.cpp

@ -1,13 +1,19 @@
#include "aixlog.hpp"
#include "hexgrid.h" #include "hexgrid.h"
// forward declarations
void createHexagonGrid(hexgrid hg);
// interface // interface
void void
hgCreateHexes(vector<hex_info> *hxi_array, const Layout &layout, uint32 color) hgCreateHexes(hexgrid hg)
{ {
if (hg.gridT == HEXAGON)
createHexagonGrid(hg);
} }
hex_info* hex_info*
@ -26,7 +32,34 @@ hgResetHexes()
// internal // internal
void void
createHexagonGrid(vector<hex_info> *hxi_array, const Layout &layout, uint32 color, uint radius) createHexagonGrid(hexgrid hg)
{ {
if (hg.hex_array == nullptr) {
LOG(ERROR) << "hg.hex_array is not initialized\n";
return;
}
int hr = hg.hex_radius;
int nhr = hg.hex_radius * -1;
for (int q = nhr; q <= hr; q++)
{
int r1 = std::max(nhr, -q - hr);
int r2 = std::min(hr, -q + hr);
for (int r = r1; r <= r2; r++)
{
hex_info hxi;
hxi.hexID = (int32) hg.hex_array->size();
hxi.hex.q = q; hxi.hex.r = r; hxi.hex.s = -q-r;
Point p = hex_to_pixel(hg.hexlib_layout, hxi.hex);
hxi.XPos = p.x;
hxi.YPos = p.y;
hxi.current_color = hg.fill_color;
hxi.stored_color = hg.fill_color;
hxi.vertices = polygon_corners(hg.hexlib_layout, hxi.hex);
hxi.vertices.shrink_to_fit();
hg.hex_array->push_back(hxi);
}
}
} }

21
src/hexgrid.h

@ -7,6 +7,7 @@
#include "util.h" #include "util.h"
// TODO: rename to "hex_draw_mode" to fit project style
enum HexDrawMode enum HexDrawMode
{ {
NONE, NONE,
@ -16,6 +17,15 @@ enum HexDrawMode
PATHFINDING PATHFINDING
}; };
// TODO: implement more hexgrid types
enum grid_type
{
HEXAGON,
PARALLELOGRAM,
RHOMBUS,
HASH_MAP
};
struct hex_info struct hex_info
{ {
int32 hexID = 0; int32 hexID = 0;
@ -30,13 +40,16 @@ struct hex_info
struct hexgrid struct hexgrid
{ {
Layout hex_layout; grid_type gridT = HEXAGON;
Layout hexlib_layout;
Orientation hexlib_orientation;
v3f position = v3f(0, 0, 0);
uint hex_size = 10; uint hex_size = 10;
uint hex_radius = 0; uint hex_radius = 0;
uint32 fill_color = 0x565656FF; uint32 fill_color = 0x565656FF;
uint32 selected_color = 0xF46000FF; uint32 selected_fill_color = 0xF46000FF;
std::vector<hex_info> *hex_array; std::vector<hex_info>* hex_array = nullptr;
hex_info* start_hex = nullptr; hex_info* start_hex = nullptr;
hex_info* current_hex = nullptr; hex_info* current_hex = nullptr;
vector<hex_info> selected_hexes; vector<hex_info> selected_hexes;
@ -45,6 +58,6 @@ struct hexgrid
}; };
void hgCreateHexes(vector<hex_info> *hxi_array, const Layout &layout, uint32 color); void hgCreateHexes(hexgrid hg);
hex_info* hgGetSingleHex(int32 x, int32 y); hex_info* hgGetSingleHex(int32 x, int32 y);
void hgResetHexes(); void hgResetHexes();

32
src/scene_loader.cpp

@ -9,6 +9,7 @@
#include "entity.h" #include "entity.h"
#include "hexgrid.h" #include "hexgrid.h"
#include "hexlib.h"
#include "mesh.h" #include "mesh.h"
#include "util.h" #include "util.h"
#include "scene_loader.h" #include "scene_loader.h"
@ -24,6 +25,7 @@ struct slSceneDoc
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);
uint32 parseHex32(std::string s);
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);
@ -108,15 +110,39 @@ slParseCamera(slSceneDoc* sd, camera& cam)
} }
void void
slParseHexGrid(slSceneDoc* sd, hexgrid& hg) slParseHexGrid(slSceneDoc* sd, hexgrid& hg, uint32& fill_color, uint32&selected_fill_color)
{ {
const rapidjson::Value& json_grid = (*sd->doc)["hex_grid"]; const rapidjson::Value& json_grid = (*sd->doc)["hex_grid"];
std::string layout_type = json_grid["layout_type"].GetString();
if (layout_type == "hexagon") { std::string grid_type_str = json_grid["grid_type"].GetString();
if (grid_type_str == "hexagon")
hg.gridT = HEXAGON;
else if (grid_type_str == "parallelogram")
hg.gridT = PARALLELOGRAM;
else if (grid_type_str == "rhombus")
hg.gridT = RHOMBUS;
else if (grid_type_str == "hash_map")
hg.gridT = HASH_MAP;
hg.hex_size = json_grid["hex_size"].GetInt(); hg.hex_size = json_grid["hex_size"].GetInt();
glm::vec3 pos = parseVec3(json_grid["position"]);
hg.position.x = pos.x;
hg.position.y = pos.y;
hg.position.z = pos.z;
std::string fill_color_str = json_grid["fill_color"].GetString();
hg.fill_color = std::stoul(fill_color_str, nullptr, 16);
std::string selected_fill_color_str = json_grid["selected_fill_color"].GetString();
hg.selected_fill_color = std::stoul(selected_fill_color_str, nullptr, 16);
std::string orientation_str = json_grid["hexlib_orientation"].GetString();
hg.hexlib_orientation = (orientation_str == "layout_flat") ? layout_flat : layout_pointy;
Layout lo(hg.hexlib_orientation, Point(hg.hex_size, hg.hex_size), Point(hg.position.x, hg.position.y));
hg.hexlib_layout = lo;
if (hg.gridT == HEXAGON) {
hg.hex_radius = json_grid["hex_radius"].GetInt(); hg.hex_radius = json_grid["hex_radius"].GetInt();
} }
hgCreateHexes(hg);
} }

4
src/scene_loader.h

@ -18,4 +18,6 @@ bool slParseEntities(
); );
void slParseCamera(slSceneDoc* sd, camera& cam); void slParseCamera(slSceneDoc* sd, camera& cam);
void slParseHexGrid(slSceneDoc* sd, hexgrid& hg);
// TODO: remove fill_color,selected_fill_color refs here, and use hex_grid object in renderer
void slParseHexGrid(slSceneDoc* sd, hexgrid& hg, uint32& fill_color, uint32&selected_fill_color);

Loading…
Cancel
Save