/*
	Ninja Library
	
	COPYRIGHT (C) SEGA ENTERPRISES,LTD.

	Create 2000.03.01

*/
#include <Ninja.h>
#include <Njutil.h>
#include <sg_gd.h>

/****************************************************************/
/*  関数名：nuReLoadTextureNum					                */
/*  機  能：テクスチャをリロードする                            */
/*  入  力：Uint32 n											*/
/*			void *texaddr										*/
/*			Uint32 attr NJD_TEXATTR_TYPE_FILE,(MEMORY)			*/
/*			Uint32 lod	0,1,2,4,8,16,32,64,128,256,512,1024		*/
/*						0は全部リロード							*/
/*  出  力：なし                                                */
/*  戻り値：なし				                                */
/*  概　要：                                                    */
/*  備　考：Ninja1と違いファイルの場合、全部ロードできる		*/
/*			バッファが必要です									*/
/*                                                              */
/****************************************************************/
Sint32 nuReLoadTextureNum(Uint32 n,void *texaddr,Uint32 attr,Uint32 lod)
{
	Sint8 *pTexture;
	char  filename[32];
	Sint8 *texbuf;
	Int texbuf_size;
	NJS_TEXLIST *texlist;
	Uint32 Surfaceflag;
	Uint32 nTextureType;
	Uint32 nWidth;
	Uint32 nHeight;
	Uint32 size;
	NJS_PVRHEADERDATA pvrh;
	Sint8 *tex_start;
	NJS_TEXMANAGE *texmanage;
	
	if(attr & NJD_TEXATTR_TYPE_MEMORY){
		pTexture = texaddr;
	}
	else{
		njCheckFileName(filename,(char*)texaddr,".PVR");
		nuGetTextureBuffer(&texbuf,&texbuf_size);

		if(nuTexFreadAll(filename,texbuf,texbuf_size) < 0){
			return -1;
		}

		if((tex_start = njLoadPvrHeader(&pvrh,NULL,texbuf)) == NULL){
			pTexture = texbuf;
		}
		else{
			pTexture = tex_start;
		}
		
		texlist = njGetCurrentTexList();
		Surfaceflag = NJM_TEXTURE_SURFACEFLAG(texlist,n);
		if((lod != 0) && (Surfaceflag & NJD_SURFACEFLAGS_MIPMAPED)){
			nTextureType = NJM_TEXTURE_TYPE(texlist,n);
			nWidth = nHeight = (lod >> 1);
			size = njCalcTextureSize(nWidth,nHeight,nTextureType);
			pTexture = (Sint8*)((Uint32)pTexture + size);
		}
	}
	return njReLoadTextureLodNum(n,pTexture,lod);

}

/****************************************************************/
/*  関数名：nuReLoadTextureNumG					                */
/*  機  能：テクスチャをリロードする                            */
/*  入  力：Uint32 globalIndex									*/
/*			void *texaddr										*/
/*			Uint32 attr NJD_TEXATTR_TYPE_FILE,(MEMORY)			*/
/*			Uint32 lod	0,1,2,4,8,16,32,64,128,256,512,1024		*/
/*						0は全部リロード							*/
/*  出  力：なし                                                */
/*  戻り値：なし				                                */
/*  概　要：                                                    */
/*  備　考：Ninja1と違いファイルの場合、全部ロードできる		*/
/*			バッファが必要です									*/
/*                                                              */
/****************************************************************/
Sint32 nuReLoadTextureNumG(Uint32 globalIndex,void *texaddr,Uint32 attr,Uint32 lod)
{
	Sint8 *pTexture;
	char  filename[32];
	Sint8 *texbuf;
	Int texbuf_size;
	Uint32 Surfaceflag;
	Uint32 nTextureType;
	Uint32 nWidth;
	Uint32 nHeight;
	Uint32 size;
	NJS_PVRHEADERDATA pvrh;
	Sint8 *tex_start;
	Sint32 texmng_no;
	Sint32 ret;
	
	if(attr & NJD_TEXATTR_TYPE_MEMORY){
		pTexture = texaddr;
	}
	else{
		njCheckFileName(filename,(char*)texaddr,".PVR");
		nuGetTextureBuffer(&texbuf,&texbuf_size);

		if(nuTexFreadAll(filename,texbuf,texbuf_size) < 0){
			return -1;
		}

		if((tex_start = njLoadPvrHeader(&pvrh,NULL,texbuf)) == NULL){
			pTexture = texbuf;
		}
		else{
			pTexture = tex_start;
		}
		
		ret = njSearchTextureManageNum(globalIndex,0,0,&texmng_no);
		if(ret != 1){
			return -1;
		}
		Surfaceflag = _nj_texmanage[texmng_no].texsys->texsurface.fSurfaceFlags;
		if((lod != 0) && (Surfaceflag & NJD_SURFACEFLAGS_MIPMAPED)){
			nTextureType = (_nj_texmanage[texmng_no].texsys->texsurface.Type >> 16);
			nWidth = nHeight = (lod >> 1);
			size = njCalcTextureSize(nWidth,nHeight,nTextureType);
			pTexture = (Sint8*)((Uint32)pTexture + size);
		}
	}
	return njReLoadTextureLodNumG(globalIndex,pTexture,lod);

}
