/*
 *   スプライト８９３定義サンプル
 *
 *   注）setscrn.obj, gra.obj, io.obj, extspdef.objをリンクしてください
 */

#include <stdio.h>
#include <fmc.h>        /* KYBD */
#include <egb.h>
#include <spr.h>

extern NormalHigh(),screenterm(),wait(),EGB_singleLine();
extern ESPR_define(),ESPR_setPaletteBlock(),ESPR_setAttribute();

char    *setPat();
char    *setPat16();
void    spDef();

void main()
{
        unsigned int  ky,enc;
        int  sx,sy,dx,dy;

        char egbwork[1536];

        struct ttl { short int x,y,num; char moji[29]; };
        struct ttl title = {1,17,29,"スプライト８９３定義サンプル "};

        dx = 1;   dy = 1;
        sx = 60;
        sy = 120;


        KYB_init();
        KYB_clic(1);

        NormalHigh(egbwork);

        EGB_writePage(egbwork,0);      /* ＣＧ画面 */
        EGB_color(egbwork,1,4);        /* 背景は暗い青 */
        EGB_clearScreen(egbwork);
        EGB_color(egbwork,0,0x7fff);
        EGB_sjisString(egbwork,(char *)&title);
        EGB_singleLine(egbwork,0,120,256,120);
        EGB_singleLine(egbwork,128,18,128,240);
        EGB_singleLine(egbwork,0,18,0,239);
        EGB_singleLine(egbwork,0,239,255,239);
        EGB_singleLine(egbwork,255,239,255,18);
        EGB_singleLine(egbwork,255,18,0,18);


        /* スプライト定義 */
        spDef();

        /* 表示開始 表示個数は４個 */
        SPR_display(1, 4);

        ky = 0xffff;
        while (ky == 0xffff) {  /* キー入力で終了 */ 

                SPR_setPosition(0,1023,1,1,sx,sy+18);
                SPR_setPosition(0,1022,1,1,240-sx,206-sy+18);
                SPR_setPosition(0,1021,1,1,sx,206-sy+18);
                SPR_setPosition(0,1020,1,1,240-sx,sy+18);

                sx = sx + dx;
                sy = sy + dy;
                if( sx ==  0 || sx == 240 ) dx = (dx>0) ? -1:1;
                if( sy ==  0 || sy == 206 ) dy = (dy>0) ? -1:1;

                wait(0x2000);
                ky = KYB_read(1,&enc);
        }

        /* 表示終了  */
        SPR_display(0, 4);

        screenterm(egbwork);
}

void spDef()
{
        char    *Pat;
        char    *Pat16;

        short pal[] = { 
                0,0,0,0,0,0x7380,0,0,0,0,0,0,0,0,0x7fe0,0x7ff0,
                0,0,0,0,0,0x4000,0,0,0,0,0,0,0,0,0x4c00,0x7000,
                0,0,0,0,0,0x300,0,0,0,0,0,0,0,0,0x3e0,0x13e4
         };


        SPR_init();  /* 初期化 */

         /* pattern dataを定義 */
        Pat = setPat();
        Pat16  = setPat16();

         /* ３万色 pattern dataをパターン番号64に設定(本当はパレット領域) */
        ESPR_define( 1 , 64, 1, 1, Pat);

         /* 16色 pattern dataをパターン番号68に設定 */
        ESPR_define( 0 , 68, 1, 1, Pat16);

         /* palette dataをパレット番号0〜2に設定(本当はインデックス領域)　*/
        ESPR_setPaletteBlock(0,3,&pal);

         /* スプライト番号1023に32K色パターンを設定 */
        ESPR_setAttribute( 1023 , 1, 1, 64, 0);

         /* スプライト番号1022〜1020に16色パターンを設定 */
        ESPR_setAttribute( 1022 , 1, 1, 68, 0x8000); /* パレット０ */
        ESPR_setAttribute( 1021 , 1, 1, 68, 0x8001); /* パレット１ */
        ESPR_setAttribute( 1020 , 1, 1, 68, 0x8002); /* パレット２ */


}

char    *setPat()
{
static short pat[] = { 
        0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x5fff, 0x5fff,
        0x57ff, 0x4fff, 0x4fff, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
        0x8000, 0x8000, 0x8000, 0x8000, 0x5fff, 0x5fff, 0x6bff, 0x6bff,
        0x5fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x8000, 0x8000, 0x8000,
        0x8000, 0x8000, 0x8000, 0x5fff, 0x6bff, 0x73ff, 0x73ff, 0x6bff,
        0x5fff, 0x57ff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x8000, 0x8000,
        0x8000, 0x8000, 0x5fff, 0x5fff, 0x73ff, 0x73ff, 0x6bff, 0x5fff,
        0x5fff, 0x57ff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x8000,
        0x8000, 0x8000, 0x5fff, 0x6bff, 0x6bff, 0x6bff, 0x5fff, 0x5fff,
        0x57ff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x8000,
        0x8000, 0x4fff, 0x5fff, 0x5fff, 0x5fff, 0x5fff, 0x5fff, 0x57ff,
        0x57ff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x3fff,
        0x8000, 0x4fff, 0x4fff, 0x57ff, 0x57ff, 0x57ff, 0x57ff, 0x4fff,
        0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x3fff,
        0x8000, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff,
        0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x3fff,
        0x8000, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff,
        0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x3fff, 0x2fff,
        0x8000, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff,
        0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x3fff, 0x2fff,
        0x8000, 0x8000, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff,
        0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x3fff, 0x2fff, 0x8000,
        0x8000, 0x8000, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff,
        0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x3fff, 0x2fff, 0x8000,
        0x8000, 0x8000, 0x8000, 0x4fff, 0x4fff, 0x4fff, 0x4fff, 0x4fff,
        0x4fff, 0x4fff, 0x4fff, 0x3fff, 0x3fff, 0x2fff, 0x8000, 0x8000,
        0x8000, 0x8000, 0x8000, 0x8000, 0x4fff, 0x4fff, 0x4fff, 0x4fff,
        0x3fff, 0x3fff, 0x3fff, 0x2fff, 0x2fff, 0x8000, 0x8000, 0x8000,
        0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x3fff, 0x3fff,
        0x3fff, 0x2fff, 0x2fff, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
        0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
        0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000

};

        return (char *)pat ;
}

char    *setPat16()
{
static short pat[] = { 
        0x0000, 0xee00, 0x0eee, 0x0000,
        0x0000, 0xeeee, 0xeeee, 0x000e,
        0xe000, 0xeffe, 0xeeee, 0x00ee,
        0xee00, 0xeeef, 0xeeee, 0x0eee,
        0xee00, 0xe888, 0x88ee, 0x0ee8,
        0x8ee0, 0x8eee, 0xee8e, 0xee8e,
        0xeee0, 0xeeee, 0xeeee, 0xeeee,
        0xeee0, 0xeeee, 0xeeee, 0xeeee,
        0xeee0, 0xeeee, 0xeeee, 0xeeee,
        0xeee0, 0xee8e, 0xeeee, 0xeeee,
        0xee00, 0x88ee, 0x8888, 0x0eee,
        0xee00, 0xeeee, 0xeeee, 0x06ee,
        0xe000, 0xeeee, 0xeeee, 0x006e,
        0x0000, 0xeeee, 0x6eee, 0x0006,
        0x0000, 0x6600, 0x0666, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000
};

        return (char *)pat ;
}

