sprite.c 19.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
/*
 * Copyright 1995, Silicon Graphics, Inc.
 * ALL RIGHTS RESERVED
 *
 * UNPUBLISHED -- Rights reserved under the copyright laws of the United
 * States.   Use of a copyright notice is precautionary only and does not
 * imply publication or disclosure.
 *
 * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
 * Use, duplication or disclosure by the Government is subject to restrictions
 * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
 * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
 * in similar or successor clauses in the FAR, or the DOD or NASA FAR
 * Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
 * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
 *
 * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
 * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
 * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
 * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
 * GRAPHICS, INC.
 */
/*---------------------------------------------------------------------*
        Copyright (C) 1995 Nintendo. (Originated by SGI)
        
        $RCSfile: sprite.c,v $
        $Revision: 1.1.1.1 $
        $Date: 2002/05/02 03:27:33 $
 *---------------------------------------------------------------------*/

#include "hsp.h"

#define SP_CUTOUT_ALPHA_LEVEL 0x80
#define G_CC_PRIMI       0,0,0,PRIMITIVE,0,PRIMITIVE,0,TEXEL0

#if 0
/*
 * spSetZ()
 *
 */
void 
spSetZ (Sprite *sp, s32 z)
{
    sp->zdepth = (s16)z;
}

/*
 * spMove()
 *
 *   Moves a sprite on the screen.
 */
void 
spMove (Sprite *sp, s32 x, s32 y)
{
    sp->x = (s16)x;
    sp->y = (s16)y;
}

static s32 scissor_xmax;
static s32 scissor_ymax;

static s32 scissor_xmin;
static s32 scissor_ymin;

/*
 * spScissor()
 *
 *   Sets the scissoring box to be used for subsequent Sprite Drawing
 */
void 
spScissor (s32 xmin, s32 xmax, s32 ymin, s32 ymax )
{
    scissor_xmin = xmin;
    scissor_ymin = ymin;

    scissor_xmax = xmax;
    scissor_ymax = ymax;
}

static int *prev_bmbuf = NULL;

/*
 * drawbitmap()
 *
 *   Draws a bitmap from a sprite at a location on the screen.  If the
 *   "usevert" flag is set, then the routine will use vertices and triangles
 *   to draw the bitmap.  Otherwise, the texture rectangle instruction
 *   will be used.
 */

static void 
drawbitmap (Gfx **glp, Sprite *s, Bitmap *b,
	    s32 x, s32 y, s32 xx, s32 yy, s32 fs, s32 ft, s32 sx, s32 sy)
{
    s32 rs, rt;
    s32 rxh, ryh;
    s32 rxl, ryl;
    Gfx *gl;
    s32 tex_width, tex_height;
    Gtexrect gt, *g;
    s32 s_clamp, t_clamp,
	s_mask,  t_mask, 
	s_lod,   t_lod;

    g = >

    tex_width = b->width_img;
    tex_height= s->bmHreal;
    
    gl = *glp;

    /* Scissoring */
    if( (x >= scissor_xmax) || (y >= scissor_ymax) ) {
	return;
    }

    if( (xx < scissor_xmin) || (yy < scissor_ymin) ) {
	return;
    }


	if (x < scissor_xmin) {
	    rxh = scissor_xmin*4;
	    rs = (b->s<<5) + fs + (((scissor_xmin - x)*sx)>>5);
	} else {
	    rxh = x*4;
	    rs = (b->s<<5) + fs;
	};

	if (y < scissor_ymin) {
	    ryh = scissor_ymin*4;
	    rt = (b->t<<5) + ft + (((scissor_ymin-y)*sy)>>5);
	} else {
	    ryh = y*4;
	    rt = (b->t<<5) + ft;
	}

	if( xx >= scissor_xmax ) {
	    rxl = scissor_xmax*4;
	} else {
	    rxl = xx*4;
	};

	if( yy >= scissor_ymax ) {
	    ryl = scissor_ymax*4;
	} else {
	    ryl = yy*4;
	};

	s_clamp = G_TX_CLAMP;	t_clamp = G_TX_CLAMP;
	s_mask  = G_TX_NOMASK;	t_mask  = G_TX_NOMASK;
	s_lod   = G_TX_NOLOD;	t_lod   = G_TX_NOLOD;

#define LOAD_TEX

#ifdef LOAD_TEX
	switch(s->bmsiz) {
	  case G_IM_SIZ_4b:
	    if (s->attr & SP_TEXSHUF) {
		gDPLoadTextureBlock_4bS(gl++, b->buf, s->bmfmt,
				       tex_width, tex_height, 0, 
				       s_clamp, t_clamp,
				       s_mask,  t_mask, 
				       s_lod,   t_lod);
	    } else {
#if 0
		gDPLoadTextureBlock_4b(gl++, b->buf, s->bmfmt,
				       tex_width, tex_height, 0, 
				       s_clamp, t_clamp,
				       s_mask,  t_mask, 
				       s_lod,   t_lod);
#else
		gDPLoadTextureTile_4b(gl++, b->buf, s->bmfmt, tex_width, tex_height,
							  b->s, b->t,
							  b->s + b->width,b->t + b->actualHeight,
							  0,
							  s_clamp, t_clamp,
							  s_mask,  t_mask, 
							  s_lod,   t_lod);
#endif	    
	    };
	    break;
	  case G_IM_SIZ_8b:
	    if (s->attr & SP_TEXSHUF) {
		gDPLoadTextureBlockS(gl++, b->buf, s->bmfmt, G_IM_SIZ_8b,
				    tex_width, tex_height, 0,
				       s_clamp, t_clamp,
				       s_mask,  t_mask, 
				       s_lod,   t_lod);
	    } else {
		gDPLoadTextureBlock(gl++, b->buf, s->bmfmt, G_IM_SIZ_8b,
				    tex_width, tex_height, 0,
				       s_clamp, t_clamp,
				       s_mask,  t_mask, 
				       s_lod,   t_lod);
	    };
	    break;
	  case G_IM_SIZ_16b:
	    if (s->bmfmt == G_IM_FMT_YUV )
		  {
			if (s->attr & SP_TEXSHUF)
			  {
				gDPLoadTextureBlockYuvS(gl++, b->buf, s->bmfmt, G_IM_SIZ_16b,
									  tex_width, tex_height, 0,
									  s_clamp, t_clamp,
									  s_mask,  t_mask, 
									  s_lod,   t_lod);
			  }
			else
			  {
				if( b->LUToffset != 0 ) 
				  { /* Split Y and UV areas */
					unsigned char *uv, *addr;
					int j;
					s32 tmem, siz;
					
					gDPSetTextureImage( gl++, G_IM_FMT_I, G_IM_SIZ_8b, 1, b->buf);
					gDPSetTile( gl++, G_IM_FMT_I, G_IM_SIZ_8b, 0, 256, G_TX_LOADTILE, 0,
							   t_clamp, t_mask, t_lod,
							   s_clamp, s_mask, s_lod );
					gDPLoadSync( gl++ );
					gDPLoadBlock( gl++, G_TX_LOADTILE, 0,0, tex_width*tex_height -1,
								 CALC_DXT(tex_width,1) );
					gDPLoadSync( gl++ );

					uv = ((unsigned char *)b->buf) +
					  ((tex_width*tex_height)/2)*b->LUToffset;
					
					for( j=0; j<=(tex_height/2); j++ ) {
					  addr = uv + ((tex_height/2) -2 -j)*tex_width;
					  siz = 3*tex_width;
					  tmem = (tex_width/8)*(tex_height - 2 - 2*j);
					  if( tmem < 0 ) 
						{
						  addr += (-tmem)*8;
						  siz -= (-tmem)*8;
						  tmem = 0;
						};
					  if( j == 0 )
						siz = 2*tex_width;
					  
					  gDPSetTextureImage( gl++, G_IM_FMT_I, G_IM_SIZ_8b, 1, addr);
					  gDPSetTile( gl++, G_IM_FMT_I, G_IM_SIZ_8b, 0, tmem,
								 G_TX_LOADTILE, 0,
								 t_clamp, t_mask, t_lod,
								 s_clamp, s_mask, s_lod );
					  gDPLoadSync( gl++ );
					  gDPLoadBlock( gl++, G_TX_LOADTILE, 0,0, siz -1,
								   CALC_DXT(tex_width,1) );
					  gDPLoadSync( gl++ );
					};

					gDPSetTile( gl++, s->bmfmt, G_IM_SIZ_16b, (((tex_width) * 1)+7)>>3, 0,
							   G_TX_RENDERTILE, 0,
							   t_clamp, t_mask, t_lod,
							   s_clamp, s_mask, s_lod );
					gDPSetTileSize( gl++, G_TX_RENDERTILE, 0, 0,
								   ((tex_width)-1) << G_TEXTURE_IMAGE_FRAC,
								   ((tex_height)-1) << G_TEXTURE_IMAGE_FRAC );

				  }
				else /*  b->LUToffset != 0 */
				  {
					gDPLoadTextureBlockYuv(gl++, b->buf, s->bmfmt, G_IM_SIZ_16b,
										   tex_width, tex_height, 0,
										   s_clamp, t_clamp,
										   s_mask,  t_mask, 
										   s_lod,   t_lod);
				  };
			  };
		  } 
		else /*  not G_IM_FMT_YUV */
		  {
			if (s->attr & SP_TEXSHUF)
			  {
				gDPLoadTextureBlockS(gl++, b->buf, s->bmfmt, G_IM_SIZ_16b,
									 tex_width, tex_height, 0,
									 s_clamp, t_clamp,
									 s_mask,  t_mask, 
									 s_lod,   t_lod);
			  }
			else
			  {
				gDPLoadTextureBlock(gl++, b->buf, s->bmfmt, G_IM_SIZ_16b,
									tex_width, tex_height, 0,
									s_clamp, t_clamp,
									s_mask,  t_mask, 
									s_lod,   t_lod);
			  };
		  };
	    break;
	  case G_IM_SIZ_32b:
	    if (s->attr & SP_TEXSHUF) {
		gDPLoadTextureBlockS(gl++, b->buf, s->bmfmt, G_IM_SIZ_32b,
				    tex_width, tex_height, 0,
				       s_clamp, t_clamp,
				       s_mask,  t_mask, 
				       s_lod,   t_lod);
	    } else {
		gDPLoadTextureBlock(gl++, b->buf, s->bmfmt, G_IM_SIZ_32b,
				    tex_width, tex_height, 0,
				       s_clamp, t_clamp,
				       s_mask,  t_mask, 
				       s_lod,   t_lod);
	    };
	    break;
       }
       prev_bmbuf = b->buf;

#endif /* LOAD_TEX */


	g->cmd = G_TEXRECT;
	g->tile = 0;

	g->xh = rxh;
	g->xl = rxl;
	g->yh = ryh;
	g->yl = ryl;

	g->s = rs;
	g->t = rt;

	if ((s->attr & SP_FASTCOPY) && (s->bmfmt != G_IM_FMT_YUV) )
	    g->dsdx = sx * 4;
	else
	    g->dsdx = sx;

	g->dtdy = sy;

    gSPTextureRectangle(gl++,g->xh,g->yh,g->xl,g->yl,g->tile,g->s,g->t,g->dsdx,g->dtdy);
    gDPPipeSync(gl++);

    *glp = gl;
}

static u16 sp_attr = 0;

void
spInit( Gfx **glistp )
{
    Gfx		*gl;

    gl = *glistp;

    sp_attr = 0;

    scissor_xmin = 0;
    scissor_ymin = 0;

    scissor_xmax = 320;
    scissor_ymax = 240;

    /* Turn on texturing */
	/*  RDP attribute 変更前の呪文 */
    gDPPipeSync         ( gl++ );				
	/*  1サイクルモードではかなりクオリティの高いピクセルを生成できます。 */
    gDPSetCycleType     ( gl++, G_CYC_1CYCLE);  

	/*  gSPTexture(Gfx *gdl, int sc, int tc, int level, int tile, int on) */
	/*  sc,tc : テクスチャ座標スケーリングパラメータ  .16 フォーマット 下の場合 0.5  */
	/*  level : ミップマップレベル(ミップマップ使ってないので1こしかないので0) */
	/*  tile  : テクスチャタイル番号。よーわからんが使用する一番大きいミップマップの番号……かな。 */
    gSPTexture          ( gl++, 0x8000, 0x8000, 0,  G_TX_RENDERTILE, G_ON);

    gDPSetAlphaCompare  ( gl++, G_AC_NONE   );

	/*   テクスチャ長方形(スプライト)を描く時は、常に透視変換補正を行わない。 引数は真偽値 */
    gDPSetTexturePersp  ( gl++, G_TP_NONE   );

	/*   対象ピクセルを囲んでいる4つのテクセルからバイリニアフィルタリングを行う */
	/*   G_TF_POINT, G_TF_AVERAGE, G_TF_BILERP */
    gDPSetTextureFilter ( gl++, G_TF_BILERP );

	/*   出力にフィルタ処理(G_TC_FILT)をする */
	/*   (G_TC_CONV, G_TC_FILTCONV, G_TC_FILT) */
    gDPSetTextureConvert( gl++, G_TC_FILT   );

	/*  複数のテクスチャタイルの内の最高レベルのディテールテクスチャを使用します。 */
	/*  ( G_TD_CLAMP, G_TD_SHARPEN, G_TD_DETAIL) */
    gDPSetTextureDetail ( gl++, G_TD_CLAMP  );

	/*   LOD( : Level of Detail ミップマップ自動選択 )機能オフ。 */
	/*   gSPTextureで選択したタイル(mipmap)と mipmap+1 を使用 */
	/*   ( G_TL_TILE, G_TL_LOD ) */
    gDPSetTextureLOD    ( gl++, G_TL_TILE );

	/*  TLUT (Texture look up table : 要するにカラーパレット) を使用しない。 */
	/*  TLUT を使用するためには対応するカレーパレットがロードされている必要がある */
	/*  (G_TT_NONE, G_TT_RGBA16, G_TT_IA16) */
    gDPSetTextureLUT    ( gl++, G_TT_NONE   );

#define SPRITE_SURF	G_RM_TEX_EDGE
#define SPRITE_SURF2	G_RM_TEX_EDGE2

	/*  ブレンダ:ピクセルとフレームバッファを混ぜ合わせる機能。 */
	/*  半透明ポリゴンの描画、アンチエイリアス処理、フォグ処理、ディザ処理などを扱う。  */
	/*  gDPSetRenderMode(Gfx *gdl,u32 mode1,u32 mode2 ) */
	/*  1cycle モードでは mode2 の指定は意味を持たない。 */
    gDPSetRenderMode( gl++, G_RM_AA_OPA_SURF,G_RM_AA_OPA_SURF2 );

    *glistp = gl;
}


/*
 * spDraw()
 *
 *   Draws all of the sprites that are associated with bitmaps and that
 *   are supposed to be drawn (spDraw()).  They are drawn from 0 to maxsp,
 *   so sprites with higher numbers will overwrite lower-numbered sprites.
 *
 */
Gfx *
spDraw ( Sprite   *s )
{
    int		i;
    s32		x, y;
    float	sx, sy;
    Bitmap 	*b;
    Gfx		*gl;
    Gfx		*dl_start;
    s32		isx, isy;
    s32		tx, ty;
    s32		tx2, ty2;
    s32		x2, y2;
    float	ftx, fty;
    s32		fs, ft;
    s32		ex, ey;
	u16     attr_change , attr_on;
	u32     op0,op1;

    if (s->attr & SP_HIDDEN)
	return(NULL);

    prev_bmbuf = NULL;

    gl = s->rsp_dl_next;
    if( gl == NULL )
	gl = s->rsp_dl;
    dl_start = gl;

    b  = s->bitmap;
    ex = 0;
    ey = 0;

    if (s->attr & SP_EXTERN)	/* current attr is extern? */
	  sp_attr = s->attr;	/* Assume all settings should remain the same */
    else if (sp_attr & SP_EXTERN)/* previous attr was extern? */
	  sp_attr = ~s->attr;	/* Assume previous modes are all different */

	attr_change = s->attr ^ sp_attr;
	attr_on     = s->attr & attr_change;

    if ( attr_change )
	  {
		if ( attr_change & SP_TRANSPARENT )
		  {
#if 1
			op0 = ( attr_on & SP_TRANSPARENT ) ? G_RM_XLU_SURF : G_RM_AA_OPA_SURF;
			/*  ↓意味なし */
			op1 = ( attr_on & SP_TRANSPARENT ) ? G_RM_XLU_SURF2 : G_RM_AA_OPA_SURF2;

#else
			op0 = ( attr_on & SP_TRANSPARENT ) ? G_RM_AA_XLU_SURF : G_RM_AA_OPA_SURF;
			op1 = ( attr_on & SP_TRANSPARENT ) ? G_RM_AA_XLU_SURF2 : G_RM_AA_OPA_SURF2;
#endif
			gDPSetRenderMode( gl++, op0, op1);
		  }
		
		if ( attr_change & SP_CUTOUT )
		  {
			op0 = ( attr_on & SP_CUTOUT ) ? G_AC_THRESHOLD : G_AC_NONE;
			if(op0 == G_AC_THRESHOLD)
			  {
				/*  αの敷居値をセット */
				gDPSetBlendColor ( gl++, 0xff, 0xff, 0xff, SP_CUTOUT_ALPHA_LEVEL);
			  }
			/*  α  SP_CUTOUT_ALPHA_LEVEL 以下切捨てモードのオンオフ  */
			gDPSetAlphaCompare ( gl++, op0 ) ;
		  }
		
		if ( attr_change & SP_TEXSHIFT )
		  {
			op0 = ( attr_on & SP_TEXSHIFT ) ? G_TF_AVERAGE :  G_TF_BILERP;
			gDPSetTextureFilter( gl++, op0 );
		  }
	  };

    sp_attr = s->attr;

    if( sp_attr & SP_Z )
	  {
		gDPSetPrimDepth(gl++, s->zdepth, 0);
	  };

	/*  primitive color 設定 */
	/*  gDPSetPrimColor(Gfx *gdl, u32 m, u32 l, r,g,b,a) */
	/*  m: LOD<1.0のときの最小クランプ値。 */
	/*  l: LODフラクション。 */
	/*  ここではカラーコンパイナ用に色をセットしてるだけ。 */

    gDPSetPrimColor( gl++, 0, 0, s->red, s->green, s->blue, s->alpha);

#define MY_K0   (175 & 0x1ff)
#define MY_K1   (-43 & 0x1ff)
#define MY_K2   (-89 & 0x1ff)
#define MY_K3   (222 & 0x1ff)
#define MY_K4   (114 & 0x1ff)
#define MY_K5   (42 & 0x1ff)

	switch(s->bmfmt)
	  {
	  case G_IM_FMT_I:
		if(s->attr & SP_TRANSPARENT)
		  {	gDPSetCombineMode ( gl++, G_CC_PRIMI , G_CC_PRIMI ); }
		else
		  {	gDPSetCombineMode ( gl++, G_CC_MODULATEI_PRIM , G_CC_MODULATEI_PRIM ); }
		break;

	  case G_IM_FMT_IA:  /*  intencity & alpha */
		/*  MODULATEIA_PRIM */
		/*  color,alpha共 :テクスチャとprim を乗算  */
		gDPSetCombineMode ( gl++, G_CC_MODULATEIA_PRIM  , G_CC_MODULATEIA_PRIM );
		break;

	  case G_IM_FMT_CI:  /*  color index  */
		/*  G_CC_MODULATERGBA_PRIM  (modulate rgba prim) MODULATEIA_PRIMに同じ */
		gDPSetCombineMode ( gl++, G_CC_MODULATERGBA_PRIM, G_CC_MODULATERGBA_PRIM );

		/*  カラーパレット使用モード  */
		/*  パレットカラーは RGBA16 か IA16。(ここでは前者) */
		gDPSetTextureLUT( gl++, G_TT_RGBA16);  

		/*  カラーパレットのロード。 */
		/*  sp では 256 パレットのみサポートする。 */
		gDPLoadTLUT_pal256( gl++, s->LUT );
		gDPLoadSync( gl++ );
		break;

	  case G_IM_FMT_YUV: /*  輝度成分(Y)と色差成分(UV) */
		gDPSetCombineMode ( gl++, G_CC_MODULATERGBA_PRIM, G_CC_MODULATERGBA_PRIM );

		if(s->attr & SP_FASTCOPY)
		  {
			gDPSetTextureConvert( gl++, G_TC_CONV);
			gDPSetTextureFilter( gl++, G_TF_POINT);
			gDPSetCombineMode( gl++, G_CC_1CYUV2RGB, G_CC_1CYUV2RGB);
			gDPSetConvert( gl++, MY_K0, MY_K1, MY_K2, MY_K3, MY_K4, MY_K5);
		  }
		else
		  {
			gDPSetCycleType( gl++, G_CYC_2CYCLE );
			gDPSetTextureFilter( gl++, G_TF_BILERP);
			gDPSetTextureConvert( gl++, G_TC_FILTCONV);
			gDPSetConvert( gl++, MY_K0, MY_K1, MY_K2, MY_K3, MY_K4, MY_K5);
			gDPSetCombineMode( gl++, G_CC_YUV2RGB, G_CC_PASS2);
		  }
		break;

	  case G_IM_FMT_RGBA:
		gDPSetCombineMode ( gl++, G_CC_MODULATERGBA_PRIM, G_CC_MODULATERGBA_PRIM );
		break;

	  default:					
		break;
	  }

    if (s->attr & SP_SCALE)
	  {
		sx = s->scalex;
		sy = s->scaley;
	  }
	else
	  {
		sx = 1.0F;
		sy = 1.0F;
	  }

    isx = (int) ((1<<10) / sx + 0.5F);
    isy = (int) ((1<<10) / sy + 0.5F);

    if( b )
	  {
		
		if( (s->attr & SP_FASTCOPY) && (s->bmfmt != G_IM_FMT_YUV) )
		  gDPSetCycleType( gl++, G_CYC_COPY);

		x = 0.0F;
		y = 0.0F;

		fty = s->y+y*sy;
		ty = (int) ( fty  + 0.99999F);
		ft = (int) (isy * (ty - fty));
		ft = (ft + 16)>>5;
		if( s->attr & SP_TEXSHIFT )
		  ft += 16;			/* 1/2 Texel for AntiAliasing */
		
		if( s->attr & SP_FRACPOS )
		  ft += s->frac_t;		/* Micro-positioning */
		
		y2 = y + s->bmheight;
		ty2 = (int) (s->y+y2*sy + 0.99999F);

		if( (s->attr & SP_FASTCOPY) && (s->bmfmt != G_IM_FMT_YUV) )
		  ty2--;
		

		for( i=0; (i < s->nbitmaps) && (b->width > 0); i++, b++ )
		  {
			if( (x+b->width) > s->width )
			  { /* Next bitmap hits edge? */
				int bh;

				x = 0;
				fs = 0;
				ex = 0;
				ey += s->expy;
				y += s->bmheight;	     /* Wrap to next line */
				y2 = y + s->bmheight;	     /* Wrap to next line */
				
				fty = s->y+y*sy;
				ty = (int) ( fty  + 0.9999F);
				ft = (int) (isy * (ty - fty));
				ft = (ft + 16)>>5;
				if( s->attr & SP_TEXSHIFT )
				  ft += 16;			/* 1/2 Texel for AntiAliasing */
				
				if( s->attr & SP_FRACPOS )
				  ft += s->frac_t;		/* Micro-positioning */

				ty2 = (int) (s->y+y2*sy + 0.9999F);

				ty  += ey;
				ty2 += ey;
				
				if( (s->attr & SP_FASTCOPY) && (s->bmfmt != G_IM_FMT_YUV) )
				  ty2--;

				/* Fill out to rect. bdy? */
				
				if( (b->actualHeight != 0) )
				  bh = b->actualHeight;
				else
				  bh = s->bmheight;
				
				if( (y + bh) > s->height ) /* Can't wrap any more? */
				  break;
			  };

			
			ftx = s->x+x*sx;
			tx = (s32) (ftx + 0.9999F);
			fs = (s32) (isx * (tx - ftx));
			fs = (fs + 16)>>5;
			if( s->attr & SP_TEXSHIFT )
			  fs += 16;			/* 1/2 Texel for AntiAliasing */
			
			if( s->attr & SP_FRACPOS )
			  fs += s->frac_s;		/* Micro-positioning */
			
			x2 = x + b->width;
			tx2 = (int) (s->x+x2*sx + 0.9999F);
			
			if( (b->actualHeight != 0) )
			  {
				y2 = y + b->actualHeight;
				ty2 = (s32) (s->y+y2*sy + 0.9999F);
				ty2 += ey;
				
				if( (s->attr & SP_FASTCOPY) && (s->bmfmt != G_IM_FMT_YUV) )
				  ty2--;
			  };


			tx  += ex;
			tx2 += ex;

			if( (s->attr & SP_FASTCOPY) && (s->bmfmt != G_IM_FMT_YUV) )
			  tx2--;

			if( b->buf != NULL ) 	/* Skip over null bitmaps (blanks) */
			  drawbitmap ( &gl, s, b, tx, ty, tx2, ty2, fs, ft, isx, isy);

			x += b->width;
			ex += s->expx;
		  }

		if (s->attr & SP_FASTCOPY)
		  gDPSetCycleType( gl++, G_CYC_1CYCLE);
		
		if(s->bmfmt == G_IM_FMT_YUV)
		  {
			gDPSetCycleType( gl++, G_CYC_1CYCLE);
			gDPSetTextureFilter ( gl++, G_TF_BILERP );
			gDPSetTextureConvert( gl++, G_TC_FILT   );
		  };
		
	  }
	else /*  bitmap が指定されていない */
	  {
		int rgba;

		x = (s32)s->x;
		y = (s32)s->y;
		x2 = s->x + (s->width*sx)-1;
		y2 = s->y + (s->height*sy)-1;
		
		if( ( x >= scissor_xmax) || (y >= scissor_ymax) )
		  {
		  }
		else if( ( x2 < scissor_xmin) || (y2 < scissor_ymin) ) 
		  {
		  } 
		else
		  {
			
			if( x < scissor_xmin )
			  x = scissor_xmin;
			
			if( x2 >= scissor_xmax )
			  x2 = scissor_xmax - 1;
			
			if( y < scissor_ymin )
			  y = scissor_ymin;
			
			if( y2 >= scissor_ymax )
			  y2 = scissor_ymax - 1;

			rgba = GPACK_RGBA5551((s->red), (s->green), (s->blue), (s->alpha>>7));
			
			gSPTexture ( gl++, 0x8000, 0x8000, 0, G_TX_RENDERTILE, G_OFF);
			gDPSetCycleType( gl++, G_CYC_FILL);
			gDPSetFillColor( gl++, (rgba << 16) | (rgba) );
			
			gDPFillRectangle(  gl++, x,y, x2,y2 );
			gDPSetCycleType( gl++, G_CYC_1CYCLE);
			gSPTexture ( gl++, 0x8000, 0x8000, 0, G_TX_RENDERTILE, G_ON);
		  }
	  }
	
    if( s->bmfmt == G_IM_FMT_CI ) {
	  gDPSetTextureLUT( gl++, G_TT_NONE);
    };

    gSPEndDisplayList ( gl++ );

    s->rsp_dl_next   = gl;
    return(  dl_start );
}

void
spFinish( Gfx **glistp )
{
    Gfx *gl;

    gl = *glistp;

    /* Turn off texturing */
    gSPTexture ( gl++, 0x8000, 0x8000, 0, G_TX_RENDERTILE, G_OFF);
    gDPSetCombineMode ( gl++, G_CC_SHADE, G_CC_SHADE);

    if (sp_attr & SP_TRANSPARENT) {
	/* Turn off transparency */
	gDPSetRenderMode( gl++, G_RM_OPA_SURF, G_RM_OPA_SURF2);
    }

    if (sp_attr & SP_CUTOUT) {
	/* Turn off Alpha write enable */
	gDPSetAlphaCompare ( gl++, G_AC_NONE) ;
    }

    gSPEndDisplayList ( gl++ );

    *glistp = gl;
}

#endif
Gfx *
spFillRect(Gfx * dl ,s32 x,s32 y,s32 x2,s32 y2 ,u8 r,u8 g,u8 b,u8 a)
{
  gDPPipeSync         ( dl++ );	
  gDPSetCycleType( dl++, G_CYC_1CYCLE ); 
  gSPTexture ( dl++, 0x8000, 0x8000, 0, G_TX_RENDERTILE, G_OFF); 
  gDPSetAlphaCompare  ( dl++, G_AC_NONE   ); 
  gDPSetTexturePersp  ( dl++, G_TP_NONE   ); 
  gDPSetTextureFilter ( dl++, G_TF_BILERP ); 
  gDPSetTextureConvert( dl++, G_TC_FILT   ); 
  gDPSetTextureDetail ( dl++, G_TD_CLAMP  ); 
  gDPSetPrimColor( dl++, 0, 0, r, g, b, a); 
  gDPSetRenderMode( dl++, G_RM_XLU_SURF, G_RM_XLU_SURF2 ); 
  gDPSetCombineMode ( dl++, G_CC_PRIMITIVE , G_CC_PRIMITIVE ); 
  gSPTextureRectangle(dl++, x<<2, y<<2, x2<<2, y2<<2 ,G_TX_RENDERTILE, 0, 0, 0x400, 0x400);  
  gSPEndDisplayList ( dl++ ); 

  return dl;
}