Optimizations
This commit is contained in:
parent
a48026f9a1
commit
47af62fc17
105
gol_apple2.c
105
gol_apple2.c
|
@ -16,7 +16,7 @@ uint8_t toggle_cell( const uint8_t x, const uint8_t y ); /* toggles the cell at
|
|||
|
||||
void run( void ); /* runs the simulation */
|
||||
void update( void ); /* updates the simulation */
|
||||
uint8_t count_neighbours( const uint8_t x, const uint8_t y); /* counts nb neighbours of a cell */
|
||||
void count_neighbours( uint8_t* cell, uint8_t* count ); /* counts nb neighbours of the cell */
|
||||
|
||||
void quit( void );
|
||||
|
||||
|
@ -24,16 +24,18 @@ void quit( void );
|
|||
|
||||
#define NB_LINES 23
|
||||
#define NB_COLUMNS 40
|
||||
const uint8_t JUMP_BEGINNING_NEXT_LINE = NB_LINES - 2u;
|
||||
|
||||
#define ALIVE '0'
|
||||
#define DEAD ' '
|
||||
|
||||
#define ALIVE 1u
|
||||
#define DEAD 0u
|
||||
#define SPRITE_ALIVE '0'
|
||||
#define SPRITE_DEAD ' '
|
||||
|
||||
|
||||
/******************* STATIC GLOBAL VARIABLES ******************/
|
||||
|
||||
char Cells[ NB_COLUMNS ][ NB_LINES ];
|
||||
char Cells_Future[ NB_COLUMNS ][ NB_LINES ];
|
||||
uint8_t Cells[ NB_COLUMNS ][ NB_LINES ];
|
||||
uint8_t Cells_Future[ NB_COLUMNS ][ NB_LINES ];
|
||||
|
||||
|
||||
/******************** CODE ************************/
|
||||
|
@ -43,17 +45,17 @@ int main( int argc, char** argv )
|
|||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
|
||||
printf("HELLO");
|
||||
|
||||
|
||||
/* Initial state */
|
||||
memset( Cells, DEAD, sizeof(Cells) );
|
||||
init_display();
|
||||
|
||||
|
||||
/* go */
|
||||
draw_cells();
|
||||
run();
|
||||
|
||||
|
||||
quit();
|
||||
return 0;
|
||||
}
|
||||
|
@ -81,46 +83,44 @@ void init_display( void )
|
|||
cputcxy ( NB_COLUMNS-1u, NB_LINES-1u,'+');
|
||||
}
|
||||
|
||||
|
||||
|
||||
void draw_cells( void )
|
||||
{
|
||||
#define KEY_LEFT 'j'
|
||||
#define KEY_DOWN 'k'
|
||||
#define KEY_UP 'i'
|
||||
#define KEY_RIGHT 'l'
|
||||
|
||||
|
||||
uint8_t quit = 0;
|
||||
uint8_t ch;
|
||||
|
||||
|
||||
uint8_t x = NB_COLUMNS >> 1u;
|
||||
uint8_t y = NB_LINES >> 1u;
|
||||
gotoxy(x,y);
|
||||
|
||||
|
||||
while ( quit == 0)
|
||||
{
|
||||
cursor(1);
|
||||
ch = cgetc();
|
||||
switch (ch) {
|
||||
case KEY_LEFT:
|
||||
cursor(1);
|
||||
ch = cgetc();
|
||||
switch (ch) {
|
||||
case KEY_LEFT:
|
||||
if( x > 1u ) { gotoxy( --x, y ); }
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
if( y < NB_LINES-2u ) { gotoxy( x, ++y ); }
|
||||
break;
|
||||
case KEY_UP:
|
||||
break;
|
||||
case KEY_UP:
|
||||
if( y > 1u ) { gotoxy( x, --y ); }
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
if( x < NB_COLUMNS-2u ) { gotoxy( ++x, y ); }
|
||||
break;
|
||||
case ' ':
|
||||
break;
|
||||
case ' ':
|
||||
x = toggle_cell( x, y );
|
||||
break;
|
||||
case 'q':
|
||||
quit = 1;
|
||||
break;
|
||||
}
|
||||
case 'q':
|
||||
quit = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Cells was updated by the calls to toggle() */
|
||||
|
@ -131,15 +131,16 @@ void draw_cells( void )
|
|||
|
||||
uint8_t toggle_cell( const uint8_t x, const uint8_t y )
|
||||
{
|
||||
char* cell;
|
||||
if( x == 0u || x >= NB_COLUMNS-1u || y == 0u || y >= NB_LINES-1u ) { return x; }
|
||||
char* cell;
|
||||
if( x == 0u || x >= NB_COLUMNS-1u || y == 0u || y >= NB_LINES-1u ) { return x; }
|
||||
cell = &Cells[x][y];
|
||||
if( *cell == DEAD ) {
|
||||
*cell = ALIVE;
|
||||
cputc( SPRITE_ALIVE );
|
||||
} else {
|
||||
*cell = DEAD;
|
||||
cputc( SPRITE_DEAD );
|
||||
}
|
||||
cputc( *cell ); /* shift the cursor */
|
||||
return wherex ();
|
||||
}
|
||||
|
||||
|
@ -148,13 +149,13 @@ void run( void )
|
|||
{
|
||||
char str_nb_iteration [5];
|
||||
uint16_t nb_iterations = 2u;
|
||||
char ch = '\0';
|
||||
char ch = '\0';
|
||||
cursor(0);
|
||||
|
||||
gotoxy( 0u, NB_LINES );
|
||||
printf("Iteration: 1");
|
||||
while( ch != 'q')
|
||||
{
|
||||
{
|
||||
update();
|
||||
gotoxy(11u, NB_LINES);
|
||||
printf( itoa(nb_iterations++, str_nb_iteration, 10) );
|
||||
|
@ -170,16 +171,18 @@ void update( void )
|
|||
{
|
||||
for( x = 1u; x < NB_COLUMNS-1; ++x)
|
||||
{
|
||||
nb_neighbours = count_neighbours(x,y);
|
||||
if( Cells[x][y] == ALIVE && \
|
||||
(nb_neighbours < 2u || nb_neighbours > 3u )
|
||||
) {
|
||||
register uint8_t nb_neighbours;
|
||||
uint8_t* cell = &Cells[x-1u][y-1u];
|
||||
count_neighbours( cell, &nb_neighbours );
|
||||
if( Cells[x][y] == ALIVE && \
|
||||
(nb_neighbours < 2u || nb_neighbours > 3u )
|
||||
) {
|
||||
Cells_Future[x][y] = DEAD;
|
||||
cputcxy( x, y, DEAD );
|
||||
cputcxy( x, y, SPRITE_DEAD );
|
||||
}
|
||||
else if( Cells[x][y] == DEAD && nb_neighbours == 3u ) {
|
||||
Cells_Future[x][y] = ALIVE;
|
||||
cputcxy( x, y, ALIVE );
|
||||
cputcxy( x, y, SPRITE_ALIVE );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -187,17 +190,11 @@ void update( void )
|
|||
}
|
||||
|
||||
|
||||
uint8_t count_neighbours( const uint8_t x, const uint8_t y)
|
||||
void count_neighbours( uint8_t* cell, uint8_t* count )
|
||||
{
|
||||
uint8_t count = 0;
|
||||
if( Cells[x-1][y-1] == ALIVE ) { ++count; }
|
||||
if( Cells[x-1][y] == ALIVE ) { ++count; }
|
||||
if( Cells[x-1][y+1] == ALIVE ) { ++count; }
|
||||
if( Cells[x][y-1] == ALIVE ) { ++count; }
|
||||
if( Cells[x][y+1] == ALIVE ) { ++count; }
|
||||
if( Cells[x+1][y-1] == ALIVE ) { ++count; }
|
||||
if( Cells[x+1][y] == ALIVE ) { ++count; }
|
||||
if( Cells[x+1][y+1] == ALIVE ) { ++count; }
|
||||
return count;
|
||||
*count = *cell++; *count += *cell++; *count += *cell;
|
||||
cell += JUMP_BEGINNING_NEXT_LINE;
|
||||
*count += *cell; cell+=2 ; *count += *cell;
|
||||
cell += JUMP_BEGINNING_NEXT_LINE;
|
||||
*count += *cell++; *count += *cell++; *count += *cell;
|
||||
}
|
||||
|
||||
|
|
66
gol_linux.c
66
gol_linux.c
|
@ -16,23 +16,25 @@ void toggle_cell( uint8_t x, uint8_t y ); /* toggles the cell at the given coo
|
|||
|
||||
void run( void ); /* runs the simulation */
|
||||
void update( void ); /* updates the simulation */
|
||||
uint8_t count_neighbours( const uint8_t x, const uint8_t y); /* counts nb neighbours of a cell */
|
||||
void count_neighbours( uint8_t* cell, uint8_t* count ); /* counts nb neighbours of the cell */
|
||||
|
||||
|
||||
/******************* CUSTOM TYPES AND VALUES DEFINITIONS ****************/
|
||||
|
||||
#define NB_LINES 24
|
||||
#define NB_LINES 23
|
||||
#define NB_COLUMNS 40
|
||||
|
||||
#define ALIVE '0'
|
||||
#define DEAD ' '
|
||||
#define ALIVE 1u
|
||||
#define DEAD 0u
|
||||
#define SPRITE_ALIVE '0'
|
||||
#define SPRITE_DEAD ' '
|
||||
|
||||
|
||||
|
||||
/******************* STATIC GLOBAL VARIABLES ******************/
|
||||
|
||||
char Cells[ NB_COLUMNS ][ NB_LINES ];
|
||||
char Cells_Future[ NB_COLUMNS ][ NB_LINES ];
|
||||
uint8_t Cells[ NB_COLUMNS ][ NB_LINES ];
|
||||
uint8_t Cells_Future[ NB_COLUMNS ][ NB_LINES ];
|
||||
|
||||
|
||||
/******************** CODE ************************/
|
||||
|
@ -45,7 +47,7 @@ int main( void )
|
|||
/* Initial state */
|
||||
memset( Cells, DEAD, sizeof(Cells) );
|
||||
init_display();
|
||||
|
||||
|
||||
/* go */
|
||||
draw_cells();
|
||||
run();
|
||||
|
@ -66,7 +68,7 @@ void init_display( void )
|
|||
intrflush(stdscr, FALSE);
|
||||
keypad(stdscr, TRUE);
|
||||
nodelay(stdscr, TRUE);
|
||||
|
||||
|
||||
/* Init displayed playfield */
|
||||
uint8_t i;
|
||||
for( i = 0u; i < NB_COLUMNS; ++i ) {
|
||||
|
@ -87,11 +89,11 @@ void init_display( void )
|
|||
|
||||
void draw_cells( void )
|
||||
{
|
||||
int quit = 0;
|
||||
uint8_t quit = 0u;
|
||||
|
||||
int ch;
|
||||
int y = NB_LINES / 2;
|
||||
int x = NB_COLUMNS / 2;
|
||||
uint8_t ch;
|
||||
uint8_t y = NB_LINES >> 1;
|
||||
uint8_t x = NB_COLUMNS >> 1;
|
||||
|
||||
move(y,x);
|
||||
refresh();
|
||||
|
@ -101,16 +103,16 @@ void draw_cells( void )
|
|||
getyx(stdscr, y, x);
|
||||
ch = getch();
|
||||
switch (ch) {
|
||||
case KEY_LEFT:
|
||||
case 'j':
|
||||
if( x > 1u ) { move(y, --x); }
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
case 'k':
|
||||
if( y < NB_LINES - 2u ) { move(++y, x); }
|
||||
break;
|
||||
case KEY_UP:
|
||||
case 'i':
|
||||
if( y > 1u ) { move(--y, x); }
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
case 'l':
|
||||
if( x < NB_COLUMNS - 2u ) { move(y, ++x); }
|
||||
break;
|
||||
case ' ':
|
||||
|
@ -133,20 +135,21 @@ void draw_cells( void )
|
|||
void toggle_cell( const uint8_t x, const uint8_t y )
|
||||
{
|
||||
if( x == 0u || x >= NB_COLUMNS-1 || y == 0u || y >= NB_LINES-1 ) { return; }
|
||||
char* cell = &Cells[x][y];
|
||||
uint8_t* cell = &Cells[x][y];
|
||||
if( *cell == DEAD ) {
|
||||
*cell = ALIVE;
|
||||
mvaddch(y,x,SPRITE_ALIVE);
|
||||
} else {
|
||||
*cell = DEAD;
|
||||
mvaddch(y,x,SPRITE_DEAD);
|
||||
}
|
||||
mvaddch(y,x,*cell);
|
||||
refresh();
|
||||
}
|
||||
|
||||
|
||||
void run( void )
|
||||
{
|
||||
#define PERIOD_MS 500000u
|
||||
#define PERIOD_MS 200000u
|
||||
curs_set(0);
|
||||
char ch = '\0';
|
||||
while( ch != 'q')
|
||||
|
@ -165,16 +168,18 @@ void update( void )
|
|||
{
|
||||
for( x = 1u; x < NB_COLUMNS-1; ++x)
|
||||
{
|
||||
uint8_t nb_neighbours = count_neighbours(x,y);
|
||||
uint8_t nb_neighbours;
|
||||
uint8_t* cell = &Cells[x-1u][y-1u];
|
||||
count_neighbours( cell, &nb_neighbours );
|
||||
if( Cells[x][y] == ALIVE && \
|
||||
(nb_neighbours < 2u || nb_neighbours > 3u )
|
||||
) {
|
||||
Cells_Future[x][y] = DEAD;
|
||||
mvaddch(y,x,DEAD);
|
||||
mvaddch(y,x,SPRITE_DEAD);
|
||||
}
|
||||
else if( Cells[x][y] == DEAD && nb_neighbours == 3u ) {
|
||||
Cells_Future[x][y] = ALIVE;
|
||||
mvaddch(y,x,ALIVE);
|
||||
mvaddch(y,x,SPRITE_ALIVE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -183,18 +188,13 @@ void update( void )
|
|||
}
|
||||
|
||||
|
||||
uint8_t count_neighbours( const uint8_t x, const uint8_t y)
|
||||
void count_neighbours( uint8_t* cell, uint8_t* count )
|
||||
{
|
||||
uint8_t count = 0;
|
||||
if( Cells[x-1][y-1] == ALIVE ) { ++count; }
|
||||
if( Cells[x-1][y] == ALIVE ) { ++count; }
|
||||
if( Cells[x-1][y+1] == ALIVE ) { ++count; }
|
||||
if( Cells[x][y-1] == ALIVE ) { ++count; }
|
||||
if( Cells[x][y+1] == ALIVE ) { ++count; }
|
||||
if( Cells[x+1][y-1] == ALIVE ) { ++count; }
|
||||
if( Cells[x+1][y] == ALIVE ) { ++count; }
|
||||
if( Cells[x+1][y+1] == ALIVE ) { ++count; }
|
||||
return count;
|
||||
*count = *cell++; *count += *cell++; *count += *cell;
|
||||
cell += NB_LINES - 2u;
|
||||
*count += *cell; cell+=2 ; *count += *cell;
|
||||
cell += NB_LINES - 2u;
|
||||
*count += *cell++; *count += *cell++; *count += *cell;
|
||||
}
|
||||
|
||||
static void finish(int sig)
|
||||
|
|
Loading…
Reference in New Issue