/* This is the initialisation file for the Merlin
   It includes all Merlin includes, defines the librarybase symbols
   opens the libraries.
   Then it opens a screen through the builtin screenmode requester.
   It returns a hrh handle which allows direct writing to the screen for
   fast replays. No chunky to planar conversion and back, which is what
   happens if the ASL requester is used to open an Intuition screen.

   All the initialisation of the EGS/Picasso/Retina should take place here
   and also the corresponding Close function  should be supplied which cleans
   up during exit
*/
include <hrg/hrg_all.h>

struct Library *HRGSystemBase;
struct Library *HRGRenderBase;
struct Library *HRGToolBase;
/* return value of Merlin openscreen, for later use when writing to screen
   and setting colors
*/
struct HRHandle *hrh;

ULONG error;
/* screen depths that are allowed to be selected */
UWORD modes[]= { HRCOL_PACKED8, HRCOL_XGA, HRCOL_RGB24, NULL };
/* tags for the merlin open screen function */
ULONG *hrgtags[] = {
	(ULONG *)HTRM_BitMapTypes, (ULONG *)&modes[0],
	(ULONG *)TAG_END,0
};

struct TagItem screentags[] = {
	{ HTOS_BitMapType, HRCOL_PACKED8 },
	{ HTOS_Width, 192 },
	{ HTOS_Height, 144 },
	{ HTOS_Error, &error },
	{ HTOS_Title, "Xanim Amiga" },
	{ HTOS_EazyScreen, HRES_SCROLL },
/*	{ HTOS_DoDither, TRUE },*/
	{ TAG_DONE,		0}
};


void CloseMerlin()
/* This function cleans up during exit or fault conditions
   Picasso version should be called 'ClosePicasso', EGS--> 'CloseEGS' etc
*/
{

	if(hrh)	hrCloseScreen(hrh);
	if(HRGSystemBase)	CloseLibrary(HRGSystemBase);
	if(HRGRenderBase)	CloseLibrary(HRGRenderBase);
	if(HRGToolBase)		CloseLibrary(HRGToolBase);
}

/****************************************************************************/

void OpenMerlin()
{
/* Open needed libraries
   For the Picasso it might look like:
   if(!(VilIntuiSupBase=OpenLibrary((UBYTE *)"vilintuisup.library", xxL))) {ClosePicasso();exit(20);}
*/
	if(!(HRGSystemBase=OpenLibrary((UBYTE *)"hrgsystem.library", 38L))) {CloseMerlin();exit(20);}
	if(!(HRGRenderBase=OpenLibrary((UBYTE *)"hrgrender.library", 38L))) {CloseMerlin();exit(20);}
	if(!(HRGToolBase=OpenLibrary((UBYTE *)"hrgtool.library", 38L))) {CloseMerlin();exit(20);}
/* Open Merlin screen with help of builtin screenmode requester
   Presents user with choice of 8/16/24bit screens, prefers 320x200 screen
*/
	if(!hrh) hrh=hrEazyOpenScreen( hrRequestModeTagList(( struct TagItem * )&hrgtags[0]) , 320, 200, HRES_SCROLL, (UBYTE *)"Xanim Amiga", &error);
	if(!hrh) {CloseMerlin();exit(20);}

/* the next 2 lines get the memory start of the screen. It is not used anymore
   because the builtin library routines are able to handle offsets which is rather
   difficult if you must do it yourself.
*/
	memstart=hrLockBitMap(&hrh->BitMap);
	hrUnlockBitMap(&hrh->BitMap);

/*	hrSwitchDisplay(hrh,HRDISP_ON|HRDISP_BACK);*/
}
