Browse Source

move hex functions from hexgame.cpp to hexgrid

master
cinnaboot 8 years ago
parent
commit
d45d33d87f
  1. 192
      src/hexgame.cpp
  2. 125
      src/hexgrid.cpp
  3. 15
      src/hexgrid.h

192
src/hexgame.cpp

@ -1,9 +1,6 @@
/*******************************************************************************
* TODO:
* - renderer
* - move hexgrid buffer functions from renderer to hexgrid
* - remove references to hexes from renderer functions
* - move hex functions from hexgame.cpp to hexgrid
* - add prefix to interface function names for eg) gooey.h, renderer.h
* - clean up main(), split out initialization and scene loading to new functions
* - check over renderer, camera, gooey init after changes
@ -87,35 +84,6 @@ mapMouseToViewport(int32 x, int32 y)
return coords;
}
void
resetHexes()
{
g_game_state->grid.start_hex = g_game_state->grid.current_hex = 0;
for (hex_info &hxi : *g_game_state->grid.hex_array)
{
hxi.selected = false;
hxi.current_color = hxi.stored_color;
}
}
hex_info *
getSingleHex(int32 x, int32 y)
{
v2i dims = g_render_state->viewport_dims;
v2f v = cameraUnproject(g_render_state->cam, x, y, dims.x, dims.y);
Point p(v.x, v.y);
Hex h = hex_round(pixel_to_hex(g_game_state->grid.hexlib_layout, p));
for (hex_info &hxi : *g_game_state->grid.hex_array)
{
if (hex_equal(h, hxi.hex))
return &hxi;
}
return 0;
}
void
setStartHex(hex_info *hex)
{
@ -128,120 +96,14 @@ setStartHex(hex_info *hex)
void
startDrawHelper(int32 x, int32 y)
{
resetHexes();
hex_info *hex = getSingleHex(x, y);
hgResetHexes(g_game_state->grid);
v2i dims = g_render_state->viewport_dims;
v2f v = cameraUnproject(g_render_state->cam, x, y, dims.x, dims.y);
hex_info *hex = hgGetSingleHex(g_game_state->grid, v.x, v.y);
if (hex)
setStartHex(hex);
}
void
updateHexFill(int32 x, int32 y)
{
hex_info *hxi = getSingleHex(x, y);
if (hxi && (hxi != g_game_state->grid.current_hex) && g_game_state->grid.start_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->grid.hex_array)
{
if (hex_distance(g_game_state->grid.start_hex->hex, h.hex) <= l)
{
h.selected = true;
h.current_color = g_game_state->grid.selected_fill_color;
}
else
{
h.selected = false;
h.current_color = h.stored_color;
}
}
}
}
void
updateHexLineDraw(int32 x, int32 y)
{
hex_info *hxi = getSingleHex(x, y);
if (hxi && (hxi != g_game_state->grid.current_hex) && g_game_state->grid.start_hex)
{
g_game_state->grid.current_hex = hxi;
vector<Hex> hexLine = hex_linedraw(g_game_state->grid.start_hex->hex, hxi->hex);
for (hex_info &h1 : *g_game_state->grid.hex_array)
{
for (uint i = 0; i < hexLine.size(); i++)
{
Hex h2 = hexLine[i];
if (hex_equal(h1.hex, h2))
{
h1.selected = true;
h1.current_color = g_game_state->grid.selected_fill_color;
break;
}
else if (i == hexLine.size() - 1)
{
h1.selected = false;
h1.current_color = h1.stored_color;
}
}
}
}
}
void
updateHexConeFill(int32 x, int32 y)
{
hex_info *hxi = getSingleHex(x, y);
if (hxi && (hxi != g_game_state->grid.current_hex) && g_game_state->grid.start_hex)
{
g_game_state->grid.current_hex = hxi;
// TODO: Remove debug code here and from gooey.h
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
real64 x1 = len * std::cos(angle);
real64 y1 = len * std::sin(angle);
// top of cone
real64 topX = x1 * std::cos(coneAngle) - y1 * std::sin(coneAngle) + p1.x;
real64 topY = x1 * std::sin(coneAngle) + y1 * std::cos(coneAngle) + p1.y;
// bottom of cone
real64 botX = x1 * std::cos(coneAngle) + y1 * std::sin(coneAngle) + p1.x;
real64 botY = x1 * std::sin(-1 * coneAngle) + y1 * std::cos(coneAngle) + p1.y;
Point test_p;
Point vert2 = Point(botX, botY);
Point vert4 = Point(topX, topY);
std::vector<Point> vertices = {p1, vert2, p2, vert4};
for (hex_info &h : *g_game_state->grid.hex_array)
{
test_p.x = h.XPos;
test_p.y = h.YPos;
if (crossingTest(vertices, test_p))
{
h.selected = true;
h.current_color = g_game_state->grid.selected_fill_color;
}
else
{
h.selected = false;
h.current_color = h.stored_color;
}
}
g_polygon_select_vertices[0] = p1;
g_polygon_select_vertices[1] = vert2;
g_polygon_select_vertices[2] = p2;
g_polygon_select_vertices[3] = vert4;
}
}
void
handleMouseDown(SDL_MouseButtonEvent &e)
{
@ -268,7 +130,9 @@ handleMouseDown(SDL_MouseButtonEvent &e)
// fall through
default:
{
hex_info *hex = getSingleHex(coords.x, coords.y);
v2i dims = g_render_state->viewport_dims;
v2f v = cameraUnproject(g_render_state->cam, coords.x, coords.y, dims.x, dims.y);
hex_info *hex = hgGetSingleHex(g_game_state->grid, v.x, v.y);
if (hex)
{
hex->selected = !hex->selected;
@ -289,48 +153,21 @@ void
handleMouseMove(SDL_MouseMotionEvent &e)
{
v2i coords = mapMouseToViewport(e.x, e.y);
v2i dims = g_render_state->viewport_dims;
v2f v = cameraUnproject(g_render_state->cam, coords.x, coords.y, dims.x, dims.y);
switch (g_game_state->grid.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:
break;
}
}
void
handleMouseUp(SDL_MouseButtonEvent &e)
{
//v2i coords = mapMouseToViewport(e.x, e.y);
switch (g_game_state->grid.draw_mode)
{
case FILL:
g_game_state->grid.is_selecting = false;
hgUpdateHexFill(g_game_state->grid, v.x, v.y);
break;
case LINE:
g_game_state->grid.is_selecting = false;
hgUpdateHexLineDraw(g_game_state->grid, v.x, v.y);
break;
case CONE_FILL:
g_game_state->grid.is_selecting = false;
hgUpdateHexConeFill(g_game_state->grid, v.x, v.y, CONE_ANGLE, g_polygon_select_vertices);
break;
case PATHFINDING:
@ -407,15 +244,10 @@ processSDLEvents()
if (!gooey_wants)
{
if (e.button.button == SDL_BUTTON_LEFT)
{
g->grid.is_selecting = false;
handleMouseUp(e.button);
}
else if (e.button.button == SDL_BUTTON_RIGHT)
{
g->is_camera_rotate = false;
}
}
break;
case SDL_MOUSEMOTION:
if (!gooey_wants && g->grid.is_selecting)

125
src/hexgrid.cpp

@ -71,15 +71,136 @@ hgInitGLBuffers(hexgrid& hg, rg_shader_program program, bool is_filled_hexes)
}
hex_info*
hgGetSingleHex(int32 x, int32 y)
hgGetSingleHex(hexgrid& hg, real32 x, real32 y)
{
Point p(x, y);
Hex h = hex_round(pixel_to_hex(hg.hexlib_layout, p));
for (hex_info &hxi : *hg.hex_array) {
if (hex_equal(h, hxi.hex))
return &hxi;
}
return nullptr;
}
void
hgResetHexes()
hgResetHexes(hexgrid& hg)
{
hg.start_hex = hg.current_hex = 0;
for (hex_info &hxi : *hg.hex_array) {
hxi.selected = false;
hxi.current_color = hxi.stored_color;
}
}
void
hgUpdateHexFill(hexgrid& hg, int32 x, int32 y)
{
hex_info *hxi = hgGetSingleHex(hg, x, y);
if (hxi && (hxi != hg.current_hex) && hg.start_hex)
{
hg.current_hex = hxi;
int l = hex_distance(hg.start_hex->hex, hg.current_hex->hex);
for (hex_info &h : *hg.hex_array)
{
if (hex_distance(hg.start_hex->hex, h.hex) <= l)
{
h.selected = true;
h.current_color = hg.selected_fill_color;
}
else
{
h.selected = false;
h.current_color = h.stored_color;
}
}
}
}
void
hgUpdateHexLineDraw(hexgrid& hg, int32 x, int32 y)
{
hex_info *hxi = hgGetSingleHex(hg, x, y);
if (hxi && (hxi != hg.current_hex) && hg.start_hex)
{
hg.current_hex = hxi;
vector<Hex> hexLine = hex_linedraw(hg.start_hex->hex, hxi->hex);
for (hex_info &h1 : *hg.hex_array)
{
for (uint i = 0; i < hexLine.size(); i++)
{
Hex h2 = hexLine[i];
if (hex_equal(h1.hex, h2))
{
h1.selected = true;
h1.current_color = hg.selected_fill_color;
break;
}
else if (i == hexLine.size() - 1)
{
h1.selected = false;
h1.current_color = h1.stored_color;
}
}
}
}
}
void
hgUpdateHexConeFill(hexgrid& hg, int32 x, int32 y, float cone_angle, std::vector<Point>& debug_vertices)
{
hex_info *hxi = hgGetSingleHex(hg, x, y);
if (hxi && (hxi != hg.current_hex) && hg.start_hex)
{
hg.current_hex = hxi;
// TODO: Remove debug code here and from gooey.h
Point p1(hg.start_hex->XPos, hg.start_hex->YPos);
Point p2(hg.current_hex->XPos, hg.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
real64 topX = x1 * std::cos(coneAngle) - y1 * std::sin(coneAngle) + p1.x;
real64 topY = x1 * std::sin(coneAngle) + y1 * std::cos(coneAngle) + p1.y;
// bottom of cone
real64 botX = x1 * std::cos(coneAngle) + y1 * std::sin(coneAngle) + p1.x;
real64 botY = x1 * std::sin(-1 * coneAngle) + y1 * std::cos(coneAngle) + p1.y;
Point test_p;
Point vert2 = Point(botX, botY);
Point vert4 = Point(topX, topY);
std::vector<Point> vertices = {p1, vert2, p2, vert4};
for (hex_info &h : *hg.hex_array)
{
test_p.x = h.XPos;
test_p.y = h.YPos;
if (crossingTest(vertices, test_p))
{
h.selected = true;
h.current_color = hg.selected_fill_color;
}
else
{
h.selected = false;
h.current_color = h.stored_color;
}
}
debug_vertices[0] = p1;
debug_vertices[1] = vert2;
debug_vertices[2] = p2;
debug_vertices[3] = vert4;
}
}
void

15
src/hexgrid.h

@ -59,7 +59,18 @@ struct hexgrid
void hgCreateHexes(hexgrid& hg);
render_group* hgInitGLBuffers(hexgrid& hg, rg_shader_program program, bool is_filled_hex);
hex_info* hgGetSingleHex(int32 x, int32 y);
void hgResetHexes();
hex_info* hgGetSingleHex(hexgrid& hg, real32 x, real32 y);
void hgResetHexes(hexgrid& hg);
void hgUpdateHexFill(hexgrid& hg, int32 x, int32 y);
void hgUpdateHexLineDraw(hexgrid& hg, int32 x, int32 y);
void hgUpdateHexConeFill(hexgrid& hg, int32 x, int32 y, float cone_angle, std::vector<Point>& debug_vertices);
void hgUpdateColorBuffer(gl_buffer& color_buf, std::vector<hex_info>* hex_array);

Loading…
Cancel
Save