/************************************************************
  Ninja Sample
  COPYRIGHT (C) SEGA ENTERPRISES, LTD.

  タイトル ： ミップマップ効果
              mipmap
  編集履歴 ： 98-04-24 製作開始
              98-06-19 SET5対応
              98-07-10 ポーズ機能(文字スクロール&半透明ポリゴン)、
                       焼付き防止対応
              98-09-16 テクスチャバッファ確保のサイズ変更
              99-04-05 appends the English comments
************************************************************/

#include <shinobi.h>
#include <njdef.h>

void main(void)
{
#ifdef __GNUC__
	shinobi_workaround();
#endif
    njSetBorderColor( 0x00000000 );
        njUserInit();
        while (njUserMain() == NJD_USER_CONTINUE) {
                njWaitVSync();
        }
        njUserExit();
}

#define MAX_MATRIX_DEPTH 10
#define MAX_VERTEXES 10000
#define MAX_POLYGON_VTX 10
#define SIZE_CHANGE 0.001F
#define SPRITE_Z -1500.F

static NJS_MATRIX *matrix;
static NJS_VERTEX_BUF *vbuf;
static NJS_POLYGON_VTX *poly;

NJS_VIEW view;
NJS_LIGHT light;
NJS_TEXNAME texname[2] ;
NJS_SPRITE sprite;
NJS_TEXLIST texlist = {texname, 2};
NJS_TEXANIM anim[] = {{256, 256, 128, 128, 0, 0, 255, 255, 1, 0},
                      {256, 256, 128, 128, 0, 0, 255, 255, 0, 0}
                     };
NJS_TEXMEMLIST  texmemlist[2];

float c1 = 0.0f;/*拡大縮小用*/
float c2 = 0.0f;/*拡大縮小用*/
Sint8 bilinear = TRUE;
Sint8 DIRECTION = TRUE;
/*----------------------------------------------------------------------
           初期化
           initialize
----------------------------------------------------------------------*/
void njUserInit(void)
{
    /* システムの初期化 */
    /* Initializes the Dreamcast H/W and readies the SEGA library */
    sbInitSystem(NJD_RESOLUTION_640x480_NTSCI, NJD_FRAMEBUFFER_MODE_RGB565, 1);

    /*matrixとvertexバッファの確保*/
    /* allocates the memory */
    matrix = (NJS_MATRIX *)syMalloc(sizeof (NJS_MATRIX) * MAX_MATRIX_DEPTH);
    vbuf = (NJS_VERTEX_BUF *)syMalloc(sizeof (NJS_VERTEX_BUF) * MAX_VERTEXES);
    poly = (NJS_POLYGON_VTX *)syMalloc(sizeof (NJS_POLYGON_VTX) * MAX_POLYGON_VTX);

    /* 背景色の設定 */
	/* sets background color */
    njSetBackColor(ARGB(0, 0, 0, 0), ARGB(0, 0, 0, 0), ARGB(0, 0, 0, 255));

    /* テクスチャバッファの確保 */
    /* sets necessary memory for work area of a texture */
    njInitTextureBuffer((Sint8 *)vbuf,0x800*10);

    /* 頂点格納バッファの確保 */
    /* allocates the buffers for registration of vertex data */
    njInitVertexBuffer(MAX_VERTEXES * 64 / 4, 0, MAX_VERTEXES * 64 / 4, 0, 0);
	njInitPrint( NULL, 0, NJD_TEXFMT_ARGB_1555 );

    /* マトリクススタックの初期化 */
  	/* initializes the matrix stack */
    njInitMatrix(matrix, MAX_MATRIX_DEPTH, 1);

    /* 3Dシステムの初期化 */
    /* initializes 3D system  */
    njInit3D(vbuf, MAX_VERTEXES);

    /* 光源の初期化 */
    /* initializes the light source  */
    njCreateLight(&light, NJD_DIR_LIGHT);

    /* ビューの初期化 */
    /* initializes the user-defined view */
    njInitView(&view);

    /* ビューの設定 */
	/* specifies the current view */
    njSetView(&view);

    njInitTexture(texmemlist, 2);
    /*一枚のテクスチャーを設定*/
    /* sets data into texture name structure */
    njSetTextureName(&texname[0], "TEST02a.pvr", 0, NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
    njSetTextureName(&texname[1], "TEST02b.pvr", 1, NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
    njLoadTexture( &texlist);
    njSetTexture(&texlist);

    /*debug print color(焼付き防止)*/
    /* sets the text color */
    njPrintColor(0xffc0c0c0);

/*---------------------------------
    POLYGON
----------------------------------*/
    poly[0].x = 0.F;
    poly[0].y = 0.F;
    poly[0].z = 5.0F;
    poly[0].col = 0x77000000;
    poly[1].x = 0.F;
    poly[1].y = 480.F;
    poly[1].z = 5.0F;
    poly[1].col = 0x77000000;
    poly[2].x = 640.F;
    poly[2].y = 0.F;
    poly[2].z = 5.0F;
    poly[2].col = 0x77000000;
    poly[3].x = 640.F;
    poly[3].y = 480.F;
    poly[3].z = 5.0F;
    poly[3].col = 0x77000000;

}

/*----------------------------------------------------------------------
         メイン
         main
----------------------------------------------------------------------*/
Sint32 njUserMain(void)
{
    static PDS_PERIPHERAL *input;
    static Bool pause = FALSE; /*flag for pausing*/
    static Sint16 pauseCnt = 1;
    static float printSize = 0;
    static float yy = 0;

    input = pdGetPeripheral(PDD_PORT_A0);/*get key state*/
    /*Checking key*/
    if (input->press & PDD_DGT_ST) {
        pause = !pause;
    }
    /*PAUSEの文字を半透明＆拡大し、スクロールさせる。*/
    /* pause : magnifies and scroll the 'PAUSE' string(translucent) */
    if (pause) {
        njPrintColor(0x88c0c000);
        njPrintC(NJM_LOCATION(0, (Sint16)yy), "PAUSE");
        if (printSize < 130){
            njPrintSize((Sint16)printSize);
            printSize += 1.0F;
        } else {
            /*半透明ポリゴン全画面表示*/
            /* draw the translucent polygon on full screen */
            njDrawPolygon(poly, 4, TRUE);
            if (yy < 5) {
                yy += 0.03F;
            } else {
                yy = 0;
            }
        }
    } else {    /*ポーズ中は表示データの更新はしない*/
                /* don't update data during pause   */
        njPrintColor(0xffc0c0c0);
        njPrintSize(16);
        njPrintC(NJM_LOCATION(4, 24), "mipmap : off");
        njPrintC(NJM_LOCATION(20, 24), "mipmap : on");

        /*バイリニアフィルター*/
        /* bilinear filter    */
        if (bilinear){
            njTextureFilterMode(NJD_TEXTUREFILTER_BILINEAR);
            njPrintC(NJM_LOCATION(13, 7), "BILINEAR : on");
        }else{
            njPrintC(NJM_LOCATION(13, 7), "BILINEAR : off");
            njTextureFilterMode(0);
        }
        /*ポーズ用変数の初期化 */
        /* initialize pause flag */
        printSize = 0;
        yy = 0;

        /* サンプル名の表示 */
		/* prints the sample program name */
        njPrintC(NJM_LOCATION(10, 5), "Ninja Sample MipMap");

        /* マトリクススタックのクリア */
        /* clears the matrix stack */
        njClearMatrix();
    }
    /*mipmapなしスプライトの表示*/
    /* draws the sprite without mipmap */
    njSetTextureNum(0);
    sprite.tlist = &texlist;
    sprite.ang = 0x0000;
    sprite.sx = c1;
    sprite.sy = c2;
    sprite.p.x = 150.0F;
    sprite.p.y = 240.0F;
    sprite.tanim = anim;
    njDrawSprite2D(&sprite, 0, -1500.f, NJD_SPRITE_ANGLE | NJD_SPRITE_ALPHA);

    /*mipmap付きスプライトの表示*/
    /* draws the sprite with mipmap */
    sprite.tlist = &texlist;
    sprite.ang = 0x0000;
    sprite.sx = c1;
    sprite.sy = c2;
    sprite.p.x = 400.0F;
    sprite.p.y = 240.0F;
    sprite.tanim = &anim[1];
    njDrawSprite2D(&sprite, 0, SPRITE_Z, NJD_SPRITE_ANGLE | NJD_SPRITE_ALPHA);
    if (!pause) {
        /*スプライトのサイズ更新・バイリニアのON,OFF*/
        /* updates sprite size and turn on(off) biliear filter */
        if(DIRECTION && c1 < 1){
            c1 += SIZE_CHANGE;
            c2 += SIZE_CHANGE;
        }else{
            DIRECTION = FALSE;
            c1 -= SIZE_CHANGE;
            c2 -= SIZE_CHANGE;
            if(c1 < 0.F){
                DIRECTION = TRUE;
                bilinear = !bilinear;
            }
        }
    }
    return NJD_USER_CONTINUE;
}

/*----------------------------------------------------------------------
         終了処理
         exit
----------------------------------------------------------------------*/
void njUserExit(void)
{
	njExitPrint();

    /*テクスチャーの開放*/
    /* exits texture use */
    njExitTexture();

    /*matrixとvbufのクリア*/
	/* frees the memory area */
    syFree(matrix);
    syFree(vbuf);

    /* システムの終了 */
    /* exits the system    */
    sbExitSystem();
}

