/* display.c, Picasso IV PiP interface */

/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
// Frank Pagels, 1998

/*
 * Disclaimer of Warranty
 *
 * These software programs are available to the user without any license fee or
 * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
 * any and all warranties, whether express, implied, or statuary, including any
 * implied warranties or merchantability or of fitness for a particular
 * purpose.  In no event shall the copyright-holder be liable for any
 * incidental, punitive, or consequential damages of any kind whatsoever
 * arising from the use of these programs.
 *
 * This disclaimer of warranty extends to the user of these programs and user's
 * customers, employees, agents, transferees, successors, and assigns.
 *
 * The MPEG Software Simulation Group does not represent or warrant that the
 * programs furnished hereunder are free of infringement of any third-party
 * patents.
 *
 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
 * are subject to royalty fees to patent holders.  Many of these patents are
 * general enough such that they are unavoidable regardless of implementation
 * design.
 *
 */

#include <stdio.h>
#include <stdlib.h>

#include "config.h"
#include "global.h"

#include "mplay.h"
#include "screen.h"
#include "commandline.h"

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/picasso96.h>
#include <proto/guigfx.h>
#include <proto/intuition.h>
#include <proto/gadtools.h>
#include <proto/graphics.h>
#include <guigfx/guigfx.h>

#include <clib/macros.h>

#ifdef __PPC__
#include <powerup/ppclib/interface.h>
#include <powerup/ppclib/time.h>
//#include <powerup/gcclib/powerup_protos.h>
//#include <proto/ppc.h>
#endif


struct mvscreen *mvscreen = NULL;
struct mvwindow *mvwindow = NULL;

static BOOL initialized = FALSE;



void Terminate_Display_Process();



void Initialize_Display_Process(char *name)
{
	BOOL success = FALSE;


	if (!initialized)
	{
		int framerate = 0;
		
		if (opts[OPT_FRAMERATE])
		{
			framerate = *((LONG *)opts[OPT_FRAMERATE]);
		}
	
		if (mvscreen = CreateMVScreen())
		{
			if (mvwindow = CreateMVWindow(mvscreen, opts[OPT_PIP], horizontal_size, vertical_size, opts[OPT_GREY], name, framerate))
			{
	#ifdef __PPC__
				struct TagItem ctoTags[] = { {PPCTIMERTAG_CPU, TRUE},{TAG_END, 0} };
				ULONG *Tick[2];
	#endif
				if (opts[OPT_SNAP])
				{
					int borderWidth, borderHeight;
					UpdateWindowParameters(mvwindow);
					borderWidth = mvwindow->winwidth - mvwindow->innerwidth;
					borderHeight = mvwindow->winheight - mvwindow->innerheight;
					
					SizeWindow(mvwindow->window,
						(horizontal_size + borderWidth) - mvwindow->winwidth,
						(vertical_size + borderHeight) - mvwindow->winheight);
						
					MoveWindow(mvwindow->window,
						-((horizontal_size + borderWidth) - mvwindow->winwidth)/2,
						-((vertical_size + borderHeight) - mvwindow->winheight)/2);
					
				}
			
				initialized = TRUE;
				success = TRUE;
	
	#ifdef __PPC__
				mvwindow->timerObject=PPCCreateTimerObject(ctoTags);
				PPCGetTimerObject(mvwindow->timerObject,PPCTIMERTAG_TICKSPERSEC,&Tick);
				mvwindow->ticks=(long)Tick[1]/1000; /* tick per second*/
	#endif
			}
			else
			{
			doneFlag = TRUE;
			printf("Can't open window!\n");
			}
		}
		else
		{
			doneFlag = TRUE;
			printf("Can't open screen!\n");
		}
	}

}


void Terminate_Display_Process()
{
  if (mvwindow)
  {	
	mvwindow->frame = 0;
	
	if (!(!doneFlag && opts[OPT_LOOP]))
	{
		if (mvwindow)
		{
			DeleteMVWindow(mvwindow);
		}
		
		if (mvscreen)
		{
			DeleteMVScreen(mvscreen);
		}

#ifdef __PPC__
  		if (mvwindow->timerObject)
  		{
  			PPCDeleteTimerObject(mvwindow->timerObject);
		}
#endif

	}
  }
}


void Display_Image(src)
unsigned char *src[];
{
	doneFlag = !mplay(mvwindow, src);
}

