From 5f726a561fd7af82643b30410cbd7e23b5458ea7 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Fri, 31 Mar 2017 10:45:31 +0100 Subject: [PATCH] Initial commit --- Makefile | 2 ++ array.c | 42 ++++++++++++++++++++++++++++++++++++++++++ array.h | 29 +++++++++++++++++++++++++++++ gol | Bin 0 -> 15496 bytes gol.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 Makefile create mode 100644 array.c create mode 100644 array.h create mode 100755 gol create mode 100644 gol.c 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 0000000000000000000000000000000000000000..a0f9c49125f86a7314f1a7fdea3546d87dc591db GIT binary patch literal 15496 zcmeHOdu&_Rc|VtwY}!(2`V}j264SMvD$PVaY`JnB+oT@$RVK$Tsom1r*A#grF_1`& z4_OLkM1^7_grd#`g02`^%qzMAduPM8CRrQ5QoAY8#c8m>OS?hHl7)&LOl>kxt=(|H z@7(iImse!lZTX`ZbcB1q^Zm~EoyT_`yyu)xPjqzcb!nR5;1at9ans9!60+Vxe0Qs4 z71kn_iJ;gdJ|>m`sl*eMDMYOp(>awjQ;(8!gI3^aaall%D<~sO@3COPlxs+o%9{^C z6}9L?K8s~4gkJ)rM|JhxCt1+rGQxCR)#Hq!tVb;AwJ5z7rN{KFsu)vlAGM91Zk1oR zD+rGGAQ4roc}lVr4!)g^Qv54k$$|!DglU%r3#P1h2zpe{Uo|K*iNmVAa(0weHKHHVN(+*FRkv~%g|Kl?Fx69yHfcx<{ zLuHwE{SbnFr!e^c#Ip*c>6#~4oQnB!q`j-Dt|`z!v!p?QGuq8mI++;zOe`KvoDfE5 z_kJU4Ce8j>Ds3ja_qPou;%0ZachD3@Uo0MzI{U-JhxfJ0XlFc@7KYJ3l!zOtbU2wd z42kcJ4GtbQBWcMT%0P$;YfGBpw5iG>V4s<`xFeZlUpQh)sh;6zLCTJ|n}gvoVLUuy z99H#f8w{sXX3DB|=}M!{X@QnUsL9)fISO{j9EuE&iJ|b|U?PIrQ?jc4P&gJBeMu9| zHBpN&QhQS=D-lpUD)x1Cwze5ffyQlxXj7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!i?x`5an$?gHm?XC*Fe#c`r^mPKd2tk3(q%jzJT z4>GyFEXTAP`gDfOJf9=aIrN?Nr?M$T@KD~Y&-=w%>J)@gi%1!YI@>=4jPzS65rOxW zXO%vmo0AN7PYd)<0HZO%=k~nMzorV>h%|d(J1|BEa(4Z~K6RNTG0lRC`W*Bmc)LFD zXQ!3^?>h=)ea?2mq0i?Mqd178poqob?Z{4Kfl>Kvf8HNIr4&~dG_8nJ|BDWN{(mq2 zKd`P}qW^~ueco^9RQcUhIa}cVXXsB5ArUoQ3j2)0|An!jqE7v19s0aa^sB9^)80;f z+CSOL=Y4*I(s#-``ENm=#va$7_e1=DduLVqY5q`PeXiGcfZ5CEbAxMYOI<6q=3lJO z^m`8d!uf*V(iyi%;mvwXX+LN$pZ62veoKYx$r-R7^Z$$pVcdTHEif#*|C`c(*rDKH1^FBk=Cohk7OPa+N81ClOh1kY$L(^*@3**dJBYZ`ex%E5 zk6m7P{tW+M_?@bLKI-#=O=dF`-v9;=cji C4fp>5 literal 0 HcmV?d00001 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); +} + + -- 2.46.2