/*
    Copyright (C) 1995-2014, The AROS Development Team. All rights reserved.
*/

#include <proto/graphics.h>
#include <proto/exec.h>
#include <intuition/intuitionbase.h>
#include <intuition/screens.h>
#include <graphics/view.h>

#include <stdio.h>

int main(void)
{
  struct IntuitionBase * IntuitionBase;
  struct Library * GfxBase;
  struct ViewPort * vp;
  struct ColorMap * cm;
#define NUMCOLORS 32
  ULONG n[2*NUMCOLORS];
  ULONG i;

  IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  GfxBase = OpenLibrary("graphics.library",0);

  vp = &IntuitionBase->ActiveScreen->ViewPort;
  cm = vp->ColorMap;
  
  n[0]  = ObtainPen(cm,
                    -1,
                    0xa0000000,
                    0xb0000000,
                    0xc0000000,
                    0);
            
  printf("Got pen number %ld.\n",(long)n[0]);
  
  printf("Trying to obtain the same pen as shared.\n");
  n[1]  = ObtainPen(cm,
                    -1,
                    0xa0000000,
                    0xb0000000,
                    0xc0000000,
                    0);
  
  printf("Got pen number %ld (=%ld!).\n",(long)n[1],(long)n[0]);
  
  printf("Trying to obtain a pen as exclusive.\n");
  n[2]  = ObtainPen(cm,
                    -1,
                    0xa0000000,
                    0xb0000000,
                    0xc0000000,
                    PENF_EXCLUSIVE);
    
  printf("Got pen number %ld.\n",(long)n[2]);

  printf("Trying to allocate pen %ld as exclusive again.\n",(long)n[2]);
  n[3]  = ObtainPen(cm,
                    n[2],
                    0xa0000000,
                    0xb0000000,
                    0xc0000000,
                    PENF_EXCLUSIVE);
    
  printf("Got pen number %ld.\n",(long)n[3]);

  printf("Trying to allocate pen %ld as shared.\n",(long)n[2]);
  n[4]  = ObtainPen(cm,
                    n[2],
                    0xa0000000,
                    0xb0000000,
                    0xc0000000,
                    0);
    
  printf("Got pen number %ld.\n",(long)n[4]);

  printf("Releasing all pens.\n");
  ReleasePen(cm, n[4]);
  ReleasePen(cm, n[3]);
  ReleasePen(cm, n[2]);
  ReleasePen(cm, n[1]);
  ReleasePen(cm, n[0]);

  printf("Obtaining a shared pen.\n");
  n[0]  = ObtainPen(cm,
                    -1,
                    0xa0000000,
                    0xb0000000,
                    0xc0000000,
                    0);
            
  printf("Got pen number %ld.\n",(long)n[0]);
  
  ReleasePen(cm, n[0]);

  printf("---------------------------\n");

  printf("Trying to allocate %d shared pens\n",NUMCOLORS);
  i = 0;
  while (i < NUMCOLORS)
  {
    n[i] = ObtainPen(cm, -1, i << 28, i << 24, i << 20,0);
    
    printf("Got pen %ld\n",(long)n[i]);
    i++;
  }

  printf("Trying to get %d exclusive pens (no pen was released)\n",NUMCOLORS);
  i = 0;
  while (i < NUMCOLORS)
  {
    n[i+NUMCOLORS] = ObtainPen(cm, -1, 0,0,0,PENF_EXCLUSIVE);
    
    printf("Got pen %ld (=-1)\n",(long)n[i+NUMCOLORS]);
    i++;
  }
 
  printf("Releasing all pens!\n");
  i = 0;
  while (i < 2 * NUMCOLORS)
  {
    ReleasePen(cm, n[i]);
    i++;
  }

  printf("---------------------------\n");

  
  printf("Trying to get %d exclusive pens\n",NUMCOLORS);
  i = 0;
  while (i < NUMCOLORS)
  {
    n[i] = ObtainPen(cm, -1, 0,0,0,PENF_EXCLUSIVE);
    
    printf("Got pen %ld\n",(long)n[i]);
    i++;
  }

  printf("Trying to allocate %d shared pens (no pen was released)\n",NUMCOLORS);
  i = 0;
  while (i < NUMCOLORS)
  {
    n[i+NUMCOLORS] = ObtainPen(cm, -1, 0,0,0,0);
    
    printf("Got pen %ld (=-1)\n",(long)n[i+NUMCOLORS]);
    i++;
  }
 
  printf("Releasing all pens!\n");
  i = 0;
  while (i < 2*NUMCOLORS)
  {
    ReleasePen(cm, n[i]);
    i++;
  }

  printf("---------------------------\n");

  printf("Trying to allocate %d shared pens at certain indices\n",NUMCOLORS);
  i = 0;
  while (i < NUMCOLORS)
  {
    n[i] = ObtainPen(cm, i, 0, 0, 0,0);
    
    printf("Got pen %ld\n",(long)n[i]);
    i++;
  }

  printf("Trying to get %d exclusive pens (no pen was released)\n",NUMCOLORS);
  i = 0;
  while (i < NUMCOLORS)
  {
    n[i+NUMCOLORS] = ObtainPen(cm, -1, 0,0,0,PENF_EXCLUSIVE);
    
    printf("Got pen %ld (=-1)\n",(long)n[i+NUMCOLORS]);
    i++;
  }
 
  printf("Releasing all pens!\n");
  i = 0;
  while (i < 2 * NUMCOLORS)
  {
    ReleasePen(cm, n[i]);
    i++;
  }
  
  printf("---------------------------\n");

  printf("Trying to get %d exclusive pens at certain indices\n",NUMCOLORS);
  i = 0;
  while (i < NUMCOLORS)
  {
    n[i] = ObtainPen(cm, i, 0,0,0,PENF_EXCLUSIVE);
    
    printf("Got pen %ld\n",(long)n[i]);
    i++;
  }

  printf("Trying to allocate %d shared pens (no pen was released)\n",NUMCOLORS);
  i = 0;
  while (i < NUMCOLORS)
  {
    n[i+NUMCOLORS] = ObtainPen(cm, -1, 0,0,0,0);
    
    printf("Got pen %ld (=-1)\n",(long)n[i+NUMCOLORS]);
    i++;
  }
 
  printf("Releasing all pens!\n");
  i = 0;
  while (i < 2*NUMCOLORS)
  {
    ReleasePen(cm, n[i]);
    i++;
  }
  
  printf("---------------------------\n");
  
    return 0;
}
