/*
    Gdfs Sample

    COPYRIGHT (C) SEGA ENTERSRISES,LTD.

    FileRead Sample for SET5

    1998.6.4
*/

#include <shinobi.h>


/*** Message Scroll routine ***/

#define MAX_WIN_LINE  20
#define MAX_MSG_LEN  80


Sint8 *MessageWin[MAX_WIN_LINE];
Sint8 MessageWinWork[MAX_WIN_LINE*MAX_MSG_LEN];
Sint8 msgbuf[MAX_MSG_LEN];
Sint32 MsgLine;


void scpy(Sint8 *src, Sint8 *dst)
{
    while (*src != (Sint8) NULL) {
        *dst++ = *src++;
    }
    *dst = (Sint8) NULL;
}


void initMsg(void)
{
    Sint32 i;
    Sint8 **mesp;
    Sint8 *mp;

    MsgLine = 0;
    for (mesp = MessageWin, mp = MessageWinWork; mesp < MessageWin + MAX_WIN_LINE; mesp++) {
        *mesp = mp;
        *mp = 0;
        mp += MAX_MSG_LEN;
    }
}


void PutMsg(Sint8 *msg)
{
    Sint32 i;

    if (MsgLine < MAX_WIN_LINE) {
        scpy(msg, MessageWin[MsgLine]);

        MsgLine++;

    } else {
        Sint8 *tmp;

        tmp = MessageWin[0];
        for (i = 1; i < MAX_WIN_LINE; i++) {
            MessageWin[i - 1] = MessageWin[i];
        }
        MessageWin[MAX_WIN_LINE - 1] = tmp;

        scpy(msg, MessageWin[MAX_WIN_LINE - 1]);
    }
}


/*** main routine ***/


Sint32 mode;


void njUserInit(void)
{
    njSetBorderColor( 0 ) ;
    sbInitSystem( NJD_RESOLUTION_640x480_NTSCI, NJD_FRAMEBUFFER_MODE_RGB565, 1 );

    njInitVertexBuffer( 100000, 0, 100000, 0, 0 );
    njInitPrint(NULL, 0, 0);

    njSetBackColor(0x00000000,0x00000000,0x000000FF);

    njPrintSize(16);

    initMsg();

    mode = 0;
}



Uint32 *ReadBuf1;
char fname[] = "F0000.";
GDFS gdfs;
Sint32 rsize = 0;
Sint32 rnum = 0;

Sint32 njUserMain(void)
{
    Sint32 i;
    Sint32 ret;

    switch (mode){
    case 0:

    case 1:
        sprintf((char *) msgbuf, "Filename: %s", fname);
        PutMsg(msgbuf);

        mode = 2;
        break;

    case 2:
        /* open file */
        gdfs = gdFsOpen(fname, NULL);
        sprintf((char *) msgbuf, "gdFsOpen: %08x", (int) gdfs);
        PutMsg(msgbuf);

        if (gdfs == NULL) {
            mode = 99;
            break;
        }

        mode = 3;
        break;

    case 3:
        ReadBuf1 = syMalloc(0x180000);

        if (ReadBuf1 == NULL) {
            sprintf((char *) msgbuf, "not enough memory.");
            PutMsg(msgbuf);

            mode = 99;
            break;
        }

        mode = 4;
        break;

    case 4:
        /* get file size */
        gdFsGetFileSize(gdfs, &rsize);
        sprintf((char *) msgbuf, "FileSize: %d", (int) rsize);
        PutMsg(msgbuf);

        /* calc sctor size */
        rsize = gdFsCalcSctSize(rsize);
        sprintf((char *) msgbuf, "FileSctSize: %d", (int) rsize);
        PutMsg(msgbuf);

        mode = 5;
        break;

    case 5:
        /* read file */
        ret = gdFsReqRd32(gdfs, rsize, ReadBuf1);
        sprintf((char *) msgbuf, "gdFsReqRd32: %d", (int) ret);
        PutMsg(msgbuf);

        if (ret < 0) {
            sprintf((char *) msgbuf, "can't read.");
            PutMsg(msgbuf);

            mode = 99;
            break;
        }

        mode = 6;
        break;

    case 6:
        rnum = gdFsGetNumRd(gdfs);

        if (gdFsGetStat(gdfs) == GDD_STAT_COMPLETE) {
            mode = 7;
            break;
        }
        break;

    case 7:
        /* close file */
        gdFsClose(gdfs);

        PutMsg((Sint8 *) "Complete:");
        mode = 99;
        break;

    default:
        break;
    }

    njPrintC(NJM_LOCATION(2,2), "GDFS SAMPLE");

    if (rsize > 0) {
        sprintf((char *) msgbuf, "%d (%d/%d)", rnum, rnum*100/rsize/2048, 100);
        njPrintC(NJM_LOCATION(2,4), (char *) msgbuf);
    }

    for (i = MAX_WIN_LINE; i > 0; i--) {
        njPrintC(NJM_LOCATION(2,6+i-1), (char *) MessageWin[i-1]);
    }

    return NJD_USER_CONTINUE;
}


void njUserExit(void)
{
    njExitPrint();
    sbExitSystem();
}

/* eof */
