#include <libraries/dosextens.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>

#define BITMAP

#include "maze.h"

/* Just change these defines for a new resolution */
#define SCREENWIDTH  320
#define SCREENHEIGHT 212
#define VIEWMODE     NULL

struct NewScreen ns=
{
 0,0,SCREENWIDTH,SCREENHEIGHT,1,
 1,0,
 VIEWMODE,
 CUSTOMSCREEN|SHOWTITLE,
 NULL,
 NULL,
 NULL,
 NULL
};
struct NewWindow nw=
{
 0,0,SCREENWIDTH,SCREENHEIGHT,
 1,0,
 VANILLAKEY|CLOSEWINDOW|GADGETUP,
 BORDERLESS|NOCAREREFRESH|WINDOWCLOSE|ACTIVATE,
 NULL, NULL,
 "Maze Demo",/*NULL,*/
 NULL, NULL, NULL,NULL,NULL,NULL,
 CUSTOMSCREEN
};

struct Library *GfxBase = NULL,*IntuitionBase = NULL;
struct Screen *screen = NULL;
struct Window *window = NULL;
struct BitMap *bm;
struct IntuiMessage *msg;
ULONG  class;
BOOL   WorkBench=NULL;
void newmaze(),leave(),main();

void main(argc,argv)
short argc;
char argv[];
{
 if (!argc) WorkBench=TRUE;
 if (!(IntuitionBase = (struct Library*) OpenLibrary("intuition.library",0L)))
    leave("No Intuition Library");
 if (!(GfxBase = (struct Library*) OpenLibrary("graphics.library",0L)))
    leave("No Graphics Library");

 if (!(screen = (struct Screen*) OpenScreen(&ns)))
    leave ("Can't open Screen");

 nw.Screen=screen;
 if (!(window = (struct Window*) OpenWindow(&nw)))
    leave("Can't open Window");

 SetRGB4(&screen->ViewPort,0,0,0,0);
 SetRGB4(&screen->ViewPort,1,12,12,12);
 bm = screen->ViewPort.RasInfo->BitMap;

 newmaze();

 for(;;)
 {
  WaitPort(window->UserPort);
  if(msg=(struct IntuiMessage*)GetMsg(window->UserPort))
  {
   class=msg->Class;
   ReplyMsg(msg);
   switch(class)
   {
    case CLOSEWINDOW:
     leave("");
    case VANILLAKEY:
     newmaze();
   }
  }
 }
}
void leave(error)
char *error;
{
 BPTR file;

 if(window)        CloseWindow(window);
 if(screen)        CloseScreen(screen);
 if(IntuitionBase) CloseLibrary(IntuitionBase);
 if(GfxBase)       CloseLibrary(GfxBase);

 if (*error && WorkBench &&
     (file=Open("con:20/70/400/60/Maze Demo",MODE_OLDFILE)))
 {
  Write(file,error,strlen(error));
  Delay(200L);
  Close(file);
 }
 else if (*error) printf("%s\n",error);
 exit(0);
}

void newmaze()
{
 BltClear (bm->Planes[0]+12*bm->BytesPerRow,  /*skip gadgets & title */
           (bm->Rows-12)*bm->BytesPerRow,NULL);

 /* Get parameters from our BitMap,so we don't have to change them on
    different resolutions */
 if(!(maze(bm->BytesPerRow*8,bm->Rows-12,
           bm->Planes[0]+12*bm->BytesPerRow)))
   leave("Couldn't allocate Maze-Stack");
}

