Esempio n. 1
0
int main(int argc, char **argv)
{
    struct dist_g_ba dist={0};
    struct shape shape1={0}, shape2={0}, shear={0};

    struct vec2 Qsum={0}, mean_shear={0};
    struct mtx2 Cinv_sum={0},C={0};

    double sigma=0.3;


    if (argc < 5) {
        fprintf(stderr,"usage: test-pqr seed shear1 shear2 npair\n");
        exit(1);
    }

    const char *seed_str=argv[1];
    double shear1=atof(argv[2]);
    double shear2=atof(argv[3]);
    long npair=atol(argv[4]);

    shape_set_g(&shear, shear1, shear2);
    init_genrand_str(seed_str);

    dist_g_ba_fill(&dist, sigma);

    for (long i=0; i<npair; i++) {
        //if ( (i % 1000) == 0) {
        //    printf("%ld/%ld\n", i+1, npair);
        //}
        while (1) {
            dist_g_ba_sample(&dist, &shape1);
            shape2 = shape1;
            shape_rotate(&shape2, M_PI_2);

            shape_add_inplace(&shape1, &shear);
            shape_add_inplace(&shape2, &shear);

            if (do_pqr_pair(&dist,
                            &shape1,
                            &shape2,
                            &Qsum,
                            &Cinv_sum)) {
                break;
            }
        }
    }

    mtx2_invert(&Cinv_sum, &C);
    mtx2_vec2prod(&C, &Qsum, &mean_shear);

    printf("%.16g %.16g %.16g   %.16g %.16g %.16g\n",
           shear1, mean_shear.v1/shear1-1, sqrt(C.m11)/shear1,
           shear2, mean_shear.v2/shear2-1, sqrt(C.m22)/shear2);


}
Esempio n. 2
0
int main()
{
  init_geometry();
  shape_t *planet = new_sphere(50, 20, 20);
  shape_t *sp1 = new_sphere(10, 16, 16);
  shape_t *sp2 = new_sphere(10, 16, 16);
  shape_t *t1 = new_taurus(30, 4, 50, 10);
  shape_t *t2 = new_taurus(30, 4, 40, 10);
  shape_translate(sp1, 80, 70, 40);
  shape_translate(sp2, -90, 10, -60);
  shape_scale(t1, 5, 1.2, 5);
  shape_scale(t2, 3.5, 1.2, 3.5);
  shape_rotate(t1, -20, 0, 0, 1);
  shape_rotate(t2, 20, 0, 0, 1);
  flushOBJ(stdout);
  free_shape(planet);
  free_shape(t1);
  free_shape(t2);
  finalize_geometry();
  return 0;
}
Esempio n. 3
0
File: main.c Progetto: odrevet/GE2
game_status state_in_game(SDL_Renderer* renderer, game* p_game)
{
  bool done=false;
  
  //the return code is always GAME_OVER, the player try to beat his hi-score
  //until his demise
  game_status ret_code = GAME_OVER;
  long timelastcall=SDL_GetTicks();

  //initialize the states of completed lines to 0
  memset(p_game->state,
	 0,
	 STATE_NUM * sizeof(int));
  p_game->score = 0;

  int level_cur = 0;
  int level_max = 10;

  level_init();
  
  //declare two shapes, one controled by the player, the other to indicates
  //the type of the next shape
  shape o_falling_shape, o_preview_shape;

  //set the falling shape type randomly
  shape_set(&o_falling_shape, rand() % SHAPE_NB);
  shape_default_coord(&o_falling_shape);

  shape_type next_shape_type = rand() % SHAPE_NB;
  shape_set(&o_preview_shape, next_shape_type);

  //put the preview shape on the side of the board
  o_preview_shape.x = LEVEL_WIDTH + 1;
  o_preview_shape.y = o_preview_shape.len - SHAPE_SIZE / 2 + 2;
  
  while (!done){
    //drawing
    SDL_SetRenderDrawColor(renderer, 0, 42, 0, 255);
    SDL_RenderClear(renderer);
    //draw the falling shape
    shape_draw(&o_falling_shape, p_game->p_fontmap, renderer);

    //draw the next shape as a preview
    shape_draw(&o_preview_shape, p_game->p_fontmap, renderer);

    //draw the level (empty blocks and alderly placed shapes)
    level_draw(renderer, &o_falling_shape, p_game->p_fontmap);
    
    int game_speed = level_max * 100 - level_cur * 100;
    
    int total=0;
    for(int i=0;i<3;i++)total += p_game->state[i];

    //draw a line between the board and the score / next shape area
    SDL_SetRenderDrawColor( renderer, 0x00, 0x00, 0xFF, 0xFF );
    SDL_RenderDrawLine(renderer,
		       TILE_SIZE * 12,
		       0,
		       TILE_SIZE * 12,
		       SCREEN_HEIGHT);
      
    //draw the score
    fontmap_printf(p_game->p_fontmap,
		   SCREEN_WIDTH - 7*5,
		   SCREEN_HEIGHT - 120,
		   renderer,
		   "%d",
		   p_game->score);

    SDL_Event event;
    while (SDL_PollEvent(&event)){

      if((event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN)||
	 (event.type == SDL_JOYBUTTONDOWN && event.jbutton.button == 5)){
	ret_code = state_paused(renderer);
	if(ret_code!=STAY){
	  done = 1;
	}
	      
      }

      if(event.type ==  SDL_QUIT){
	exit(EXIT_SUCCESS);
      }

      switch ( event.type )
	{
	case SDL_KEYDOWN:
	  switch (event.key.keysym.sym)
	    {
	    case SDLK_RIGHT:
	      if(shape_move(&o_falling_shape, 1, 0))o_falling_shape.x++;
	      break;
	    case SDLK_LEFT:
	      if(shape_move(&o_falling_shape, -1, 0))o_falling_shape.x--;
	      break;
	    case SDLK_DOWN:
	      if(shape_move(&o_falling_shape, 0, 1)) timelastcall=SDL_GetTicks()+game_speed;
	      break;
	    case SDLK_SPACE:
	      while(shape_move(&o_falling_shape, 0, 1)) o_falling_shape.y++;
	      timelastcall=SDL_GetTicks()+game_speed;
	      break;
	    case SDLK_f:
	      shape_rotate(&o_falling_shape, 0);
	      break;
	    case SDLK_d:
	      shape_rotate(&o_falling_shape, 1);
	      break;
	    case SDLK_q:
	      exit(EXIT_SUCCESS);
	      break;	      
	    default:
	      break;
	    }
	  break;
	case SDL_JOYBUTTONDOWN:
	  switch(event.jbutton.button)
	    {
	    case 0:
	      while(shape_move(&o_falling_shape, 0, 1)) o_falling_shape.y++;
	      timelastcall=SDL_GetTicks()+game_speed;
	      break;
	    case 2:
	      shape_rotate(&o_falling_shape, 1);
	      break;
	    case 3:
	      shape_rotate(&o_falling_shape, 0);
	      break;
	    case 6:
	      done=true;
	      ret_code = QUIT;
	      break;
	    case 19:
	      done=true;
	      ret_code = QUIT;
	      break;
	    default:
	      break;
	    }
	  break;
	}
    }
    
    if(SDL_GetTicks() - timelastcall > game_speed) {
      //check if the falling shape can move
      if(shape_move(&o_falling_shape, 0, 1)){
	//update coord
	o_falling_shape.y++;
      }else{
	//the shape can not move
	int i;
	level_add_shape(&o_falling_shape);

	int lines_in_a_row=0;
	//tab of line index to remove
	int rem_tab[SHAPE_SIZE];
	int line_nb = level_check_line(rem_tab);

	for(i=1;i<line_nb;i++){
	  if(rem_tab[i] == rem_tab[i-1]+1)lines_in_a_row++;
	  else lines_in_a_row = 0;
	}

	if(line_nb){
	  //blink completed line(s)
	  blink(renderer, rem_tab, line_nb, p_game->p_fontmap);

	  for(i=0;i<line_nb;i++)level_remove_line(rem_tab[i]);    //remove completed line(s)
	  p_game->score +=  line_nb * (lines_in_a_row + 1);       //update score
	  p_game->state[lines_in_a_row]++;                        //update game states
	  if(line_nb == 2 && lines_in_a_row == 0)p_game->state[0]++;

	  level_cur = p_game->score / 1000;                      //eventually update level number
	  if(level_cur >= level_max)level_cur = level_max;
	}

	if(level_check_game_over()){
	  done = 1;
	  ret_code = GAME_OVER;
	}
	else{
	  //"create" a new falling block by chaning the falling shape type
	  //to the next shape and reseting the coords
	  shape_set(&o_falling_shape, next_shape_type);
	  shape_default_coord(&o_falling_shape);

	  //pick a next shape type randomly for the preview and the next falling
	  //block
	  next_shape_type = rand() % SHAPE_NB;
	  shape_set(&o_preview_shape, next_shape_type);
	}
      }

      //update timer
      timelastcall=SDL_GetTicks();
    }
    
    SDL_RenderPresent(renderer);
  }
 
    return ret_code;
}