int main() { /*------------------------------main function------------------------------*/ setcursortype(NOCURSOR); create_world(); int i; srand((unsigned)time(NULL)); for(;;) { ModTick(); check_wall();//checck_crash set_block(); new_block(); check_move();//move_block(); i++; Sleep(1); if (i>399) { i=0; move_block(DOWN); } gotoxy(0,0); printf("%3d",b_x); gotoxy(0,1); printf("%3d",b_y); gotoxy(0,3); printf("%3d",b_type); } gotoxy(0,0); getch(); } /*------------------------------main function------------------------------*/
_Bool read_world(world_t *world, char *path) { long file_size = 0; FILE *fp; char *input = NULL; fp = fopen(path, "r"); // get filesize fseek(fp, 0L, SEEK_END); file_size = ftell(fp); fseek(fp, 0L, SEEK_SET); // read file input = (char *)malloc(sizeof(char) * file_size); fread(input, file_size, sizeof(char), fp); // note: there is no syntax checking width = strstr(input, "\n") - input; // account newlines (+1) height = (file_size) / (width + 1); create_world(world); int i, j, tmp = 0; for(i = 0; i<height; i++) { for(j = 0; j<width; j++) { world->cells[i][j].alive = (input[tmp] == '*') ? true : false; tmp += 1; } tmp += 1; } return true; }
int main(){ declarevalues(); allegro_init(); install_keyboard(); install_mouse(); set_color_depth(32); set_gfx_mode(GFX_AUTODETECT_WINDOWED, Screen.getwidth(), Screen.getheight(), 0, 0); buffer = create_bitmap(Screen.getwidth(), Screen.getheight()); Loadproperties(); while (!key[KEY_ESC]) // Here is the game loop. { declarearray(); create_world(); circlefill(buffer, mouse_x, mouse_y, 5, makecol(255, 0, 0)); textprintf_ex(buffer, font, 10, 10, makecol(255, 100, 200), -1, "%s", testchar.testname.c_str() ); blit(buffer, screen, 0, 0, 0, 0, Screen.getwidth(), Screen.getheight()); clear_bitmap(buffer); } return 0; }
int main(int ac, char **av) { t_info *info; info = init(); parse_args(ac, av, info); if (info == 0) usage_server(); info->zone = create_world(info); add_server(info); server_get(info); free_info(info); return (0); }
dReal evaluate_controller(Controller* controller,noveltyitem* ni,data_record* record,bool log, bool off) { vector<float> k; dReal fitness; int timestep=0; const int simtime=1500; create_world(controller,log); while (!creatures[0]->abort() && timestep<simtime) { simulationStep(); timestep++; if (timestep%100 == 0 && novelty_function % 2 == 1) { update_behavior(k,creatures[0]); } /* if (log && timestep%100==0) cout << creatures[0]->fitness() << endl;*/ } int time=timestep; //for (int x=timestep+1; x<=simtime; x++) // if (x%100==0) while (k.size()< (simtime/100*2)) update_behavior(k,creatures[0]); fitness=creatures[0]->fitness(); ((Biped*)creatures[0])->lft.push_back(timestep); ((Biped*)creatures[0])->rft.push_back(timestep); if (ni!=NULL) { //ni->time=time; ni->novelty_scale = 1.0; ni->data.push_back(k); ni->secondary=time; } if (record!=NULL) { dVector3 com; creatures[0]->CenterOfMass(com); record->ToRec[0]=fitness; record->ToRec[1]=com[0]; record->ToRec[2]=com[1]; record->ToRec[3]=com[2]; record->ToRec[4]=timestep; } destroy_world(); return fitness; }
void create_chunk(Chunk *chunk, int p, int q) { chunk->p = p; chunk->q = q; chunk->faces = 0; chunk->dirty = 1; chunk->position_buffer = 0; chunk->normal_buffer = 0; chunk->uv_buffer = 0; Map *map = &chunk->map; map_alloc(map); create_world(map, p, q); db_load_map(map, p, q); gen_chunk_buffers(chunk); client_chunk(p, q); }
int main (int argc, char * argv[]){ int i, num_threads = 2, col_thread=0, num_gen = 100; if(argc < 5) { printf("Usage ./life <num_threads> <in_file> <col_th> <num_gen>\n"); return 0; } world w; num_threads = atoi(argv[1]); col_thread = atoi(argv[3]); num_gen = atoi(argv[4]); srand(time(NULL)); if(strcmp(argv[2], "none") == 0){ world_size_t world_size; world_size.rows = 20; world_size.cols = 10; w = create_world(world_size); seed(w); } else { w = parse_world(argv[2]); } // print(w); struct timeval start_time, end_time, diff_time; gettimeofday(&start_time, 0); for(i=0; i<num_gen; i++){ if(col_thread == 0){ w = evolve_rows(w, num_threads); } else{ w = evolve_cols(w, num_threads); } // print(w); if(i == 60) { create_pulsar(w, 5, 10); } } free_world(w); gettimeofday(&end_time, 0); if(timeval_subtract(&diff_time, &end_time, &start_time) != 1){ printf("num_threads: %d col_th: %d num_gen: %d time: %ld us\n", num_threads, col_thread, num_gen, diff_time.tv_sec * 1000000 + diff_time.tv_usec); } return 0; }
/* * Main */ int main(int argc, char *argv[]) { // Parse command line if(!optparse(argc, argv)) abort(); /* Nanosleep Setup */ int milisec = 250; // length of time to sleep, in miliseconds struct timespec req = {0}; req.tv_sec = 0; req.tv_nsec = milisec * 1000000L; // Initialisieren der Welt und den Zellen world_t world; /* Allocate Memory */ allocateMemory(&world); /* Welt befüllen */ if(path) { read_world(&world, path); } else if (!create_world(&world)) { printf("error creating world\n"); exit(-2); } else { fillWorld(&world); } /* Welt ausgeben */ printWorld(&world); /* * Nächster Schritt ausführen * TODO: Beenden der Schleife wenn sich nichts mehr ändert */ while (true) { (automode) ? nanosleep(&req, (struct timespec *)NULL) : bwait(); nextStep(&world); printWorld(&world); } return 0; }
int main (int argc, char * argv[]){ int i, num_threads = 2, in_file = 0; world w; if(argc >= 2){ num_threads = atoi(argv[1]); } if(argc == 3){ in_file = 1; } srand(time(NULL)); if(in_file == 0){ world_size_t world_size; world_size.rows = 20; world_size.cols = 10; w = create_world(world_size); seed(w); } else { w = parse_world(argv[2]); } // print(w); struct timeval start_time, end_time, diff_time; gettimeofday(&start_time, 0); for(i=0; i<1000; i++){ w = evolve(w, num_threads); // print(w); if(i == 60) { create_pulsar(w, 5, 10); } } free_world(w); gettimeofday(&end_time, 0); if(timeval_subtract(&diff_time, &end_time, &start_time) != 1){ printf("Time elapsed: %d us\n", diff_time.tv_sec * 1000000 + diff_time.tv_usec); } return 0; }
static void greets_init_effect (void *params, backbuffer_info *bbuf) { greets_data *gdata = (greets_data *) params; gdata->world = create_world (0); world_set_perspective (gdata->world, 60.0, 1.33f, 10.0f, 300.0f); world_set_pos_lookat_up (gdata->world, (guVector) { 0, 0, 50 }, (guVector) { 0, 0, 0 }, (guVector) { 0, 1, 0 }); TPL_OpenTPLFromMemory (&fontTPL, (void *) font_tpl, font_tpl_size); TPL_GetTexture (&fontTPL, font, &gdata->fontobj); GX_InitTexObjFilterMode (&gdata->fontobj, GX_LINEAR, GX_LINEAR); GX_InitTexObjWrapMode (&gdata->fontobj, GX_REPEAT, GX_REPEAT); gdata->tileidx = memalign (32, GX_GetTexBufferSize (TILES_W, TILES_H, TILES_FMT, GX_FALSE, 0)); GX_InitTexObj (&gdata->tileidxobj, gdata->tileidx, TILES_W, TILES_H, TILES_FMT, GX_REPEAT, GX_REPEAT, GX_FALSE); GX_InitTexObjFilterMode (&gdata->tileidxobj, GX_NEAR, GX_NEAR); gdata->tile_shader = create_shader (&init_tile_shader, NULL); shader_append_texmap (gdata->tile_shader, &gdata->fontobj, GX_TEXMAP0); shader_append_texmap (gdata->tile_shader, &gdata->tileidxobj, GX_TEXMAP1); shader_append_texcoordgen (gdata->tile_shader, GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY); memset (gdata->tileidx, 0, GX_GetTexBufferSize (TILES_W, TILES_H, TILES_FMT, GX_FALSE, 0)); init_ascii_to_char (); /*for (i = 0; i < 4; i++) put_text (gdata->tileidx, i, i, (unsigned char *) "OOOOOOOOOOOOOO");*/ object_loc_initialise (&gdata->greets_loc, GX_PNMTX0); }
int main(int argc, char *argv[]) { // 创建一个世界 WINDOW *world; // 创建一条蛇 Snake *snake; // 创建食物所在位置坐标 Point food_point; // 初始移动方向为向右 Direction direction = RIGHT; srand(time(NULL)); // curses库初始化 initscr(); /* 初始化屏幕 */ noecho(); /* 禁止输入字符getch读取回显 */ cbreak(); /* 关闭行缓冲 */ timeout(TICKRATE); /* 每隔TICKRATE的时间检测一次窗口读操作 */ keypad(stdscr, TRUE); /* 开启键盘的键区,可以使用方向键 */ curs_set(0); /* 禁止显示鼠标指针 */ refresh(); /* 刷新屏幕,准备重画 */ // 初始化世界 world = create_world(); // 以符号*界定世界窗口的边界 box(world, '|' , '-'); // 将世界窗口显示在终端上 wrefresh(world); // 蛇初始化 snake = snake_create(SNAKE_INITIAL_LENGTH); // 食物位置初始化 food_point = new_food_position(); int ch; // 因为没TICKRATE检测一次,并且设置了nodelay模式,getch不会 // 阻塞,每隔TICKRATE下述循环将被执行一次 while ((ch = getch()) != 'q') { // 每当检测到用户输入,重画世界 wclear(world); box(world, '|' , '-'); // 蛇移动,修改蛇的各个部分移动之后的坐标值 snake_move(snake, direction, WORLD_WIDTH, WORLD_HEIGHT); Point *snake_head = (Point *)snake->head->value; // 蛇如果吃掉了食物,在新位置生成新的食物 if (are_points_equal(food_point, *snake_head)) { food_point = new_food_position(); snake_add_part_to_tail(snake); } // 绘制出蛇以及食物 mvwaddch(world, food_point.y, food_point.x, '$'); draw_snake(world, snake); // 将当前绘制的窗口显示到终端 wrefresh(world); // 在nodelay模式下,getch读取不到字符则返回ERR if(ch != ERR) { // 修正移动方向 direction = direction_for_key(ch); } } ll_free_list(snake); delwin(world); endwin(); return 0; }
/* Main method runs initialize() and update() */ int main(int argc, char **argv) { /* get num bodies from the command line */ int num_bodies; num_bodies = (argc == 2) ? atoi(argv[1]) : DEF_NUM_BODIES; printf("Universe has %d bodies.\n", num_bodies); /* set up the universe */ time_t cur_time; time(&cur_time); srand48((long)cur_time); // seed the RNG used in create_world struct world *world = create_world(num_bodies); /* set up graphics using Xlib */ Display *disp = XOpenDisplay(NULL); int scr = DefaultScreen(disp); Window win = XCreateSimpleWindow( disp, RootWindow(disp, scr), 0, 0, WIDTH, HEIGHT, 0, BlackPixel(disp, scr), WhitePixel(disp, scr)); XStoreName(disp, win, "N-Body Simulator"); Pixmap back_buf = XCreatePixmap(disp, RootWindow(disp, scr), WIDTH, HEIGHT, DefaultDepth(disp, scr)); GC gc = XCreateGC(disp, back_buf, 0, 0); // Make sure we're only looking for messages about closing the window Atom del_window = XInternAtom(disp, "WM_DELETE_WINDOW", 0); XSetWMProtocols(disp, win, &del_window, 1); XSelectInput(disp, win, StructureNotifyMask); XMapWindow(disp, win); XEvent event; // wait until window is mapped while (1) { XNextEvent(disp, &event); if (event.type == MapNotify) { break; } } struct timespec delay={0, 1000000000 / 60}; // for 60 FPS struct timespec remaining; int frame_num = 0; while (1) { // check if the window has been closed if (XCheckTypedEvent(disp, ClientMessage, &event)) { break; } // we first draw to the back buffer then copy it to the front (`win`) draw_world(disp, back_buf, gc, world); XCopyArea(disp, back_buf, win, gc, 0, 0, WIDTH, HEIGHT, 0, 0); step_world(world, 0.1); frame_num++; nanosleep(&delay, &remaining); } XFreeGC(disp, gc); XFreePixmap(disp, back_buf); XDestroyWindow(disp, win); XCloseDisplay(disp); return 0; }
int main(int argc, char *argv[]) { world_t world; unsigned int each_iteration; unsigned int each_organism; unsigned int x; unsigned int y; char c; char color; organism_t organisms[ORGANISM_COUNT]; srandom(RANDOM_SEED); #if CURSES_VISUALIZATION initscr(); start_color(); #if CURSES_SOLID_COLORS init_pair(1, COLOR_BLACK, COLOR_RED); init_pair(2, COLOR_BLACK, COLOR_GREEN); init_pair(3, COLOR_BLACK, COLOR_BLUE); init_pair(4, COLOR_BLACK, COLOR_WHITE); init_pair(5, COLOR_BLACK, COLOR_BLACK); #else init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(3, COLOR_BLUE, COLOR_BLACK); init_pair(4, COLOR_WHITE, COLOR_BLACK); init_pair(5, COLOR_BLACK, COLOR_BLACK); #endif #endif create_world(&world); for (each_organism = 0; each_organism < ORGANISM_COUNT; each_organism++) { create_organism(&world, &organisms[each_organism]); } for (each_iteration = 0; each_iteration < ITERATIONS; each_iteration++) { for (each_organism = 0; each_organism < ORGANISM_COUNT; each_organism++) { move_organism(&world, &organisms[each_organism]); } #if CURSES_VISUALIZATION for (x = 0; x < WORLD_WIDTH; x++) { for (y = 0; y < WORLD_HEIGHT; y++) { if (NULL == world.cells[x][y]) { color = 'x'; c = ' '; } else { color = display_color(world.cells[x][y]); c = world.cells[x][y]->face; } switch (color) { case 'r': mvaddch(y, x, c | COLOR_PAIR(1)); break; case 'g': mvaddch(y, x, c | COLOR_PAIR(2)); break; case 'b': mvaddch(y, x, c | COLOR_PAIR(3)); break; case 'w': mvaddch(y, x, c | COLOR_PAIR(4)); break; default: mvaddch(y, x, c | COLOR_PAIR(5)); break; } } } refresh(); usleep(SLEEP_US); #endif } for (each_organism = 0; each_organism < ORGANISM_COUNT; each_organism++) { destroy_organism(&world, &organisms[each_organism]); } #if CURSES_VISUALIZATION endwin(); #endif return 0; }
world evolve_cols(world w, int num_threads) { int i, j, n, nedge=0; pthread_t bound_th[4]; pthread_t *evolve_th = (pthread_t *)malloc(num_threads * sizeof(pthread_t)); evolve_param_cols_t *evp = (evolve_param_cols_t *)malloc(num_threads * sizeof(evolve_param_cols_t)); world newworld; world nextgen = create_world(w.size); // threading and shared data num_threads = num_threads < w.size.cols ? num_threads : w.size.cols; for(i=0; i<num_threads; i++){ evp[i].start_col = i * (w.size.cols / num_threads); if(i==num_threads-1) { evp[i].end_col = w.size.cols; } else { evp[i].end_col = (i+1) * (w.size.cols / num_threads); } evp[i].w = &w; evp[i].nw = &nextgen; pthread_create(&evolve_th[i], NULL, &process_world_slice_cols, (void *)(evp+i)); } // wait for all threads; synchronization for(i=0;i<num_threads;i++){ pthread_join(evolve_th[i], NULL); } free(evolve_th); free(evp); check_bound_params cbp; cbp.res = &nedge; cbp.w = nextgen; // num_threads threads for(j=0; j<4;){ for(i=0; i<(num_threads<4?num_threads:4);i++){ cbp.pos = i+j; pthread_create(&bound_th[i], NULL, &check_boundary, (void *)&cbp); } for(i=0;i<(num_threads<4?num_threads:4);i++){ pthread_join(bound_th[i], NULL); } j += i; } free_world(w); newworld = nextgen; if(nedge != 0){ // printf("Expanding world... %x\n", nedge); int pos_row = 0; int pos_col = 0; world_size_t new_world_size; new_world_size.rows = nextgen.size.rows; new_world_size.cols = nextgen.size.cols; if(nedge & NORTH){ new_world_size.rows += nextgen.size.rows; pos_row += nextgen.size.rows; } if(nedge & SOUTH){ new_world_size.rows += nextgen.size.rows; } if(nedge & EAST){ new_world_size.cols += nextgen.size.cols; } if(nedge & WEST){ new_world_size.cols += nextgen.size.cols; pos_col += nextgen.size.cols; } newworld = create_world(new_world_size); // TODO: multi-threaded populate_world_cols(nextgen, newworld, pos_row, pos_col, num_threads); free_world(nextgen); } return newworld; }
int main(int argc, char **argv) { world_t world; game_t game; double last_time = 0; double current_time = 0; float deltatime = 0; float sleep_time; if (config_from_file("anko.cfg", 1) == 2) return EXIT_FAILURE; if (config_from_args(argc, argv)) return EXIT_FAILURE; if (!init()) goto err_init; if (!new_game(&game, config.board_width, config.board_height, &config.gen_params, config.sim_speed * 1000)) goto err_game; if (!create_world(&world, &game)) goto err_world; if ((current_ui = init_ui_game(&world)) == NULL) goto err_ui; world_set_active_player(&world, add_player(&game, TEAM_BURNER)); events_link_frame(¤t_ui); // link window event to game ui frame glClearColor(0, 0, 0, 1); last_time = 0; while(!glfwWindowShouldClose(window)) { current_time = glfwGetTime(); deltatime = current_time - last_time; update_speed(deltatime); glfwPollEvents(); // Update update_ui(current_ui, deltatime); update_world(&world); if (update_game(&game, deltatime * 1000)) refresh_world(&world); if(should_quit) glfwSetWindowShouldClose(window, GL_TRUE); // Rendering glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_ui(current_ui); font_swap_buffers(); glfwSwapBuffers(window); // Update speed and sleep if necessary last_time = current_time; sleep_time = (current_time + MAX_MSPF - glfwGetTime()) * 1000 * 1000; if(sleep_time > 0) usleep(sleep_time); } destroy_ui(current_ui); game_over(&game); end_of_the_world(&world); // Tin tin tin terminate(); return EXIT_SUCCESS; err_ui: end_of_the_world(&world); err_world: game_over(&game); err_game: terminate(); err_init: return EXIT_FAILURE; }
int main(int argc, char *argv[]) { world_t world; unsigned int each_iteration; unsigned int each_organism; unsigned int x; unsigned int y; char c; char color; organism_t organisms[ORGANISM_COUNT]; char iteration_string[16]; srandom(RANDOM_SEED); initscr(); start_color(); init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(3, COLOR_BLUE, COLOR_BLACK); init_pair(4, COLOR_WHITE, COLOR_BLUE); init_pair(5, COLOR_WHITE, COLOR_BLACK); attron(COLOR_PAIR(4)); mvaddstr(39, 0, " " " "); mvaddstr(0, 0, "teoc - the evolution of culture - " "created 2007 by inhaesio zha "); create_world(&world); for (each_organism = 0; each_organism < ORGANISM_COUNT; each_organism++) { create_organism(&world, &organisms[each_organism]); //print_organism(&organisms[each_organism]); } for (each_iteration = 0; each_iteration < ITERATIONS; each_iteration++) { for (each_organism = 0; each_organism < ORGANISM_COUNT; each_organism++) { move_organism(&world, &organisms[each_organism]); } for (x = 0; x < WORLD_WIDTH; x++) { for (y = 0; y < WORLD_HEIGHT; y++) { c = ' '; if (NULL != world.cells[x][y]) { color = display_color(world.cells[x][y]); c = world.cells[x][y]->face; } switch (color) { case 'r': mvaddch(y + 1, x, c | COLOR_PAIR(1)); break; case 'g': mvaddch(y + 1, x, c | COLOR_PAIR(2)); break; case 'b': mvaddch(y + 1, x, c | COLOR_PAIR(3)); break; default: mvaddch(y + 1, x, c | COLOR_PAIR(5)); break; } } } attron(COLOR_PAIR(4)); //sprintf(iteration_string, "%s", each_iteration); //mvaddstr(39, 0, iteration_string); refresh(); usleep(SLEEP_US); } endwin(); return 0; }