#include <exec/types.h>
#include <exec/exec.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>
#include <libraries/diskfont.h>
#include <devices/printer.h>

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct DosBase *DosBase;
struct DiskfontBase *DiskfontBase;
struct Port *printerPort;
struct TextFont *Topaz;
struct TextAttr TopazAttr;

union printerIO
{
   struct IOStdReq ios;
   struct IODRPReq iodrp;
   struct IOPrtCmdReq iopc;
};

union printerIO *printerMsg;


#define MAX_X 960
#define MAX_Y 720
#define STARTINGROW 0

struct NewScreen NewScr = 
{
   0,0,640,200,1,0,1,HIRES,CUSTOMSCREEN,NULL,
   "PrintFonts V1.10 (c)1989, 1990 by Dave Schreiber.  All Rights Reserved.",
   NULL,NULL
};

struct NewWindow NewWdw =
{
   0,10,640,190,-1,-1,NULL,SUPER_BITMAP|BORDERLESS|GIMMEZEROZERO,
   NULL,NULL,NULL,NULL,NULL,0,0,0,0,CUSTOMSCREEN
};


struct Screen *Scr;
struct Window *Wdw;
struct BitMap BM;

void startup();
void ConstructBitmap();
void OpenPrinter();
void closeup();
void ExitProg();

void startup()        /*Open libraries, etc.*/
{
   if((IntuitionBase=(struct IntuitionBase *)OpenLibrary
         ("intuition.library",0))==NULL)     /*IntuitionBase*/
      ExitProg("Can't open intuitionbase!");
   
   if((GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0))==NULL)
      ExitProg("Can't open GfxBase!");       /*GfxBase*/
      
   if((DosBase=(struct DosBase *)OpenLibrary("dos.library",0))==NULL)
      ExitProg("Can't open DosBase!");       /*DosBase*/
      
   if((DiskfontBase=(struct DiskfontBase *)OpenLibrary("diskfont.library",
         0))==NULL)                           /*DiskfontBase*/
      ExitProg("Can't open DiskfontBase!");
   
   OpenPrinter(); /*Open the printer.device*/
   Scr=(struct Screen *)OpenScreen(&NewScr);
   if(Scr == NULL)
      ExitProg("Couldn't open the screen!");
   ScreenToBack(Scr); /*Push the brand new screen to the back (hide it)*/
   NewWdw.Screen=(struct Screen *)Scr;
   ConstructBitmap(&NewWdw,&BM);
   Wdw=(struct Window *)OpenWindow(&NewWdw); /*Open the window*/
   if(Wdw==NULL)
   {
      CloseScreen(Scr);
      ExitProg("Couldn't open the window!");
   }
   else
   {
      SetRGB4(ViewPortAddress(Wdw),0,0xf,0xf,0xf);  /*White background*/
      SetRGB4(ViewPortAddress(Wdw),1,0,0,0);        /*Black text*/
   }
   
}
   
void ConstructBitmap(nw,bm) /*Setup the bitmap for the window*/
struct NewWindow *nw;
struct BitMap *bm;
{
   InitBitMap(bm,1,MAX_X,MAX_Y);    /*Initialize the superbitmap*/
   if((bm->Planes[0]=(PLANEPTR)AllocRaster(MAX_X,MAX_Y))==NULL)
      ExitProg("Couldn't allocate enough memory!");
   nw->BitMap=(struct BitMap *)bm;     /*Link the BitMap into the Window*/   
}

void OpenPrinter() /*Open the printer device*/
{
   printerPort=(struct Port *)CreatePort("my.print.port",0);
   if(printerPort == NULL)
      ExitProg("Couldn't access the printer!");
   printerMsg=(union printerIO *)CreateExtIO(printerPort,
         sizeof(union printerIO));
   if(printerMsg==NULL)
      ExitProg("Couldn't access the printer!");
   if(OpenDevice("printer.device",0,printerMsg,0)!=NULL)
      ExitProg("Couldn't access the printer!");
}

void closeup()         /*Close libraries, etc.*/
{
   CloseFont(Topaz);
   CloseDevice(printerMsg);
   DeleteExtIO(printerMsg,sizeof(union printerIO));
   DeletePort(printerPort);
   CloseWindow(Wdw);
   FreeRaster(BM.Planes[0],MAX_X,MAX_Y);
   CloseScreen(Scr);
   CloseLibrary(DiskfontBase);   /*Close diskfont.library*/
   CloseLibrary(DosBase);         /*Close dos.library*/
   CloseLibrary(GfxBase);         /*Close graphics.library*/
   CloseLibrary(IntuitionBase);   /*Close intuition.library*/
}

void ExitProg(err)        /*Error.  So print a message & terminate*/
char *err;
{
   puts(err);  /*Print the error*/
   exit(100);  /*and exit*/
}
