
#define __SCENE_C__

/*--------------------
   組み込みファイル名
  --------------------*/
#include "scene.h"
#include "smputil.h"

/*-----------------------
   CAMERA のメモリの確保
  -----------------------*/
CAMERA *CameraAllocate(Uint32 nb)
{
	CAMERA *top, *cam;
	Sint32  i;
	cam = smpMemAlloc(sizeof(CAMERA)*nb);
	top = cam;
	for(i=0; i<(Sint32)nb; i++, cam++){
		cam->Rotation.x		= 0.0f;
		cam->Rotation.y		= 0.0f;
		cam->Rotation.z		= 0.0f;
		cam->Position.x		= 0.0f;
		cam->Position.y		= 0.0f;
		cam->Position.z		= 20.0f;
		cam->Interest.x		= 0.0f;
		cam->Interest.y		= 0.0f;
		cam->Interest.z		= 0.0f;

		cam->NearPlane		= 1.0f;
		cam->FarPlane		= 10000.0f;

		cam->FieldOfView	= 45.0f;
	}
	return(top);
}

/*-----------------------
   CAMERA のメモリの解放
  -----------------------*/
void CameraDispose(Sint32 nbCam, CAMERA *cam)
{
	UNREFERENCED_PARAMETER(nbCam);
	if( cam != NULL ){
		smpMemFree( cam );
	}
}

/*----------------------
   LIGHT のメモリの確保
  ----------------------*/
LIGHT *LightAllocate(Uint32 nb)
{
	LIGHT *top, *lit;
	Sint32 i;
	lit = smpMemAlloc(sizeof(LIGHT)*nb);
	top = lit;
	for(i=0; i<(Sint32)nb; i++, lit++){
		lit->Type			= NND_LIGHTTYPE_PARALLEL;
		lit->pLight			= NULL;

		lit->Color.r		= lit->Color.g = lit->Color.b = lit->Color.a = 1.0f;
		lit->Intensity		= 1.0f;
		lit->Position.x		= 0.0f;
		lit->Position.y		= 0.0f;
		lit->Position.z		= 1.0f;
		lit->FallOffStart	= 10.0f;
		lit->FallOffEnd		= 10.0f;
	}
	return(top);
}

/*----------------------
   LIGHT のメモリの解放
  ----------------------*/
void LightDispose(Sint32 nbLit, LIGHT *lit)
{
	LIGHT *top;
	Sint32 i;
	if( lit != NULL ){
		top = lit;
		for(i=0; i<nbLit; i++, lit++){
			if(lit->pLight != NULL){
				smpMemFree(lit->pLight);
			}
		}
		smpMemFree( top );
	}
}

/*----------------------
   MODEL のメモリの確保
  ----------------------*/
MODEL *ModelAllocate(Uint32 nb)
{
	MODEL *top, *mdl;
	Sint32 i;
	mdl = smpMemAlloc(sizeof(MODEL)*nb);
	top = mdl;
	for(i=0; i<(Sint32)nb; i++, mdl++){
		mdl->RenderToTexFlg	= NNE_OFF;

		mdl->Scaling.x		= 1.0f;
		mdl->Scaling.y		= 1.0f;
		mdl->Scaling.z		= 1.0f;
		mdl->Translation.x	= 0.0f;
		mdl->Translation.y	= 0.0f;
		mdl->Translation.z	= 0.0f;

		mdl->pMatrix = smpMemAlloc(sizeof(NNS_MATRIX));
		nnMakeUnitMatrix( mdl->pMatrix);

		mdl->pObject		= NULL;
#if 1 /* 2008.09.10 */
		mdl->pObjectBuf		= NULL;
#else
		mdl->pObjectBase	= NULL;
		mdl->pObjectDataBuf	= NULL;
#endif
		mdl->pPliableObject	= NULL;
		mdl->pMtxList		= NULL;	
		mdl->pMtxPal		= NULL;
		mdl->pNodestatList	= NULL;
		mdl->pTexList		= NULL;
		mdl->pTexListBuf	= NULL;

		mdl->pMotion		= NULL;
		mdl->pMotionMat		= NULL;

		mdl->pMotionBuf		= NULL;
		mdl->pMotionMatBuf	= NULL;

		mdl->pMatMotObj		= NULL;
	}
	return(top);
}

/*----------------------
   MODEL のメモリの解放
  ----------------------*/
void ModelDispose(Sint32 nbMdl, MODEL *mdl)
{
	MODEL *top;
	Sint32    i;
	if( mdl != NULL ){
		top = mdl;
		for(i=0; i<nbMdl; i++, mdl++){
			if( mdl->pMatrix != NULL){
				smpMemFree( mdl->pMatrix );
			}

			if( mdl->pPliableObject != NULL ){
#if 1 /* 2008.06.26(nnExitPliableObject(…)を呼ぶ必要あり) */
				/* 2008.09.10(オブジェクトの解放より先に呼ぶ) */
				nnExitPliableObject(mdl->pPliableObject);
#endif
				smpMemFree( mdl->pPliableObject );
			}
#if 1 /* 2008.09.10 */
			if(mdl->pObjectBuf != NULL){
				/* ※※※ オブジェクトに割り当てたD3Dリソース解放 ※※※ */
				nnDeleteVertexObjectDXG20( mdl->pObjectBuf );
				/* オブジェクトの解放 */
				smpMemFree( mdl->pObjectBuf );
			}
#else
			/* ※※※ オブジェクトに割り当てたD3Dリソース解放 ※※※ */
			nnDeleteVertexObjectDXG20( mdl->pObjectBase );
			//
			smpMemFree( mdl->pObjectDataBuf );
#endif
			/* マトリクスリストの解放 */
			if( mdl->pMtxList != NULL ){
				smpMemFree( mdl->pMtxList );
			}
			/* マトリクスパレット領域の解放 */
			if( mdl->pMtxPal != NULL ){
				smpMemFree( mdl->pMtxPal );
			}
			/* ノードステータスリスト領域の解放 */
			if( mdl->pNodestatList != NULL ){
				smpMemFree( mdl->pNodestatList );
			}
			/* テクスチャの解放 */
			if( mdl->pTexList != NULL ){
				if ( smpTexRelease( mdl->pTexList ) < 0 ){
					while(1){}	/* Texture release error */
				}
			}
			/* テクスチャ情報領域の解放  */
			if( mdl->pTexListBuf != NULL ){
				smpMemFree( mdl->pTexListBuf );
			}
			/* モーションの領域を解放 */
			if(mdl->pMotionBuf != NULL){
				smpMemFree(mdl->pMotionBuf);
			}
			/* マテリアルのモーションの領域を解放 */
			if(mdl->pMotionMatBuf != NULL){
				smpMemFree(mdl->pMotionMatBuf);
			}
			/* マテリアルモーションオブジェクト用メモリ解放 */
			if(mdl->pMatMotObj != NULL){
				smpMemFree( mdl->pMatMotObj );
			}
		}
		smpMemFree( top );
	}
}

/*----------------------
   SCENE のメモリの確保
  ----------------------*/
SCENE *SceneAllocate(void)
{
	SCENE *scn;
	scn = smpMemAlloc(sizeof(SCENE));
	scn->NbCamera		= 0;
	scn->pCamera		= NULL;
	scn->NbLight		= 0;
	scn->pLight			= NULL;
	scn->NbModel		= 0;
	scn->pModel			= NULL;

	scn->StartFrame		= 0.0f;
	scn->EndFrame		= 100.0f;
	scn->CurrentFrame	= 0.0f;
	scn->Step			= 1.0f;
	return(scn);
}

/*----------------------
   SCENE のメモリの解放
  ----------------------*/
void SceneDispose(SCENE **scn)
{
	if( *scn != NULL){
		/* CAMERA のメモリの解放 */
		CameraDispose((*scn)->NbCamera, (*scn)->pCamera);
		/* LIGHT のメモリの解放 */
		LightDispose((*scn)->NbLight, (*scn)->pLight);
		/* MODEL のメモリの解放 */
		ModelDispose((*scn)->NbModel, (*scn)->pModel);

		smpMemFree( *scn );
		*scn = NULL;
	}
}
