/* This program "demos" most of the features of ezlib.
 *
 * The big thing to notice is how much it abstracts the code you are
 * writing.  You no longer have to write gobs and gobs of crap to deal
 * with screens, and windows.
 *
 * Also notice, that you still should error check results from the ez.lib
 * functions.  This is the Amiga and not a protected memory environment,
 * so failure to check the return values could be dangerous.
 */

#include "ezlib.h"          /* get in the door with ez.lib */


main()
{
 struct Screen *screen=NULL;		/* our custom screen	       */
 struct Window *wind=NULL;		/* our window		       */
 struct Gadget *gadg=NULL;		/* do-nothing gadget	       */
 struct RastPort *rp;			/* rastport for Gfx calls      */
 struct TextFont *tf=NULL;		/* disk font ptr	       */
 char *string=NULL;			/* return ptr from getstring() */


 /* in reality, this call to openlibs() isn't even needed as it will happen
  * in makescreen() or makewindow().  However it makes things look more
  * symetric with the closelibs() call at the end (which you really should
  * make).
  */
 if (openlibs(GFX|INTUI) == NULL)
   {  MSG("no libs?\n"); exit(10L); }


 screen = makescreen(HIRES, 3);       /* gets a 640x400 8 color screen */
 if (screen == NULL)
  { closelibs(); exit(10); }          /* safe exit just in case... */


 /* set up some some nice colors */
 setcolor(screen, 0, BLACK);   setcolor(screen, 1, PINK);
 setcolor(screen, 2, YELLOW);  setcolor(screen, 3, ORANGE);
 setcolor(screen, 4, INDIGO);  setcolor(screen, 5, GREEN);


 /* notice that even though these window dimensions are totally off, they
  * are corrected in makewindow()
  */
 wind = makewindow(screen, 0, 0, -1, -1);
 if (wind == NULL)
  { killscreen(screen); closelibs(); exit(10); }   /* exit path */


 SetWindowTitles(wind, "EzLib Demo Program", -1);

 rp = wind->RPort;	   /* get a RastPort for calling the gfx library */


 /* if you happen to have this font in your fonts: directory, you're all
  * set.  If not, no biggie - we'll just use the default font.
  */

 tf = getfont("toronto.font", 12);
 if (tf != NULL)
   SetFont(rp, tf);

 /* get an input string from the user. */

 string = getstring(screen, "Enter a string here:", "This is a default string");

 if (string != NULL) {
   Move(rp, 160, 40);
   Print(rp, "You typed :");    /* ezlib #define for printing window text */
   Print(rp, string);
   FreeMem(string, strlen(string)+1);
 }


 draw_stuff(rp);       /* draw some simple graphics in the window */


 /* create a simple do-nothing gagdet */
 gadg = makeboolgadget(wind, 210, 175, "Don't have a cow dude!", 43);

 Move(rp, 200, 150);
 Print(rp, "Ezlib makes programming fun.");     /* be obnoxious here */
 Move(rp, 190, 170);
 Print(rp, "Click the close gadget to exit");


 /* since the window has a close gadget, the first message will be from
  * the user clicking the CLOSE gadget.  Soooooo, we can just exit.
  */
 WaitPort(wind->UserPort);

 getyn(wind, "Boy this sure is neat huh?");   /* just be annoying */

 /* here we go through the motions of closing up shop */

 if (tf)
   CloseFont(tf);  /* close the font we opened with the call to getfont() */

 if (gadg)
   killgadget(wind, gadg);   /* free gadget resources                      */

 killwindow(wind);           /* close the window with error checking       */
 killscreen(screen);         /* do same for the screen                     */
 closelibs();                /* close any opened libraries here            */
 exit(0);                    /* and finally call the normal exit() routine */
}


/* this function will draw some simple graphics into your rastport.
 * Some numbers are hardcoded, yes, but this is just a demo....
 */
draw_stuff(rp)
 struct RastPort *rp;
{
 int i;

 /* lets just draw some stuff on the screen */
 for(i=40; i < 160; i++ ) {
   Line(rp, 10, i, 620, (200 - i) );   /* ezlib #define for drawing lines */
   SetAPen(rp, i % 16);                /* change colors for each line */
 }

 for(i=20; i < 50; i++) {
   Line(rp, 10, i, 620, i);
   Circle(rp, 310, i, 10);       /* ezlib #define for drawing circles */
   SetAPen(rp, i % 16);
 }
}

