/* PointerDemo © 1989 AMIGA Plus
   v1.0 Friday 14-Jul-89 11:48:18
   By Aki Rimpilainen & Arnie Cachelin */

#include <exec/exec.h>
#include <exec/types.h>
#include <intuition/intuition.h>
#include <functions.h>
#include <stdio.h>

#define rp1 wd1->RPort     /* save a bit of typing... */
#define rp2 wd2->RPort

struct IntuitionBase *IntuitionBase;
struct GfxBase       *GfxBase;
struct Window        *wd1,*wd2;
struct ViewPort      *vp;
struct ColorMap      *cm1,*cm2;

struct NewWindow nw1 =
{
  50L,50L,
  540L,60L,
  3L,2L,     /* odd window pen selection for interesting look %) */
  CLOSEWINDOW,
  WINDOWCLOSE | ACTIVATE,
  NULL,
  NULL,
  (UBYTE *)"AMIGA Plus PointerDemo, (click on close box to exit)",
  NULL,
  NULL,
  0L,0L,
  0L,0L,
  WBENCHSCREEN
};

#include "Ptr1.h"   /* These includes, created with GetPointer, contain */
#include "Ptr2.h"   /* colors for new pointers and SimpleSprite-structures */

UWORD *SpriteData1, *SpriteData2;

main()
{

/* Open Some libraries...    */
  IntuitionBase = (struct IntuitionBase *)
    OpenLibrary("intuition.library",LIBRARY_VERSION);
  if (IntuitionBase==0) { Closing("Can't open intuition."); Exit(0L); }

  GfxBase = (struct GfxBase *)
    OpenLibrary("graphics.library",LIBRARY_VERSION);
  if (GfxBase==0) { Closing("Can't open graphics."); Exit(0L); }

/* Allocate some memory ...    */
  SpriteData1 = AllocMem(80L,MEMF_CLEAR | MEMF_CHIP);
  if (SpriteData1==0) { Closing("Can't allocate memory."); Exit(0L); }

  SpriteData2 = AllocMem(80L,MEMF_CLEAR | MEMF_CHIP);
  if (SpriteData2==0) { Closing("Can't allocate memory."); Exit(0L); }

/* open first window */
  wd1 = (struct Window *)OpenWindow(&nw1);
  if (wd1==0) { Closing("Can't open window 1"); Exit(0L); }

/* open second window by modifing existing NewWindow-structure */
  nw1.IDCMPFlags=0L; /* get rid of close gadget in 2nd window */
  nw1.Flags=ACTIVATE;
  nw1.TopEdge=120L;
  nw1.Title=(UBYTE *)"PointerDemo, Window 2";
  wd2 = (struct Window *)OpenWindow(&nw1);
  if (wd2==0) { Closing("Can't open window 2"); Exit(0L); }

  vp=ViewPortAddress(wd1);

  cm1=vp->ColorMap;                  /* saving old colormap */
  cm2=GetColorMap(32L);
  if (cm2==0) { Closing("Can't get colormap."); Exit(0L); }
  vp->ColorMap=cm2;                  /* allocating new colormap */
                                     /* displaying pointers */
  SetPointer(wd1,&Ptr1[0],16L,2L,Ptr1_XOff,Ptr1_YOff);
  SetPointer(wd2,&Ptr2[0],16L,2L,Ptr2_XOff,Ptr2_YOff);
                                     /* setting the right color */
  SetRGB4(vp,17L,Ptr1_RGB17[0],Ptr1_RGB17[1],Ptr1_RGB17[2]);
  SetRGB4(vp,18L,Ptr1_RGB18[0],Ptr1_RGB18[1],Ptr1_RGB18[2]);
  SetRGB4(vp,19L,Ptr1_RGB19[0],Ptr1_RGB19[1],Ptr1_RGB19[2]);

  Wait(1L<<wd1->UserPort->mp_SigBit); /* wait for a click on close box */

  vp->ColorMap=cm1;         /* restore colors */
  RemakeDisplay();

  Closing(0L);
}


Closing(mes)  /* Clean up after our program... */
char *mes;    /* and give any error message     */

{
  if (cm2) FreeColorMap(cm2);
  if (wd2) CloseWindow(wd2);
  if (wd1) CloseWindow(wd1);
  if (SpriteData2) FreeMem(SpriteData2,80L);
  if (SpriteData1) FreeMem(SpriteData1,80L);
  if (GfxBase) CloseLibrary(GfxBase);
  if (IntuitionBase) CloseLibrary(IntuitionBase);
}

