
#include <exec/exec.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <dos/dos.h>
#include <libraries/asl.h>

#include <proto/asl.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <proto/dos.h>

#include "video.h"
#include "proto.h"

struct RastPort temprp;
struct BitMap tempbm;
int plane, i;

static void Quit(char *why, int failcode)
{
	puts(why);
	exit(failcode);
}

void DrawGrayImage(void *data, int x, int y)
{
/*	int win_x = cyber_window->Width - cyber_window->BorderLeft - cyber_window->BorderRight;
	int win_y = cyber_window->Height - cyber_window->BorderTop - cyber_window->BorderBottom;

	if(win_x == x && win_y == y)
*/
	{
		WritePixelArray8(cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, cyber_window->BorderLeft + x - 1, cyber_window->BorderTop + y - 1, data, &temprp);
	}
}

static void output_term(void)
{
	close_timer();

	for(i = 0; i < plane; i++)
		FreeRaster(tempbm.Planes[i], cyber_screen->Width, 1);

	if(cyber_window)
		CloseWindow(cyber_window);
	
	if(cyber_screen)
		CloseScreen(cyber_screen);

	if (IntuitionBase) CloseLibrary((struct Library *) IntuitionBase);
	if (GfxBase) CloseLibrary((struct Library *) GfxBase);
}

int get_modeid(void)
{
	struct ScreenModeRequester *scrMdReq;
	int modeid = 0xffffffff;

	if(scrMdReq = AllocAslRequestTags(ASL_ScreenModeRequest,
																		ASLSM_MinWidth, 200,
																		ASLSM_MinHeight, 200,
																		ASLSM_MinDepth, 8,
																		ASLSM_MaxDepth, 8,
																		TAG_DONE))
	{
		if(AslRequestTags(scrMdReq, TAG_DONE))
			modeid = scrMdReq->sm_DisplayID;

		FreeAslRequest(scrMdReq);
	}

	return(modeid);
}

void init_gray(void)
{
	atexit(output_term);

	if ((GfxBase=(struct GfxBase *) OpenLibrary("graphics.library",37))==NULL)
		Quit("graphics.library is too old, <V37",25);
	if ((IntuitionBase=(struct IntuitionBase *) OpenLibrary("intuition.library",37))==NULL)
		Quit("intuition.library is too old, <V37",25);

}

unsigned long InitGrayDisplay(unsigned long modeid)
{
	int rgb = 0;
	UWORD pens[] =
	{
		0xf0, 0x40, 0x70, 0xff, 0x0, 0x80, 0xff, 0xc0, 0xa0, 0x0, 0xff, 0x80,
		~0
	};

	init_gray();

		if((modeid != 0xffffffff) || (modeid = get_modeid()) != 0xffffffff)
		{
			if(cyber_screen = OpenScreenTags(NULL,
							SA_Width, STDSCREENWIDTH,
							SA_Height, STDSCREENHEIGHT,
							SA_DisplayID, modeid,
							SA_Title, (UBYTE *)"aMiPEG 0.7 by Michael Rausch and Miloslaw Smyk",
							SA_Depth, 8,
							SA_Pens, &pens,
							SA_SysFont, 1,
//							SA_BackFill, LAYERS_BACKFILL,
							TAG_DONE))
			{

				/* set palette to 0 - 255 */
				for(i = 0; i < 256; i++)
				{
					SetRGB32(&cyber_screen->ViewPort,i,rgb,rgb,rgb);
					rgb += 0x01010101;
				}

				if(cyber_window = OpenWindowTags(NULL,
									WA_CustomScreen, cyber_screen,
									WA_Flags, WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_SIZEBBOTTOM | WFLG_ACTIVATE,
                	WA_IDCMP, CLOSEWINDOW | MOUSEBUTTONS,
	                WA_InnerWidth, 160,
  	              WA_InnerHeight,120,
  	              WA_MaxWidth, ~0,
  	              WA_MaxHeight, ~0,
  	              WA_MinWidth, 60,
  	              WA_MinHeight, 60,
    	            WA_Title, "Playing MPEG animation",
      	          TAG_DONE))
				{
					InitRastPort(&temprp);
					InitBitMap(&tempbm, cyber_window->RPort->BitMap->Depth, cyber_screen->Width, 1);
					temprp.BitMap = &tempbm;

					for (plane = 0; plane < tempbm.Depth; plane++)
						if(!(tempbm.Planes[plane] = AllocRaster(cyber_screen->Width, 1)))
							break;

					if(plane != tempbm.Depth)
						modeid = 0xffffffff;

					return(modeid);
				}
			}
		}

	Quit("aMiPEG: unable to open screen/window...", 25);
}
