
/* this source implements aMiPEG's interface to CyberGraphX */


#include <string.h>
#include <stdlib.h>

#include <exec/exec.h>
#include <cybergraphics/cybergraphics.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <dos/dos.h>

#include <proto/exec.h>
#include <proto/cybergraphics.h>
#include <proto/intuition.h>
#include <proto/dos.h>

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

extern int ditherType;

struct Library *CyberGfxBase;
ULONG cyber_mode;
ULONG depth;
struct Screen *cyber_screen;
struct Window *cyber_window;
int original_x, original_y;

extern UBYTE Cb_r_tab[2048];

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

static void output_term(void)
{
	if(cyber_window)
		CloseWindow(cyber_window);

	if(cyber_screen)
		CloseScreen(cyber_screen);

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

void DrawCyberGfxImage(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)
	{
		if(ditherType == CYBERGFXGRAY_DITHER)
			WritePixelArray(data, 0, 0, x, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, x, y, RECTFMT_GREY8);
		else
			WritePixelArray(data, 0, 0, x * 4, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, x, y, RECTFMT_ARGB);
	}
	else
	{
		if(ditherType == CYBERGFXGRAY_DITHER)
			ScalePixelArray(data, x, y, x, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, win_x, win_y, RECTFMT_GREY8);
		else
			ScalePixelArray(data, x, y, x * 4, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, win_x, win_y, RECTFMT_ARGB);
	}
}

int handle_window(void)
{
	struct IntuiMessage *imsg;
	struct IntuiMessage msg;
	static ULONG last_secs, last_micros;

	if(cyber_window)
	{
		while(imsg = (struct IntuiMessage *)GetMsg(cyber_window->UserPort))
		{
			CopyMem((char *)imsg, (char *)&msg, (long)sizeof(struct IntuiMessage));
			ReplyMsg((struct Message *)imsg);
			switch(msg.Class)
			{
				case IDCMP_CLOSEWINDOW:
				return(FALSE);

				case IDCMP_MOUSEBUTTONS:
					if(msg.Code == SELECTDOWN)
					{
						if(DoubleClick(last_secs, last_micros, msg.Seconds, msg.Micros))
							ChangeWindowBox(cyber_window, cyber_window->LeftEdge, cyber_window->TopEdge, original_x, original_y);

						last_secs = msg.Seconds;
						last_micros = msg.Micros;
					}
				break;

				default:
				break;
			}
		}
	}
	return(TRUE);
}


BOOL init_cybergfx(void)
{
	if(IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",37))
		if(CyberGfxBase = OpenLibrary("cybergraphics.library", 40))
			return(TRUE);
		else
			Quit("cybergraphics.library v40+ not present", 25);
	else
		Quit("intuition.library is too old, <V39", 25);
}

int get_cyber_mode(void)
{
	return((int)CModeRequestTags(NULL,
													CYBRMREQ_WinTitle, "Select a screenmode for MPEG Window",
													CYBRMREQ_MinDepth, 15,
													TAG_DONE));
}


unsigned long InitCyberGfxDisplay(unsigned long modeid)
{
	atexit(output_term);

	if(init_cybergfx())
	{
		if((modeid != 0xffffffff) || (modeid = get_cyber_mode()) != 0xffffffff)
		{
			cyber_mode = modeid;
			depth = GetCyberIDAttr(CYBRIDATTR_DEPTH, cyber_mode);
			if(depth < 15)
				Quit("aMiPEG: this screen is not deep enough (needed at least 15 bits).", 25);

			if(cyber_screen = OpenScreenTags(NULL,
							SA_Width, STDSCREENWIDTH,
							SA_Height, STDSCREENHEIGHT,
							SA_DisplayID, cyber_mode,
							SA_Title, (UBYTE *)"CyberGraphX aMiPEG 0.5 by Michael Rausch and Miloslaw Smyk",
							SA_Depth, depth,
							TAG_DONE))
			{

				if(cyber_window = OpenWindowTags(NULL,
									WA_CustomScreen, cyber_screen,
									WA_Flags, WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_SIZEGADGET | 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))
					return(modeid);
			}
		}
	}
	Quit("aMiPEG: unable to open screen/window...", 25);
}
