/****************************************************************************\
*                                                                           *
* tpNinja.c-   TrueMotion player video hardware/system specific interface   *
*                                                                           *
*       TruePlay2 v 7.0.0                                                   *
*       Copyright (c) 1994-98, The Duck Corp. All rights reserved.          * 
*                                                                           *
*       Created:    8/5/98                                                  *
*                                                                           *
\***************************************************************************/
#include <Nindows.h>
#include "tpApi.h"

#include "shinobi.h"
#include "sg_gd.h"

NJS_TEXTURE_VTX	poly[4];

static int firstFrame;
int counted,total,count1;

static PLAYER_STATUS_CODE tp_afterDecompress(MovieHandle movie)
{
	int x, y, w, h;

	DXL_GetXImageXYWH(movie->xImage, &x, &y, &w, &h);

	total += syTmrDiffCount(count1, syTmrGetCount());
	/* flush the decompressed frame from the cache */
	{
		unsigned long entry;

		for(entry = 0; entry < 512; entry++){
		  *((volatile unsigned long *)(0xF4000000 | (entry << 5))) = 0x00000000; 
		  /* OC address array write */
		}
	}
	/* how to download a frame to the display... */
	/* 
		since the internal format of the xImage matches the 
		UYVY format of the texture, the application transfers it 
		directly to the powerVR
	*/
	njReLoadTexturePartNumG(0,
		(signed char *)DXL_GetXImageFrameBuffer(movie->xImage),
		0,
		w*h*2
		);

	/* draw the new texture and wait for VSync */
	njDrawTexture( poly, 4, 0,FALSE);
	return TP_OK;
}

static PLAYER_STATUS_CODE tp_beforeDecompress(MovieHandle movie)
{

	njWaitVSync();
	counted++;

	count1 = syTmrGetCount();

	return TP_OK;
}

static PLAYER_STATUS_CODE tp_centerScreen(MovieHandle movie) 
{
	/* called when screen geometry changes */
	int x, y, w, h;

	total = counted = 0;

	DXL_GetXImageXYWH(movie->xImage, &x, &y, &w, &h);

	/* this sets the geometry for the textured polygon */

	poly[0].z = poly[1].z = 
		poly[2].z = poly[3].z = 0.48;
	
	poly[0].col = poly[1].col = 
		poly[2].col = poly[3].col =  0xFFFFFFFF;

	poly[0].x = poly[1].x = 000;
	poly[0].y = poly[2].y = 000;
	poly[0].u = poly[1].u = 0;
	poly[0].v = poly[2].v = 0;

	/*
		we're using a stride texture, adjust the v and u
		to reflect the true width of the frame
		and set sender width to the real width
	*/

	poly[2].u = poly[3].u = (float)w/1024.f;
	poly[1].v = poly[3].v = (float)h/512.f;
	
	njSetRenderWidth(w);

	/* 
		for frames smaller than 640x480, enable filtering 
		otherwise enable point sampling
	*/

	if ((w < 640) || (h < 480))
		njTextureFilterMode( NJD_TEXTUREFILTER_TRILINEAR );
	else{
		njTextureFilterMode( NJD_TEXTUREFILTER_POINT_SAMPLE );
	}

	if (h > w) /* if width is less than height, scale horizontally */
		w *= 2;

	/* 
		the height and width of the frame are scaled 
		to the width of the screen (640) 
	*/	

	poly[0].y = poly[2].y = (480-((h*640)/w))/2;
	poly[1].y = poly[3].y = ((h*640)/w) + poly[0].y;
	poly[2].x = poly[3].x = (w*640)/w;

	return TP_OK;

}

static PLAYER_STATUS_CODE tp_restoreDisplay(MovieHandle movie) 
{
	/* release graphics resources (texture) */
	
	/* in dreamcast sdk 1.0, njReleaseTexture causes 
		a problem here, the next time njLoadTexture() 
		is called by tp_setupDisplay(), it is not loaded */

	/*
	njReleaseTexture(&texlist);
	njReleaseTextureNumG(0);
	*/

	return TP_OK;
}

PLAYER_STATUS_CODE tp_setupDisplay(MovieHandle movie)
{

	movie->beforeDecompress = tp_beforeDecompress;
	movie->afterDecompress = tp_afterDecompress;
	movie->restoreDisplay = tp_restoreDisplay;
	movie->centerScreen = tp_centerScreen;

	return TP_OK;
}

