/*********************************************************************************************/
/*                                                                                           */
/* Frontend Specific Stuff                                                                   */
/*                                                                                           */
/*********************************************************************************************/

#include "standard.h"
#include "global.h"
#include "frontend.h"
#include "display.h"
#include "input.h"
#include "output.h"
#include "fmv.h"


#include "../game/text.h"
#include "../game/inventry.h"

//#include "d:\escape\dosplay.h"

/*********************************** FUNCTION CODE *******************************************/

SG_COL S_COLOUR(int R, int G, int B)
{
	/* Find closest colour in palette */
	return (SG_COL)SWR_FindNearestPaletteEntry(game_palette,R,G,B,false);	// Windows colours are OK.
}

void S_DrawScreenLine( sint32 sx, sint32 sy, sint32 z, sint32 w, sint32 h, sint32 col, uint16 *grdptr, uint16 flags)
{
//	col = inv_colours[col];
	InsertLine(sx,sy,sx+w,sy+h,phd_znear+(z<<3),(char)col);
}

void S_DrawLine( sint32 x1, sint32 y1, sint32 z, sint32 x2, sint32 y2, sint32 col, uint16 *grdptr, uint16 flags)
{
//	col = inv_colours[col];
	InsertLine(x1,y1,x2,y2,phd_znear+(z<<3),(char)col);
}



void S_DrawScreenBox( sint32 sx, sint32 sy, sint32 z, sint32	w, sint32 h, sint32 col, uint16 *grdptr, uint16 flags)
{
#define B_YELLOW 15
#define B_ORANGE 31

#if 0
	/* Just the outline of a box */
	S_DrawScreenLine(sx, sy-1, z, w+1, 0, B_YELLOW, NULL ,flags);
	S_DrawScreenLine(sx, sy, z, w, 0, B_ORANGE, NULL ,flags);

	S_DrawScreenLine(sx+w, sy, z, 0, h, B_YELLOW, NULL ,flags);
	S_DrawScreenLine(sx+w+1, sy, z, 0, h+1, B_ORANGE, NULL ,flags);

	S_DrawScreenLine(sx+w, sy+h, z, -w, 0, B_YELLOW, NULL,flags);
	S_DrawScreenLine(sx+w, sy+h+1, z, -w-1, 0, B_ORANGE, NULL ,flags);

	S_DrawScreenLine(sx-1, sy+h, z, 0, -h-1, B_YELLOW, NULL ,flags); // grdptr stuff not correct for final bit
	S_DrawScreenLine(sx, sy+h, z, 0, -h, B_ORANGE, NULL ,flags); // grdptr stuff not correct for final bit
#else
	S_DrawScreenLine(sx, sy-1, z, w+1, 0, B_YELLOW, NULL ,flags);
	S_DrawScreenLine(sx+1, sy, z, w-1, 0, B_ORANGE, NULL ,flags);

	S_DrawScreenLine(sx+w, sy+1, z, 0, h-1, B_YELLOW, NULL ,flags);
	S_DrawScreenLine(sx+w+1, sy, z, 0, h+1, B_ORANGE, NULL ,flags);

	S_DrawScreenLine(sx-1, sy-1, z, 0, h+1, B_YELLOW, NULL ,flags);
	S_DrawScreenLine(sx, sy, z, 0, h-1, B_ORANGE, NULL ,flags);

	S_DrawScreenLine(sx, sy+h, z, w-1, 0, B_YELLOW, NULL ,flags);
	S_DrawScreenLine(sx-1, sy+h+1, z, w+1, 0, B_ORANGE, NULL ,flags);
#endif
}


void S_DrawScreenFBox( sint32 sx, sint32 sy, sint32 z, sint32	w, sint32 h, sint32 col, uint16 *grdptr, uint16 flags)
{
	/* Draw flat shaded box (should be gouraud but will look crap) */
	InsertTransQuad(phd_winxmin+sx,phd_winymin+sy,w+1,h+1,phd_znear+(z<<3));	// Dude fixed this!  To make it one pixel bigger -> right/bottom.
}


void S_SetBackgroundGamma(sint16 gamma)
{

}


void S_SetGamma(sint16 gamma)
{

}

void	S_SwapScreens(void)
{
}
 

/****************************************************************************
 *						Finish doing Inventry
 ***************************************************************************/
void	S_FinishInventory( void )
{
	if (Inventory_Mode != INV_TITLE_MODE)
		TempVideoRemove();
}


void S_FadeToBlack(void)
{
	int i;
	uint8 *p;

	for (i=0, p=game_palette; i<256*3; i++, p++)
		*p = 0;

	FadeToPal(10, game_palette);
	FadeWait();

	ScreenClear();
	ScreenDump();
	ScreenClear();
	ScreenDump();
}

void S_LoadingScreen(int level_number)
{
// Displays loading screen if needed
}

void S_Wait(int frames, int anykey)
{
	//	Waits for 'frames'. Check input for user impatience.
	int npassed;

	if (anykey)
	{
		while (frames > 0)
		{
			if (!input)
				break;
			S_UpdateInput();

			do
				npassed = Sync();
			while (!npassed);
			frames -= npassed;
		}
	}

	while (frames > 0)
	{
		S_UpdateInput();
		if (anykey && input)
			return;

		do
			npassed = Sync();
		while (!npassed);
		frames -= npassed;
	}
}


int S_PlayFMV( char *fname, int sequence )
{
	return FMV_Play(fname);
}


int S_IntroFMV(char *movie1, char *movie2)
{
	return FMV_PlayIntro(movie1, movie2);
}

