/*--------------------------------------------------------------------------
  Project:  Manual 2D Library - m2d
  File:     objManager.h

  Copyright (C)2007-2009 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.

  $Revision: 1758 $
 *-------------------------------------------------------------------------*/

#ifndef NTMV_M2D_OBJMANAGER_H_
#define NTMV_M2D_OBJMANAGER_H_

#if defined(__CWCC__)
#pragma once
#endif

#include <nitro.h>
#include <nnsys/fnd.h>
#include <nnsys/g2d/g2d_OAM.h>
#include <nnsys/g2d/g2d_CellAnimation.h>

namespace ntmv
{
namespace m2d
{

class ObjectConstructor;

// Maximum number of cell animations that can be used at once
// If this is not enough, increase it to the appropriate amount and recompile.
#define OBJMANAGER_MAX_CELLANIM_NUM    128


// Class that manages OBJ rendering.
class ObjManager
{
    // Number of OBJs used by the OAM manager
    static const int    OBJMANAGER_NUM_OAM  = 128;

public:
   // Cell handle
   typedef int          CellHandle; 

   // Invalid cell handle
   static const CellHandle
                        InvalidCellHandle   = -1;

    static void         LoadCommonRes();

    // Configuration-related
    static GXOBJVRamModeChar
                        GetOBJVRamMappingMode();

    // Constructor
                        ObjManager();

    void                InitializeMain(NNSFndAllocator* pAllocator);
    void                InitializeSub(NNSFndAllocator* pAllocator);

    void                Finalize(const ObjectConstructor& objectConstructor);

    // Reset
    void                DisposeCellAnimAll();
    void                Dispose();
    
    // Cell animation
    CellHandle          CreateCellAnim( u16 sequeceID );
    bool                SetCellAnimSequenceToCell( CellHandle handle, u16 sequeceID );
    
    bool                TickCellAnim( CellHandle handle, fx32 step );
    bool                RestartCellAnim( CellHandle handle );
    bool                SetCellAnimFrame( CellHandle handle, u16 index );
    // Cell animation rendering
    bool                DrawCellAnim( CellHandle handle, fx32 x, fx32 y );
    
    void                TransferDataToHW();

   
private:
    NNSG2dCellAnimation*
                        GetCellAnimationByHandle_( CellHandle handle );

    NNSFndAllocator*    mpAllocator;

    void*               mpCellFile;
    void*               mpAnimFile;

    NNSG2dCellDataBank* mpCellBank;             // Cell data bank
    NNSG2dAnimBankData* mpAnimBank;             // Animation data bank
    NNSG2dCellAnimation mCellAnimationSet[OBJMANAGER_MAX_CELLANIM_NUM];
                                                // Cell animation set
    int                 mNumCellAnimationUsed;  // Current number of cell animations in use

    
    NNSG2dOamManagerInstance                    // OAM manager for OBJ output
                        mOamManager;

    // Temporary buffer for render functions
    GXOamAttr           mTempOamBuffer[OBJMANAGER_NUM_OAM]; // Temporary OAM buffer
};

}   // namespace m2d
}   // namespace ntmv

/* NTMV_M2D_OBJMANAGER_H_ */
#endif

