From add17d025733bb70e05359604c8369d718488032 Mon Sep 17 00:00:00 2001 From: Christophe Meneboeuf Date: Thu, 15 Sep 2016 23:43:00 +0200 Subject: [PATCH] Adding the state machine --- gol_apple2.c | 133 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 42 deletions(-) diff --git a/gol_apple2.c b/gol_apple2.c index 2ab2b3c..80b360a 100755 --- a/gol_apple2.c +++ b/gol_apple2.c @@ -9,8 +9,9 @@ /******************* FUNCTION DEFINITIONS **************/ -void init_display( void ); /* Inits displayed playfield */ -void draw_cells( void ); /* lets the user draw some starting cells */ +void init_display( void ); /* Inits displayed playfield */ +void draw_cells( void ); /* Draws the actual cells */ +void editor( void ); /* lets the user draw some starting cells */ uint8_t toggle_cell( const uint8_t x, const uint8_t y ); /* toggles the cell at the given coordinates. \ Returns the cursor X position */ @@ -28,15 +29,24 @@ const uint8_t JUMP_BEGINNING_NEXT_LINE = NB_LINES - 2u; #define ALIVE 1u #define DEAD 0u -#define SPRITE_ALIVE '0' -#define SPRITE_DEAD ' ' +#define SPRITE_ALIVE '0' +#define SPRITE_DEAD ' ' + +#define STATE_INIT 0u +#define STATE_EDITOR 1u +#define STATE_RUN 2u +#define STATE_QUIT 3u +uint8_t State = STATE_INIT; + +#define NO_KEY '\0' +char KeyPressed = NO_KEY; /******************* STATIC GLOBAL VARIABLES ******************/ uint8_t Cells[ NB_COLUMNS ][ NB_LINES ]; uint8_t Cells_Future[ NB_COLUMNS ][ NB_LINES ]; - +uint8_t Cells_Initial[ NB_COLUMNS ][ NB_LINES ]; /******************** CODE ************************/ @@ -46,15 +56,37 @@ 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(); + /* Running the state machine */ + while( State != STATE_QUIT ) + { + switch (State) { + case STATE_INIT: + memset( Cells, DEAD, sizeof(Cells) ); + init_display(); + State = STATE_EDITOR; + break; + case STATE_EDITOR: + editor(); + State = STATE_RUN; + break; + case STATE_RUN: + run(); + if( KeyPressed == 'e' ) { /* Go back to editor */ + State = STATE_EDITOR; + } else if( KeyPressed == 'r' ) { /* reset and rerun */ + memcpy( Cells, Cells_Initial, sizeof(Cells) ); + memcpy( Cells_Future, Cells_Initial, sizeof(Cells_Future) ); + draw_cells(); + } else if( KeyPressed == 'q' ) { /* quit */ + State = STATE_QUIT; + } + break; + default: + printf("ERROR!"); + State = STATE_QUIT; + break; + } + } quit(); return 0; @@ -83,7 +115,24 @@ void init_display( void ) cputcxy ( NB_COLUMNS-1u, NB_LINES-1u,'+'); } -void draw_cells( void ) + +void draw_cells( void ) { + uint8_t x, y; + for( x = 1u; x < NB_COLUMNS - 1u; ++x ) + { + for( y = 1u; y < NB_LINES - 1u; ++y ) + { + if( Cells[x][y] == ALIVE ) { + cputcxy ( x, y, SPRITE_ALIVE ); + } else { + cputcxy ( x, y, SPRITE_DEAD ); + } + } + } +} + + +void editor( void ) { #define KEY_LEFT 'j' #define KEY_DOWN 'k' @@ -91,7 +140,6 @@ void draw_cells( void ) #define KEY_RIGHT 'l' uint8_t quit = 0; - uint8_t ch; uint8_t x = NB_COLUMNS >> 1u; uint8_t y = NB_LINES >> 1u; @@ -99,33 +147,33 @@ void draw_cells( void ) while ( quit == 0) { - cursor(1); - ch = cgetc(); - switch (ch) { - case KEY_LEFT: - if( x > 1u ) { gotoxy( --x, y ); } - break; - case KEY_DOWN: - if( y < NB_LINES-2u ) { gotoxy( x, ++y ); } - break; - case KEY_UP: - if( y > 1u ) { gotoxy( x, --y ); } - break; - case KEY_RIGHT: - if( x < NB_COLUMNS-2u ) { gotoxy( ++x, y ); } - break; - case ' ': - x = toggle_cell( x, y ); - break; - case 'q': - quit = 1; - break; - } + cursor(1); + KeyPressed = cgetc(); + switch (KeyPressed) { + case KEY_LEFT: + if( x > 1u ) { gotoxy( --x, y ); } + break; + case KEY_DOWN: + if( y < NB_LINES-2u ) { gotoxy( x, ++y ); } + break; + case KEY_UP: + if( y > 1u ) { gotoxy( x, --y ); } + break; + case KEY_RIGHT: + if( x < NB_COLUMNS-2u ) { gotoxy( ++x, y ); } + break; + case ' ': + x = toggle_cell( x, y ); + break; + case 'q': + quit = 1; + break; + } } /* Cells was updated by the calls to toggle() */ memcpy( Cells_Future, Cells, sizeof(Cells_Future) ); - + memcpy( Cells_Initial, Cells, sizeof(Cells_Initial) ); } @@ -149,12 +197,12 @@ void run( void ) { char str_nb_iteration [5]; uint16_t nb_iterations = 2u; - char ch = '\0'; + KeyPressed = NO_KEY; cursor(0); gotoxy( 0u, NB_LINES ); printf("Iteration: 1"); - while( ch != 'q') + while( KeyPressed == NO_KEY) { /* Evolving the cells */ update(); @@ -163,7 +211,8 @@ void run( void ) printf( itoa(nb_iterations++, str_nb_iteration, 10) ); /* Testing key pressed */ if( kbhit() ){ - ch = cgetc(); + KeyPressed = cgetc(); + break; } } }