/********************************************************************
*		ShowAll - v1.2	(formerly Plop)			    *
*********************************************************************
This is a Public Domain Property, so feel free to copy, to modified,
etc as long as it is not for commercial use and this notice is intact

Original Author : Jim Kent      (April   86)
V1.1		: Andry Rachmat (October 86)
V1.2        : Brian Conrad  (November 86)
*********************************************************************

V1.1 Modification:
	Add Window and Close Window Gadget, remove 2 seconds timer
	Recover memory on OpenScreen failure
	Clean up the code to compile cleanly on Aztec C

Bugs:
	Argc loop seems not working for unknown reason
	On 1.1 Workbench Close Window Gadget will shown on the top left
	corner (I don't how to hide the window).
	MAJOR BUGS on 1.1 is the OpenScreen failure (mostly due to
	insufficient memory available to open hires screen) will recover
	99% of memory used, but it CRASHES the next time you run the
	program! (Works fine on 1.2 Workbench)

*********************************************************************

V1.2 Modification:
	Window Closes on MOUSEBUTTONS instead of CLOSEWINDOW
	No need to move pointer to Close Window Gadget

*********************************************************************/
#ifdef LATER
#define DEBUG
#endif LATER
#define PARANOID

#include	<exec/types.h>
#include 	<exec/nodes.h>
#include	<exec/libraries.h>
#include	<graphics/gfx.h>
#include	<graphics/rastport.h>
#include	<graphics/text.h>
#include 	<graphics/view.h>
#include	<intuition/intuition.h>
#include 	<stdio.h>
#include 	"jiff.h"




/*This should be in exec/libraries.h */
extern struct Library *OpenLibrary();

/*This should be in intuition/intuition.h */
extern struct Screen *OpenScreen();

extern struct Window *OpenWindow();
extern struct IntuiMessage *GetMsg();

/* These are the bases for the libraries you need to do any graphics
   much at all and still multi-task */
struct Library *GfxBase = NULL;
struct Library *LayersBase = NULL;
struct Library	*IntuitionBase = NULL;
struct IntuiMessage *message;

/* Well the workbench Screen isn't 320x200x5 so...*/
struct Screen *PlopScreen = NULL;
struct Window *window = NULL;

/* Just one font for me... not static cause you might want it
   in your menu modules. */
struct TextAttr PlopFont =
    {
	(UBYTE *) "topaz.font",  /* I think this is the ROM font */
	8,
	0,
	0,
    };

/* a NewScreen - I don't know what half of this means either */
static
struct NewScreen PlopNewScreen =
	{
	0, 0, XMAX, YMAX, PLANES,
	0, 1,
	0,
	CUSTOMSCREEN,
	&PlopFont,
	(UBYTE *) "",
	NULL,
	NULL,
	};

static
struct NewWindow MyWindow =
	{
	0,0,14,10,
	0,1,
	CLOSEWINDOW|RAWKEY| MOUSEBUTTONS | MOUSEMOVE |SELECTUP,
	SMART_REFRESH|ACTIVATE |RMBTRAP| WINDOWCLOSE | REPORTMOUSE | BORDERLESS,
	NULL,
	NULL,
	(UBYTE *) "",
	NULL,
	NULL,
	14,10,14,10,
	CUSTOMSCREEN,
	};
	
struct RastPort PlopRastPort;

/*put_ea_cmap given an ea-type color map:
		an array of unsigned chars of form ea_cmap[] = {r, g, b, r, g, b...}
  turn it into an amiga-type color map:
  		an array of unsigned short of form amiga_cmap = {0xrgb, 0xrgb, ...}
  and then tell Dale this is the colors we want for our viewport */
void
put_ea_cmap(cmap, colors)
unsigned char *cmap;
int colors;
{
unsigned short amy_cmap[MAXCOL];
int i;
unsigned char r, g, b;

if (colors > MAXCOL)	/*color clipping*/
	colors = MAXCOL;
for (i=0; i<colors; i++)
	{
	amy_cmap[i] = 
		((*cmap++ & 0xf0) << 4) + (*cmap++ & 0xf0) + ((*cmap++ & 0xf0) >> 4);
	}
LoadRGB4( &PlopScreen->ViewPort, amy_cmap, (long)colors);
}

/* back out backwards */
void
plop_cleanup()
{
if (PlopScreen != NULL)
	CloseScreen(PlopScreen);
if (IntuitionBase != NULL)
	CloseLibrary(IntuitionBase);
if (LayersBase != NULL)
	CloseLibrary(LayersBase);
if (GfxBase != NULL)
	CloseLibrary(GfxBase);
}


main(argc, argv)
int argc;
char *argv[];
{
int i, class,code;
struct ILBM_info *info=NULL;

if ((argc==1) || (argv[1][0]=='?'))
	{printf("Usage : [33mShowPrint[31m filename\n\n");
	 return(0);}

/*Before we can do ANYTHING have to get our libraries... */
if ((GfxBase =  OpenLibrary("graphics.library", (long)0)) == NULL)
	{
#ifdef PARANOID
	printf("Can't open Graphics Library\n");
#endif PARANOID
	return(-1);
	}

if ((LayersBase =  OpenLibrary("layers.library", (long)0)) == NULL)
	{
#ifdef PARANOID
	printf("Can't open Layers Library\n");
#endif PARANOID
	plop_cleanup();
	return(-2);
	}

/* Two for Dale and one for RJ */
if ((IntuitionBase =  OpenLibrary("intuition.library",(long) 0)) == NULL)
	{
#ifdef PARANOID
	printf("Can't open Intuition Library\n");
#endif PARANOID
	plop_cleanup();
	return(-3);
	}

for (i=1; i<argc; i++)
	{
#ifdef DEBUG
	puts(argv[i]);
#endif DEBUG
	if ( (info = read_iff(argv[i], 0) ) != NULL)
		{
		if (PlopScreen != NULL)
			CloseScreen(PlopScreen);
				PlopScreen = NULL;
		PlopNewScreen.LeftEdge = info->header.x;
		PlopNewScreen.TopEdge = info->header.y;
		PlopNewScreen.Width = info->header.w;
		PlopNewScreen.Height = info->header.h;
		PlopNewScreen.Depth = info->header.nPlanes;
		PlopNewScreen.ViewModes = 0;   /*start at default */
		if (PlopNewScreen.Depth == 6)
			PlopNewScreen.ViewModes |= HAM;
		if (PlopNewScreen.Width > 320)
			PlopNewScreen.ViewModes |= HIRES;
		if (PlopNewScreen.Height > 200)
			PlopNewScreen.ViewModes |= LACE;
		if ( (PlopScreen = OpenScreen(&PlopNewScreen) ) == NULL)
			{
#ifdef PARANOID
			printf("OpenScreen failed\n");
#endif PARANOID		
			printf("Not Enough Memory Available\n");
			free_planes(&info->bitmap);
			plop_cleanup();
			return(-4);
			}
		MyWindow.Screen=PlopScreen;
		if ((window = OpenWindow(&MyWindow)) == NULL)
			{
#ifdef PARANOID
			printf("OpenWindow failed\n");
#endif PARANOID
			printf("Not Enough Memory Available\n");
			free_planes(&info->bitmap);
			plop_cleanup();
			return(-5);
			}
		InitRastPort(&PlopRastPort);
		PlopRastPort.Mask = (1<<PLANES)-1;  /*just play it safe in case someone
											tries to load 6 bit planes...*/

		put_ea_cmap(&info->cmap, (1 << info->bitmap.Depth));
		PlopRastPort.BitMap = &info->bitmap;
		ClipBlit( &PlopRastPort, (long)0, (long)0,
			&PlopScreen->RastPort, (long)info->header.x, (long)info->header.y,
			(long)info->header.w, (long)info->header.h, (long)COPY_MINTERM);
		
		pdump(window);	/* printer dump */
		for (;;) {
			WaitPort(window->UserPort);
			message = GetMsg(window->UserPort);
			class=message->Class;
			code=message->Code;
			ReplyMsg(message);
			switch(class){
				case CLOSEWINDOW:	CloseWindow(window);
							free_planes(&info->bitmap);
							plop_cleanup();
							return(0);
							break;

				case MOUSEBUTTONS:	printf("Code = %d\n",code);
							CloseWindow(window);
							free_planes(&info->bitmap);
							plop_cleanup();
							return(0);
							break;		
				}
			}
		}
#ifdef PARANOID
	else
		printf("Couldn't load %s as an IFF file\n",argv[i]);
#endif PARANOID
	}
plop_cleanup();
}


