diff options
Diffstat (limited to 'src/simulator/main.c')
-rw-r--r-- | src/simulator/main.c | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/simulator/main.c b/src/simulator/main.c new file mode 100644 index 0000000..5c875fe --- /dev/null +++ b/src/simulator/main.c @@ -0,0 +1,132 @@ +#include <stdio.h> +#include <time.h> +#include <assert.h> +#include <SDL2/SDL.h> + +#include "SDL_events.h" +#include "./rendering.h" +#include "../game.h" + +Game games[2] = {0}; + +const char *tsoding_shift(int *argc, char ***argv) { + assert(*argc > 0); + const char* result = **argv; + *argc -= 1; + *argv += 1; + return result; +} + +void usage(FILE *stream) { + fprintf(stream, "./gp_simulator [input.bin]\n"); +} + +int main(int argc, char *argv[]) { + + int current = 0; + srand(time(NULL)); + + tsoding_shift(&argc, &argv); + + if (argc == 0) { + printf("WARNING: No input file provided, Generating Random State...\n"); + init_game(&games[current]); + } else { + const char *input_filepath = tsoding_shift(&argc, &argv); + printf("Loading state from %s...\n", input_filepath); + load_game(input_filepath, &games[current]); + } + + + + + + //print_chromo(stdout, game.chromos); + + scc(SDL_Init(SDL_INIT_VIDEO)); + + // Create Window + SDL_Window *window = scp(SDL_CreateWindow( + "Question Mark? (floating)", + 0, 0, + SCREEN_WIDTH, SCREEN_HEIGHT, + SDL_WINDOW_RESIZABLE)); + + // Create GPU Accelerated Renderer + SDL_Renderer *renderer = scp(SDL_CreateRenderer( + window, + -1, + SDL_RENDERER_ACCELERATED)); + + // Set Window to Scale Rendered Content on resize + scc(SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT)); + + // Keep SDL alive and continuously poll events and render. + int quit = 0; + while (!quit) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: { + quit = 1; + } break; + case SDL_KEYDOWN: { + switch (event.key.keysym.sym) { + case SDLK_SPACE: { + step_game(&games[current]); + } break; + case SDLK_r: { + init_game(&games[current]); + } break; + case SDLK_n: { + //print_best_agents(stdout, &games[current], SELECTION_POOL); + int next = 1 - current; + make_next_generation(&games[current], &games[next]); + current = next; + } break; + case SDLK_q: { + SDL_DestroyRenderer(renderer); // Writes an error, but without this, + // program doesn't close properly? + SDL_DestroyWindow(window); + SDL_Quit(); + } break; + //case SDLK_d: { + // dump_game(DUMP_FILEPATH, &games[current]); + //} break; + } + } break; + case SDL_MOUSEBUTTONDOWN: { + Coord cell; + cell.x = (int) floorf(event.button.x / CELL_WIDTH); + cell.y = (int) floorf(event.button.y / CELL_HEIGHT); + Agent *agent = agent_at(&games[current], cell); + + if (agent) { + print_agent(stdout, agent); + } + } break; + } + } + + // Draw Background + SDL_SetRenderDrawColor(renderer, HEX_COLOR(BACKGROUND_COLOR)); + scc(SDL_RenderClear(renderer)); + + // Draw Board Grid + // render_board_grid(renderer); + + // Render Agents + render_game(renderer, &games[current]); + + // Render + SDL_RenderPresent(renderer); + + } + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + + SDL_Quit(); + + return 0; +} |