
#ifdef _WSSIM
#include	<wssim.h>
#include	<wssim2.h>
#else
#include	<sys\bios.h>
#endif
#include	<stdio.h>

/* ----------------------------------- */
/* BG1 & BG2 & SPRITE を使ったサンプル */
/*                   for wssim library */
/* ----------------------------------- */

extern	short	font_y4u[];
extern	short	font_y4d[];

/* ネームテーブルの値を得る */
#define	BG_CHR(v,h,p,c)			(((v)<<15)+((h)<<14)+((p)<<9)+(c))
#define	SPR_CHR(v,h,pr,ct,p,c)	(((v)<<15)+((h)<<14)+((pr)<<13)+((ct)<<12)+((p)<<9)+(c))

typedef struct {
	int	x,y;
} POINT;

/* スプライトのイメージ */
typedef	struct {
	unsigned short atr;
	unsigned char y;
	unsigned char x;
} SPR_BUFFER;

#define	SPRITE_COUNT_X	13
#define	SPRITE_COUNT_Y	8
#define	SPRITE_COUNT	(SPRITE_COUNT_X*SPRITE_COUNT_Y)
SPR_BUFFER	sprites[SPRITE_COUNT];


#define	BG1_PALET	0
#define	BG2_PALET	4
#define	SPR_PALET	12

/* カラーのアニメーション */
int		pptable[16] = {
	0xf840,
	0xe840,
	0xd740,
	0xc740,
	0xb630,
	0xa630,
	0x9530,
	0x8530,
	0x7420,
	0x6420,
	0x5320,
	0x4320,
	0x3210,
	0x2210,
	0x1110,
	0x0110,
};

#ifdef _WSSIM
char	*modemes[3] = {
	"BG1",
	"BG2",
	"SPR",
};
#endif

main()
{
	int		x,y,p;
	int		key,lastkey,keyedge;
	POINT	pt[3];
	int		m;
	short	bgbuf[32][32];

	/*-------------------------------------*/
	/* 画面の初期設定 */
	display_control( 0x027 );
	lcd_set_color( 0xf840,0xc840 );
/*	lcd_set_segments( 4 ); */

#ifdef _WSSIM
	wssim_terminal_print( 0,0,"bg1 bg2 sprite sample",0x7,0x8 );
	wssim_key_push_mode(1);
#endif

	palette_set_color( BG1_PALET,0x1230 );		/* BG1 */
	palette_set_color( BG2_PALET,0x5674 );		/* BG2 */
	palette_set_color( SPR_PALET,0x7565 );		/* 噴出し */

	font_set_colordata(   0,256,font_y4u );
	font_set_colordata( 256,256,font_y4d );

	screen2_set_window( 8,8,224-16,144-16 );

	/*-------------------------------------*/
	/* BGマップを初期化して設定する */
	for( y=0; y<32; y+=2 ){
		for( x=0; x<32; x+=2 ){
			bgbuf[y]  [x]   = BG_CHR(0,0,BG1_PALET,0x180);
			bgbuf[y]  [x+1] = BG_CHR(0,0,BG1_PALET,0x181);
			bgbuf[y+1][x]   = BG_CHR(0,0,BG1_PALET,0x190);
			bgbuf[y+1][x+1] = BG_CHR(0,0,BG1_PALET,0x191);
		}
	}
	screen_set_char( /*screen*/0, /*x,y*/0,0, /*w,h*/32,32,bgbuf );
	p = 0;
	for( y=0; y<32; y++ ){
		if( y >= 24 ) p = 0;
		for( x=0; x<16; x++ ){
			bgbuf[y][x]    = BG_CHR(0,0,BG2_PALET,p);
			bgbuf[y][x+16] = BG_CHR(0,0,BG2_PALET,p);
			if( y < 24 ) p++;
		}
	}
	screen_set_char( /*screen*/1, /*x,y*/0,0, /*w,h*/32,32,bgbuf );

	/*-------------------------------------*/
	/* SPRを初期化して設定する */
	sprite_set_range( 0,SPRITE_COUNT );
	p = 0x180;
	for( y=0; y<SPRITE_COUNT_Y; y++ ){
		p += 16-SPRITE_COUNT_X;
		for( x=0; x<SPRITE_COUNT_X; x++ ){
												   /* VfHfPrCtPc          Ch*/
			sprites[x+SPRITE_COUNT_X*y].atr = SPR_CHR(0,0,0,0,SPR_PALET-8,p);
			p++;
		}
	}

	/*-------------------------------------*/
	/* 座標の初期化 */
	pt[0].x = 0; pt[0].y = 0;  /*BG1*/
	pt[1].x = 0; pt[1].y = 0;  /*BG2*/
	pt[2].x = 56; pt[2].y = -16;  /*SPR*/

	/*-------------------------------------*/

	m = 0;
	key = 0;
	p = 0;
	while(1){
		char	ss[64];
#ifdef _WSSIM
		sprintf( ss,"mode=%s",modemes[m] );
		wssim_terminal_print( 0,1,ss,0x7,0x8 );
#endif
		screen_set_scroll( /*screen*/ 0,pt[0].x,pt[0].y );
		screen_set_scroll( /*screen*/ 1,pt[1].x,pt[1].y );

		/* パレットアニメーションの動作 */
		lcd_set_color( pptable[p/2],0xc840 );
		palette_set_color( BG1_PALET,m==0 ? 0x0123:0x4567 );		/* BG1 */
		palette_set_color( BG2_PALET,m==1 ? 0x1230:0x5674 );		/* BG2 */
		palette_set_color( SPR_PALET,m==2 ? 0x1320:0x5764 );		/* 噴出し */

		for( y=0; y<SPRITE_COUNT_Y; y++ ){
			for( x=0; x<SPRITE_COUNT_X; x++ ){
				sprites[x+SPRITE_COUNT_X*y].x = pt[2].x+x*8;
				sprites[x+SPRITE_COUNT_X*y].y = pt[2].y+y*8;
			}
		}
		sprite_set_data( 0,SPRITE_COUNT,sprites );

		lastkey = key;
		key = key_press_check();
		keyedge = lastkey ^ ( key & lastkey );
		if( key & (KEY_UP1   | KEY_UP2   )) pt[m].y--;
		if( key & (KEY_DOWN1 | KEY_DOWN2 )) pt[m].y++;
		if( key & (KEY_RIGHT1| KEY_RIGHT2)) pt[m].x++;
		if( key & (KEY_LEFT1 | KEY_LEFT2 )) pt[m].x--;
		if( keyedge & (KEY_A|KEY_B) ){
			m++; if( m == 3 ) m = 0;
		}
		if( keyedge & KEY_START ){
#ifdef _WSSIM
			wssim_terminal_print( 0,2,"snapshot execute",0x7,0x2 );
			wssim_snapshot_screen( "snapshot.bmp" );
#else
			break;
#endif
		}
		p++;
		if( p == 16*2 ) p = 0;
		sys_wait(1);
	}
	/*-------------------------------------*/
}

/* おわり */

