/*
    Gdfs Sample

    COPYRIGHT (C) SEGA ENTERSRISES,LTD.

    FileRead Sample for SET5

    1998.6.4
*/

#include <shinobi.h>
#include <sg_gd.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]);
    }
}


/*** reading file routine ***/


Sint32 ReadFromFile(Sint8 *fname, GDFS_DIRREC dirrec, void *buf)
{
    GDFS gdfs;
    Sint32 ret;
    Sint32 size;

    sprintf(msgbuf, "Filename: %s", fname);
    PutMsg(msgbuf);

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

    if (gdfs == NULL) return -1;

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

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

    /* read file */
    ret = gdFsRead(gdfs, size, buf);
    sprintf(msgbuf, "gdFsRead: %d", (int) ret);
    PutMsg(msgbuf);

    /* close file */
    gdFsClose(gdfs);

    if (ret == GDD_ERR_OK) return 0;

    return -2;
}


/*** 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[8192/4];
Uint32 ReadBuf2[8192/4];
Uint32 ReadBuf3[8192/4];

Uint32 dirbuf[gdFsGetDirrecSize(64)/4];
GDFS_DIRREC sub2;


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

    switch (mode){
    case 0:
        /* load directory to buffer */
        sub2 = gdFsCreateDirhn((void *) dirbuf, 64);
        ret = gdFsLoadDir("SUBDIR2", sub2);

        mode = 1;

    case 1:
        /* read a file on RootDirector */
        ret = ReadFromFile("ABC.DAT", NULL, (void *) ReadBuf1);

        if (ret != 0) mode = 99;
        else mode = 2;
        break;

    case 2:
        /* read a file with changing directory */
        gdFsChangeDir("SUBDIR1");

        ret = ReadFromFile("FFFF_FILLDATA.DAT", NULL, (void *) ReadBuf2);

        if (ret != 0) mode = 99;
        else mode = 3;
        break;

    case 3:
        /* read a file from other directory */

        ret = ReadFromFile("8888_FILLDATA.DAT", sub2, (void *) ReadBuf3);

        if (ret != 0) mode = 99;
        else mode = 4;
        break;

    case 4:

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

    default:
        break;
    }

    njPrintC(NJM_LOCATION(2,2), "GDFS SAMPLE");
    for (i = MAX_WIN_LINE; i > 0; i--) {
        njPrintC(NJM_LOCATION(2,4+i-1), MessageWin[i-1]);
    }

    return NJD_USER_CONTINUE;
}


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

/* eof */
