Exemplo n.º 1
0
void run_life(world *w, WINDOW *win){

  draw_world(w, win);
  nodelay(win, 1);
  curs_set(0);
  struct timeval tv1,tv2;
  while(1){
    double interupt = float_sleep(wait_time);
    step_world(w);
    draw_world(w, win);
    if(wgetch(win) != ERR){
      break;
    }
  }
  nodelay(win, 0);
  curs_set(1);
  return;
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
//interactively read initial conditons from the user
void setup_initial_conditons(world *w, WINDOW *win){
  keypad(win, 1);
  int c,y,x,ymax,xmax;
  getyx(win, y ,x);
  getmaxyx(win, ymax, xmax);
  wmove(win, 0, 0);
  while(c = wgetch(win)){
    switch(c){
      case KEY_UP:
        //      case KEY_SUP:
        y = y > 0 ? y-1 : ymax;
        break;
      case KEY_DOWN:
        //      case KEY_SDOWN:
        y = y < ymax ? y+1 : 0;
        break;
      case KEY_LEFT:
        //      case KEY_SLEFT:
        x = x > 0 ? x-1 : xmax;
        break;
      case KEY_RIGHT:
        //      case KEY_SRIGHT:
        x = x < xmax ? x+1 : 0;
        break;
      case KEY_BACKSPACE:
        reset_grid(w);
        draw_world(w, win);
        x = y = 0;
        break;
      case '+':
        wait_time += 0.05;
        break;
      case '-':
        wait_time -= 0.05;
        break;
      case 'q':
        exit(0);
      case 'r':
        randomize_grid(w);
        draw_world(w, win);
        x = y = 0;
        break;
      case 's':
        step_world(w);
        draw_world(w, win);
        break;
      case '\r':
      case '\n':
        waddch(win, '#');
        w->grid[y*w->cols + x] = 1;
        break;
      case ' ':
        return;
    }
    wmove(win, y ,x);
    //    if(c == KEY_SUP || c == KEY_SDOWN | c == KEY_SLEFT || c = KEY_SRIGHT){
    //      waddch(win, '#');
    //      wmove(win, y ,x);
    //    }

  }
}