/*----------------------------------------------------------------------------
	SoH DirectX Layer
----------------------------------------------------------------------------*/

#include "soh.h"

extern void sxBmp2Tex(void *dst, void *src);
/*--------------------------------------------------------------------------*/
SXERR sxReleaseTexture(SX_TEXID id)
{
	SX_TEXMEMLST *MEM=NULL;

	if (id>=0) {
		if (SDX->mem_lst[id].isUsed==TRUE) {
			MEM = &SDX->mem_lst[id];
			njReleaseTexture(&MEM->list);
			njReleaseCacheTextureNumG(id);
			sxMemSetL( MEM, 0, sizeof(SX_TEXMEMLST)/4 );
		}
	}
	return SX_OK;
}
/*--------------------------------------------------------------------------*/
SXERR sxReleaseTextureAll(void)
{
	SLONG i;
	SX_TEXMEMLST *MEM=NULL;

	for(i=0; i<SX_MAX_MEMLST; i++) {			/* 使用中メモリリスト検索	*/
		if (SDX->mem_lst[i].isUsed==TRUE) {
			MEM = &SDX->mem_lst[i];
			njReleaseTexture(&MEM->list);
			njReleaseCacheTextureNumG(i);
			sxMemSetL( MEM, 0, sizeof(SX_TEXMEMLST)/4 );
		}
	}
	return SX_OK;
}
/*--------------------------------------------------------------------------*/
/* PVRファイルからの読み込み												*/
SXERR sxLoadTexturePVR(SCHAR *name, SX_TEXID *id, ULONG flag)
{
	SLONG i,n,t;
	SX_TEXMEMLST *MEM=NULL;
	
	n=-1;
	for(i=0; i<SX_MAX_MEMLST; i++) {			/* 空きメモリリスト検索		*/
		if (SDX->mem_lst[i].isUsed==FALSE) {
			n=i;
			break;
		}
	}
	MEM = &SDX->mem_lst[n];

	if (flag&SX_TEX_CACHEONLY) {
		njSetTextureName( &MEM->name, name, n,
			NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX/*|NJD_TEXATTR_CACHE*/);
		MEM->isReal = FALSE;
	} else {
		njSetTextureName( &MEM->name, name, n,
			NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX/*|NJD_TEXATTR_BOTH*/);
		MEM->isReal = TRUE;
	}
	
	MEM->list.textures = &MEM->name;
	MEM->list.nbTexture = 1;
	
	njLoadTexture( &MEM->list );
	
	MEM->isUsed = TRUE;
	SDX->mem_num++;
	*id=n;										/* テクスチャＩＤ			*/
	
	return SX_OK;
}
/*--------------------------------------------------------------------------*/
/* BMPファイルからの読み込み												*/
SXERR sxLoadTextureBMP(SCHAR *name, SX_TEXID *id, ULONG flag)
{
#if 0
	SLONG i,n,t;
	SCHAR *buf=NULL;							/* LZS展開前				*/
	SCHAR *buf2=NULL;							/* LZS展開後				*/
	SCHAR *buf3=NULL;							/* PVR変換後				*/
	SCHAR *buf4=NULL;							/* TWWIDLED変換後			*/
	ULONG size;
	SX_TEXMEMLST *MEM=NULL;
	
	size = sxFileSize(name);
	buf = (SCHAR*)syMalloc(size+2048);
	buf2= (SCHAR*)syMalloc(256*1024);
	buf3= (SCHAR*)syMalloc(256*1024);
	buf4= (SCHAR*)syMalloc(256*1024);
	sxLoadBin(name, buf, size);
	LzSH((UCHAR*)buf,(UCHAR*)buf2);
	sxBmp2Tex(buf3, buf2);
	njTwiddledTexture(buf4, buf3, GETLONG(buf2,0x12));
	syCacheOCBP(buf4 , 256*1024 );
	
	n=-1;
	for(i=0; i<SX_MAX_MEMLST; i++) {			/* 空きメモリリスト検索		*/
		if (SDX->mem_lst[i].isUsed==FALSE) {
			n=i;
			break;
		}
	}
	MEM = &SDX->mem_lst[n];
	njSetTextureInfo( &MEM->info, buf4, NJD_TEXFMT_ARGB_1555|NJD_TEXFMT_TWIDDLED,
		GETLONG(buf2,0x12), GETLONG(buf2,0x16) );

	if (flag&SX_TEX_CACHEONLY) {
		njSetTextureName( &MEM->name, &MEM->info, n,
			NJD_TEXATTR_TYPE_MEMORY|NJD_TEXATTR_GLOBALINDEX|NJD_TEXATTR_CACHE);
		MEM->isReal = FALSE;
	} else {
		njSetTextureName( &MEM->name, &MEM->info, n,
			NJD_TEXATTR_TYPE_MEMORY|NJD_TEXATTR_GLOBALINDEX|NJD_TEXATTR_BOTH);
		MEM->isReal = TRUE;
	}
	
	MEM->list.textures = &MEM->name;
	MEM->list.nbTexture = 1;
	
	njLoadTexture( &MEM->list );
	
	MEM->isUsed = TRUE;
	SDX->mem_num++;
	
	syFree(buf);
	syFree(buf2);
	syFree(buf3);
	syFree(buf4);
	*id=n;										/* テクスチャＩＤ			*/
#endif
	return SX_OK;
}
/*--------------------------------------------------------------------------*/
/* 透明色の設定																*/
SXERR sxSetColorKey(SX_TEXID id)
{
}
/*--------------------------------------------------------------------------*/
/* テクスチャのセット														*/
SXERR sxSetTexture(SX_TEXID id)
{
	if (id==SX_TEX_NONE) {
		SDX->isGI = -1;
		return SX_OK;
	}
	
	njSetTexture( &SDX->mem_lst[id].list );
	njSetTextureNumG( id );
	SDX->isGI = id;
	return SX_OK;
}
/*--------------------------------------------------------------------------*/
/* BMP形式からARGB1555フォーマットへ変換										*/
void sxBmp2Tex(void *dst, void *src)
{
	ULONG W = GETLONG(src,0x12);
	ULONG H = GETLONG(src,0x16);
	USHORT *DST;
	UCHAR  *SRC,*L;
	SLONG i,j;
	ULONG sftR,sftG,sftB;
	ULONG sclR,sclG,sclB;
	ULONG tmp,dot;
	
	SRC = (UCHAR  *)((ULONG)src + 0x36);
	DST = dst;
												/* BIT変換用				*/
	sxMakeBits(&sftR,&sclR, 0x7c00);
	sxMakeBits(&sftG,&sclG, 0x03e0);
	sxMakeBits(&sftB,&sclB, 0x001f);
												/* BMPは上下反転してる		*/
	for(j=0; j<H; j++) {
		L=&SRC[ (H-j-1) * W*3 ];
		for(i=0; i<W; i++) {
			dot  = (*L++ / sclB) << sftB;
			dot |= (*L++ / sclG) << sftG;
			dot |= (*L++ / sclR) << sftR;
			if (dot) dot|=0x8000;
			*DST++ = (USHORT)dot;
		}
	}
}
/*--------------------------------------------------------------------------*/
SXERR sxTextureCache(SX_TEXID id, ULONG flag)
{
	SX_TEXMEMLST *MEM=NULL;

	if (id>=0) {
		MEM = &SDX->mem_lst[id];
		if (MEM->isUsed) {
			switch (flag) {
			case SX_TEX_ENABLE:					/* リアルイネーブル			*/
				if (!MEM->isReal) {
					njLoadCacheTextureNumG(id);
					MEM->isReal = TRUE;
				}
				break;
			case SX_TEX_DISABLE:				/* リアルディゼーブル		*/
				if (MEM->isReal) {
					njReleaseTextureNumG(id);
					MEM->isReal  = FALSE;
				}
				break;
			}
			return SX_OK;
		}
	}
	return SX_ERR;
}
/*--------------------------------------------------------------------------*/
SXERR sxTextureCache2(SX_TEXID id, ULONG flag)
{
	
}
/*--------------------------------------------------------------------------*/
