/****************************************************************
 *								*
 *	Dit moet een vectorballsprogramma worden.		*
 *								*
 *	Klaas.							*
 *								*
 *								*
 *								*
 *								*
 *								*
 ****************************************************************/
#include <exec/types.h>
#include <graphics/gfx.h>
#include <graphics/gfxmacros.h>
#include <graphics/view.h>
#include <graphics/rastport.h>
#include <graphics/gfxbase.h>
#include <exec/exec.h>
#include "uni4.h"


extern struct DisplayInfo di;	/*predefined in uni#.h, defined in ch#.c */

struct View view, *oldview;
struct ViewPort viewport;
struct ColorMap *cm;

struct BitMap bitmp1, bitmp2, sourcebitmap;
struct BitMap *bitmap;

extern struct RastPort rastport;
struct RasInfo rasinfo;
extern struct ColorMap *GetColorMap ();

struct GfxBase *gfxbase;

USHORT colortable[] =
/* colours 0-7 are for the screen
 * back, white, black, blue , red , yellow,green, no use */
{0x242, 0xeee, 0x000, 0x33d, 0xd33, 0xcc3, 0x3e3, 0x222,
/* colour 8-15 are not in use */
 9, 9, 9, 9, 9, 9, 9, 9,
/* colours 16-31 are for sprites, as we don't use sprites, all NULL.*/
 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0};
UWORD *colorpalette;


struct Task *mytask;
BYTE oldpriority;

/* * * * * * * * * * * * * * * * * * * * * * */
SetUp ()
{
  LONG i;

  mytask = (struct Task *) FindTask (NULL);	/*search mytask */
  oldpriority = SetTaskPri (&mytask, 21);	/* set TaskPri very high */

  gfxbase = (struct GfxBase *) OpenLibrary ("graphics.library", 0L);

  oldview = gfxbase->ActiView;

  InitView (&view);
  InitVPort (&viewport);
  view.ViewPort = &viewport;
  view.Modes = NULL;
  viewport.Modes = NULL;	/*just a normal Lo-res*/

/* Initialize bitmaps */
  InitBitMap (&bitmp1, PDEPTH, PWIDTH, PHEIGHT);
  InitBitMap (&bitmp2, PDEPTH, PWIDTH, PHEIGHT);
  InitBitMap (&sourcebitmap, SDEPTH, SWIDTH, SHEIGHT);

  InitRastPort (&rastport);
  rastport.BitMap = &bitmp1;
  SetDrMd (&rastport, JAM1);	/*see-through holes in the text */
  rasinfo.BitMap = &bitmp1;
  rasinfo.RxOffset = (PWIDTH - VWIDTH) / 2;
  rasinfo.RyOffset = (PHEIGHT - VHEIGHT) / 2;
  rasinfo.Next = NULL;
  for (i = 0; i < PDEPTH; i++)
    {
      bitmp1.Planes[i] = (PLANEPTR) AllocRaster (PWIDTH, PHEIGHT);
      if (bitmp1.Planes[i] == NULL)
	exit (-1);
      BltClear (bitmp1.Planes[i], RASSIZE (PWIDTH, PHEIGHT), 1L);

      bitmp2.Planes[i] = (PLANEPTR) AllocRaster (PWIDTH, PHEIGHT);
      if (bitmp2.Planes[i] == NULL)
	exit (-1);
      BltClear (bitmp2.Planes[i], RASSIZE (PWIDTH, PHEIGHT), 1L);

      sourcebitmap.Planes[i] = (PLANEPTR) AllocRaster (SWIDTH, SHEIGHT);
      if (sourcebitmap.Planes[i] == NULL)
	exit (-1);
      BltClear (sourcebitmap.Planes[i], RASSIZE (SWIDTH, SHEIGHT), 1L);
    }

  viewport.DWidth = VWIDTH;
  viewport.DHeight = VHEIGHT;
  viewport.DxOffset = -12;
  viewport.DyOffset = 0;
  viewport.RasInfo = &rasinfo;

/********************** colourmap ***********/
  cm = GetColorMap (32L);

  colorpalette = (UWORD *) cm->ColorTable;
  for (i = 0; i < 32; i++)
    *colorpalette++ = colortable[i];

  viewport.ColorMap = cm;

  LoadPic ();			/* put picture in sourcebitmap */

  MakeVPort (&view, &viewport);	/* calculate display*/
  MrgCop (&view);
  LoadView (&view);

  bitmap = &bitmp1;
}				/* end-of-SetUp()  */

Quit ()
{
  LONG i;
  for (i = 0; i < PDEPTH; i++)	/* free bitplanes */
    {
      if (bitmp1.Planes[i])
	FreeRaster (bitmp1.Planes[i], PWIDTH, PHEIGHT);
      if (bitmp2.Planes[i])
	FreeRaster (bitmp2.Planes[i], PWIDTH, PHEIGHT);
      if (sourcebitmap.Planes[i])
	FreeRaster (sourcebitmap.Planes[i], SWIDTH, SHEIGHT);
    }
  if (cm)			/* free other stuff */
    FreeColorMap (cm);
  FreeVPortCopLists (&viewport);
  FreeCprList (view.LOFCprList);

/* restore the system's old screen */
  LoadView (oldview);
/* restore the old priority of the task */
  SetTaskPri (&mytask, oldpriority);

}				/* end-of-Quit() */



/*************************************SwapShow
 * swap the displayed and the drawn bitmap.
 * and clear the before displayed bitmap.
 */
SwapShow ()
{
  int n;

  WaitTOF ();			/* wait until bottom line is displayed */
  if (di.whichone == 1)
    {
      di.whichone = 2;
      rasinfo.BitMap = &bitmp1;	/* swap the displayed bitmap */
      ScrollVPort (&viewport);	/* change display */
      WaitTOF ();
      rastport.BitMap = bitmap = &bitmp2;	/* change drawing bitmap */
      for (n = 0; n < PDEPTH; n++)	/* 'clean' it */
	BltClear (bitmp2.Planes[n], RASSIZE (PWIDTH, PHEIGHT), 1L);
    }
  else
    {
      di.whichone = 1;
      rasinfo.BitMap = &bitmp2;
      ScrollVPort (&viewport);
      WaitTOF ();
      rastport.BitMap = bitmap = &bitmp1;
      for (n = 0; n < PDEPTH; n++)	/* 'clean' it */
	BltClear (bitmp1.Planes[n], RASSIZE (PWIDTH, PHEIGHT), 1L);
    }
}				/* end-of-SwapShow() */
