Browse Source

update hexgame to hexgrid object mapping

master
cinnaboot 8 years ago
parent
commit
8d7f9df199
  1. 92
      src/hexgame.cpp
  2. 14
      src/hexgame.h
  3. 11
      src/hexgrid.cpp
  4. 11
      src/hexgrid.h
  5. 12
      src/scene_loader.cpp
  6. 3
      src/scene_loader.h

92
src/hexgame.cpp

@ -116,9 +116,9 @@ mapMouseToViewport(int32 x, int32 y)
void void
resetHexes() 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.selected = false;
hxi.current_color = hxi.stored_color; hxi.current_color = hxi.stored_color;
@ -131,9 +131,9 @@ 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->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)) if (hex_equal(h, hxi.hex))
return &hxi; return &hxi;
@ -147,8 +147,8 @@ setStartHex(hex_info *hex)
{ {
hex->selected = true; hex->selected = true;
hex->current_color = g_render_state->selected_fill_color; hex->current_color = g_render_state->selected_fill_color;
g_game_state->start_hex = g_game_state->current_hex = hex; g_game_state->grid.start_hex = g_game_state->grid.current_hex = hex;
g_game_state->is_selecting = true; g_game_state->grid.is_selecting = true;
} }
void void
@ -164,14 +164,14 @@ void
updateHexFill(int32 x, int32 y) updateHexFill(int32 x, int32 y)
{ {
hex_info *hxi = getSingleHex(x, 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;
int l = hex_distance(g_game_state->start_hex->hex, g_game_state->current_hex->hex); 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.selected = true;
h.current_color = g_render_state->selected_fill_color; h.current_color = g_render_state->selected_fill_color;
@ -189,12 +189,12 @@ void
updateHexLineDraw(int32 x, int32 y) updateHexLineDraw(int32 x, int32 y)
{ {
hex_info *hxi = getSingleHex(x, 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;
vector<Hex> hexLine = hex_linedraw(g_game_state->start_hex->hex, hxi->hex); vector<Hex> 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++) for (uint i = 0; i < hexLine.size(); i++)
{ {
@ -219,13 +219,13 @@ void
updateHexConeFill(int32 x, int32 y) updateHexConeFill(int32 x, int32 y)
{ {
hex_info *hxi = getSingleHex(x, 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 // TODO: Remove debug code here and from gooey.h
Point p1(g_game_state->start_hex->XPos, g_game_state->start_hex->YPos); Point p1(g_game_state->grid.start_hex->XPos, g_game_state->grid.start_hex->YPos);
Point p2(g_game_state->current_hex->XPos, g_game_state->current_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 angle = std::atan2(p2.y - p1.y, p2.x - p1.x);
real64 len = std::hypot(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 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); Point vert4 = Point(topX, topY);
std::vector<Point> vertices = {p1, vert2, p2, vert4}; std::vector<Point> 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.x = h.XPos;
test_p.y = h.YPos; test_p.y = h.YPos;
@ -273,7 +273,7 @@ handleMouseDown(SDL_MouseButtonEvent &e)
{ {
v2i coords = mapMouseToViewport(e.x, e.y); v2i coords = mapMouseToViewport(e.x, e.y);
switch (g_game_state->draw_mode) switch (g_game_state->grid.draw_mode)
{ {
case FILL: case FILL:
startDrawHelper(coords.x, coords.y); startDrawHelper(coords.x, coords.y);
@ -316,7 +316,7 @@ handleMouseMove(SDL_MouseMotionEvent &e)
{ {
v2i coords = mapMouseToViewport(e.x, e.y); v2i coords = mapMouseToViewport(e.x, e.y);
switch (g_game_state->draw_mode) switch (g_game_state->grid.draw_mode)
{ {
case FILL: case FILL:
updateHexFill(coords.x, coords.y); updateHexFill(coords.x, coords.y);
@ -345,18 +345,18 @@ handleMouseUp(SDL_MouseButtonEvent &e)
{ {
//v2i coords = mapMouseToViewport(e.x, e.y); //v2i coords = mapMouseToViewport(e.x, e.y);
switch (g_game_state->draw_mode) switch (g_game_state->grid.draw_mode)
{ {
case FILL: case FILL:
g_game_state->is_selecting = false; g_game_state->grid.is_selecting = false;
break; break;
case LINE: case LINE:
g_game_state->is_selecting = false; g_game_state->grid.is_selecting = false;
break; break;
case CONE_FILL: case CONE_FILL:
g_game_state->is_selecting = false; g_game_state->grid.is_selecting = false;
break; break;
case PATHFINDING: case PATHFINDING:
@ -420,7 +420,7 @@ processSDLEvents()
{ {
if (e.button.button == SDL_BUTTON_LEFT) if (e.button.button == SDL_BUTTON_LEFT)
{ {
g->is_selecting = true; g->grid.is_selecting = true;
handleMouseDown(e.button); handleMouseDown(e.button);
} }
else if (e.button.button == SDL_BUTTON_RIGHT) else if (e.button.button == SDL_BUTTON_RIGHT)
@ -434,7 +434,7 @@ processSDLEvents()
{ {
if (e.button.button == SDL_BUTTON_LEFT) if (e.button.button == SDL_BUTTON_LEFT)
{ {
g->is_selecting = false; g->grid.is_selecting = false;
handleMouseUp(e.button); handleMouseUp(e.button);
} }
else if (e.button.button == SDL_BUTTON_RIGHT) else if (e.button.button == SDL_BUTTON_RIGHT)
@ -444,7 +444,7 @@ processSDLEvents()
} }
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
if (!gooey_wants && g->is_selecting) if (!gooey_wants && g->grid.is_selecting)
handleMouseMove(e.motion); handleMouseMove(e.motion);
else if(!gooey_wants && g->is_camera_rotate) else if(!gooey_wants && g->is_camera_rotate)
rotateCamera(e.motion.xrel, e.motion.yrel); rotateCamera(e.motion.xrel, e.motion.yrel);
@ -472,8 +472,8 @@ cleanUp(SDL_Handles &handles)
game_state* g = g_game_state; game_state* g = g_game_state;
if (g_game_state->hex_array) if (g_game_state->grid.hex_array)
delete g_game_state->hex_array; delete g_game_state->grid.hex_array;
for (uint i = 0; i < g->entity_count; i++) { for (uint i = 0; i < g->entity_count; i++) {
meFreeMeshGroup(g->entities[i].mesh_group); meFreeMeshGroup(g->entities[i].mesh_group);
@ -481,9 +481,9 @@ cleanUp(SDL_Handles &handles)
} }
utilSafeFree(g->entities); utilSafeFree(g->entities);
utilSafeFree(g_render_state);
utilSafeFree(g_game_state);
delete g_render_state;
delete g_game_state;
return true; return true;
} }
@ -503,25 +503,20 @@ int main(int argc, char* argv[])
// init global game state // init global game state
Layout layout(HEX_ORIENTATION, Point(HEX_SIZE, HEX_SIZE), Point(VIEWPORT_WIDTH / 2, VIEWPORT_HEIGHT / 2)); 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 = UTIL_ALLOC(1, game_state);
g_game_state->hex_array = new vector<hex_info>; g_game_state->grid.hex_layout = layout;
g_game_state->is_selecting = false; g_game_state->grid.hex_array = new vector<hex_info>;
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->entities = UTIL_ALLOC(MAX_ENTITIES, 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 = 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->fill_color = FILL_COLOR;
g_render_state->selected_fill_color = SELECTED_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; SDL_Handles handles;
if (SDL_Init(SDL_INIT_VIDEO) != 0) { 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, slParseEntities(sd, g_game_state->entities, g_game_state->entity_count, MAX_ENTITIES,
DATA_DIR); DATA_DIR);
slParseCamera(sd, renGetCamera()); slParseCamera(sd, renGetCamera());
slParseHexGrid(sd, g_game_state->grid);
slFreeSceneDoc(sd); slFreeSceneDoc(sd);
} else { } else {
LOG(ERROR) << "Error loading scene, exiting\n"; LOG(ERROR) << "Error loading scene, exiting\n";
return 1; 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"; LOG(ERROR) << "Error in vertex data, exiting\n";
return 1; 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, moveCamera(g->is_moveup, g->is_moveleft, g->is_movedown, g->is_moveright,
g->is_moveforward, g->is_movebackward); 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); renderDebug(g_polygon_select_vertices);
renderGooey(handles, g->draw_mode, r->is_debug_draw, g->start_hex, renderGooey(handles, g->grid.draw_mode, r->is_debug_draw, g->grid.start_hex,
g->current_hex, g->is_selecting, getCameraPosition()); g->grid.current_hex, g->grid.is_selecting, getCameraPosition());
SDL_GL_SwapWindow(handles.window); SDL_GL_SwapWindow(handles.window);

14
src/hexgame.h

@ -1,31 +1,22 @@
#pragma once #pragma once
#include <vector>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "entity.h" #include "entity.h"
#include "hexgrid.h" #include "hexgrid.h"
#include "hexlib.h"
#include "util.h" #include "util.h"
struct game_state struct game_state
{ {
std::vector<hex_info> *hex_array; hexgrid grid;
hex_info *start_hex;
hex_info *current_hex;
vector<hex_info> selected_hexes;
HexDrawMode draw_mode;
const Layout hex_layout;
// TODO: WiP // TODO: WiP
uint32 entity_count = 0; uint32 entity_count = 0;
Entity* entities = nullptr; Entity* entities = nullptr;
// camera movement controls // camera movement controls
bool is_selecting = false;
bool is_camera_rotate = false; bool is_camera_rotate = false;
bool is_moveforward = false; bool is_moveforward = false;
bool is_movebackward = false; bool is_movebackward = false;
@ -35,8 +26,5 @@ struct game_state
bool is_moveright = false; bool is_moveright = false;
bool is_rotateCW = false; bool is_rotateCW = false;
bool is_rotateCCW = false; bool is_rotateCCW = false;
// TODO: initialize this in hexgrid.h to remove hexlib.h dep?
game_state(Layout &l) : hex_layout(l) {}
}; };

11
src/hexgrid.cpp

@ -2,6 +2,8 @@
#include "hexgrid.h" #include "hexgrid.h"
// interface
void void
hgCreateHexes(vector<hex_info> *hxi_array, const Layout &layout, uint32 color) hgCreateHexes(vector<hex_info> *hxi_array, const Layout &layout, uint32 color)
{ {
@ -19,3 +21,12 @@ hgResetHexes()
{ {
} }
// internal
void
createHexagonGrid(vector<hex_info> *hxi_array, const Layout &layout, uint32 color, uint radius)
{
}

11
src/hexgrid.h

@ -31,6 +31,17 @@ struct hex_info
struct hexgrid struct hexgrid
{ {
Layout hex_layout; Layout hex_layout;
uint hex_size = 10;
uint hex_radius = 0;
uint32 fill_color = 0x565656FF;
uint32 selected_color = 0xF46000FF;
std::vector<hex_info> *hex_array;
hex_info* start_hex = nullptr;
hex_info* current_hex = nullptr;
vector<hex_info> selected_hexes;
HexDrawMode draw_mode = CONE_FILL;
bool is_selecting = false;
}; };

12
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 // internal

3
src/scene_loader.h

@ -1,5 +1,6 @@
#include "camera.h" #include "camera.h"
#include "hexgrid.h"
#include "util.h" #include "util.h"
@ -17,4 +18,4 @@ bool slParseEntities(
); );
void slParseCamera(slSceneDoc* sd, camera& cam); void slParseCamera(slSceneDoc* sd, camera& cam);
//bool slParseHexGrid(slSceneDoc* sd, std::vector<hex_info*>& hexes); void slParseHexGrid(slSceneDoc* sd, hexgrid& hg);

Loading…
Cancel
Save