/**********************************************************************
 *
 *       fichier:  main.c
 *
 *       fonction: test des fonctions de   BMapRPort.c
 *
 *       creation: 19-Sep-88  jm forgeas
 *       revision: 19-Sep-88  jm forgeas
 *
 **********************************************************************/


/******* Included Files ***********************************************/

#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <exec/types.h>
#include <exec/tasks.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <graphics/GfxBase.h>
#include <graphics/gels.h>
#include <graphics/gfxmacros.h>
#include <graphics/view.h>

#include <intuition/intuisup.h>


/******* Exported *****************************************************/

struct GfxBase  *GfxBase=0;
struct IntuitionBase  *IntuitionBase=0;
#ifdef ISUP_RUNTIME
struct   Library  *iSupBase;
#endif


/******* Imported *****************************************************/


/******* Program ******************************************************/

static  VOID    Cleanup();

#define DEPTH   4
#define COLNUM  (2 << DEPTH)

static  SHORT WIDTH    = 450;
static  SHORT HEIGHT   = 100;
static  SHORT RXOFFSET =  30;

static struct View      *v=0, *oldview=0;
static struct ViewPort  *vp=0;
static struct RasInfo   *ri=0;
static struct BitMap    *bm=0;
static struct RastPort  *rp=0;

static USHORT  colortable[] = {
        0x000, 0xf00, 0x0f0, 0x00f,
        0xff0, 0xdd0, 0xbb0, 0x880,
        0x440, 0x000, 0xfff, 0xfff,
        0xfff, 0xccc, 0x888, 0x444
        };


/**********************************************************************
 *
 *      Routines
 *
 **********************************************************************/

static SetWBLike( view, adrwidth, adrheight )
struct View *view;
SHORT *adrwidth, *adrheight;
{
    struct Screen wbScrData;

        if (! (IntuitionBase = (struct IntuitionBase *) OpenLibrary( "intuition.library", 0))) return(NULL);
        if (! (GetScreenData( &wbScrData, sizeof(struct Screen), WBENCHSCREEN, NULL ))) return(NULL);
        view->DxOffset = IntuitionBase->ViewLord.DxOffset;
        view->DyOffset = IntuitionBase->ViewLord.DyOffset;
        *adrwidth  = wbScrData.Width;
        *adrheight = wbScrData.Height;
        CloseLibrary( IntuitionBase ); IntuitionBase=0;
        return(TRUE);
}

static VOID AllocAllocs()
{
        if (! (v  = (struct View *) AllocMem( sizeof(struct View), MEMF_PUBLIC | MEMF_CLEAR))) Cleanup();
        InitView( v );

        if (! (vp = (struct ViewPort *) AllocMem( sizeof(struct ViewPort), MEMF_PUBLIC | MEMF_CLEAR))) Cleanup();
        InitVPort( vp );
        v->ViewPort  = vp;
        if (! (vp->ColorMap = (struct ColorMap *) GetColorMap( COLNUM ))) Cleanup();

        if (! (ri = (struct RasInfo *) AllocMem( sizeof(struct RasInfo), MEMF_PUBLIC | MEMF_CLEAR))) Cleanup();
        vp->RasInfo  = ri;

        if (! (bm = (struct BitMap *) is_AllocBMap( DEPTH, WIDTH, HEIGHT ))) Cleanup();
        if (! (rp = (struct RastPort *) is_AllocRPort())) Cleanup();
/*
        if (! (rp = (struct RastPort *) is_AllocBMapRPort( DEPTH, WIDTH, HEIGHT ))) Cleanup();
        bm = rp->BitMap;
*/
}

static VOID FreeAllocs()
{
/*
        is_FreeBMapRPort( rp );
*/
        is_FreeBMap( bm );
        is_FreeRPort( rp );

        if (ri) FreeMem( ri, sizeof(struct RasInfo) );
        if (vp) {
                if (vp->ColorMap) FreeColorMap( vp->ColorMap );
                FreeVPortCopLists( vp );
                FreeMem( vp, sizeof(struct ViewPort) );
                }
        if (v)  {
                if (v->LOFCprList) FreeCprList( v->LOFCprList );
                if (v->SHFCprList) FreeCprList( v->SHFCprList );
                FreeMem( v, sizeof(struct View) );
                }
}

/**********************************************************************
 *
 *      Main program
 *
 **********************************************************************/

static VOID Cleanup()
{
        if (oldview) LoadView( oldview );
        FreeAllocs();
#ifdef ISUP_RUNTIME
        if (iSupBase) CloseLibrary(iSupBase);
#endif
        if (GfxBase)  CloseLibrary(GfxBase);
        exit(0);
}

static VOID DoItNice()
{
    register SHORT x;

        /*--------- fond ---------*/
        SetAPen( rp, 5 ); RectFill( rp, 0,0, 145,90 );
        SetAPen( rp, 2 ); RectFill( rp, 150,0, 295,90 );
        SetAPen( rp, 3 ); RectFill( rp, 300,0, 449,90 );

        /*--------- bords --------*/
        for (x=0; x<6; x++)
                { SetAPen( rp, 9-x ); RectFill( rp, RXOFFSET+x,x, RXOFFSET+vp->DWidth-1-x, 10-x ); }
        BltBitMap( bm, RXOFFSET,0, bm, RXOFFSET,vp->DHeight-12, vp->DWidth,11, 0xc0, 0xff, 0 );
}

VOID main()
{
    register SHORT i, BMinc=3, time=0;
    SHORT i_height, i_width;

    if (  !(GfxBase       = (struct GfxBase *)OpenLibrary("graphics.library", 0))
#ifdef ISUP_RUNTIME
       || !(iSupBase      = (struct Library *)OpenLibrary("isup.library", 0))
#endif
       ) Cleanup();;
        oldview = GfxBase->ActiView;

        AllocAllocs();
        if (! SetWBLike( v, &i_width, &i_height )) Cleanup();

        vp->DyOffset = i_height - HEIGHT;
        vp->DWidth   = i_width >> 1;
        vp->DHeight  = HEIGHT;

        ri->BitMap = rp->BitMap = bm;    /* avec is_AllocBMap()      */
       /* ri->BitMap = rp->BitMap;*/     /* avec is_AllocBMapRPort() */
        ri->RxOffset = RXOFFSET;

        LoadRGB4( vp, colortable, COLNUM );
        DoItNice();

        MakeVPort( v, vp );
        MrgCop( v );
        LoadView( v );
        FOREVER
            {
            if (++time > 5*50) break;

            i = ri->RxOffset;
            i += BMinc; if (i>100 || i<1) BMinc = -BMinc;
            ri->RxOffset = i;

            MakeVPort( v, vp );
            WaitBOVP( vp );
            MrgCop( v );
            }
        Cleanup();
}

