Exemplo n.º 1
0
void* user_func(void* arg) {
  operation_stats* stats = (operation_stats*) arg;
  struct timeval start;
  struct drand48_data ctx;
  char* buf;
  void* vbuf;
  char* zbuf = (char*) malloc(data_block_size*3);
  int zbuf_len = data_block_size*3;

  assert(!posix_memalign(&vbuf, data_block_size, data_block_size));
  buf = vbuf;

  init_rand_ctx(&ctx);

  while (!shutdown) {
    int x = lock_random_file(&ctx);
    DFILE* dfile = &dfiles[x];
    longlong block_no = rand_block(&ctx, dfile->dfile_len);
    longlong offset = block_no * data_block_size;

    now(&start);
    check_pread(dfile->dfile_fd, buf, data_block_size,
		offset, "user", dfile->dfile_fname);

    page_check_checksum(buf);

    if (compress_level)
      decompress_page(zbuf, zbuf_len);

    stats_report(stats, &start, &ctx, data_block_size, 0);

    pthread_mutex_lock(&dfiles_mutex);
    assert(dfiles[x].dfile_locked == 1);
    dfiles[x].dfile_locked = 0;
    pthread_mutex_unlock(&dfiles_mutex);
  }

  free(buf);
  free(zbuf);
  return arg;
}
Exemplo n.º 2
0
static void init_gblock(Block *b) {
	b->x = TILE_W/2;
	b->y = 0;
	srand((unsigned int)SDL_GetTicks());
	rand_block(b);
}
Exemplo n.º 3
0
/* parses options and stores the main game loop */
int main(int argc, char **argv)
{
    /* init ncurses environment */
    initscr();
    cbreak();
    noecho();
    curs_set(FALSE);

    /* init variables */
    file = ".hs2048g";
    hs = 0;
    s  = 0;
    sl = 0;
    SZ = 4;
    MAXVAL = 4;
    CALLOC2D(g, SZ);

    load_score();
    int n_blocks = 1;
    
    /* parse options */
    int c;
    while ((c = getopt(argc, argv, "rchs:b:")) != -1) {
        switch (c) {
            // color support - assumes your terminal can display colours
            // should still work regardless
            case 'c':
                start_color();
                init_pair(1, 1, 0);
                init_pair(2, 2, 0);
                init_pair(3, 3, 0);
                init_pair(4, 4, 0);
                init_pair(5, 5, 0);
                init_pair(6, 6, 0);
                init_pair(7, 7, 0);
                break;
            // different board sizes
            case 's':
                FREE2D(g, SZ);
                int optint = atoi(optarg);
                SZ = optint > 4 ? optint : 4;
                CALLOC2D(g, SZ);
                break;
            // different block spawn rate
            case 'b':
                n_blocks = atoi(optarg);
                break;
            // reset hiscores
            case 'r':
                endwin();
                printf("Are you sure you want to reset your highscores? (Y)es or (N)o\n");
                int response;
                if ((response = getchar()) == 'y' || response == 'Y') {
                    FILE *fd = fopen(file, "w+");
                    fclose(fd);
                }
                exit(EXIT_SUCCESS);
            // help menu
            case 'h':
                endwin();
                printf("Controls:\n"
                       "    hjkl, wasd      Movement\n"
                       "    q               Quit\n"
                       "\n"
                       "Usage:\n"
                       "    2048 [options]\n"
                       "\n"
                       "Options:\n"
                       "    -s <size>       Set the grid border length\n"
                       "    -b <rate>       Set the block spawn rate\n"
                       "    -c              Enables color support\n");
                exit(EXIT_SUCCESS);
        }
    }
    
    int width  = SZ * (MAXVAL + 2) + 1;
    int height = SZ * (MAXVAL + 2) + 3;

    // might center in middle of screen
    WINDOW *gamewin = newwin(height, width, 1, 1);
    keypad(gamewin, TRUE);

    /* random seed */
    srand((unsigned int)time(NULL));
    ITER(2, rand_block());
    draw_grid(gamewin);

    int key, moved;
    while (1) {
        /* will goto this if we didn't get a valid keypress */
        retry:;
        moved = 0;
        key = wgetch(gamewin);
        sl = 0;

        /* should check if anything changed during merge and if not retry */
        switch (key) {
            case 'h':
            case 'a':
            case KEY_LEFT:
                moved = TURN(DL);
                break;
            case 'l':
            case 'd':
            case KEY_RIGHT:
                moved = TURN(DR);
                break;
            case 'j':
            case 's':
            case KEY_DOWN:
                moved = TURN(DD);
                break;
            case 'k':
            case 'w':
            case KEY_UP:
                moved = TURN(DU);
                break;
            case 'q':
                FREE2D(g, SZ);
                erase();
                refresh();
                endwin();
                save_score();
                exit(EXIT_SUCCESS);
            default:
                goto retry;
        }
        
        if (!moves_available()) {
            endwin();
            printf("\n"
                   "YOU LOSE! - Your score was %d\n", s);
            save_score();
            exit(EXIT_SUCCESS);
        }

        if (moved) {
            ITER(n_blocks, rand_block());
            draw_grid(gamewin);
        }
    }
}