diff --git a/src/hexgame.cpp b/src/hexgame.cpp index 237c357..6c9d470 100644 --- a/src/hexgame.cpp +++ b/src/hexgame.cpp @@ -116,9 +116,9 @@ mapMouseToViewport(int32 x, int32 y) void resetHexes() { - g_game_state->start_hex = g_game_state->current_hex = 0; + g_game_state->grid.start_hex = g_game_state->grid.current_hex = 0; - for (hex_info &hxi : *g_game_state->hex_array) + for (hex_info &hxi : *g_game_state->grid.hex_array) { hxi.selected = false; hxi.current_color = hxi.stored_color; @@ -131,9 +131,9 @@ getSingleHex(int32 x, int32 y) v2i dims = g_render_state->viewport_dims; 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)); + Hex h = hex_round(pixel_to_hex(g_game_state->grid.hex_layout, p)); - for (hex_info &hxi : *g_game_state->hex_array) + for (hex_info &hxi : *g_game_state->grid.hex_array) { if (hex_equal(h, hxi.hex)) return &hxi; @@ -147,8 +147,8 @@ setStartHex(hex_info *hex) { hex->selected = true; hex->current_color = g_render_state->selected_fill_color; - g_game_state->start_hex = g_game_state->current_hex = hex; - g_game_state->is_selecting = true; + g_game_state->grid.start_hex = g_game_state->grid.current_hex = hex; + g_game_state->grid.is_selecting = true; } void @@ -164,14 +164,14 @@ void updateHexFill(int32 x, int32 y) { hex_info *hxi = getSingleHex(x, y); - if (hxi && (hxi != g_game_state->current_hex) && g_game_state->start_hex) + if (hxi && (hxi != g_game_state->grid.current_hex) && g_game_state->grid.start_hex) { - g_game_state->current_hex = hxi; - int l = hex_distance(g_game_state->start_hex->hex, g_game_state->current_hex->hex); + g_game_state->grid.current_hex = hxi; + int l = hex_distance(g_game_state->grid.start_hex->hex, g_game_state->grid.current_hex->hex); - for (hex_info &h : *g_game_state->hex_array) + for (hex_info &h : *g_game_state->grid.hex_array) { - if (hex_distance(g_game_state->start_hex->hex, h.hex) <= l) + if (hex_distance(g_game_state->grid.start_hex->hex, h.hex) <= l) { h.selected = true; h.current_color = g_render_state->selected_fill_color; @@ -189,12 +189,12 @@ void updateHexLineDraw(int32 x, int32 y) { hex_info *hxi = getSingleHex(x, y); - if (hxi && (hxi != g_game_state->current_hex) && g_game_state->start_hex) + if (hxi && (hxi != g_game_state->grid.current_hex) && g_game_state->grid.start_hex) { - g_game_state->current_hex = hxi; - vector hexLine = hex_linedraw(g_game_state->start_hex->hex, hxi->hex); + g_game_state->grid.current_hex = hxi; + vector hexLine = hex_linedraw(g_game_state->grid.start_hex->hex, hxi->hex); - for (hex_info &h1 : *g_game_state->hex_array) + for (hex_info &h1 : *g_game_state->grid.hex_array) { for (uint i = 0; i < hexLine.size(); i++) { @@ -219,13 +219,13 @@ void updateHexConeFill(int32 x, int32 y) { hex_info *hxi = getSingleHex(x, y); - if (hxi && (hxi != g_game_state->current_hex) && g_game_state->start_hex) + if (hxi && (hxi != g_game_state->grid.current_hex) && g_game_state->grid.start_hex) { - g_game_state->current_hex = hxi; + g_game_state->grid.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); + Point p1(g_game_state->grid.start_hex->XPos, g_game_state->grid.start_hex->YPos); + Point p2(g_game_state->grid.current_hex->XPos, g_game_state->grid.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 @@ -244,7 +244,7 @@ updateHexConeFill(int32 x, int32 y) Point vert4 = Point(topX, topY); std::vector vertices = {p1, vert2, p2, vert4}; - for (hex_info &h : *g_game_state->hex_array) + for (hex_info &h : *g_game_state->grid.hex_array) { test_p.x = h.XPos; test_p.y = h.YPos; @@ -273,7 +273,7 @@ handleMouseDown(SDL_MouseButtonEvent &e) { v2i coords = mapMouseToViewport(e.x, e.y); - switch (g_game_state->draw_mode) + switch (g_game_state->grid.draw_mode) { case FILL: startDrawHelper(coords.x, coords.y); @@ -316,7 +316,7 @@ handleMouseMove(SDL_MouseMotionEvent &e) { v2i coords = mapMouseToViewport(e.x, e.y); - switch (g_game_state->draw_mode) + switch (g_game_state->grid.draw_mode) { case FILL: updateHexFill(coords.x, coords.y); @@ -345,18 +345,18 @@ handleMouseUp(SDL_MouseButtonEvent &e) { //v2i coords = mapMouseToViewport(e.x, e.y); - switch (g_game_state->draw_mode) + switch (g_game_state->grid.draw_mode) { case FILL: - g_game_state->is_selecting = false; + g_game_state->grid.is_selecting = false; break; case LINE: - g_game_state->is_selecting = false; + g_game_state->grid.is_selecting = false; break; case CONE_FILL: - g_game_state->is_selecting = false; + g_game_state->grid.is_selecting = false; break; case PATHFINDING: @@ -420,7 +420,7 @@ processSDLEvents() { if (e.button.button == SDL_BUTTON_LEFT) { - g->is_selecting = true; + g->grid.is_selecting = true; handleMouseDown(e.button); } else if (e.button.button == SDL_BUTTON_RIGHT) @@ -434,7 +434,7 @@ processSDLEvents() { if (e.button.button == SDL_BUTTON_LEFT) { - g->is_selecting = false; + g->grid.is_selecting = false; handleMouseUp(e.button); } else if (e.button.button == SDL_BUTTON_RIGHT) @@ -444,7 +444,7 @@ processSDLEvents() } break; case SDL_MOUSEMOTION: - if (!gooey_wants && g->is_selecting) + if (!gooey_wants && g->grid.is_selecting) handleMouseMove(e.motion); else if(!gooey_wants && g->is_camera_rotate) rotateCamera(e.motion.xrel, e.motion.yrel); @@ -472,8 +472,8 @@ cleanUp(SDL_Handles &handles) game_state* g = g_game_state; - if (g_game_state->hex_array) - delete g_game_state->hex_array; + if (g_game_state->grid.hex_array) + delete g_game_state->grid.hex_array; for (uint i = 0; i < g->entity_count; i++) { meFreeMeshGroup(g->entities[i].mesh_group); @@ -481,9 +481,9 @@ cleanUp(SDL_Handles &handles) } utilSafeFree(g->entities); + utilSafeFree(g_render_state); + utilSafeFree(g_game_state); - delete g_render_state; - delete g_game_state; return true; } @@ -503,25 +503,20 @@ int main(int argc, char* argv[]) // init global game state Layout layout(HEX_ORIENTATION, Point(HEX_SIZE, HEX_SIZE), Point(VIEWPORT_WIDTH / 2, VIEWPORT_HEIGHT / 2)); - g_game_state = new game_state(layout); - g_game_state->hex_array = new vector; - g_game_state->is_selecting = false; - g_game_state->start_hex = 0x0; - g_game_state->current_hex = 0x0; - g_game_state->draw_mode = CONE_FILL; - // TODO: better entity memory management - // TODO: hard coded limit of 1000 entities here + g_game_state = UTIL_ALLOC(1, game_state); + g_game_state->grid.hex_layout = layout; + g_game_state->grid.hex_array = new vector; g_game_state->entities = UTIL_ALLOC(MAX_ENTITIES, Entity); // init global render state - g_render_state = new render_state(); + g_render_state = UTIL_ALLOC(1, render_state); g_render_state->is_debug_draw = DEBUG_DRAW; 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->hex_array, g_game_state->hex_layout, g_render_state->fill_color); + createHexes(g_game_state->grid.hex_array, g_game_state->grid.hex_layout, g_render_state->fill_color); SDL_Handles handles; if (SDL_Init(SDL_INIT_VIDEO) != 0) { @@ -567,13 +562,14 @@ int main(int argc, char* argv[]) slParseEntities(sd, g_game_state->entities, g_game_state->entity_count, MAX_ENTITIES, DATA_DIR); slParseCamera(sd, renGetCamera()); + slParseHexGrid(sd, g_game_state->grid); slFreeSceneDoc(sd); } else { LOG(ERROR) << "Error loading scene, exiting\n"; return 1; } - if (!createScene(g_game_state->hex_array, g_game_state->entities, g_game_state->entity_count)) { + if (!createScene(g_game_state->grid.hex_array, g_game_state->entities, g_game_state->entity_count)) { LOG(ERROR) << "Error in vertex data, exiting\n"; return 1; } @@ -605,13 +601,13 @@ int main(int argc, char* argv[]) 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); + renderFrame(g->grid.hex_array, g->entities, g->entity_count); - if (r->is_debug_draw && g->draw_mode == CONE_FILL) + if (r->is_debug_draw && g->grid.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()); + renderGooey(handles, g->grid.draw_mode, r->is_debug_draw, g->grid.start_hex, + g->grid.current_hex, g->grid.is_selecting, getCameraPosition()); SDL_GL_SwapWindow(handles.window); diff --git a/src/hexgame.h b/src/hexgame.h index c5cc93d..4fa6c04 100644 --- a/src/hexgame.h +++ b/src/hexgame.h @@ -1,31 +1,22 @@ #pragma once -#include - #include #include "entity.h" #include "hexgrid.h" -#include "hexlib.h" #include "util.h" struct game_state { - std::vector *hex_array; - hex_info *start_hex; - hex_info *current_hex; - vector selected_hexes; - HexDrawMode draw_mode; - const Layout hex_layout; + hexgrid grid; // TODO: WiP uint32 entity_count = 0; Entity* entities = nullptr; // camera movement controls - bool is_selecting = false; bool is_camera_rotate = false; bool is_moveforward = false; bool is_movebackward = false; @@ -35,8 +26,5 @@ struct game_state bool is_moveright = false; bool is_rotateCW = false; bool is_rotateCCW = false; - - // TODO: initialize this in hexgrid.h to remove hexlib.h dep? - game_state(Layout &l) : hex_layout(l) {} }; diff --git a/src/hexgrid.cpp b/src/hexgrid.cpp index 23ec008..42573ce 100644 --- a/src/hexgrid.cpp +++ b/src/hexgrid.cpp @@ -2,6 +2,8 @@ #include "hexgrid.h" +// interface + void hgCreateHexes(vector *hxi_array, const Layout &layout, uint32 color) { @@ -19,3 +21,12 @@ hgResetHexes() { } + + +// internal + +void +createHexagonGrid(vector *hxi_array, const Layout &layout, uint32 color, uint radius) +{ + +} diff --git a/src/hexgrid.h b/src/hexgrid.h index c3cc2d4..ba412c1 100644 --- a/src/hexgrid.h +++ b/src/hexgrid.h @@ -31,6 +31,17 @@ struct hex_info struct hexgrid { Layout hex_layout; + uint hex_size = 10; + uint hex_radius = 0; + uint32 fill_color = 0x565656FF; + uint32 selected_color = 0xF46000FF; + + std::vector *hex_array; + hex_info* start_hex = nullptr; + hex_info* current_hex = nullptr; + vector selected_hexes; + HexDrawMode draw_mode = CONE_FILL; + bool is_selecting = false; }; diff --git a/src/hexlib.h b/src/hexlib.h index 10d073b..0c977aa 100644 --- a/src/hexlib.h +++ b/src/hexlib.h @@ -47,7 +47,7 @@ struct Orientation double start_angle; Orientation(double f0_, double f1_, double f2_, double f3_, double b0_, double b1_, double b2_, double b3_, double start_angle_): - f0(f0_), f1(f1_), f2(f2_), f3(f3_), b0(b0_), b1(b1_), b2(b2_), + f0(f0_), f1(f1_), f2(f2_), f3(f3_), b0(b0_), b1(b1_), b2(b2_), b3(b3_), start_angle(start_angle_) {} }; diff --git a/src/scene_loader.cpp b/src/scene_loader.cpp index cb52c07..9bc42f6 100644 --- a/src/scene_loader.cpp +++ b/src/scene_loader.cpp @@ -107,6 +107,18 @@ slParseCamera(slSceneDoc* sd, camera& cam) ); } +void +slParseHexGrid(slSceneDoc* sd, hexgrid& hg) +{ + const rapidjson::Value& json_grid = (*sd->doc)["hex_grid"]; + std::string layout_type = json_grid["layout_type"].GetString(); + + if (layout_type == "hexagon") { + hg.hex_size = json_grid["hex_size"].GetInt(); + hg.hex_radius = json_grid["hex_radius"].GetInt(); + } +} + // internal diff --git a/src/scene_loader.h b/src/scene_loader.h index b0cfa7f..dad960a 100644 --- a/src/scene_loader.h +++ b/src/scene_loader.h @@ -1,5 +1,6 @@ #include "camera.h" +#include "hexgrid.h" #include "util.h" @@ -17,4 +18,4 @@ bool slParseEntities( ); void slParseCamera(slSceneDoc* sd, camera& cam); -//bool slParseHexGrid(slSceneDoc* sd, std::vector& hexes); +void slParseHexGrid(slSceneDoc* sd, hexgrid& hg);