From 04eee8849e667f277a49303ef81470a9187d5d25 Mon Sep 17 00:00:00 2001 From: Christian Kroll Date: Fri, 11 Mar 2011 06:48:55 +0000 Subject: [PATCH] saved 404 bytes --- animations/gameoflife.c | 98 ++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/animations/gameoflife.c b/animations/gameoflife.c index 53427b2..302a7c1 100644 --- a/animations/gameoflife.c +++ b/animations/gameoflife.c @@ -23,6 +23,13 @@ #define XSIZE UNUM_COLS #define YSIZE UNUM_ROWS +// optimizing for 8 bit archs while retaining compatibility with dimensions >255 +#if UNUM_COLS < 256 && UNUM_ROWS < 256 +typedef uint8_t coord_t; +#else +typedef unsigned int coord_t; +#endif + /* * last line is for debug information */ @@ -49,10 +56,10 @@ //#define GLIDER_TEST #define BITSTUFFED -#define LOOP_DETECT_BUFFER_SIZE 8 +#define LOOP_DETECT_BUFFER_SIZE 8U #ifndef GOL_DELAY - #define GOL_DELAY 1 /* milliseconds */ + #define GOL_DELAY 250 /* milliseconds */ #endif #ifndef GOL_CYCLES @@ -62,7 +69,12 @@ /******************************************************************************/ /******************************************************************************/ -typedef enum{dead=0, alive=1} cell_t; +enum cell_e {dead=0, alive=1}; +#ifdef NDEBUG + typedef uint8_t cell_t; +#else + typedef enum cell_e cell_t; +#endif #ifndef BITSTUFFED @@ -73,13 +85,13 @@ typedef cell_t field_t[FIELD_XSIZE][FIELD_YSIZE]; /******************************************************************************/ -void setcell(field_t pf, int x, int y, cell_t value){ +void setcell(field_t pf, coord_t x, coord_t y, cell_t value){ pf[(x+FIELD_XSIZE)%FIELD_XSIZE][(y+FIELD_YSIZE)%FIELD_YSIZE] = value; } /******************************************************************************/ -cell_t getcell(field_t pf, int x, int y){ +cell_t getcell(field_t pf, coord_t x, coord_t y){ return pf[(x+FIELD_XSIZE)%FIELD_XSIZE][(y+FIELD_YSIZE)%FIELD_YSIZE]; } @@ -92,10 +104,8 @@ typedef uint8_t field_t[FIELD_XSIZE][FIELD_YSIZE]; /******************************************************************************/ -void setcell(field_t pf, int x, int y, cell_t value){ +void setcell(field_t pf, coord_t x, coord_t y, cell_t value){ uint8_t t; - x = (x+XSIZE) % XSIZE; - y = (y+YSIZE) % YSIZE; t = pf[x/8][y]; if(value==alive){ @@ -108,35 +118,30 @@ void setcell(field_t pf, int x, int y, cell_t value){ /******************************************************************************/ -cell_t getcell(field_t pf, int x, int y){ - x = (x+XSIZE) % XSIZE; - y = (y+YSIZE) % YSIZE; - +static cell_t getcell(field_t pf, coord_t x, coord_t y){ return ((pf[x/8][y])&(1<<(x&7)))?alive:dead; } #endif /******************************************************************************/ -uint8_t countsurroundingalive(field_t pf, int x, int y){ - uint8_t ret=0; - ret += (getcell(pf, x-1, y-1)==alive)?1:0; - ret += (getcell(pf, x , y-1)==alive)?1:0; - ret += (getcell(pf, x+1, y-1)==alive)?1:0; - - ret += (getcell(pf, x-1, y )==alive)?1:0; - ret += (getcell(pf, x+1, y )==alive)?1:0; - - ret += (getcell(pf, x-1, y+1)==alive)?1:0; - ret += (getcell(pf, x , y+1)==alive)?1:0; - ret += (getcell(pf, x+1, y+1)==alive)?1:0; +uint8_t countsurroundingalive(field_t pf, coord_t x, coord_t y){ + + static int8_t const offset[] = {-1, -1, 0, +1, +1, +1, 0, -1, -1, -1}; + uint8_t i, ret=0; + for (i = 8; i--;) + { + // getcell(...) returns either 0 or 1 + ret += getcell(pf,(XSIZE+x+offset[i+2])%XSIZE, (YSIZE+y+offset[i])%YSIZE); + } + return ret; } /******************************************************************************/ void nextiteration(field_t dest, field_t src){ - int x,y; + coord_t x,y; uint8_t tc; for(y=0; y