/*
 * $Id$
 *
 * $Date$
 * $Revision$
 *
 * (C) 1999 by Hyperion
 * All rights reserved
 *
 * This file is part of the MiniGL library project
 * See the file Licence.txt for more details
 *
 */

#include "sysinc.h"
#include <stdio.h>

static char rcsid[] UNUSED = "$Id$";


struct Library *UtilityBase;
struct UtilityIFace *IUtility;
struct Library *IntuitionBase;
struct IntuitionIFace *IIntuition;
struct Library *GfxBase;
struct GraphicsIFace *IGraphics;
struct Library *Warp3DBase;
struct Warp3DIFace *IWarp3D;
struct Library *P96Base;
struct P96IFace *IP96;
struct Device *TimerBase;
struct TimerIFace *ITimer;
struct Library *SysBase;
struct ExecIFace *IExec;
struct Library *DOSBase;
struct DOSIFace *IDOS;

void MGL_SINCOS_Init(void);
void InitVertexArray(void);

void MGLTerm(void);

GLboolean MGLInit(void)
{
    IntuitionBase = IExec->OpenLibrary("intuition.library", 0L);

    if (IntuitionBase) 
    	IIntuition = (struct IntuitionIFace *)IExec->GetInterface((struct Library *)IntuitionBase, "main", 1, NULL);

    GfxBase = IExec->OpenLibrary("graphics.library", 0L);
    if (GfxBase) 
    	IGraphics = (struct GraphicsIFace *)IExec->GetInterface((struct Library *)GfxBase, "main", 1, NULL);

    UtilityBase = IExec->OpenLibrary("utility.library", 0L);
    if (UtilityBase) 
    	IUtility = (struct UtilityIFace *)IExec->GetInterface((struct Library *)UtilityBase, "main", 1, NULL);

    Warp3DBase = IExec->OpenLibrary("Warp3D.library", 2L);
    if (Warp3DBase) 
    	IWarp3D = (struct Warp3DIFace *)IExec->GetInterface((struct Library *)Warp3DBase, "main", 1, NULL);     

    P96Base = IExec->OpenLibrary("Picasso96API.library", 1L);
    if (P96Base) 
    	IP96 = (struct P96IFace *)IExec->GetInterface(P96Base, "main", 1, NULL);

	if (!TimerBase)
	{
		TimerBase = (struct Device *)
			IExec->FindName(&((struct ExecBase *)SysBase)->DeviceList, "timer.device");
	}
	
	if (TimerBase && !ITimer)
        	ITimer = (struct TimerIFace *)IExec->GetInterface((struct Library *)TimerBase, "main", 1, NULL);
	
	
    if (!IntuitionBase || !GfxBase || !UtilityBase || !P96Base 
    	|| !Warp3DBase || !IIntuition || !IGraphics || !IUtility || !IWarp3D ) 
    {

        MGLTerm();
        return GL_FALSE;
    }

	InitVertexArray();

#ifdef TRIGTABLES
    MGL_SINCOS_Init();
#endif

	return GL_TRUE;

}

void MGLTerm(void)
{
	if (ITimer)
	{
		IExec->DropInterface((struct Interface *)ITimer);
		ITimer = NULL;
	}
	
    if (IIntuition)         
    {
    	IExec->DropInterface((struct Interface *)IIntuition);
    	IIntuition = NULL;
    }
    
    if (IGraphics)
    {
    	IExec->DropInterface((struct Interface *)IGraphics);
    	IGraphics = NULL;
    }
    
    if (IWarp3D)
    {
    	IExec->DropInterface((struct Interface *)IWarp3D);
    	IWarp3D = NULL;
    }
    
    if (IP96)
	{
		IExec->DropInterface((struct Interface *)IP96);
    	IP96 = NULL;
	}
    
    if (P96Base)
	{
		IExec->CloseLibrary(P96Base);
    	P96Base = NULL;
	}

    if (Warp3DBase)
	{
		IExec->CloseLibrary(Warp3DBase);
    	Warp3DBase = NULL;
	}

    if (IntuitionBase)
    {
    	IExec->CloseLibrary(IntuitionBase);
	    IntuitionBase = NULL;
    }

    if (GfxBase)
    {
    	IExec->CloseLibrary(GfxBase);
    	GfxBase = NULL;
    }

    if (UtilityBase)
	{
		IExec->CloseLibrary(UtilityBase);
    	UtilityBase = NULL;
	}

}
