From: Maximilian Friedersdorff Date: Fri, 31 Mar 2017 09:45:31 +0000 (+0100) Subject: Initial commit X-Git-Url: https://git.friedersdorff.com/?p=max%2Fgol.git;a=commitdiff_plain;h=5f726a561fd7af82643b30410cbd7e23b5458ea7 Initial commit --- 5f726a561fd7af82643b30410cbd7e23b5458ea7 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7bd0d2c --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +gol: gol.c array.c array.h + $(CC) -o gol gol.c array.c --std=gnu99 -lSDL2 diff --git a/array.c b/array.c new file mode 100644 index 0000000..469be38 --- /dev/null +++ b/array.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include "array.h" + +/* + * Implements a linear search. + */ +int gol_is_live(struct gol_board *const state, int_least32_t x, int_least32_t y) +{ + for (unsigned int i = 0; i < state->n; i += 2) { + if (state->live_cells[i] == x && + state->live_cells[i+1] == y) { + return 1; + } + } + return 0; +} + +int gol_vivify(struct gol_board *const state, int_least32_t x, int_least32_t y) +{ + int_least32_t *tmp_cells = NULL; + size_t tmp_size = 0; + if (state->n == state->size) { + tmp_size = 2 * state->size; + tmp_cells = malloc(tmp_size * sizeof(typeof(tmp_cells))); + if (!tmp_cells) { + return 1; + } + memcpy(tmp_cells, state->live_cells, + tmp_size * sizeof(typeof(tmp_cells))); + state->size = tmp_size; + free(state->live_cells); + state->live_cells = tmp_cells; + } + + state->live_cells[state->n] = x; + state->live_cells[state->n + 1] = y; + + state->n += 2; + return 1; +} diff --git a/array.h b/array.h new file mode 100644 index 0000000..42c5415 --- /dev/null +++ b/array.h @@ -0,0 +1,29 @@ +#ifndef GOL_ARRAY_H +#define GOL_ARRAY_H + +#include + + +/* + * Represents a state of the Game of Life. The coordinates of all live cells + * is stored in an array: [x1, y1, x2, y2, x3, y3,...]. N is the number of + * live cells, size is the size of the array. + */ + +struct gol_board { + int_least32_t *live_cells; + uint_least32_t n; + uint_least32_t size; +}; + +/* + * Check that a cell located at (x,y) in the game board is live. + */ +int gol_is_live(struct gol_board *state, int_least32_t x, int_least32_t y); + +/* + * Append the coordinates of the live cell to the array. + */ +int gol_vivify(struct gol_board *state, int_least32_t x, int_least32_t y); + +#endif diff --git a/gol b/gol new file mode 100755 index 0000000..a0f9c49 Binary files /dev/null and b/gol differ diff --git a/gol.c b/gol.c new file mode 100644 index 0000000..0ad687d --- /dev/null +++ b/gol.c @@ -0,0 +1,46 @@ +#include +#include +#include "array.h" + +void quit(int e_st) +{ + SDL_Quit(); + exit(e_st); +} + +int main(int argc, char* args[]) +{ + SDL_Window* window = NULL; + SDL_Surface* screen = NULL; + + if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { + quit(1); + } + + //screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); + + window = SDL_CreateWindow("SDL Tutorial", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + 640, + 320, + SDL_WINDOW_SHOWN); + if (!window) { + quit(1); + } + + screen = SDL_GetWindowSurface(window); + if (!screen) { + quit(1); + } + + SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF)); + SDL_UpdateWindowSurface(window); + + SDL_Delay(2000); + + SDL_DestroyWindow(window); + quit(0); +} + +