/*
    Gdfs Sample

    COPYRIGHT (C) SEGA ENTERSRISES,LTD.

    FileRead Sample for SET5

    1998.8.24
*/

#include <shinobi.h>

#include "req.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;
Sint32 wait;

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();

    ReqInit();

    mode = 0;
    wait = 30;
}


GDFS filehn[8];
Uint32 *ReadBuf[8];

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

    switch (mode){
    case 0:
        if (--wait <= 0) mode = 1;
        break;
    
    case 1:
        mode = 2;
        
        PutMsg((Sint8 *) "Alloc Buffer:");
        
        /* alloc buffer */
        for (i = 0; i < 8; i++) {
            if ((ReadBuf[i] = syMalloc(32768)) == NULL) mode = 99;
        }
        break;
    
    case 2:
        mode = 3;
        
        PutMsg((Sint8 *) "File Open:");
        
        /* open file */
        for (i = 0; i < 8; i++) {
            sprintf((char *) msgbuf, "F%04X.", (int) i);
            if ((filehn[i] = gdFsOpen((char *) msgbuf, NULL)) == NULL) mode = 99;
        }
        break;
    
    case 3:
        mode = 4;
        
        PutMsg((Sint8 *) "Request:");
        
        /* request to read */
        for (i = 0; i < 8; i++) {
            ReqRead(filehn[i], 16, ReadBuf[i]);
        }
        break;
    
    case 4:
        ret = ReqExec();
        sprintf((char *) msgbuf, "Rest: %d", (int) ret);
        PutMsg(msgbuf);
        
        if (ret == 0) mode = 5;
        break;
    
    case 5:
        PutMsg((Sint8 *) "Close:");
        
        /* close file */
        for (i = 0; i < 8; i++) {
            gdFsClose(filehn[i]);
        }
        
        mode = 6;
        break;
    
    case 6:
        PutMsg((Sint8 *) "Complete:");
        mode = 99;
        break;

    default:
        break;
    }


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

    return NJD_USER_CONTINUE;
}


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

/* eof */
