|
|
|
|
@ -5,7 +5,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// forward declarations
|
|
|
|
|
bool createHexagonGrid(hexgrid hg); |
|
|
|
|
bool createHexagonGrid(hexgrid& hg); |
|
|
|
|
|
|
|
|
|
// interface
|
|
|
|
|
|
|
|
|
|
@ -29,10 +29,10 @@ 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; |
|
|
|
|
} |
|
|
|
|
auto it = hg.hex_map.find(h); |
|
|
|
|
|
|
|
|
|
if (it != hg.hex_map.end()) |
|
|
|
|
return &it->second; |
|
|
|
|
|
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
@ -40,11 +40,10 @@ hgGetSingleHex(hexgrid& hg, real32 x, real32 y)
|
|
|
|
|
void |
|
|
|
|
hgResetHexes(hexgrid& hg) |
|
|
|
|
{ |
|
|
|
|
hg.start_hex = hg.current_hex = 0; |
|
|
|
|
hg.start_hex = hg.current_hex = nullptr; |
|
|
|
|
|
|
|
|
|
for (hex_info &hxi : *hg.hex_array) { |
|
|
|
|
hxi.selected = false; |
|
|
|
|
} |
|
|
|
|
for (auto& it : hg.hex_map) |
|
|
|
|
it.second.selected = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
@ -56,12 +55,11 @@ hgUpdateHexFill(hexgrid& hg, int32 x, int32 y)
|
|
|
|
|
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; |
|
|
|
|
for (auto& it: hg.hex_map) { |
|
|
|
|
if (hex_distance(hg.start_hex->hex, it.second.hex) <= l) |
|
|
|
|
it.second.selected = true; |
|
|
|
|
else |
|
|
|
|
h.selected = false; |
|
|
|
|
it.second.selected = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -72,24 +70,18 @@ hgUpdateHexLineDraw(hexgrid& hg, int32 x, int32 y)
|
|
|
|
|
hex_info *hxi = hgGetSingleHex(hg, x, y); |
|
|
|
|
if (hxi && (hxi != hg.current_hex) && hg.start_hex) |
|
|
|
|
{ |
|
|
|
|
// TODO: can avoid this loop by caching selected hexes
|
|
|
|
|
for (auto& it : hg.hex_map) |
|
|
|
|
it.second.selected = false; |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
else if (i == hexLine.size() - 1) |
|
|
|
|
{ |
|
|
|
|
h1.selected = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
for (Hex h : hexLine) { |
|
|
|
|
auto it = hg.hex_map.find(h); |
|
|
|
|
|
|
|
|
|
if (it != hg.hex_map.end()) |
|
|
|
|
it->second.selected = true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -123,8 +115,9 @@ hgUpdateHexConeFill(hexgrid& hg, int32 x, int32 y, float cone_angle, std::vector
|
|
|
|
|
Point vert4 = Point(topX, topY); |
|
|
|
|
std::vector<Point> vertices = {p1, vert2, p2, vert4}; |
|
|
|
|
|
|
|
|
|
for (hex_info &h : *hg.hex_array) |
|
|
|
|
{ |
|
|
|
|
for (auto& it : hg.hex_map) { |
|
|
|
|
hex_info& h = it.second; |
|
|
|
|
|
|
|
|
|
test_p.x = h.XPos; |
|
|
|
|
test_p.y = h.YPos; |
|
|
|
|
|
|
|
|
|
@ -146,12 +139,13 @@ hgUpdateUVBuffer(hexgrid& hg, float* uv_buffer, uint buf_len)
|
|
|
|
|
{ |
|
|
|
|
// NOTE: 6 triangles * 3 vertices * 3 floats (not 2 to stay aligned with vbuffer)
|
|
|
|
|
uint buf_len_per_hex = 54; |
|
|
|
|
assert(buf_len == hg.hex_array->size() * buf_len_per_hex); |
|
|
|
|
|
|
|
|
|
for (uint i = 0; i < hg.hex_array->size(); i++) { |
|
|
|
|
hex_info hxi = (*hg.hex_array)[i]; |
|
|
|
|
assert(buf_len == hg.hex_map.size() * buf_len_per_hex); |
|
|
|
|
|
|
|
|
|
for (auto& it : hg.hex_map) { |
|
|
|
|
hex_info hxi = it.second; |
|
|
|
|
v2f uv_coords = (hxi.selected) ? hg.selected_fill_color_uv : hg.fill_color_uv; |
|
|
|
|
uint buf_idx = i * buf_len_per_hex; |
|
|
|
|
uint buf_idx = hxi.hexID * buf_len_per_hex; |
|
|
|
|
|
|
|
|
|
for (uint j = 0; j < buf_len_per_hex; j +=3) { |
|
|
|
|
uv_buffer[buf_idx + j + 0] = uv_coords.x; |
|
|
|
|
@ -164,25 +158,19 @@ hgUpdateUVBuffer(hexgrid& hg, float* uv_buffer, uint buf_len)
|
|
|
|
|
// internal
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
createHexagonGrid(hexgrid hg) |
|
|
|
|
createHexagonGrid(hexgrid& hg) |
|
|
|
|
{ |
|
|
|
|
if (hg.hex_array == nullptr) { |
|
|
|
|
LOG(ERROR) << "hg.hex_array is not initialized\n"; |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int hr = hg.hex_radius; |
|
|
|
|
int nhr = hg.hex_radius * -1; |
|
|
|
|
|
|
|
|
|
for (int q = nhr; q <= hr; q++) |
|
|
|
|
{ |
|
|
|
|
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++) |
|
|
|
|
{ |
|
|
|
|
for (int r = r1; r <= r2; r++) { |
|
|
|
|
hex_info hxi; |
|
|
|
|
hxi.hexID = (int32) hg.hex_array->size(); |
|
|
|
|
hxi.hexID = (int32) hg.hex_map.size(); |
|
|
|
|
hxi.selected = false; |
|
|
|
|
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; |
|
|
|
|
@ -194,7 +182,7 @@ createHexagonGrid(hexgrid hg)
|
|
|
|
|
if ((q > -2 && q < 2) && (r > -2 && r < 2)) { |
|
|
|
|
// skip
|
|
|
|
|
} else { |
|
|
|
|
hg.hex_array->push_back(hxi); |
|
|
|
|
hg.hex_map.insert({hxi.hex, hxi}); |
|
|
|
|
} |
|
|
|
|
/////////////
|
|
|
|
|
} |
|
|
|
|
|