diff --git a/src/hexgame.cpp b/src/hexgame.cpp index c4b4381..8faf551 100644 --- a/src/hexgame.cpp +++ b/src/hexgame.cpp @@ -88,9 +88,7 @@ getSingleHex(int32 x, int32 y) for (hex_info &hxi : *g_game_state->hex_array) { if (hex_equal(h, hxi.hex)) - { return &hxi; - } } return 0; @@ -111,34 +109,29 @@ startHexFill(int32 x, int32 y) resetHexes(); hex_info *hex = getSingleHex(x, y); if (hex) - { setStartHex(hex); - } } void updateHexFill(int32 x, int32 y) { - if (g_game_state->is_selecting) + hex_info *hxi = getSingleHex(x, y); + if (hxi && (hxi != g_game_state->current_hex)) { - hex_info *hxi = getSingleHex(x, y); - if (hxi && (hxi != g_game_state->current_hex)) + g_game_state->current_hex = hxi; + int l = hex_distance(g_game_state->start_hex->hex, g_game_state->current_hex->hex); + + for (hex_info &h : *g_game_state->hex_array) { - g_game_state->current_hex = hxi; - int l = hex_distance(g_game_state->start_hex->hex, g_game_state->current_hex->hex); - - for (hex_info &h : *g_game_state->hex_array) + if (hex_distance(g_game_state->start_hex->hex, h.hex) <= l) { - if (hex_distance(g_game_state->start_hex->hex, h.hex) <= l) - { - h.selected = true; - h.current_color = g_render_state->selected_fill_color; - } - else - { - h.selected = false; - h.current_color = h.stored_color; - } + h.selected = true; + h.current_color = g_render_state->selected_fill_color; + } + else + { + h.selected = false; + h.current_color = h.stored_color; } } } @@ -150,38 +143,33 @@ startHexLineDraw(int32 x, int32 y) resetHexes(); hex_info *hex = getSingleHex(x, y); if (hex) - { setStartHex(hex); - } } void updateHexLineDraw(int32 x, int32 y) { - if (g_game_state->is_selecting) + hex_info *hxi = getSingleHex(x, y); + if (hxi && (hxi != g_game_state->current_hex)) { - hex_info *hxi = getSingleHex(x, y); - if (hxi && (hxi != g_game_state->current_hex)) + g_game_state->current_hex = hxi; + vector hexLine = hex_linedraw(g_game_state->start_hex->hex, hxi->hex); + + for (hex_info &h1 : *g_game_state->hex_array) { - g_game_state->current_hex = hxi; - vector hexLine = hex_linedraw(g_game_state->start_hex->hex, hxi->hex); - - for (hex_info &h1 : *g_game_state->hex_array) - { - for (uint32 i = 0; i < hexLine.size(); i++) - { - Hex h2 = hexLine[i]; - if (hex_equal(h1.hex, h2)) - { - h1.selected = true; - h1.current_color = g_render_state->selected_fill_color; - break; - } - else if (i == hexLine.size() - 1) - { - h1.selected = false; - h1.current_color = h1.stored_color; - } + 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_render_state->selected_fill_color; + break; + } + else if (i == hexLine.size() - 1) + { + h1.selected = false; + h1.current_color = h1.stored_color; } } } @@ -194,39 +182,33 @@ startHexConeFill(int32 x, int32 y) resetHexes(); hex_info *hex = getSingleHex(x, y); if (hex) - { setStartHex(hex); - } } void updateHexConeFill(int32 x, int32 y) { - if (g_game_state->is_selecting) + hex_info *hxi = getSingleHex(x, y); + if (hxi && (hxi != g_game_state->current_hex)) { - hex_info *hxi = getSingleHex(x, y); - if (hxi && (hxi != g_game_state->current_hex)) + g_game_state->current_hex = hxi; + + for (hex_info &h : *g_game_state->hex_array) { - g_game_state->current_hex = hxi; - - for (hex_info &h : *g_game_state->hex_array) + // TODO: define a system of linear inequalities in hex-space to represent + // cones/arbitrary polygons + if (&h == g_game_state->start_hex) + continue; + + if (hex_equal(h.hex, hxi->hex)) { - // TODO: define a system of linear inequalities in hex-space to represent - // cones/arbitrary polygons - if (&h == g_game_state->start_hex) - { - continue; - } - if (hex_equal(h.hex, hxi->hex)) - { - h.selected = true; - h.current_color = g_render_state->selected_fill_color; - } - else - { - h.selected = false; - h.current_color = h.stored_color; - } + h.selected = true; + h.current_color = g_render_state->selected_fill_color; + } + else + { + h.selected = false; + h.current_color = h.stored_color; } } } @@ -370,7 +352,7 @@ processSDLEvents(SDL_Handles &handles) } break; case SDL_MOUSEMOTION: - if (!gooey_wants) + if (!gooey_wants && g_game_state->is_selecting) handleMouseMove(e.motion); break; default: diff --git a/src/hexgame.h b/src/hexgame.h index 92a794d..2569963 100644 --- a/src/hexgame.h +++ b/src/hexgame.h @@ -1,111 +1,116 @@ - -#ifndef HEXGAME_H -#define HEXGAME_H - -#include -#include - -#include -#include "aixlog.hpp" - -#include "hexlib.h" - -typedef float real32; -typedef double real64; -typedef int32_t bool32; -typedef int32_t int32; -typedef int64_t int64; -typedef uint8_t uint8; -typedef uint32_t uint32; - -struct v2f -{ - v2f(): x(0), y(0) {} - v2f(real64 a, real64 b): x(a), y(b) {} - real64 x; - real64 y; -}; - -struct v2i -{ - v2i(int a, int b): x(a), y(b) {} - v2i() : x(0), y(0) {} - int32 x; - int32 y; -}; - -struct v4i -{ - int32 x0; - int32 y0; - int32 x1; - int32 y1; -}; - -struct SDL_Handles -{ - SDL_Window *window; - SDL_GLContext glContext; - SDL_DisplayMode currentDisplayMode; - std::vector texSurfaces; -}; - -struct hex_info -{ - int32 hexID = 0; - Hex hex = {}; - real64 XPos = 0; - real64 YPos = 0; - uint32 current_color = 0; // RGBA - uint32 stored_color = 0; // RGBA - bool selected = false; - std::vector vertices; -}; - -enum HexDrawMode -{ - NONE, - FILL, - LINE, - CONE_FILL, - PATHFINDING -}; - -struct render_state -{ - v2i viewport_dims; - bool is_debug_draw; - uint32 fill_color; - uint32 selected_fill_color; -}; - -struct game_state -{ - bool is_selecting; - std::vector *hex_array; - hex_info *start_hex; - hex_info *current_hex; - vector selected_hexes; - HexDrawMode draw_mode; - const Layout hex_layout; - - game_state(Layout &l) : hex_layout(l) {} -}; - -real32 -SafeRatio(real32 dividend, real32 divisor) -{ - if (divisor == 0) - return dividend; - else - return dividend / divisor; -} - -int32 -SafeTruncateToInt32(int64 val) -{ - assert(val <= INT32_MAX && val >= INT32_MIN); - return (int32) val; -} - -#endif // HEXGAME_H + +#ifndef HEXGAME_H +#define HEXGAME_H + +#include +#include + +#if defined (_WIN32) + #include +#else + #include +#endif +#include "aixlog.hpp" + +#include "hexlib.h" + +typedef float real32; +typedef double real64; +typedef int32_t bool32; +typedef int32_t int32; +typedef int64_t int64; +typedef uint8_t uint8; +typedef uint32_t uint32; + +struct v2f +{ + v2f(): x(0), y(0) {} + v2f(real64 a, real64 b): x(a), y(b) {} + real64 x; + real64 y; +}; + +struct v2i +{ + v2i(int a, int b): x(a), y(b) {} + v2i() : x(0), y(0) {} + int32 x; + int32 y; +}; + +struct v4i +{ + int32 x0; + int32 y0; + int32 x1; + int32 y1; +}; + +struct SDL_Handles +{ + SDL_Window *window; + SDL_GLContext glContext; + SDL_DisplayMode currentDisplayMode; + std::vector texSurfaces; +}; + +struct hex_info +{ + int32 hexID = 0; + Hex hex = {}; + real64 XPos = 0; + real64 YPos = 0; + uint32 current_color = 0; // RGBA + uint32 stored_color = 0; // RGBA + bool selected = false; + std::vector vertices; +}; + +enum HexDrawMode +{ + NONE, + FILL, + LINE, + CONE_FILL, + PATHFINDING +}; + +struct render_state +{ + v2i viewport_dims; + bool is_debug_draw; + uint32 fill_color; + uint32 selected_fill_color; +}; + +struct game_state +{ + bool is_selecting; + std::vector *hex_array; + hex_info *start_hex; + hex_info *current_hex; + vector selected_hexes; + HexDrawMode draw_mode; + const Layout hex_layout; + + game_state(Layout &l) : hex_layout(l) {} +}; + +real32 +SafeRatio(real32 dividend, real32 divisor) +{ + if (divisor == 0) + return dividend; + else + return dividend / divisor; +} + +int32 +SafeTruncateToInt32(int64 val) +{ + assert(val <= INT32_MAX && val >= INT32_MIN); + return (int32) val; +} + +#endif // HEXGAME_H + diff --git a/src/imgui.ini b/src/imgui.ini new file mode 100644 index 0000000..a8ffb8c --- /dev/null +++ b/src/imgui.ini @@ -0,0 +1,5 @@ +[Window][Debug##Default] +Pos=0,5 +Size=400,400 +Collapsed=0 + diff --git a/src/renderer.h b/src/renderer.h index d61e255..dfd1ecb 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -493,7 +493,7 @@ renderDebug(hex_info *start_hex, hex_info *current_hex) Point p2(current_hex->XPos, 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 = 24.5f * M_PI / 180; // M_PI is non-standard and may not be portable + real64 coneAngle = 30.f * M_PI / 180; // M_PI is non-standard and may not be portable // TODO: add matrix math library for rotation transform? // |x| | cos(a + b) -sin(a + b) 0 |