Esempio n. 1
0
/** In debug mode, this method is used to draw the frame of the fighters.

\param a_pcName The name of the polygon (in the perl namespace)
\param a_iColor The game color to draw the polygon with.
*/
void CGame::DrawPoly( const char* a_pcName, int a_iColor )
{
	AV *poList;
	int n;

	poList = get_av( a_pcName, FALSE );
	if ( poList == NULL )
	{
		return;
	}

	n = av_len( poList ) + 1;

	if ( n< 2 )
	{
		return;
	}

	for ( int i=0; i<n; i += 2 )
	{
		int j = (i+2) % n;

		int x1 = SvIV( *av_fetch( poList, i, false) );
		int y1 = SvIV( *av_fetch( poList, i+1, false) );
		int x2 = SvIV( *av_fetch( poList, j, false) );
		int y2 = SvIV( *av_fetch( poList, j+1, false) );

		sge_Line( gamescreen, x1, y1 + m_iYOffset, x2, y2 + m_iYOffset, a_iColor ) ;
	}
}
/*
ポリゴン描画
*/
static VALUE drawing_draw_polygon(int argc, VALUE *argv, VALUE self)
{
  VALUE vdst;
  VALUE pairs;
  VALUE mcolor;
  VALUE fill;
  VALUE aa;
  Uint8 alpha;
  Uint32 color;
  int i, vertexes;
  
  rb_scan_args(argc, argv, "32", &vdst, &pairs, &mcolor, &fill, &aa);
  
  // bitmapメソッドを持っていれば、メソッドの値をvdstとする
  VALUE methods = rb_funcall(vdst, rb_intern("methods"), 0);
  if(rb_ary_includes(methods, rb_str_intern(rb_str_new2("to_unit"))) == Qfalse &&
     rb_ary_includes(methods, rb_str_intern(rb_str_new2("bitmap"))) == Qfalse
    )
    rb_raise(eMiyakoError, "this method needs sprite have to_method or bitmap method!");
  if(rb_ary_includes(methods, rb_str_intern(rb_str_new2("to_unit"))) == Qtrue)
    vdst = rb_funcall(vdst, rb_intern("to_unit"), 0);
  vdst = rb_funcall(vdst, rb_intern("bitmap"), 0);

  vertexes = RARRAY_LEN(pairs);
  // 頂点数チェック
  if(vertexes > 65536)
    rb_raise(eMiyakoError, "too many pairs. pairs is less than 65536.");
  
  // 範囲チェック
  for(i=0; i<vertexes; i++)
  {
    VALUE vertex = *(RARRAY_PTR(pairs)+i);
    Sint16 x, y;
    get_position(vertex, &x, &y);
  }
  
	SDL_Surface  *dst = GetSurface(vdst)->surface;

  color = value_2_color(rb_funcall(cColor, rb_intern("to_rgb"), 1, mcolor), dst->format, &alpha);

  if(RTEST(fill) && RTEST(aa) && alpha < 255)
    rb_raise(eMiyakoError, "can't draw filled antialiased alpha polygon");

  Sint16 *px = (Sint16 *)malloc(sizeof(Sint16) * vertexes);
  Sint16 *py = (Sint16 *)malloc(sizeof(Sint16) * vertexes);
  for(i=0; i<vertexes; i++)
  {
    VALUE vertex = *(RARRAY_PTR(pairs)+i);
    get_position(vertex, px+i, py+i);
  }
  
  if(!RTEST(fill) && !RTEST(aa) && alpha == 255)
  {
    for(i=0; i<vertexes-1; i++)
      sge_Line(dst, px[i], py[i], px[i+1], py[i+1], color);
    sge_Line(dst, px[vertexes-1], py[vertexes-1], px[0], py[0], color);
  }
  else if(!RTEST(fill) && !RTEST(aa) && alpha < 255)
  {
    for(i=0; i<vertexes-1; i++)
      sge_LineAlpha(dst, px[i], py[i], px[i+1], py[i+1], color, alpha);
    sge_LineAlpha(dst, px[vertexes-1], py[vertexes-1], px[0], py[0], color, alpha);
  }
  else if(!RTEST(fill) && RTEST(aa) && alpha == 255)
  {
    for(i=0; i<vertexes-1; i++)
      sge_AALine(dst, px[i], py[i], px[i+1], py[i+1], color);
    sge_AALine(dst, px[vertexes-1], py[vertexes-1], px[0], py[0], color);
  }
  else if(!RTEST(fill) && RTEST(aa) && alpha < 255)
  {
    for(i=0; i<vertexes-1; i++)
      sge_AALineAlpha(dst, px[i], py[i], px[i+1], py[i+1], color, alpha);
    sge_AALineAlpha(dst, px[vertexes-1], py[vertexes-1], px[0], py[0], color, alpha);
  }
  else if(RTEST(fill) && !RTEST(aa) && alpha == 255)
    sge_FilledPolygon(dst, (Uint16)vertexes, px, py, color);
  else if(RTEST(fill) && !RTEST(aa) && alpha < 255)
    sge_FilledPolygonAlpha(dst, (Uint16)vertexes, px, py, color, alpha);
  else if(RTEST(fill) && RTEST(aa) && alpha == 255)
    sge_AAFilledPolygon(dst, (Uint16)vertexes, px, py, color);
  
  free(py);
  free(px);

  return Qnil;
}