/************************************************************************
**********                                                     **********
**********                 PI Extern Module                    **********
**********                    ----------                       **********
**********                                                     **********
**********        Copyright (C) Menti Possibili, snc 1992      **********
**********       Via Di Vittorio, 56 - 50015 Grassina (FI)     **********
**********                   Italy                             **********
**********              All Rights Reserved.                   **********
**********                                                     **********
*************************************************************************
***
***  MODULE:
***
***	ExampleModule.c
***
***  PURPOSE:
***
***	Show example of simple PI Fx Module
***
***  HISTORY:
***
***	1.00 Andrea Focardi	First Release.
***
***  NOTE:
***	
***	You can copy, modify and distribute without any restriction.
***
************************************************************************/

#include <exec/types.h>
#include <graphics/gfxbase.h>
#include <intuition/intuitionbase.h>
#include <proto/all.h>
#include <stdio.h>
#include <string.h>

#include <paramPI.h>

struct IntuitionBase   	*IntuitionBase = NULL ;
struct GfxBase    	*GfxBase = NULL ;

struct Window     	*w, *tbw;
struct RastPort		*mainrp, *tbrp, *targetrp;

struct Parameters_PI	*pi;


void make(void)
{
	Move(mainrp,100,100); Text(mainrp,"Hello, World!",13);
}


void ExitPI(
	char 	*errore,
	int 	ret)
{
	printf(errore);

	if ( ret )
		Delay(50);

	if ( GfxBase )
		CloseLibrary( (struct Library *) GfxBase);

	if ( IntuitionBase )
		CloseLibrary( (struct Library *) IntuitionBase);

	exit(ret);
}

main(int argc,char *argv[])
{   
	printf("\nPI Image Example Fx Module.\n");

	if ( argc != 2 )
		ExitPI("\nError: wrong parameters.\n",RETURN_ERROR);

	if ( FindPort("MPA") == NULL )
		ExitPI("\nError: PI Image not run.\n",RETURN_ERROR);

	pi = (struct Parameters_PI *) atol(argv[1]);
	
	if ( pi->magic != MOD_MAGIC )
		ExitPI("\nError: incorrect struct.\n",RETURN_FAIL);

	if ( IntuitionBase = ( struct IntuitionBase * ) OpenLibrary ("intuition.library",0) )
		{
		if ( GfxBase = ( struct GfxBase * ) OpenLibrary ("graphics.library",0) )
			{

			/* get ptr */
	
			w  		= pi->mainwindow ;
			tbw		= pi->tbwindow ;
			mainrp	= w->RPort ;
			tbrp 		= tbw->RPort ;
			targetrp	= pi->targetrp ;
				
			/* application starts */


			/* write only "Hello, World!" and then exit */

			make();
	
			/* application ends */			

			}
		else
			ExitPI("\nError: can't open graphics.library.\n",RETURN_FAIL);

		}
	else
		ExitPI("\nError: can't open intuition.library.\n",RETURN_FAIL);
	
	ExitPI("",RETURN_OK);
}

