#include "include.h"

int fmv_lock = 0;

//#define NOFMV
#ifdef NOFMV					 

// DUMMY STUBS FOR DEMO
void ShutdownEverything(void)
{
}

int FMV_Play(char *szMovieName)
{
return 0;
}

int FMV_PlayIntro(char *szMovie1, char *szMovie2)
{
return 0;
}

#else


// NORMAL CODE
#include "..\eidos\include\winplay.h"

#define SYNCH IS_DYNAMIC

LPMOVIEHANDLE LhMovie=0;
LPVIDEOHANDLE LhVideo=0;
LPSOUNDHANDLE LhSound=0;

local void Escape_Play(char* szMovieName, bool init_playbackmode)
	{
	PLRESULT pRes;
	RECT rcPlayRect={0,0,640,480};

	pRes=Player_PassInDirectDrawObject(G_DirectDraw);
	if (pRes!=PLAYER_OK)
		{
		Log("Player_InitVideoSystem failed");
		return;
		}

	pRes=Player_InitMovie(&LhMovie, NULL, 0, szMovieName, 2<<20);
	if (pRes!=PLAYER_OK)
		{
		Log("Player_InitMovie failed: %s", szMovieName);
		return;
		}
	if (Movie_GetFormat(LhMovie)!=130)
		{
		Log("Movie is not 130 format");
		return;
		}

//	pRes=Player_InitVideoSystem(GhWnd, TRUE);

	pRes=Player_InitVideo(&LhVideo, LhMovie, Movie_GetXSize(LhMovie), Movie_GetYSize(LhMovie),
						  0,240-Movie_GetYSize(LhMovie), 0,0, 640,480, FALSE, TRUE,			// fullscreen
						  DFLAG_INVIEWPORT|DFLAG_CLEAROUTPUT|DFLAG_DOUBLED);
	if (pRes!=PLAYER_OK)
		{
		Log("Player_InitVideo failed");
		return;
		}

	if (init_playbackmode)
	{
		pRes=Player_InitPlaybackMode(GhWnd, LhVideo, TRUE, FALSE);
		if (pRes!=PLAYER_OK)
			{
			Log("Player_InitPlaybackMode failed");
			return;
			}
	}

	if (Player_BlankScreen(rcPlayRect) != PLAYER_OK)
		Log("FAILED TO CLEAR SCREEN");

	pRes=Player_InitSoundSystem(GhWnd);
	if (pRes!=PLAYER_OK)
		{
		Log("Player_InitSoundSystem failed");
		return;
		}

	pRes=Player_GetDSErrorCode();

	if (FAILED(pRes))
		{
		Log("Player_GetDSErrorCode returned != DS_OK");
		return;
		}

	pRes=Player_InitSound(&LhSound,
						  16384,
						  Movie_GetSoundPrecision(LhMovie)==4?4:1,
						  Movie_GetSoundPrecision(LhMovie)!=4,
						  4096,
						  Movie_GetSoundChannels(LhMovie),
						  Movie_GetSoundRate(LhMovie),
						  Movie_GetSoundPrecision(LhMovie),
						  SYNCH);
	if (pRes!=PLAYER_OK)
		{
		Log("Player_InitSound failed");
		return;
		}

	Movie_SetSyncAdjust(LhMovie,LhSound,4);

	pRes=Player_InitMoviePlayback(LhMovie, LhVideo, LhSound);
	if (pRes!=PLAYER_OK)
		{
		Log("Player_InitMoviePlayback failed");
		return;
		}

	S_UpdateInput(); // need to spin message loop to avoid window borders being drawn after BlankScreen()
	Player_StartTimer(LhMovie);
	Player_BlankScreen(rcPlayRect);
	S_UpdateInput();
//	ShowCursor(FALSE);

	while ((Movie_GetCurrentFrame(LhMovie) < Movie_GetTotalFrames(LhMovie)) && (pRes==PLAYER_OK))
		{
		pRes=Player_PlayFrame(LhMovie, LhVideo, LhSound, 0, &rcPlayRect, 0, 0, 0);

		if (S_UpdateInput())
			break;
		if (input&IN_OPTION)
			break;
		}
	}

local void Escape_Cleanup(bool return_playbackmode)
	{
	Player_StopTimer(LhMovie);
	Player_ShutDownSound(&LhSound);
	Player_ShutDownVideo(&LhVideo);
	Player_ShutDownMovie(&LhMovie);
//	Player_ShutDownVideoSystem();
	Player_ShutDownSoundSystem();
	if (return_playbackmode)
		Player_ReturnPlaybackMode();
	}

int FMV_Play(char* szMovieName)
	{
	if (!G_AppSettings.tDisableFMV)
		{
		fmv_lock = 1;
		S_CDStop();
		ShowCursor(FALSE);
		DX_Finish();
#ifdef CD_LOAD
		szMovieName = GetFullPath(szMovieName);
#endif
		Escape_Play(szMovieName, true);
		Escape_Cleanup(true);

		fmv_lock = 0; 
		if (!GtWindowClosed)
			DX_PostFMVStart();
		ShowCursor(TRUE);
		}
	return GtWindowClosed;
	}

int FMV_PlayIntro(char *szMovie1, char *szMovie2)
	{
	Log("FMV_PlayIntro(%s,%s)",szMovie1,szMovie2);
	if (!G_AppSettings.tDisableFMV)
		{
		fmv_lock = 1;
		ShowCursor(FALSE);
		DX_Finish();
#ifdef CD_LOAD
		szMovie1 = GetFullPath(szMovie1);
#endif
		Escape_Play(szMovie1, true);
		Escape_Cleanup(true);

#ifdef CD_LOAD
		szMovie2 = GetFullPath(szMovie2);
#endif
		Escape_Play(szMovie2, true);
		Escape_Cleanup(true);

		fmv_lock = 0;
		if (!GtWindowClosed)
			DX_PostFMVStart();
		ShowCursor(TRUE);
		}
	return GtWindowClosed;
	}

#endif