You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
456 lines
8.9 KiB
456 lines
8.9 KiB
|
|
// Some defaults for the game layout |
|
#define HEX_SIZE 25 |
|
#define HEX_RADIUS 7 |
|
#define HEX_ORIENTATION layout_flat |
|
#define FILL_COLOR 0x5C5C5CFF |
|
#define SELECTED_FILL_COLOR 0xF46000FF |
|
#define DEBUG_DRAW true |
|
#define VIEWPORT_WIDTH 1280 |
|
#define VIEWPORT_HEIGHT 720 |
|
|
|
|
|
#include <string> |
|
#include <vector> |
|
|
|
#if defined(_WIN32) |
|
#include <windows.h> |
|
#include <Winuser.h> |
|
#endif |
|
|
|
#include <SDL.h> |
|
|
|
#include "hexgame.h" |
|
#include "hexlib.h" |
|
#include "renderer.h" |
|
#include "gooey.h" |
|
|
|
using std::vector; |
|
|
|
|
|
game_state* g_game_state; |
|
render_state* g_render_state; |
|
|
|
void |
|
createHexes(vector<hex_info> *hxi_array, const Layout &layout, uint32 color) |
|
{ |
|
// create a hexagonal grid of hexagons |
|
int map_radius = 7; |
|
for (int q = -map_radius; q <= map_radius; q++) |
|
{ |
|
int r1 = std::max(-map_radius, -q - map_radius); |
|
int r2 = std::min(map_radius, -q + map_radius); |
|
|
|
for (int r = r1; r <= r2; r++) |
|
{ |
|
hex_info hxi; |
|
hxi.hexID = (int32) hxi_array->size(); |
|
hxi.hex.q = q; hxi.hex.r = r; hxi.hex.s = -q-r; |
|
Point p = hex_to_pixel(layout, hxi.hex); |
|
hxi.XPos = p.x; |
|
hxi.YPos = p.y; |
|
hxi.current_color = color; |
|
hxi.stored_color = color; |
|
hxi.vertices = polygon_corners(layout, hxi.hex); |
|
hxi.vertices.shrink_to_fit(); |
|
hxi_array->push_back(hxi); |
|
} |
|
} |
|
} |
|
|
|
v2i |
|
mapMouseToViewport(int32 x, int32 y) |
|
{ |
|
v2i coords; |
|
coords.x = x; |
|
coords.y = g_render_state->viewport_dims.y - y; |
|
return coords; |
|
} |
|
|
|
void |
|
resetHexes() |
|
{ |
|
g_game_state->start_hex = g_game_state->current_hex = 0; |
|
|
|
for (hex_info &hxi : *g_game_state->hex_array) |
|
{ |
|
hxi.selected = false; |
|
hxi.current_color = hxi.stored_color; |
|
} |
|
} |
|
|
|
hex_info * |
|
getSingleHex(int32 x, int32 y) |
|
{ |
|
Point p(x, y); |
|
Hex h = hex_round(pixel_to_hex(g_game_state->hex_layout, p)); |
|
|
|
for (hex_info &hxi : *g_game_state->hex_array) |
|
{ |
|
if (hex_equal(h, hxi.hex)) |
|
return &hxi; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
void |
|
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; |
|
} |
|
|
|
void |
|
startHexFill(int32 x, int32 y) |
|
{ |
|
resetHexes(); |
|
hex_info *hex = getSingleHex(x, y); |
|
if (hex) |
|
setStartHex(hex); |
|
} |
|
|
|
void |
|
updateHexFill(int32 x, int32 y) |
|
{ |
|
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) |
|
{ |
|
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; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void |
|
startHexLineDraw(int32 x, int32 y) |
|
{ |
|
resetHexes(); |
|
hex_info *hex = getSingleHex(x, y); |
|
if (hex) |
|
setStartHex(hex); |
|
} |
|
|
|
void |
|
updateHexLineDraw(int32 x, int32 y) |
|
{ |
|
hex_info *hxi = getSingleHex(x, y); |
|
if (hxi && (hxi != g_game_state->current_hex)) |
|
{ |
|
g_game_state->current_hex = hxi; |
|
vector<Hex> hexLine = hex_linedraw(g_game_state->start_hex->hex, hxi->hex); |
|
|
|
for (hex_info &h1 : *g_game_state->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_render_state->selected_fill_color; |
|
break; |
|
} |
|
else if (i == hexLine.size() - 1) |
|
{ |
|
h1.selected = false; |
|
h1.current_color = h1.stored_color; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
void |
|
startHexConeFill(int32 x, int32 y) |
|
{ |
|
resetHexes(); |
|
hex_info *hex = getSingleHex(x, y); |
|
if (hex) |
|
setStartHex(hex); |
|
} |
|
|
|
void |
|
updateHexConeFill(int32 x, int32 y) |
|
{ |
|
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) |
|
{ |
|
// 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; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void |
|
handleMouseDown(SDL_MouseButtonEvent &e) |
|
{ |
|
v2i coords = mapMouseToViewport(e.x, e.y); |
|
|
|
switch (g_game_state->draw_mode) |
|
{ |
|
case FILL: |
|
startHexFill(coords.x, coords.y); |
|
break; |
|
|
|
case LINE: |
|
startHexLineDraw(coords.x, coords.y); |
|
break; |
|
|
|
case CONE_FILL: |
|
startHexConeFill(coords.x, coords.y); |
|
break; |
|
|
|
case PATHFINDING: |
|
break; |
|
|
|
case NONE: |
|
// fall through |
|
default: |
|
{ |
|
hex_info *hex = getSingleHex(coords.x, coords.y); |
|
if (hex) |
|
{ |
|
hex->selected = !hex->selected; |
|
if (hex->selected) |
|
{ |
|
hex->current_color = g_render_state->selected_fill_color; |
|
} |
|
else |
|
{ |
|
hex->current_color = hex->stored_color; |
|
} |
|
} |
|
}break; |
|
} |
|
} |
|
|
|
void |
|
handleMouseMove(SDL_MouseMotionEvent &e) |
|
{ |
|
v2i coords = mapMouseToViewport(e.x, e.y); |
|
|
|
switch (g_game_state->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->draw_mode) |
|
{ |
|
case FILL: |
|
g_game_state->is_selecting = false; |
|
break; |
|
|
|
case LINE: |
|
g_game_state->is_selecting = false; |
|
break; |
|
|
|
case CONE_FILL: |
|
g_game_state->is_selecting = false; |
|
break; |
|
|
|
case PATHFINDING: |
|
break; |
|
|
|
case NONE: |
|
// fall through |
|
default: |
|
break; |
|
} |
|
} |
|
|
|
bool |
|
processSDLEvents(SDL_Handles &handles) |
|
{ |
|
SDL_Event e; |
|
bool run = true; |
|
|
|
while (SDL_PollEvent(&e)) |
|
{ |
|
// let gooey have event |
|
// TODO: need to check for both io.WantCaptureKeyboard and io.WantCaptureMouse |
|
// to fix bug with 'ESC' not passing through while in imgui |
|
bool gooey_wants = gooeyProcessEvent(e); |
|
|
|
switch (e.type) |
|
{ |
|
case SDL_QUIT: |
|
run = false; |
|
break; |
|
case SDL_KEYDOWN: |
|
if ( e.key.keysym.sym == SDLK_ESCAPE) |
|
run = false; |
|
break; |
|
case SDL_MOUSEBUTTONDOWN: |
|
if (!gooey_wants) |
|
{ |
|
g_game_state->is_selecting = true; |
|
handleMouseDown(e.button); |
|
} |
|
break; |
|
case SDL_MOUSEBUTTONUP: |
|
if (!gooey_wants) |
|
{ |
|
g_game_state->is_selecting = false; |
|
handleMouseUp(e.button); |
|
} |
|
break; |
|
case SDL_MOUSEMOTION: |
|
if (!gooey_wants && g_game_state->is_selecting) |
|
handleMouseMove(e.motion); |
|
break; |
|
default: |
|
break; |
|
} |
|
} |
|
|
|
return run; |
|
} |
|
|
|
bool |
|
cleanUp(SDL_Handles &handles) |
|
{ |
|
shutdownGooey(); |
|
freeBuffers(); // renderer.h |
|
for (SDL_Surface *surface : handles.texSurfaces) |
|
SDL_FreeSurface(surface); |
|
SDL_GL_DeleteContext(handles.glContext); |
|
SDL_DestroyWindow(handles.window); |
|
SDL_Quit(); |
|
|
|
if (g_game_state) |
|
{ |
|
if (g_game_state->hex_array) |
|
delete g_game_state->hex_array; |
|
delete g_game_state; |
|
} |
|
|
|
return true; |
|
} |
|
|
|
#if defined(_WIN32) |
|
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, |
|
LPSTR lpCmdLine, int nShowCmd) |
|
#else |
|
int main(int argc, char* argv[]) |
|
#endif |
|
{ |
|
AixLog::Log::init<AixLog::SinkCout>(AixLog::Severity::trace, AixLog::Type::normal); |
|
LOG(INFO) << "Application started\n"; |
|
|
|
// 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<hex_info>; |
|
g_game_state->is_selecting = false; |
|
g_game_state->start_hex = 0x0; |
|
g_game_state->current_hex = 0x0; |
|
g_game_state->draw_mode = NONE; |
|
|
|
// init global render state |
|
g_render_state = new 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); |
|
SDL_Handles handles; |
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) != 0) |
|
{ |
|
LOG(ERROR) << "Error, SDL_Init: " << SDL_GetError() << "\n"; |
|
return 1; |
|
} |
|
|
|
if (!initRenderer(handles, g_render_state->viewport_dims)) |
|
{ |
|
LOG(ERROR) << "Unable to initialize graphics, exiting\n"; |
|
return 1; |
|
} |
|
|
|
if (!createScene(g_game_state->hex_array)) |
|
{ |
|
LOG(ERROR) << "Error in vertex data, exiting\n"; |
|
return 1; |
|
} |
|
|
|
if (!initGooey(handles, g_render_state->viewport_dims)) |
|
{ |
|
LOG(ERROR) << "Fooey, No Gooey!\n"; |
|
return 1; |
|
} |
|
|
|
// main loop |
|
while (processSDLEvents(handles)) |
|
{ |
|
// TODO: remove hack to not peg CPU. replace with an actual frame timer |
|
SDL_Delay(16); // ~60hz |
|
renderFrame(g_game_state->hex_array); |
|
if (g_render_state->is_debug_draw && g_game_state->draw_mode == CONE_FILL) |
|
renderDebug(g_game_state->start_hex, g_game_state->current_hex); |
|
renderGooey(handles, g_game_state->draw_mode, g_render_state->is_debug_draw); |
|
SDL_GL_SwapWindow(handles.window); |
|
} |
|
|
|
cleanUp(handles); |
|
return 0; |
|
} |
|
|
|
|