static void insertglider(field_t pf) { #ifndef BITSTUFFED /* * # * # * ### */ setcell(pf, 1, 0, alive); setcell(pf, 2, 1, alive); setcell(pf, 0, 2, alive);setcell(pf, 1, 2, alive);setcell(pf, 2, 2, alive); #else pf[0][0] |= 0x02; // # pf[1][0] |= 0x04; // # pf[2][0] |= 0x07; // ### #endif }
void nextiteration(field_t dest, field_t src) { coord_t x, y; uint8_t tc; for (y = YSIZE; y--;) { for (x = XSIZE; x--;) { cell_t cell; tc = countsurroundingalive(src, x, y); switch (tc) { case 2: /* keep */ continue; break; case 3: /* alive */ cell = alive; break; default: /* dead */ cell = dead; break; } setcell(dest, x, y, cell); } } }
void gameoflife() { DEBUG_BYTE(0,0); // set debug bytes to zero DEBUG_BYTE(1,0); field_t pf1, pf2; field_t ldbuf[LOOP_DETECT_BUFFER_SIZE] = {{{0}}}; // loop detect buffer uint8_t ldbuf_idx = 0; uint16_t cycle; #ifdef GLIDER_TEST /* initialize with glider */ coord_t x,y; for(y = YSIZE; y--;) { for(x = XSIZE; x--;) { setcell(pf1, x, y, dead); } } insertglider(pf1); #else /* initialize the field with random */ pfinit(pf1); #endif /* the main part */ pfprint(pf1); for (cycle = 1; cycle < GOL_CYCLES; ++cycle) { DEBUG_BYTE(0, (uint8_t)(GOL_CYCLES-cycle) & 0xff); DEBUG_BYTE(1, SREG); wait(GOL_DELAY); pfcopy(pf2, pf1); nextiteration(pf1, pf2); pfprint(pf1); /* loop detection */ if (!pfcmp(pf1, pf2)) { insertglider(pf1); // cycle = 1; } if (pfempty(pf1)) { /* kill game */ return; } /* */ uint8_t i; for (i = 0; i < LOOP_DETECT_BUFFER_SIZE; ++i) { if (!pfcmp(pf1, ldbuf[i])) { insertglider(pf1); // cycle = 1; } } pfcopy(ldbuf[ldbuf_idx], pf1); ldbuf_idx = (ldbuf_idx + 1u) % LOOP_DETECT_BUFFER_SIZE; } }
static void pfinit(field_t pf) { coord_t x, y; #ifndef BITSTUFFED for (y = YSIZE; y--;) { for (x = XSIZE; x--;) { setcell(pf, x, y, (random8() & 1) ? alive : dead); } } #else for (y = 0; y < FIELD_YSIZE; ++y) { for (x = 0; x < FIELD_XSIZE; ++x) { pf[y][x] = random8(); } } #endif }
int GetLongOption::parse(int argc, char *const *argv) { int my_optind = 1; pname = basename(*argv); enroll_done = 1; if (argc-- <= 1) return my_optind; while (argc >= 1) { char *token = *++argv; --argc; // '--' signifies end of options if followed by space if (token[0] != optmarker || (token[1] == optmarker && strlen(token) == 2)) break; /* end of options */ ++my_optind; char *tmptoken = ++token; if (token[0] == optmarker) // Handle a double '--' tmptoken = ++token; while (*tmptoken && *tmptoken != '=') ++tmptoken; /* (tmptoken - token) is now equal to the command line option length. */ Cell *t; enum { NoMatch, ExactMatch, PartialMatch, MultipleMatch } matchStatus = NoMatch; std::ostringstream errmsg; Cell * pc = 0; // pointer to the partially-matched cell for (t = table; t != 0; t = t->next) { if (strncmp(t->option, token, (tmptoken - token)) == 0) { if ((int)strlen(t->option) == (tmptoken - token)) { /* an exact match found */ int stat = setcell(t, tmptoken, *(argv + 1), pname); if (stat == -1) return -1; else if (stat == 1) { ++argv; --argc; ++my_optind; } matchStatus = ExactMatch; break; } else { /* partial match found */ if (pc == 0) { matchStatus = PartialMatch; pc = t; } else { // Multiple partial matches...Print warning if (matchStatus == PartialMatch) { // First time, print the message header and the first // matched duplicate... errmsg << "ERROR: " << pname << ": Multiple matches found for option '" << optmarker << strtok(token, "= ") << "'.\n"; errmsg << "\t" << optmarker << pc->option << ": " << pc->description << "\n"; } errmsg << "\t" << optmarker << t->option << ": " << t->description << "\n"; matchStatus = MultipleMatch; } } } /* end if */ } /* end for */ if (matchStatus == PartialMatch) { int stat = setcell(pc, tmptoken, *(argv + 1), pname); if (stat == -1) return -1; else if (stat == 1) { ++argv; --argc; ++my_optind; } } else if (matchStatus == NoMatch) { std::cerr << pname << ": unrecognized option "; std::cerr << optmarker << strtok(token, "= ") << "\n"; return -1; /* no match */ } else if (matchStatus == MultipleMatch) { std::cerr << errmsg.str(); return -1; /* no match */ } } /* end while */ return my_optind; }
int GetLongOption::parse(char *const str, char *const p) { enroll_done = 1; char * token = strtok(str, " \t"); const char *name = p ? p : "GetLongOption"; while (token) { if (token[0] != optmarker || (token[1] == optmarker && strlen(token) == 2)) { std::cerr << name << ": nonoptions not allowed\n"; return -1; /* end of options */ } char *ladtoken = 0; /* lookahead token */ char *tmptoken = ++token; while (*tmptoken && *tmptoken != '=') ++tmptoken; /* (tmptoken - token) is now equal to the command line option length. */ Cell *t; enum { NoMatch, ExactMatch, PartialMatch } matchStatus = NoMatch; Cell *pc = 0; // pointer to the partially-matched cell for (t = table; t != 0; t = t->next) { if (strncmp(t->option, token, (tmptoken - token)) == 0) { if ((int)strlen(t->option) == (tmptoken - token)) { /* an exact match found */ ladtoken = strtok(0, " \t"); int stat = setcell(t, tmptoken, ladtoken, name); if (stat == -1) return -1; else if (stat == 1) { ladtoken = 0; } matchStatus = ExactMatch; break; } else { /* partial match found */ matchStatus = PartialMatch; pc = t; } } /* end if */ } /* end for */ if (matchStatus == PartialMatch) { ladtoken = strtok(0, " \t"); int stat = setcell(pc, tmptoken, ladtoken, name); if (stat == -1) return -1; else if (stat == 1) { ladtoken = 0; } } else if (matchStatus == NoMatch) { std::cerr << name << ": unrecognized option "; std::cerr << optmarker << strtok(token, "= ") << "\n"; return -1; /* no match */ } token = ladtoken ? ladtoken : strtok(0, " \t"); } /* end while */ return 1; }
int main(int argc, char* argv[]) { struct consolefontdesc dsc; int tty; int x, y; unsigned int ucs2; unsigned char b; char root = false; root = (argc > 1 && strcmp(argv[1], "root") == 0); if (root) { /* obtain permissions to access SEQUENCER */ ioperm(SEQ_IDX, 1, 1); ordie("ioperm (1)"); ioperm(SEQ_DATA, 1, 1); ordie("ioperm (2)"); } tty = open("/dev/tty", O_RDWR); ordie("open"); dsc.charcount = 256; dsc.charheight = 16; /* clear image */ system("setterm -foreground black -background black"); system("clear"); system("setterm -foreground white -background black -bold on"); printf("\033%%G"); /* turn on utf-8 */ if (root) { /* set width of chars to 8 pixels */ outb(1, SEQ_IDX); b = inb(SEQ_DATA); outb(1, SEQ_IDX); outb(b | 0x01, SEQ_DATA); } for (y=0; y<16; y++) { for (x=0; x<16; x++) { /* convert UCS-2 -> UTF-8 */ ucs2 = 0xF000 + y*16 + x; putchar(0xe0 | (ucs2 >> 12)); putchar(0x80 | ((ucs2 >> 6) & 0x3f)); putchar(0x80 | (ucs2 & 0x3f)); /* copy piece of image to font data */ setcell(x, y); } printf("\n"); } printf("\033%%@"); /* turn off UTF-8 */ /* save original font */ dsc.chardata = (void*)font_sav; ioctl(tty, GIO_FONTX, &dsc); ordie("GIO_FONTX"); /* set new font -- our image */ dsc.chardata = (void*)font; ioctl(tty, PIO_FONTX, &dsc); ordie("PIO_FONTX"); /* wait for user */ getchar(); /* restore original font */ dsc.chardata = (void*)font_sav; ioctl(tty, PIO_FONTX, &dsc); ordie("PIO_FONTX (2)"); close(tty); ordie("close"); system("setterm -foreground default -background default -bold off"); system("clear"); if (root) { /* set back width of chars to 9 pixels */ outb(1, SEQ_IDX); b = inb(SEQ_DATA); outb(1, SEQ_IDX); outb(b & 0xE, SEQ_DATA); } return 0; }
int main( int argc, char* args[] ) { //get a random seed. srand(time(NULL)); //mouse variables and cell types int x, y, sleepTime = 0, countVar = 0; //mouse is held variables int mouseStatusLeft = 0, mouseStatusRight = 0; //make sure the program waits for a quit int quit = false; //Initialize if( init() == false ) return 1; //Load the files if( load_files() == false ) return 2; //Update the screen //if( SDL_Flip( screen ) == -1 ) return 3; //if(SDL_RenderPresent( screen ) == -1 ) return 3; //initialize the cell stuff. This gets the cell system up and running. This also sets all cells to m_air and all the saturation to m_no_saturaion init_cell_stuff(); /* CELL_SIZE = 4; int i;// //putting test materials into grid for(i=0; i<GRID_WIDTH; i++){ if(get_rand(1,4)==1) grid[i+camera_x][GRID_HEIGHT-1-get_rand(7,15)+camera_y].mat = m_plant_root; if(get_rand(1,10)==10) grid[i+camera_x][camera_y+get_rand(20,35)].mat = m_spring; grid[i+camera_x][camera_y+get_rand(30,34)+30].mat = m_earth; grid[i+camera_x][camera_y+get_rand(30,34)+30].mat = m_earth; grid[i+camera_x][camera_y+get_rand(30,34)+30].mat = m_earth; } */ CELL_SIZE = 8; sleepTime = 0; //int i;// //putting test materials into grid /* for(i=7 ; i<GRID_WIDTH ; i+=15){ for(j=26 ; j<GRID_HEIGHT ; j+=20){ grid[i][j].mat = m_anti_scurge; } } */ //-------------------------------------- //for(i=0 ; i<GRID_WIDTH*GRID_HEIGHT / 2 ; i++) // grid[get_rand(0,GRID_WIDTH-1)][get_rand(0,GRID_HEIGHT-1)].mat = m_plant; //int keyw=0, keya=0, keys=0, keyd=0; //unsigned lastPanTime = 0; bool alt = 0; // this keeps track of the state of the alt key //these variables store the position of the user's cursor at the moment that the user decided to pan the screen int mouse_x_when_pan=0; int mouse_y_when_pan=0; // these are for keeping track of the motion of the mouse when the user is panning. these keep the panning from being really jumpy. // without them, there is a truncation error when adjusting the camera_x and camera_y materials and re-setting the mouse coordinates to its original state. // these keep track of the truncation error and add it to the next operation so that the mouse motion feels fluid. int remainder_panning_motion_x=0; int remainder_panning_motion_y=0; //last minute verification that the initial start up values for the grid size and the camera positions are valid. verify_grid_and_cell_size(); verify_camera(); //While the user hasn't quit while(1){ //While there's an event to handle while( SDL_PollEvent( &event ) ){ //If the user has Xed out the window if( event.type == SDL_QUIT || quit == true ){ //Quit the program clean_up(); return 0; } if( event.type == SDL_MOUSEBUTTONDOWN ){ /// mouse down x = event.motion.x; y = event.motion.y; if( event.button.button == SDL_BUTTON_LEFT ){ mouseStatusLeft = 1; } else if( event.button.button == SDL_BUTTON_RIGHT ){ mouseStatusRight = 1; } } else if(event.type == SDL_MOUSEWHEEL){ if(event.wheel.y > 0) zoom_in(x,y); if(event.wheel.y < 0) zoom_out(x,y); } else if(event.type == SDL_MOUSEBUTTONUP){ /// mouse up x = event.motion.x; y = event.motion.y; if( event.button.button == SDL_BUTTON_LEFT ){ mouseStatusLeft = 0; } else if( event.button.button == SDL_BUTTON_RIGHT ){ mouseStatusRight = 0; } } else if( event.type == SDL_MOUSEMOTION ){ /// mouse motion x = event.motion.x; y = event.motion.y; // if the alt key (camera panning key) is down and the coordinates have changed, then let the screen be panned! if(alt && x != mouse_x_when_pan && y != mouse_y_when_pan){ // this adjusts the x-axis camera (this scales with the CELL_SIZE) camera_x += (x-mouse_x_when_pan+remainder_panning_motion_x)/CELL_SIZE; camera_y += (y-mouse_y_when_pan+remainder_panning_motion_y)/CELL_SIZE; //calculate the remainders of the mouse motion. // these values represent the motion of the mouse that is not utilized by the discrete nature of the operation on the camera_x and camera_y values. remainder_panning_motion_x = (x-mouse_x_when_pan+remainder_panning_motion_x) - (int)((x-mouse_x_when_pan+remainder_panning_motion_x)/CELL_SIZE)*CELL_SIZE; remainder_panning_motion_y = (y-mouse_y_when_pan+remainder_panning_motion_y) - (int)((y-mouse_y_when_pan+remainder_panning_motion_y)/CELL_SIZE)*CELL_SIZE; // make sure the camera is not out of bounds. verify_camera(); //reset the user's curcor position to the original position the curcor was in when the user started panning the camera SDL_WarpMouseInWindow(window, mouse_x_when_pan, mouse_y_when_pan); } } else if(event.type == SDL_WINDOWEVENT_RESIZED ){ /// window resize //float new_cell_size = CELL_SIZE * event.resize.h/((float)SCREEN_HEIGHT); // adjust the pixel size. float new_cell_size = CELL_SIZE * event.window.data1/((float)SCREEN_HEIGHT); // adjust the pixel size. if(new_cell_size - ((int)new_cell_size) >= 0.5f) CELL_SIZE = new_cell_size + 1; else CELL_SIZE = new_cell_size; //SCREEN_WIDTH = event.resize.w; //SCREEN_HEIGHT = event.resize.h; SCREEN_WIDTH = event.window.data1; SCREEN_HEIGHT = event.window.data2; verify_grid_and_cell_size(); // make sure the window isn't too big for the cell size //set_window_size(event.resize.w, event.resize.h); set_window_size(event.window.data1, event.window.data2); verify_camera(); } if( event.type == SDL_KEYDOWN ){ ///keyboard event switch( event.key.keysym.sym ){ case SDLK_UP: break; //change block type up case SDLK_DOWN: break; // change block type down case SDLK_c: reset_cells(); break;//clear the screen case SDLK_p: print_saturation_data(); break; // prints the cellSat[][] array to stdout. this is for debuggin purposes. case SDLK_r: randomize_grid(); break; // randomize grid case SDLK_LEFT: if(paused != 1) {sleepTime /= 2;} break; //speeds up the game case SDLK_RIGHT: if(paused != 1) {if(sleepTime == 0){sleepTime = 1;} {sleepTime *= 2;} if(sleepTime > 2000) {sleepTime = 2000;}} break; //slows down the game case SDLK_SPACE: if(paused == 0) {paused = 1;} else if(paused == 1) {paused = 0;} break; //pause the game case SDLK_ESCAPE: quit = true; // quit with escape case SDLK_w: gen_world(w_normal,0); break; // generate a world /* case SDLK_w: keyw=1; break; // store key state case SDLK_a: keya=1; break; case SDLK_s: keys=1; break; case SDLK_d: keyd=1; break; */ case SDLK_LALT: case SDLK_RALT: // store the state of the alt key. alt = 1; // turn off the cursor SDL_ShowCursor(SDL_DISABLE); // store the position of the mouse. this will allow the program to make the cursor stay stationary mouse_x_when_pan = x; mouse_y_when_pan = y; //reset the remainder mouse motion variables for x and y. remainder_panning_motion_x = 0; remainder_panning_motion_y = 0; break; default: break; } } if( event.type == SDL_KEYUP ){ ///keyboard event switch( event.key.keysym.sym ){ /* case SDLK_w: keyw=0; break;//lastPanTime=0; break; // store key state case SDLK_a: keya=0; break;//lastPanTime=0; break; case SDLK_s: keys=0; break;//lastPanTime=0; break; case SDLK_d: keyd=0; break;//lastPanTime=0; break; */ case SDLK_LALT: case SDLK_RALT: // show the cursor again. SDL_ShowCursor(SDL_ENABLE); alt = 0; break; default: break; } } } // end while(event) //no more events to handle at the moment. /* //this handles time-controlled panning if(SDL_GetTicks() - MIN_PAN_INTERVAL > lastPanTime){ if(keyw) pan(D_UP); if(keya) pan(D_LEFT); if(keys) pan(D_DOWN); if(keyd) pan(D_RIGHT); lastPanTime = SDL_GetTicks(); //#if (DEBUG_GRIDSIM) // printf("\nlastPanTime = %d\n", lastPanTime); //#endif } */ //checks if the mouse is held or not if(mouseStatusLeft == 1 && mouseModifier == 0){ //make sure the mouse isn't inside either of the two GUIs. if(y >= 50 && x < SCREEN_WIDTH - 200) setcell(x, y, currentMat); } else if(mouseStatusRight == 1 && mouseModifier == 0){ deletecell(x, y, currentMat); } //speed of gameplay countVar++; if(sleepTime <= 0) { sleepTime = 0; } //evealuate cells if(countVar >= sleepTime && paused != 1){ evaluate_grid(); countVar = 0; } //updates screen with cells print_cells(); //displays selection gui selectionGUI(x, y, mouseStatusLeft); //displays brushes and misc gui brushesGUI(x, y, mouseStatusLeft); // If the user is not panning, then it is fine to show the cursor. if(!alt){ //displays cursor cursorDisplay(x, y); } //draw_line(screen, 300, 300, x, y, 1, mats[currentMat].color); // test the line drawing function //updates the screen //SDL_Flip( screen ); //SDL_RenderPresent( screen ); SDL_UpdateWindowSurface(window); }// end while(quit == false) //Free the surface and quit SDL clean_up(); return 0; }
int GetLongOpt::parse(int argc, char * const *argv) { int optind = 1; pname = strlwr( strdup( basename(*argv) ) ); enroll_done = 1; if ( argc-- <= 1 ) return optind; while ( argc >= 1 ) { char *token = *++argv; --argc; if ( token[0] != optmarker || token[1] == optmarker ) break; /* end of options */ ++optind; char *tmptoken = ++token; while ( *tmptoken && *tmptoken != '=' ) ++tmptoken; /* (tmptoken - token) is now equal to the command line option length. */ Cell *t; enum { NoMatch, ExactMatch, PartialMatch } matchStatus = NoMatch; Cell *pc = 0; // pointer to the partially-matched cell for ( t = table; t != 0; t = t->next ) { if ( strncmp(t->option, token, (tmptoken - token)) == 0 ) { if ( strlen(t->option) == (tmptoken - token) ) { /* an exact match found */ int stat = setcell(t, tmptoken, *(argv+1), pname); if ( stat == -1 ) return -1; else if ( stat == 1 ) { ++argv; --argc; ++optind; } matchStatus = ExactMatch; break; } else { /* partial match found */ matchStatus = PartialMatch; pc = t; } } /* end if */ } /* end for */ if ( matchStatus == PartialMatch ) { int stat = setcell(pc, tmptoken, *(argv+1), pname); if ( stat == -1 ) return -1; else if ( stat == 1 ) { ++argv; --argc; ++optind; } } else if ( matchStatus == NoMatch ) { cout << pname << ": unrecognized option "; cout << optmarker << strtok(token,"= ") << "\n"; return -1; /* no match */ } } /* end while */ return optind; }