saved 404 bytes
This commit is contained in:
parent
7ae7e9f1e4
commit
04eee8849e
|
@ -23,6 +23,13 @@
|
||||||
#define XSIZE UNUM_COLS
|
#define XSIZE UNUM_COLS
|
||||||
#define YSIZE UNUM_ROWS
|
#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
|
* last line is for debug information
|
||||||
*/
|
*/
|
||||||
|
@ -49,10 +56,10 @@
|
||||||
//#define GLIDER_TEST
|
//#define GLIDER_TEST
|
||||||
|
|
||||||
#define BITSTUFFED
|
#define BITSTUFFED
|
||||||
#define LOOP_DETECT_BUFFER_SIZE 8
|
#define LOOP_DETECT_BUFFER_SIZE 8U
|
||||||
|
|
||||||
#ifndef GOL_DELAY
|
#ifndef GOL_DELAY
|
||||||
#define GOL_DELAY 1 /* milliseconds */
|
#define GOL_DELAY 250 /* milliseconds */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef GOL_CYCLES
|
#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
|
#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;
|
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];
|
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;
|
uint8_t t;
|
||||||
x = (x+XSIZE) % XSIZE;
|
|
||||||
y = (y+YSIZE) % YSIZE;
|
|
||||||
|
|
||||||
t = pf[x/8][y];
|
t = pf[x/8][y];
|
||||||
if(value==alive){
|
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){
|
static cell_t getcell(field_t pf, coord_t x, coord_t y){
|
||||||
x = (x+XSIZE) % XSIZE;
|
|
||||||
y = (y+YSIZE) % YSIZE;
|
|
||||||
|
|
||||||
return ((pf[x/8][y])&(1<<(x&7)))?alive:dead;
|
return ((pf[x/8][y])&(1<<(x&7)))?alive:dead;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
uint8_t countsurroundingalive(field_t pf, int x, int y){
|
uint8_t countsurroundingalive(field_t pf, coord_t x, coord_t y){
|
||||||
uint8_t ret=0;
|
|
||||||
ret += (getcell(pf, x-1, y-1)==alive)?1:0;
|
static int8_t const offset[] = {-1, -1, 0, +1, +1, +1, 0, -1, -1, -1};
|
||||||
ret += (getcell(pf, x , y-1)==alive)?1:0;
|
uint8_t i, ret=0;
|
||||||
ret += (getcell(pf, x+1, y-1)==alive)?1:0;
|
for (i = 8; i--;)
|
||||||
|
{
|
||||||
ret += (getcell(pf, x-1, y )==alive)?1:0;
|
// getcell(...) returns either 0 or 1
|
||||||
ret += (getcell(pf, x+1, y )==alive)?1:0;
|
ret += getcell(pf,(XSIZE+x+offset[i+2])%XSIZE, (YSIZE+y+offset[i])%YSIZE);
|
||||||
|
}
|
||||||
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;
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
void nextiteration(field_t dest, field_t src){
|
void nextiteration(field_t dest, field_t src){
|
||||||
int x,y;
|
coord_t x,y;
|
||||||
uint8_t tc;
|
uint8_t tc;
|
||||||
for(y=0; y<YSIZE; ++y){
|
for(y=0; y<YSIZE; ++y){
|
||||||
for(x=0; x<XSIZE; ++x){
|
for(x=0; x<XSIZE; ++x){
|
||||||
|
@ -166,10 +171,10 @@ void nextiteration(field_t dest, field_t src){
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
void printpf(field_t pf){
|
void printpf(field_t pf){
|
||||||
int x,y;
|
coord_t x,y;
|
||||||
for(y=0; y<YSIZE; ++y){
|
for(y=YSIZE; y--;){
|
||||||
for(x=0; x<XSIZE; ++x){
|
for(x=XSIZE; x--;){
|
||||||
setpixel((pixel){x,y},(getcell(pf,x,y)==alive)?3:0);
|
setpixel((pixel){x,y},getcell(pf,x,y)*3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -177,9 +182,9 @@ void printpf(field_t pf){
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
void pfcopy(field_t dest, field_t src){
|
void pfcopy(field_t dest, field_t src){
|
||||||
int x,y;
|
coord_t x,y;
|
||||||
for(y=0; y<YSIZE; ++y){
|
for(y=YSIZE; y--;){
|
||||||
for(x=0; x<XSIZE; ++x){
|
for(x=XSIZE; x--;){
|
||||||
setcell(dest,x,y,getcell(src,x,y));
|
setcell(dest,x,y,getcell(src,x,y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -187,10 +192,10 @@ void pfcopy(field_t dest, field_t src){
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
#ifndef BITSTUFFED
|
#ifndef BITSTUFFED
|
||||||
uint8_t pfcmp(field_t dest, field_t src){
|
coord_t pfcmp(field_t dest, field_t src){
|
||||||
int x,y;
|
coord_t x,y;
|
||||||
for(y=0; y<YSIZE; ++y){
|
for(y=YSIZE; y--;){
|
||||||
for(x=0; x<XSIZE; ++x){
|
for(x=XSIZE; x--;){
|
||||||
if (getcell(src,x,y) != getcell(dest,x,y))
|
if (getcell(src,x,y) != getcell(dest,x,y))
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -200,9 +205,10 @@ uint8_t pfcmp(field_t dest, field_t src){
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
uint8 pfempty(field_t src){ int x,y;
|
uint8 pfempty(field_t src){
|
||||||
for(y=0; y<YSIZE; ++y){
|
coord_t x,y;
|
||||||
for(x=0; x<XSIZE; ++x){
|
for(y=YSIZE; y--;){
|
||||||
|
for(x=XSIZE; x--;){
|
||||||
if (getcell(src,x,y)==alive)
|
if (getcell(src,x,y)==alive)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -213,7 +219,7 @@ uint8 pfempty(field_t src){ int x,y;
|
||||||
#else
|
#else
|
||||||
|
|
||||||
uint8_t pfcmp(field_t dest, field_t src){
|
uint8_t pfcmp(field_t dest, field_t src){
|
||||||
int x,y;
|
coord_t x,y;
|
||||||
for(y=0; y<FIELD_YSIZE; ++y){
|
for(y=0; y<FIELD_YSIZE; ++y){
|
||||||
for(x=0; x<FIELD_XSIZE; ++x){
|
for(x=0; x<FIELD_XSIZE; ++x){
|
||||||
if (src[x][y] != dest[x][y])
|
if (src[x][y] != dest[x][y])
|
||||||
|
@ -226,7 +232,7 @@ uint8_t pfcmp(field_t dest, field_t src){
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
uint8_t pfempty(field_t src){
|
uint8_t pfempty(field_t src){
|
||||||
int x,y;
|
coord_t x,y;
|
||||||
for(y=0; y<FIELD_YSIZE; ++y){
|
for(y=0; y<FIELD_YSIZE; ++y){
|
||||||
for(x=0; x<FIELD_XSIZE; ++x){
|
for(x=0; x<FIELD_XSIZE; ++x){
|
||||||
if (src[x][y]!=0)
|
if (src[x][y]!=0)
|
||||||
|
@ -258,20 +264,20 @@ int gameoflife(){
|
||||||
field_t pf1,pf2;
|
field_t pf1,pf2;
|
||||||
field_t ldbuf[LOOP_DETECT_BUFFER_SIZE]={{{0}}}; // loop detect buffer
|
field_t ldbuf[LOOP_DETECT_BUFFER_SIZE]={{{0}}}; // loop detect buffer
|
||||||
uint8_t ldbuf_idx=0;
|
uint8_t ldbuf_idx=0;
|
||||||
int x,y;
|
coord_t x,y;
|
||||||
uint16_t cycle;
|
uint16_t cycle;
|
||||||
|
|
||||||
//start:
|
//start:
|
||||||
/* initalise the field with random */
|
/* initalise the field with random */
|
||||||
for(y=0;y<YSIZE;++y){
|
for(y=YSIZE; y--;){
|
||||||
for(x=0;x<XSIZE; ++x){
|
for(x=XSIZE; x--;){
|
||||||
setcell(pf1,x,y,(random8()&1)?alive:dead);
|
setcell(pf1,x,y,(random8()&1)?alive:dead);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifdef GLIDER_TEST
|
#ifdef GLIDER_TEST
|
||||||
/* initialise with glider */
|
/* initialise with glider */
|
||||||
for(y=0;y<YSIZE;++y){
|
for(y=YSIZE; y--;){
|
||||||
for(x=0;x<XSIZE; ++x){
|
for(x=XSIZE; x--;){
|
||||||
setcell(pf1,x,y,dead);
|
setcell(pf1,x,y,dead);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue