/*************************************************************************
 ***                           doubleWB.C                (JJB TEMPLAR) ***
 *** Date begun: 3/5/89.                                               ***
 *** Last modified: 5/5/89.                                            ***
 *************************************************************************/

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

#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/intuition.h>

#include "doBMap.h"
#include "hikepri.h"

#define MYPRI   0

int     noWBcons = 1;

struct GfxBase          *GfxBase;
struct IntuitionBase    *IntuitionBase;

struct Screen   *Screen;        /* WB screen */
struct BitMap   oldBM,bitmap;
struct RastPort rastport;
struct View             *view;
struct ViewPort         *vport;
struct ColorMap *colormap,*oldcmap;

extern UWORD far    image[1800];
struct Image    jjbimg = {0,0,228,60,2,&image[0],3,0,NULL};

void    dolibs();
void    dodisp();
void    cleanup(int);

void    dolibs() /*======================================================*/
{
    if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L))) {
        printf("ERROR: failed to open graphics.library!\n");
        cleanup(0);
    }
    if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0L))) {
        printf("ERROR: failed to open intuition.library!\n");
        cleanup(1);
    }
}

void    dodisp() /*======================================================*/
{
UWORD   *ctable;
int     j;

    Disable();
    WBenchToFront();
    Screen = IntuitionBase->FirstScreen;        /* Pointer to WB screen */
    view = GfxBase->ActiView;                   /* Get view */
    Enable();

    bitmap = Screen->BitMap;                        /* Get dimensions */
    if (!doBMap(&bitmap,1)) {
        printf("ERROR: failed to open bitmap!\n");
        cleanup(2);
    }
    InitRastPort(&rastport);    rastport.BitMap = &bitmap;

    Screen->BitMap.Depth = 4;
    Screen->BitMap.Planes[2] = bitmap.Planes[0];
    Screen->BitMap.Planes[3] = bitmap.Planes[1];

    colormap = (struct ColorMap *)GetColorMap(16);
    oldcmap = Screen->ViewPort.ColorMap;
    Screen->ViewPort.ColorMap = colormap;

    ctable = (UWORD *)(colormap->ColorTable);

    for (j = 0; j < 4; j++) {
        ctable[j] = GetRGB4(oldcmap,j);
        ctable[4+j] = 0x700;
        ctable[8+j] = 0x779;
        ctable[12+j] = 0x457;
    }

    vport = &(Screen->ViewPort);
    MakeVPort(view,vport);
    Screen->BitMap.Depth = 2;
    MrgCop(view);
}

void    main(argc,argv) /*===============================================*/
int     argc;
char    *argv[];
{
    hikepri(MYPRI,1);
    dolibs();
    dodisp();

    DrawImage(&rastport,&jjbimg,191,139);

    WaitBOVP(vport);
    LoadView(view);

    Delay(1500);        /* 300 seconds */

    cleanup(4);
}

void    cleanup(bra) /*==================================================*/
int     bra;
{
    switch (bra) {
        case (4): vport->ColorMap = oldcmap;
                  FreeColorMap(colormap);
                  MakeVPort(view,vport);
                  MrgCop(view);
                  LoadView(view);
        case (3): doBMap(&bitmap,0);
        case (2): if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
        case (1): if (GfxBase) CloseLibrary((struct Library *)GfxBase);
        case (0): hikepri(0,0);
    }
    exit(0);
}
