/*---------------------------------------------------------------------------*
  Project:  NITRO-System - build - demos - g2d - Text - textdemolib
  File:     cmn.h

  Copyright 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: cmn.h,v $
  Revision 1.1  05/20/2005 05:52:37 AM  hatamoto_minoru
  Registered in NitroSystem.

  Revision 1.5  03/10/2005 08:18:55 AM  hatamoto_minoru
  Added support for the changes to the API.
  Added comments.

  Revision 1.4  02/08/2005 04:06:59 AM  hatamoto_minoru
  Revised file headers.

  Revision 1.3  02/01/2005 11:52:06 AM  hatamoto_minoru
  Fixed typos.

  Revision 1.2  01/25/2005 01:49:34 AM  hatamoto_minoru
  Invalidated the CallTrace for debugging.

  Revision 1.1  01/21/2005 01:03:09 AM  hatamoto_minoru
  Registered to TextDraw.

  Revision 1.4  11/30/2004 09:18:25 AM  hatamoto_minoru
  Made additions, since the include was insufficient.

  Revision 1.3  09/29/2004 08:46:57 AM  hatamoto_minoru
  Added file operation functions and time constants.

  Revision 1.2  09/15/2004 08:57:52 AM  hatamoto_minoru
  Added CMN_LoadFile.

  Revision 1.1  09/10/2004 11:21:25 AM  hatamoto_minoru
  Registered the initial version.

  $NoKeywords: $
 *---------------------------------------------------------------------------*/
#ifndef CMN_H_
#define CMN_H_


#include <nitro/types.h>
#include <nitro/os/common/interrupt.h>
#include <nitro/os/common/arena.h>
#include <nitro/os/common/alloc.h>
#include <nnsys/fnd/allocator.h>
#include <nnsys/fnd/archive.h>

#ifdef __cplusplus
extern "C" {
#endif



#define ARY_SIZEOF(ary)     ( sizeof(ary) / sizeof( (ary)[0] ) )
#define ROUNDUP_DIV(a, b)   (( (a) + ((b) - 1) ) / (b))

const static float ONE_FRAME_US = 16715.1;  // 1 frame = 16715.1 us
const static float ONE_VBLANK_US = 4512.4;  // V-blank period = 4512.4 us


typedef struct CMNGamePad
{
    u16     trigger;
    u16     release;
    u16     button;
}
CMNGamePad;



extern CMNGamePad CMNGamePadState;



void CMN_InitInterrupt( void );
void CMN_BeginVBlankIntr( OSIrqFunction vBlankFunc );
void CMN_InitAllocator( NNSFndAllocator* pAllocator );
void CMN_InitFileSystem( NNSFndAllocator* pAllocator );
void CMN_ClearVram( void );
void CMN_ReadGamePad(void);
u32 CMN_LoadFile(void** ppFile, const char* fpath, NNSFndAllocator* pAlloc);
void CMN_UnloadFile(void* pFile, NNSFndAllocator* pAlloc);
NNSFndArchive* CMN_LoadArchive(const char* name, const char* path, NNSFndAllocator* pAllocator);
void CMN_RemoveArchive(NNSFndArchive* archive, NNSFndAllocator* pAllocator);


static inline u16 CMN_IsTrigger(u16 key)
{
    return (u16)(CMNGamePadState.trigger & key);
}
static inline u16 CMN_IsRelease(u16 key)
{
    return (u16)(CMNGamePadState.release & key);
}
static inline u16 CMN_IsPress(u16 key)
{
    return (u16)(CMNGamePadState.button & key);
}

static inline void CMN_WaitVBlankIntr(void)
{
    OS_WaitIrq(TRUE, OS_IE_V_BLANK);
}

static inline void CMN_SetPlaneVisible(GXPlaneMask plane)
{
    GX_SetVisiblePlane(GX_GetVisiblePlane() | plane);
}

static inline void CMN_SetPlaneInvisible(GXPlaneMask plane)
{
    GX_SetVisiblePlane(GX_GetVisiblePlane() & ~plane);
}





#ifdef __cplusplus
}/* extern "C"*/
#endif

#endif // CMN_H_


