/***************************************************************************

	video.c

	PSP video control functions. (support 16bit color only)

***************************************************************************/

#include "psp.h"


/***************************************************************************
	Global variables
 **************************************************************************/

u32 __attribute__((aligned(16))) gulist[262144];
int draw_frame;


/***************************************************************************
	Global structures
 **************************************************************************/

RECT full_rect = { 0, 0, SCR_WIDTH, SCR_HEIGHT };


/*--------------------------------------------------------
	Initialize video
 -------------------------------------------------------*/

void video_init(void)
{
	draw_frame = 1;

	sceKernelDcacheWritebackAll();

	sceGuInit();
	sceGuStart(GU_DIRECT, gulist);

	sceGuDispBuffer(SCR_WIDTH, SCR_HEIGHT, (void *)(FRAMESIZE * 0), BUF_WIDTH);
	sceGuDrawBuffer(GU_PSM_5551, (void *)(FRAMESIZE * 1), BUF_WIDTH);
	sceGuOffset(2048 - (SCR_WIDTH / 2), 2048 - (SCR_HEIGHT / 2));
	sceGuViewport(2048, 2048, SCR_WIDTH, SCR_HEIGHT);
	sceGuEnable(GU_SCISSOR_TEST);
	sceGuAlphaFunc(GU_LEQUAL, 0, 0x1);
	sceGuEnable(GU_ALPHA_TEST);
	sceGuEnable(GU_TEXTURE_2D);
	sceGuClearColor(0);
	sceGuClear(GU_COLOR_BUFFER_BIT);
	sceGuFinish();
	sceGuSync(0, 0);

	sceDisplayWaitVblankStart();
	sceGuDisplay(GU_TRUE);
}


/*--------------------------------------------------------
	Shutdown video
 -------------------------------------------------------*/

void video_exit(void)
{
	sceGuTerm();
}


/*--------------------------------------------------------
	Get VRAM address
 -------------------------------------------------------*/

u16 *video_vram_addr(int x, int y)
{
	return (u16 *)(((u8 *)0x44000000) + (x + y * BUF_WIDTH) * 2);
}


/*--------------------------------------------------------
	Get draw buffer address
 -------------------------------------------------------*/

u16 *video_draw_frame(int x, int y)
{
	return video_vram_addr(x, y + (draw_frame * SCR_HEIGHT));
}


/*--------------------------------------------------------
	Clear show/draw frame
 -------------------------------------------------------*/

void video_clear_screen(void)
{
	int size = FRAMESIZE / 2;
	u32 *p = (u32 *)video_vram_addr(0, 0);

	while (size--) *p++ = 0;
}


/*--------------------------------------------------------
	Clear specify frame
 -------------------------------------------------------*/

void video_clear_vram(int frame)
{
	int size = FRAMESIZE / 4;
	u32 *p = (u32 *)video_vram_addr(0, frame * SCR_HEIGHT);

	while (size--) *p++ = 0;
}


/*--------------------------------------------------------
	Clear video draw frame
 -------------------------------------------------------*/

void video_clear_draw_frame(void)
{
	sceGuStart(GU_DIRECT, gulist);
	sceGuOffset(2048 - (SCR_WIDTH / 2), 2048 - (SCR_HEIGHT / 2));
	sceGuViewport(2048, 2048, SCR_WIDTH, SCR_HEIGHT);
	sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT);
	sceGuClearColor(0);
	sceGuClear(GU_COLOR_BUFFER_BIT);
	sceGuFinish();
	sceGuSync(0, 0);
}


/*--------------------------------------------------------
	Wait vsync
 -------------------------------------------------------*/

void video_wait_vsync(void)
{
	sceDisplayWaitVblankStart();
}


/*--------------------------------------------------------
	Flip screen
 -------------------------------------------------------*/

void video_flip_screen(int vsync)
{
	if (vsync) sceDisplayWaitVblankStart();
	sceGuSwapBuffers();
	draw_frame ^= 1;
}


/*--------------------------------------------------------
	Copy rectangle
 -------------------------------------------------------*/

void video_copy_rect(int src, int dst, RECT *src_rect, RECT *dst_rect)
{
	int j, sw, dw, sh, dh;
	struct Vertex *vertices;

	sceGuStart(GU_DIRECT, gulist);

	sceGuDrawBufferList(GU_PSM_5551, (void *)(FRAMESIZE * dst), BUF_WIDTH);

	sceGuOffset(2048 - (SCR_WIDTH/2), 2048 - (SCR_HEIGHT/2));
	sceGuViewport(2048, 2048, SCR_WIDTH, SCR_HEIGHT);
	sceGuScissor(dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom);
	sceGuEnable(GU_SCISSOR_TEST);

	sceGuDisable(GU_ALPHA_TEST);

	sceGuTexMode(GU_PSM_5551, 0, 0, 0);
	sceGuTexImage(0, BUF_WIDTH, BUF_WIDTH, BUF_WIDTH, video_vram_addr(0, src * SCR_HEIGHT));
	sceGuTexFunc(GU_TFX_REPLACE, 0);
	sceGuTexFilter(GU_LINEAR, GU_LINEAR);
	sceGuTexScale(1.0f / BUF_WIDTH, 1.0f / BUF_WIDTH);
	sceGuTexOffset(0, 0);

	sceGuAmbientColor(0xffffffff);

	sw = src_rect->right - src_rect->left;
	dw = dst_rect->right - dst_rect->left;
	sh = src_rect->bottom - src_rect->top;
	dh = dst_rect->bottom - dst_rect->top;

	for (j = 0; (j + SLICE_SIZE) < sw; j = j + SLICE_SIZE)
	{
    	vertices = (struct Vertex *)sceGuGetMemory(2 * sizeof(struct Vertex));

		vertices[0].u = src_rect->left + j;
		vertices[0].v = src_rect->top;
		vertices[0].x = dst_rect->left + j * dw / sw;
		vertices[0].y = dst_rect->top;
		vertices[0].z = 0;

		vertices[1].u = src_rect->left + j + SLICE_SIZE;
		vertices[1].v = src_rect->bottom;
		vertices[1].x = dst_rect->left + (j + SLICE_SIZE) * dw / sw;
		vertices[1].y = dst_rect->bottom;
		vertices[1].z = 0;

		sceGuDrawArray(GU_SPRITES, TEXTURE_FLAGS, 2, 0, vertices);
	}

	if (j < sw)
	{
		vertices = (struct Vertex *)sceGuGetMemory(2 * sizeof(struct Vertex));

		vertices[0].u = src_rect->left + j;
		vertices[0].v = src_rect->top;
		vertices[0].x = dst_rect->left + j * dw / sw;
		vertices[0].y = dst_rect->top;
		vertices[0].z = 0;

		vertices[1].u = src_rect->right;
		vertices[1].v = src_rect->bottom;
		vertices[1].x = dst_rect->right;
		vertices[1].y = dst_rect->bottom;
		vertices[1].z = 0;

		sceGuDrawArray(GU_SPRITES, TEXTURE_FLAGS, 2, 0, vertices);
	}

	sceGuFinish();
	sceGuSync(0, 0);
}
