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

  タイトル ： スプライト（四角形ポリゴン）を回転表示する
              sprite (rotation)
  編集履歴 ： 98-04-23 スプライト用に１枚のpvrを設定
                       スプライト構造体の回転角を更新
              98-06-19 SET5対応
              98-07-14 pause機能(半透明ポリゴンの表示)、
                       焼付き防止対策
              98-09-16 テクスチャバッファ確保のサイズ変更
              99-04-07 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 100
#define MAX_VERTEXES 1000
#define SPRITE_Z -150.F
#define SPRITE_ANG 0x0050
#define MAX_POLYGON_VTX 10

NJS_TEXNAME texname[1] ;
NJS_SPRITE sprite;
NJS_TEXLIST texlist = {texname, 1};
static NJS_MATRIX *matrix;
static NJS_VERTEX_BUF *vbuf;
static NJS_POLYGON_VTX *poly;
NJS_VIEW view;
NJS_LIGHT light;
NJS_TEXANIM anim[] = {{256, 256, 128, 128, 0, 0, 255, 255, 0, 0}};
NJS_TEXMEMLIST  texmemlist[1];


/*----------------------------------------------------------------------
           初期化
----------------------------------------------------------------------*/
void njUserInit(void)
{
    /* システムの初期化 */
    /* Initializes the Dreamcast H/W and readies the SEGA library */
    sbInitSystem(NJD_RESOLUTION_320x240_NTSCNI, NJD_FRAMEBUFFER_MODE_RGB565, 1);

    /*matrixとvertexバッファの確保*/
	/* initialize matrix stack */
    matrix = (NJS_MATRIX *)syMalloc(sizeof (NJS_MATRIX) * MAX_MATRIX_DEPTH);

    /* allocates the vertex calculation work */
    vbuf = (NJS_VERTEX_BUF *)syMalloc(sizeof (NJS_VERTEX_BUF) * MAX_VERTEXES);

    /* allocates the polygon structure */
    poly = (NJS_POLYGON_VTX *)syMalloc(sizeof (NJS_POLYGON_VTX) * MAX_POLYGON_VTX);

    /* 背景色の設定 */
    /* sets the background */
    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 );

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

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

    /* 光源の初期化 */
	/* create a light */
    njCreateLight(&light, NJD_DIR_LIGHT);
    /* ビューの初期化 */
    /* initilaize view */
    njInitView(&view);
    /* ビューの設定 */
    /* set view */
    njSetView(&view);
/*--------------------------------------
         テクスチャー設定
         setup texture
----------------------------------------*/
     njInitTexture( texmemlist, 1 );
    /*一枚のテクスチャーを設定*/
    /* set only 1 texture */
    njSetTextureName(&texname[0],"sprite.pvr",0,NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
    /*テクスチャーロード*/
    /* loads the textuer */
    njLoadTexture( &texlist);
    /*テクスチャーの登録*/
    /* set current texture list */
    njSetTexture(&texlist);
    /*使うテクスチャーNo．*/
    /* set current texture */
    njSetTextureNum(0);
    /*スプライトの設定*/
    /* set sprite surface */
    sprite.tlist = &texlist;
    sprite.sx = 0.4f;
    sprite.sy = 0.4f;
    sprite.p.x = 160.0f;
    sprite.p.y = 120.0f;
    sprite.tanim = anim;
    /*回転角の初期化*/
    /* initilaize sprite angle */
    sprite.ang = 0x0000;

/*---------------------------------
    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;
    static Uint32 print_col = 0x11c00000;
    input = pdGetPeripheral(PDD_PORT_A0);   /*get key state*/
    /*checking key*/
    if (input->press & PDD_DGT_ST){
        pause = !pause;
    }
    /* マトリクススタックのクリア */
    /* clear matrix stack */
    njClearMatrix();
    /*スプライトの描画*/
    /* draw a sprite */
    njDrawSprite2D(&sprite, 0, SPRITE_Z, NJD_SPRITE_ANGLE);
    /* サンプル名の表示 */
    /* prints the sample program name */
    njPrintSize(8);
    njPrintColor(0xffc0c0c0);
    njPrintC(NJM_LOCATION(6, 5), "Ninja Sample ROT SPRITE 1");
    if(pause) {             /*文字色/サイズの更新*/
                            /* change text color & size */
        njPrintSize(20);
        njPrintColor(print_col);
        njPrintC(NJM_LOCATION(5, 4), "PAUSE");
        if (print_col < 0xffc0c0c0) {
            print_col += 0x03000000;
        }
        /*半透明ポリゴンの描画*/
        /* draw translucent polygon */
        njDrawPolygon(poly, 4, TRUE);
    } else{
        /*pause中でないなら回転角を更新*/
        /* change sprite angle */
        sprite.ang += SPRITE_ANG;
    }
    return NJD_USER_CONTINUE;
}

/*----------------------------------------------------------------------
         終了処理
         exit
----------------------------------------------------------------------*/
void njUserExit(void)
{
	njExitPrint();
    /*テクスチャーの開放*/
    /* free texture memory */
    njExitTexture();
    /*matrixとvbuf、polyの開放*/
    syFree(matrix);
    syFree(vbuf);
    syFree(poly);
    /* システムの終了 */
    /* exit system */
    sbExitSystem();
}




































