/*************************************************************************

   Picture.c
   28/02/1992
   
   Example 'C' code for use with MEGADISC article on using IFF_ILBM files.
   
*************************************************************************/

#include <exec/types.h>
#include <graphics/gfxmacros.h>
#include <intuition/intuition.h>
#include "ILBM.h"
#include "Picture.h"
                              /* functions in ILBM.o that we use.          */
extern   struct   Window   *OpenWindow();
extern   struct   Screen   *OpenScreen();
extern   PLANEPTR AllocRaster();

extern   BOOL  ReadFile();
extern   VOID  FadeDown();    /* Fade up viewport from black to CMAP1      */
extern   VOID  FadeUp();      /* Fade viewport from CMAP1 to black.        */
extern   VOID  BlackoutVP();  /* make CMAP2 black and load into viewport   */
extern   VOID  PutCMAP();     /* put CMAP1 into viewport.                  */

BOOL  SetUp();             /* Picture.c functions                      */

VOID  DoPicture();
VOID  Cleanup();

struct   IntuitionBase *IntuitionBase;    /* pointer to IntuitionBase   */
struct   GfxBase  *GfxBase;               /* pointer to Graphics Base   */
struct   Screen   *screen;                /* pointer to our screen      */
struct   Window   *window;                /* pointer to window struct   */
struct   BitMap   bm;                     /* BitMap for my use          */
struct   RastPort osrp;                   /* off screen RastPort        */
struct   RastPort *rpp;                   /* pointer to RastPort        */
struct   RastPort *rp;                    /* pointer to windows RastPort*/
struct   ViewPort *vp;

struct NewScreen new_screen=
{
  0,            /* LeftEdge  Should always be 0.                        */
  0,            /* TopEdge   Top of the display.                        */
  WIDTH,        /* Width     We are using a low-resolution screen.      */
  HEIGHT,       /* Height    Non-Interlaced NTSC (American) display.    */
  DEPTH,        /* Depth     8 colours. Defined in Picture.h            */
  0,            /* DetailPen Text should be drawn with colour reg. 0    */
  1,            /* BlockPen  Blocks should be drawn with colour reg. 1  */
  HIRES,        /* ViewModes No special modes. (Low-res, Non-Interlaced) */
  CUSTOMSCREEN | SCREENBEHIND,
                /* Type      Your own customized screen.                */
  NULL,         /* Font      Default font.                              */
  NULL,         /* Title     The screen' title.                         */
  NULL,         /* Gadget    Must for the moment be NULL.               */
  NULL          /* BitMap    No special CustomBitMap.                   */
};

struct NewWindow new_window=
{
  0,             /* LeftEdge    x position of the window.               */
  0,             /* TopEdge     y positio of the window.                */
  WIDTH,         /* Width       150 pixels wide.                        */
  HEIGHT,        /* Height      100 lines high.                         */
  0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  MOUSEBUTTONS,  /* IDCMPFlags  Tell me if a mouse button is pressed.   */
  SMART_REFRESH | BORDERLESS, 
                 /* Flags       Intuition should refresh the window.    */
  NULL,          /* FirstGadget No Custom Gadgets.                      */
  NULL,          /* CheckMark   Use Intuition's default CheckMark (v).  */
  NULL,          /* Title       Title of the window.                    */
  NULL,          /* Screen      Connected to the Workbench Screen.      */
  NULL,          /* BitMap      No Custom BitMap.                       */
  0,             /* MinWidth    We do not need to care about these      */
  0,             /* MinHeight   since we havent supplied the window with */
  0,             /* MaxWidth    a Sizing Gadget.                        */
  0,             /* MaxHeight                                           */
  CUSTOMSCREEN   /* Type                                                */
};

main()
{
   if(SetUp()) DoPicture();   /* this is the program  */
   
   Cleanup();
}

BOOL  SetUp()
{
   printf("Please Wait...\nSetting up and loading screen.\n");
   
   IntuitionBase = (struct IntuitionBase *)
      OpenLibrary( "intuition.library", 0 );
   if( IntuitionBase == NULL )
   {
      printf("Intuition failed to open.\n");
      return(FALSE);
   }
   
   GfxBase = (struct Gfxbase *) OpenLibrary("graphics.library",0);
   if(GfxBase == NULL)
   {
      printf("Graphics failed to open.\n");
      return(FALSE);
   }

   screen = OpenScreen( &new_screen );
   if(screen == NULL)
   {
      printf("Screen failed to open.\n");
      return(FALSE);
   }
   
   new_window.Screen = screen;   /* link the new_window structure to the   */
   vp = &screen->ViewPort;       /* newly opened screen.                   */

   /* We will now try to open the window: */
   window = OpenWindow( &new_window );
   if(window == NULL)
   {
      printf("No window!\n");
      return(FALSE);  
   }
   rp = window->RPort;
   
   return(TRUE);  /* if everything opened and allocated correctly */
}

VOID  DoPicture()
{
   int   success;
   
   if(MakeBitMap(&bm, DEPTH, WIDTH, HEIGHT))
   {
      InitRastPort(&osrp); /* we have to set up a RastPort and link     */
      osrp.BitMap = &bm;   /* bit maps so that we have work areas.      */
      rpp = &osrp;
   
      success = ReadFile("file.pic", &bm, vp, OK_USE_CMAP);  /* Read the file     */
      if(success)
      {
         BlackoutVP(vp);                              /* black the screen  */
         ClipBlit(rpp, 0, 0, rp, 0, 0, 640, 256, 0xc0);  /* move the image */
         ScreenToFront(screen);        /* move screen to front for display */
         FadeUp(vp);                   /* fade the colours in.             */
         Wait(1L<<window->UserPort->mp_SigBit); /* wait for mouse button   */
                                             /* press which is the only */
                                             /* event in windows IDCMP  */
         FadeDown(vp);                       /* fade the colours to black  */
      }
      else
      {
         printf("Could not open picture file.\n");
      }
   }
}

VOID  Cleanup()   /* intelligently close any resources opened by program */
{
   printf("Cleaning up.\n");
   if(window)        CloseWindow ( window );
   if(screen)        CloseScreen ( screen );
   FreeBitMap(&bm);
   if(GfxBase)       CloseLibrary( GfxBase);
   if(IntuitionBase) CloseLibrary( IntuitionBase );
   Delay(50 * 2); /* In case of errors, I need to read the reason */   
}
