/* draw32.c 25.4.3  */
/* From Amiga C for Beginners */
/* by Abacus                  */

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

extern LONG OpenLibrary();
extern struct Screen *OpenScreen();
extern struct Window *OpenWindow();

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

#define INTUITION_REV 0
#define GRAPHICS_REV 0

struct TextAttr Font =
{
   "topaz.font",
   TOPAZ_EIGHTY,
   FS_NORMAL,
   FPF_ROMFONT,
};

UBYTE screentitle[81];


struct NewScreen NewScreen =
{
   0,0,
   320, /* was 640 Width */
   200, /* Height; PAL version-may change 200 to 256 */
   2,   /* 3 bitplanes = 8 colors */
   2,3, /* another color combination */
   NULL, /* rem HIRES  */
   CUSTOMSCREEN,
   &Font,
   screentitle,
   NULL,
   NULL,
};

struct NewWindow NewWindow =
{
   20, 20,   /* X and Y Position */
   300, 180,  /* was 400 Width, Height */
   0,1,     /* Colors (0 - 7) */
   CLOSEWINDOW,
   WINDOWCLOSE | SMART_REFRESH | ACTIVATE | WINDOWSIZING |
      SIZEBRIGHT | WINDOWDRAG | WINDOWDEPTH,
   NULL,
   NULL,
   "* My window *",
   NULL,
   NULL,
   190, 20,
   320, 200, /* was 640 in PAL systems may change 200 to 256 */
   CUSTOMSCREEN
};


main(argc,argv)
int argc;
char *argv[];
{
   struct Screen *Screen;
   struct Window *Window;
   register char s[81];
   int color = 4;
   register int x, y, xalt, yalt, colour = 1 ;

   if((IntuitionBase = (struct IntuitionBase *)
     OpenLibrary("intuition.library", INTUITION_REV))
     == NULL)
     exit(FALSE);

   if ((GfxBase = (struct GfxBase *)
      OpenLibrary("graphics.library",GRAPHICS_REV))
      == NULL)
      exit(FALSE);

   if (argc != 4)
      {
         printf("Error in arguments\n");
         printf("X-Res Y-Res Bitplanes\n");
      }
   else
      {
         NewScreen.Width   =  atoi(argv[1]);
         NewScreen.Height  =  atoi(argv[2]);
         NewScreen.Depth   =  atoi(argv[3]);
         if(NewScreen.Depth > 5 || NewScreen.Depth < 1)
            NewScreen.Depth = 2;
         color = 1 << NewScreen.Depth;
         NewScreen.DetailPen  =  color - 1;
         NewScreen.BlockPen   =  color - 2;
      }



   sprintf(screentitle,"This screen has %d colors",
            color);

    if ( (Screen = OpenScreen(&NewScreen) ) == NULL)
      exit(FALSE);

   NewWindow.Screen = Screen; /* Do not forget! */

   if (argc == 4)
      {
         NewWindow.Width      =  Screen->Width/2;
         NewWindow.Height     =  Screen->Height/3;
         NewWindow.MinWidth   =  Screen->Width/3;
         NewWindow.MinHeight  =  Screen->Height/5;
         NewWindow.MaxWidth   =  Screen->Width;
         NewWindow.MaxHeight  =  Screen->Height;
      }


   if( (Window = OpenWindow(&NewWindow) ) == NULL)
     exit(FALSE);

  text(Window,"Hello there!",20,20);

   /* Initialize with start values */
   Move(Window->RPort, xalt = Window->MouseX,
           yalt = Window->MouseY);

   /* Drawing starts here until upper or*/
   /* left  border is reached */
   while( (x = Window->MouseX) > 0 &&
            (y = Window->MouseY) > 0)
      {
         sprintf(s,"X = %3d, Y =%3d", x , y);
         text(Window, s, 150, 7);
         Move(Window->RPort, xalt, yalt);
         Draw(Window->RPort, xalt = x, yalt = y);

         SetAPen(Window->RPort, colour++);  /* new  */
         if (colour == color) colour=1;     /* new */


      }

   text(Window, "Please click the close gadget", 20, 20);


   /* Wait for Close-Gadget */
   Wait(1 << Window->UserPort->mp_SigBit);

   CloseWindow(Window);
    /* Close everything in sequence*/
   CloseScreen(Screen);
   CloseLibrary(GfxBase);
   CloseLibrary(IntuitionBase);
   exit(TRUE);
}



text(w_ptr, s, x, y)
struct Window *w_ptr;
char *s;
int x, y;
{
   Move(w_ptr->RPort, x, y);
   Text(w_ptr->RPort, s, strlen(s));
}

