#include <shinobi.h>
#include <njdef.h>
#include "addr.h"
#include "test.h"

SMS_FNT_MODE_OBJ gSmsFntModeObj, *gSmsFntMode;

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

NJS_TEXNAME texname[2] ;
NJS_TEXLIST texlist = {texname, 2};
NJS_TEXMEMLIST  texmemlist[2];

Uint16 kanjiBuff[KANJI_BUFF_W*KANJI_BUFF_H];
Uint16 *kanjiBuffPtr;
NJS_TEXINFO	kanjiInfo[2];

NJS_TEXTURE_VTX	poly[4];

/*----------------------------------------------------------------------
           初期化
           initialize
----------------------------------------------------------------------*/
void njUserInit(void)
{
#if 1 /* 19990302 */
    /* システムの初期化 */
    /* Initializes the Dreamcast H/W and readies the SEGA library */
    switch( syCblCheck() ) {
    case SYE_CBL_VGA:
    	sbInitSystem(NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB565, 1);
    	break;
    case SYE_CBL_PAL:
    	sbInitSystem(NJD_RESOLUTION_320x240_PALNI, NJD_FRAMEBUFFER_MODE_RGB565, 1);
    	break;
    default:
    case SYE_CBL_NTSC:
    	sbInitSystem(NJD_RESOLUTION_320x240_NTSCNI, NJD_FRAMEBUFFER_MODE_RGB565, 1);
    	break;
    }
#else
    /* システムの初期化 */
    /* Initializes the Dreamcast H/W and readies the SEGA library */
    sbInitSystem(NJD_RESOLUTION_320x240_NTSCNI, NJD_FRAMEBUFFER_MODE_RGB565, 1);
#endif
    
    /* 背景色の設定 */
	/* sets background color */
    njSetBackColor(ARGB(0, 192, 192, 192), ARGB(0, 192, 192, 0), ARGB(0, 0, 192, 0));

   	/* 頂点格納バッファの確保 */
    /* allocates the buffers for registration of vertex data */
    njInitVertexBuffer(100000, 0, 1000000, 0, 0);

    njInitTexture(texmemlist, 2);

    njInitPrint(NULL,0,0) ;

    /*一枚のテクスチャーを設定*/
    /* initialize texture     */
	njSetTextureInfo(&kanjiInfo[0],kanjiBuff,NJD_TEXFMT_RGB_565|NJD_TEXFMT_RECTANGLE,KANJI_BUFF_W,KANJI_BUFF_H);
    njSetTextureName(&texname[0], &kanjiInfo[0], 0, NJD_TEXATTR_TYPE_MEMORY | NJD_TEXATTR_GLOBALINDEX);
	njSetTextureInfo(&kanjiInfo[1],kanjiBuff,NJD_TEXFMT_RGB_565|NJD_TEXFMT_RECTANGLE,KANJI_BUFF_W,KANJI_BUFF_H);
    njSetTextureName(&texname[1], &kanjiInfo[1], 1, NJD_TEXATTR_TYPE_MEMORY | NJD_TEXATTR_GLOBALINDEX);
    njLoadTexture(&texlist);
    njSetTexture(&texlist);

	poly[0].x = (320-KANJI_BUFF_W)/2;
	poly[0].y = (240-KANJI_BUFF_H)/2;
	poly[0].z = 0.5;
	poly[0].u = 0;
	poly[0].v = 0;
	poly[0].col = 0xFFFFFFFF;
	
	poly[1].x = poly[0].x;
	poly[1].y = poly[0].y+KANJI_BUFF_H;
	poly[1].z = 0.5;
	poly[1].u = 0;
	poly[1].v = 1.f;
	poly[1].col = 0xFFFFFFFF;
	
	poly[2].x = poly[0].x+KANJI_BUFF_W;
	poly[2].y = poly[0].y;
	poly[2].z = 0.5;
	poly[2].u = 1.f;
	poly[2].v = 0;
	poly[2].col = 0xFFFFFFFF;

	poly[3].x = poly[0].x+KANJI_BUFF_W;
	poly[3].y = poly[0].y+KANJI_BUFF_H;
	poly[3].z = 0.5;
	poly[3].u = 1.f;
	poly[3].v = 1.f;
	poly[3].col = 0xFFFFFFFF;

	kanjiBuffPtr = (Uint16 *)((Uint32)kanjiBuff | 0xa0000000);
	gSmsFntMode = &gSmsFntModeObj;
	initFntMode( gSmsFntMode );

}

/*----------------------------------------------------------------------
         メイン
         main
----------------------------------------------------------------------*/
Sint32 njUserMain(void)
{
	static Sint32 mode = 0;
	Sint32 i;
	Uint16 *addr;
	Uint16 color;

	chooseFont( gSmsFntMode );

	displayFont( gSmsFntMode );

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

    return NJD_USER_CONTINUE;
}

/*----------------------------------------------------------------------
         終了処理
         exit
----------------------------------------------------------------------*/
void njUserExit(void)
{
    /* テクスチャの開放 */
	/* exits texture use  */
    njExitTexture();
    /* システムの終了 */
    /* exits the system */
    sbExitSystem();
}
