/*---------------------------------------------------------------------------*
  Project:   NitroSystem - build - demos - g2d - demolib
  File:     loader.c

  Copyright 2004,2005 Nintendo.  All rights reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law.  They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Log: loader.c,v $
  Revision 1.11  06/29/2005 04:43:58  hatamoto_minoru
  Fixed typos: NitroSyetem -> NitroSystem

  Revision 1.10  06/23/2005 06:32:07  kitani_toshikazu
  Added G2DDemo_LoadNCGREx()

  Revision 1.9  02/22/2005 00:43:24  hatamoto_minoru
  Revised include file names.
  Response for warnings about implicit sign/unsign conversion.

  Revision 1.8  11/09/2004 08:31:37  kitani_toshikazu
  Added a screen load function

  Revision 1.7  07/08/2004 01:39:29  hatamoto_minoru
  Added function to read multicell-related files
  Revised comments

  Revision 1.6  06/14/2004 06:13:06  hatamoto_minoru
  Replaced tabs with spaces

  Revision 1.5  06/11/2004 02:15:49  hatamoto_minoru
  Fixed a problem that caused an error to occur in the debug build

  Revision 1.4  06/10/2004 08:43:51  hatamoto_minoru
  Changed the buffer used for reading files so that it can be freed by the user.
   

  Revision 1.3  06/08/2004 07:36:40  hatamoto_minoru
  Changed so that memory management performed by the heap manager

  Revision 1.2  06/03/2004 08:52:31  hatamoto_minoru
  Adjusted source

  Revision 1.1  06/03/2004 04:52:51  nishida
  Registered the initial version.

  $NoKeywords: $
 *---------------------------------------------------------------------------*/
#include "g2d_demolib.h"
#include <nnsys/g2d/g2d_Softsprite.h>
#include <nnsys/g2d/g2d_Load.h>


/*---------------------------------------------------------------------------*
  Name:         LoadFileToMainMem_

  Description:  Allocates a buffer and loads a file.

  Arguments:    pFname:        The name of the file to load.

  Returns:      If loading is successful, this function returns a pointer to a buffer storing the contents of the file.
                  This buffer must be freed using G2DDemo_Free once it is no longer necessary.
                 
                If the load fails, the function returns NULL.
 *---------------------------------------------------------------------------*/
static void* LoadFileToMainMem_( const char* pFname )
{
    FSFile  file;
    void*   pFile = NULL;
    BOOL    bSuccess;

    SDK_NULL_ASSERT( pFname );

    FS_InitFile( &file );
    bSuccess = FS_OpenFile( &file, pFname );
    
    if( bSuccess )
    {
    	const u32 szFile    = FS_GetLength( &file );


        pFile = G2DDemo_Alloc( szFile );
        SDK_NULL_ASSERT( pFile );

        if( szFile != (u32)FS_ReadFile( &file, pFile, (s32)szFile ) )
        {
            G2DDemo_Free( pFile );
            pFile = NULL;
        }

        bSuccess = FS_CloseFile( &file );
        SDK_ASSERT( bSuccess );
    }else{
        OS_Warning(" Can't find the file : %s ", pFname );
    }

    return pFile;
}

/*---------------------------------------------------------------------------*
  Name:         G2DDemo_LoadNCER

  Description:  Allocates a buffer internally and reads a cell data bank
                from an NCER file.
                After the function is successful, the buffer
                must be freed using G2DDemo_Free(return_value) once the cell data bank is no longer needed.

  Arguments:    ppCellBank: A pointer that takes a pointer to the buffer storing the cell data.
                             
                pFname:     Name of the NCER file where the cell data bank is stored.

  Returns:      If the read is successful, returns a pointer to the
                internally allocated buffer holding the file data;
                If the load fails, the function returns NULL.
 *---------------------------------------------------------------------------*/
void* G2DDemo_LoadNCER( NNSG2dCellDataBank** ppCellBank, const char* pFname )
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT( ppCellBank );
    SDK_NULL_ASSERT( pFname );

    pFile = LoadFileToMainMem_( pFname );
    if( pFile != NULL )
    {
        if( NNS_G2dGetUnpackedCellBank( pFile, ppCellBank ) )
        {
            NNS_G2dPrintCellBank( *ppCellBank );
            return pFile;
        }

        G2DDemo_Free( pFile );
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         G2DDemo_LoadNANR

  Description:  Internally allocates a buffer and reads an animation data bank
                from an NANR file.
                After the function is successful, the buffer
                must be freed using G2DDemo_Free(return_value) once the animation data bank is no longer needed.

  Arguments:    ppAnimBank: A pointer that takes a pointer to the buffer holding
                            the animation data bank.
                pFname:     The NANR file name storing the animation data bank.
                             

  Returns:      If the read is successful, returns a pointer to the
                internally allocated buffer holding the file data;
                If the load fails, the function returns NULL.
 *---------------------------------------------------------------------------*/
void* G2DDemo_LoadNANR( NNSG2dAnimBankData** ppAnimBank, const char* pFname )
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT( ppAnimBank );
    SDK_NULL_ASSERT( pFname );

    pFile = LoadFileToMainMem_( pFname );
    if( pFile != NULL )
    {
        if( NNS_G2dGetUnpackedAnimBank( pFile, ppAnimBank ) )
        {
            NNS_G2dPrintAnimBank( *ppAnimBank );
            return pFile;
        }

        G2DDemo_Free( pFile );
    }

    return NULL;
}
/*---------------------------------------------------------------------------*
  Name:         G2DDemo_LoadNMCR

  Description:  Internally allocates a buffer and reads a multicell data bank
                from an NMCR file.
                After the function is successful, the buffer
                must be freed using G2DDemo_Free(return_value) once the multicell data bank is no longer needed.

  Arguments:    ppMCBank:   A pointer that takes a pointer to the buffer storing the multicell data bank.
                             
                pFname:     The NMCR file name storing the multicell data bank.
                             

  Returns:      If the read is successful, returns a pointer to the
                internally allocated buffer holding the file data;
                If the load fails, the function returns NULL.
 *---------------------------------------------------------------------------*/
void* G2DDemo_LoadNMCR( NNSG2dMultiCellDataBank** ppMCBank, const char* pFname )
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT( ppMCBank );
    SDK_NULL_ASSERT( pFname );

    pFile = LoadFileToMainMem_( pFname );
    if( pFile != NULL )
    {
        if( NNS_G2dGetUnpackedMultiCellBank( pFile, ppMCBank ) )
        {
            NNS_G2dPrintMultiCellBank ( *ppMCBank );
            return pFile;
        }

        G2DDemo_Free( pFile );
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         G2DDemo_LoadNMAR

  Description:  Internally allocates a buffer and reads a multicell animation data bank
                from an NMAR file.
                After the function is successful, the buffer
                must be freed using G2DDemo_Free(return_value) once the multicell animation data bank is no longer needed.

  Arguments:    ppMCABank:  A pointer that takes a pointer to the buffer storing the multicell animation data.
                            the multicell animation data bank
                pFname:     NMAR file name storing the multicell animation data bank.
                             

  Returns:      If the read is successful, returns a pointer to the
                internally allocated buffer holding the file data;
                If the load fails, the function returns NULL.
 *---------------------------------------------------------------------------*/
void* G2DDemo_LoadNMAR( NNSG2dMultiCellAnimBankData** ppMCABank, const char* pFname )
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT( ppMCABank );
    SDK_NULL_ASSERT( pFname );

    pFile = LoadFileToMainMem_( pFname );
    if( pFile != NULL )
    {
        if( NNS_G2dGetUnpackedMCAnimBank( pFile, ppMCABank ) )
        {
            NNS_G2dPrintAnimBank( *ppMCABank );
            return pFile;
        }

        G2DDemo_Free( pFile );
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         G2DDemo_LoadNCGR

  Description:  Internally allocates a buffer and reads character-format character data
                from an NCGR file.
                After the function is successful, the buffer
                must be freed using G2DDemo_Free(return_value) once the cell data bank is no longer needed.

  Arguments:    ppCharData: A pointer that takes a pointer to the buffer storing the character data.
                             
                pFname:     Name of NCGR file where the character data is stored.

  Returns:      If the read is successful, returns a pointer to the
                internally allocated buffer holding the file data;
                If the load fails, the function returns NULL.
 *---------------------------------------------------------------------------*/
void* G2DDemo_LoadNCGR( NNSG2dCharacterData** ppCharData, const char* pFname )
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT( ppCharData );
    SDK_NULL_ASSERT( pFname );

    pFile = LoadFileToMainMem_( pFname );
    if( pFile != NULL )
    {
        if( NNS_G2dGetUnpackedCharacterData( pFile, ppCharData ) )
        {
            NNS_G2dPrintCharacterData( *ppCharData );
            return pFile;
        }

        G2DDemo_Free( pFile );
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         G2DDemo_LoadNCGR

  Description:  Internally allocates a buffer and reads character format character data
                from an NCGR file.
                After the function is successful, the buffer
                must be freed using G2DDemo_Free(return_value) once the character bank is no longer needed.

  Arguments:    ppCharData:     A pointer that takes a pointer to the buffer storing the character data.
                                 
                ppCharPosInfo:  Pointer that accepts a pointer to the buffer used to store character load position data.
                                  
                
                pFname:     Name of NCGR file where the character data is stored.

  Returns:      If the read is successful, returns a pointer to the
                internally allocated buffer holding the file data;
                If the load fails, the function returns NULL.
 *---------------------------------------------------------------------------*/
void* G2DDemo_LoadNCGREx
( 
    NNSG2dCharacterData**       ppCharData, 
    NNSG2dCharacterPosInfo**    ppCharPosInfo,
    const char*                 pFname 
)
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT( ppCharData );
    SDK_NULL_ASSERT( pFname );

    pFile = LoadFileToMainMem_( pFname );
    if( pFile != NULL )
    {
        if( NNS_G2dGetUnpackedCharacterData( pFile, ppCharData ) )
        {
            NNS_G2dPrintCharacterData( *ppCharData );
            
            if( NNS_G2dGetUnpackedCharacterPosInfo( pFile, ppCharPosInfo ) )
            {
                NNS_G2dPrintCharacterPosInfo( *ppCharPosInfo );   
                return pFile;
            }
        }    
    }
    
    G2DDemo_Free( pFile );
    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         G2DDemo_LoadNCLR

  Description:  Internally allocates a buffer and reads palette data from an NCLR file.
                 
                After the function is successful, the buffer must be freed using G2DDemo_Free(return_value) once
                the palette data bank is no longer needed.

  Arguments:    ppPltData: A pointer that takes a pointer to the buffer storing the palette data.
                              
                pFname:     Name of NCLR file where the palette data is stored.

  Returns:      If the read is successful, returns a pointer to the
                internally allocated buffer holding the file data;
                If the load fails, the function returns NULL.
 *---------------------------------------------------------------------------*/
void* G2DDemo_LoadNCLR( NNSG2dPaletteData** ppPltData, const char* pFname )
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT( ppPltData );
    SDK_NULL_ASSERT( pFname );

    pFile = LoadFileToMainMem_( pFname );
    if( pFile != NULL )
    {
        if( NNS_G2dGetUnpackedPaletteData( pFile, ppPltData ) )
        {
            NNS_G2dPrintPaletteData( *ppPltData );
            return pFile;
        }

        G2DDemo_Free( pFile );
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         G2DDemo_LoadNENR

  Description:  Internally allocates a buffer and reads the entity data bank from an NENR file.
                  
                After this function is successful, the buffer must be freed using G2DDemo_Free(return_value) once
                the entity data bank is no longer needed.

  Arguments:    ppEntityBank: pointer that takes a pointer to the buffer storing
                            the entity data bank.
                pFname:     The NENR file name storing the entity data bank.
                             

  Returns:      If the read is successful, returns a pointer to the
                internally allocated buffer holding the file data;
                If the load fails, the function returns NULL.
 *---------------------------------------------------------------------------*/
void* G2DDemo_LoadNENR( NNSG2dEntityDataBank** ppEntityBank, const char* pFname )
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT( ppEntityBank );
    SDK_NULL_ASSERT( pFname );

    pFile = LoadFileToMainMem_( pFname );
    if( pFile != NULL )
    {
        if( NNS_G2dGetUnpackedEntityBank( pFile, ppEntityBank ) )
        {
            NNS_G2dPrintEntityDataBank( *ppEntityBank );
            return pFile;
        }

        G2DDemo_Free( pFile );
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         G2DDemo_LoadNCBR

  Description:  Internally allocates a buffer and reads bitmap format character data
                from an NCBR file.
                After the function is successful, the buffer
                must be freed using G2DDemo_Free(return_value) once the character data is no longer needed.

  Arguments:    ppCharData: A pointer that takes a pointer to the buffer storing the character data.
                              
                pFname:     Name of NCBR file where the character data is stored.

  Returns:      If the read is successful, returns a pointer to the
                internally allocated buffer holding the file data;
                If the load fails, the function returns NULL.
 *---------------------------------------------------------------------------*/
void* G2DDemo_LoadNCBR( NNSG2dCharacterData** ppCharData, const char* pFname )
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT( ppCharData );
    SDK_NULL_ASSERT( pFname );

    pFile = LoadFileToMainMem_( pFname );
    if( pFile != NULL )
    {
        if( NNS_G2dGetUnpackedCharacterData( pFile, ppCharData ) )
        {
            NNS_G2dPrintCharacterData( *ppCharData );
            return pFile;
        }

        G2DDemo_Free( pFile );
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         G2DDemo_LoadNSCR

  Description:  Internally allocates a buffer and reads bitmap format character data
                from an NCBR file.
                After the function is successful, the buffer
                must be freed using G2DDemo_Free(return_value) once the character data is no longer needed.

  Arguments:    ppCharData: A pointer that takes a pointer to the buffer storing the character data.
                              
                pFname:     Name of NCBR file where the character data is stored.

  Returns:      If the read is successful, returns a pointer to the
                internally allocated buffer holding the file data;
                If the load fails, the function returns NULL.
 *---------------------------------------------------------------------------*/
void* G2DDemo_LoadNSCR( NNSG2dScreenData** ppScreenData, const char* pFname )
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT( ppScreenData );
    SDK_NULL_ASSERT( pFname );

    pFile = LoadFileToMainMem_( pFname );
    if( pFile != NULL )
    {
        if( NNS_G2dGetUnpackedScreenData( pFile, ppScreenData ) )
        {
            NNS_G2dPrintScreenData( *ppScreenData );
            return pFile;
        }

        G2DDemo_Free( pFile );
    }

    return NULL;
}

