Esempio n. 1
0
ENTRYPOINT void
draw_ball (ModeInfo *mi)
{
   blinkboxstruct *bp = &blinkbox[MI_SCREEN(mi)];

   Display *dpy = MI_DISPLAY(mi);
   Window window = MI_WINDOW(mi);
   int i = 0;

   if (! bp->glx_context)
     return;
   mi->polygon_count = 0;
   glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(bp->glx_context));

   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   hit_top_bottom(bp);
   hit_front_back(bp);
   hit_side(bp);

   glRotated(0.25,0,0,1);
   glRotated(0.25,0,1,0);
   glRotated(0.25,1,0,0);


   glPushMatrix();
   glScalef(0.5,0.5,0.5);

   glColor3f(1,1,1);
   glPushMatrix();

   if (!do_blur || MI_IS_WIREFRAME(mi)) {
     glTranslatef(bp->ball.x += bp->mo.x,
                  bp->ball.y += bp->mo.y,
                  bp->ball.z += bp->mo.z);

     glScalef(2,2,2);
     glCallList(bp->ballList);
     mi->polygon_count += SPHERE_SLICES*SPHERE_STACKS;

   } else {

#    define blur_detail 24.0
     float ball_alpha = 1 / blur_detail;

     glBlendFunc(GL_SRC_ALPHA,GL_ONE);
     glTranslatef(bp->ball.x, bp->ball.y, bp->ball.z);
   
     for (i = 0; i < blur_detail; ++i) {
       glTranslatef(bp->mo.x / blur_detail,
                    bp->mo.y / blur_detail,
                    bp->mo.z / blur_detail);

       /* comment the following line for quick but boring linear blur */
       ball_alpha = sin((M_PI / blur_detail) * i) / blur_detail;
     
       glColor4f(1, 1, 1, ball_alpha);

       glScalef(2, 2, 2);
       glCallList(bp->ballList);
       mi->polygon_count += SPHERE_SLICES*SPHERE_STACKS;
       glScalef(.5, .5, .5);
     }
     i = 0;
   
     bp->ball.x += bp->mo.x;
     bp->ball.y += bp->mo.y;
     bp->ball.z += bp->mo.z;
   }
   
   glPopMatrix();

   while(i < 6){
    switch(i){
      case 0:{
               bp->sp = &bp->lside;
               bp->bpos.x = bp->lside.pos.z*-1;
               bp->bpos.y = bp->lside.pos.y;
               bp->bpos.z = bbox.bottom.x - bp->bscale.d;
               if (bp->sp->hit)
                CheckBoxPos(bp, bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
               break;
             }
      case 1:{
               bp->sp = &bp->rside;
               bp->bpos.x = bp->rside.pos.z*-1;
               bp->bpos.y = bp->rside.pos.y;
               bp->bpos.z = bbox.top.x + bp->bscale.d;
               if (bp->sp->hit)
                CheckBoxPos(bp, bbox.bottom.z,bbox.top.z,bbox.bottom.y,bbox.top.y);
               break;
             }
      case 2:{
               bp->sp = &bp->tside;
               bp->bpos.x = bp->tside.pos.x;
               bp->bpos.y = bp->tside.pos.z;
               bp->bpos.z = bbox.bottom.y - bp->bscale.d;
               if (bp->sp->hit)
                CheckBoxPos(bp, bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
               break;
             }
      case 3:{
               bp->sp = &bp->bside;
               bp->bpos.x = bp->bside.pos.x;
               bp->bpos.y = bp->bside.pos.z;
               bp->bpos.z = bbox.top.y + bp->bscale.d;
               if (bp->sp->hit)
                CheckBoxPos(bp, bbox.bottom.x,bbox.top.x,bbox.bottom.z,bbox.top.z);
               break;
             }
      case 4:{
               bp->sp = &bp->fside;
               bp->bpos.x = bp->fside.pos.y;
               bp->bpos.y = bp->fside.pos.x*-1;
               bp->bpos.z = bbox.top.z + bp->bscale.d;
               if (bp->sp->hit)
                CheckBoxPos(bp, bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
               break;
             }
      case 5:{
               bp->sp = &bp->aside;
               bp->bpos.x = bp->aside.pos.y;
               bp->bpos.y = bp->aside.pos.x*-1;
               bp->bpos.z = bbox.bottom.z + bp->bscale.d;
               if (bp->sp->hit)
                CheckBoxPos(bp, bbox.bottom.y,bbox.top.y,bbox.bottom.x,bbox.top.x);
               break;
             }
    }
    if(bp->sp->hit){
      if(do_fade){
        glColor4f(bp->sp->color[0],bp->sp->color[1],bp->sp->color[2],1-(ALPHA_AMT * bp->sp->alpha_count));
      }else{
        glColor3fv(bp->sp->color);
      }
      glBlendFunc(GL_SRC_ALPHA,GL_ONE);
      glPushMatrix();
      glRotatef(bp->sp->rot[0],bp->sp->rot[1],bp->sp->rot[2],bp->sp->rot[3]);
      glTranslatef(bp->bpos.x,bp->bpos.y,bp->bpos.z);
      if (do_dissolve) {
         glScalef(bp->bscale.wh-(bp->des_amt*bp->sp->des_count),bp->bscale.wh-(bp->des_amt*bp->sp->des_count),bp->bscale.d);
      }else{
        glScalef(bp->bscale.wh,bp->bscale.wh,bp->bscale.d);
      }
      glCallList(bp->boxList);
      mi->polygon_count += 6;
      glPopMatrix();
      bp->sp->counter--;
      bp->sp->des_count++;
      bp->sp->alpha_count++;
      if(!bp->sp->counter)
      {
        bp->sp->hit = 0;
      }
    }
    i++;
  }


   glPopMatrix();
  if (mi->fps_p) do_fps (mi);
   glFinish();
   glXSwapBuffers(dpy, window);

}
Esempio n. 2
0
void
main_loop()
{
	struct colour *board[WIDTH_CELLS][HEIGHT_CELLS];
	SDL_Event e = {0};
	bool running = false;
	bool game_over = false;
	int i = 0;
	int x = 0;
	int y = 0;
	int score = 0;
	int last_x = 0;
	int last_y = 1;
	struct piece held;
	new_piece(&held);

	TTF_Font *font = TTF_OpenFont(FONT_FILE, FONT_SIZE);
	if (!font) {
		printf("TTF_OpenFont: %s\n", TTF_GetError());
		return;
	}

	for (y = 0; y < HEIGHT_CELLS; y++)
		for (x = 0; x < WIDTH_CELLS; x++)
			board[x][y] = &(palette[0]);

	running = true;
	last_x = last_y = x = y = 0;
	SDL_AddTimer(INTERVAL_NORMAL, &gravity_callback, NULL);
	char lockout;

	while (running) {
		lockout = 0;
		score += clear_rows(&board);

		if (hit_side(x, y, &held, &board))
			x = last_x;

		if (hit_floor(x, y, &held, &board))
			lockout = 1;

		draw_board(&board);

		if (!game_over)
			draw_piece(x, y, held.colour, held.bitmap);

		char score_string[16];
		snprintf(score_string, sizeof(score_string), "Score: %d", score);
		score_string[sizeof(score_string) - 1] = '\0';
		plot_text(score_string, font, ((SDL_Color){255,255,255,255}), 10, 10);

		if (game_over)
			plot_text("Game over", font, ((SDL_Color){255,255,255,255}), 90, 50);

		plot_update();
		SDL_WaitEvent(&e);
		if (pause_mode) {
			if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_p) {
				pause_mode = 0;
			}
			continue;
		}

		switch (e.type) {
		case SDL_USEREVENT:
			if (lockout && !game_over) {
				drop_piece(x, y, &held, &board);
				last_x = last_y = x = y = 0;
				new_piece(&held);
				if (piece_overlaps(&held, &board, x, y)) {
					game_over = true;
				}

				lockout = 0;
			} else {
				last_y = y++; /* gravity */
				last_x = x;
			}
			break;
		case SDL_QUIT:
			fprintf(stderr, "quit\n");
			running = false;
			break;
		case SDL_KEYDOWN:
			switch (e.key.keysym.sym)
			{
			case SDLK_p: pause_mode = 1; break;
			case SDLK_a: last_x = x--; break;
			case SDLK_d: last_x = x++; break;
			case SDLK_w:
				i = 0;
				do {
					rotate(&held, 1);
				} while(hit_side(x, y, &held, &board) && i++ < 4);
				break;
			case SDLK_s:
				speed_mode = 1;
				break;
			case SDLK_q:
				running = false;
				break;
			default:
				break;
			}
			break;
		case SDL_KEYUP:
			switch (e.key.keysym.sym)
			{
			case SDLK_s:
				speed_mode = 0;
				break;
			default:
				break;
		}
		default:
			break;
		}
	}
}