/* picotalk test */

#include <math.h>
#include <mieeedoub.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <signal.h>

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/ports.h>
#include <exec/lists.h>
#include <exec/nodes.h>

#include <egs/ownall.h>
#include <egs/egsutil.h>

#include <egs/proto/egsutil.h>
#include <proto/exec.h>
#include <proto/dos.h>

#include "PicoTalk.h"

struct MsgPort *myport;
struct Library *EGSBlitBase;

BOOL SendMsg(struct Message *msg)
{
	struct MsgPort *port;
	BOOL success;

	msg->mn_Node.ln_Type = NT_MESSAGE;
	msg->mn_ReplyPort = myport;
	msg->mn_Length = sizeof(struct PicoMsg);
	Forbid();
	if (port = FindPort("PicoTalk")){
		PutMsg(port, msg);
		WaitPort(myport);
		msg=GetMsg(myport);
		success=TRUE;
	} else
		success=FALSE;

	Permit();
	return success;
}

main()
{
	struct E_EBitMap *bitmap;
	struct PicoMsg *msg;
	ULONG winid;
	ULONG *data;
	int x,y;

	// nasty but quick - I know... :-)
	EGSBlitBase = OpenLibrary("egsblit.library",0);

	myport=CreateMsgPort();

	msg=AllocVec(sizeof(struct PicoMsg), MEMF_PUBLIC|MEMF_CLEAR);
	msg->command = PTALK_OBTAIN_CURRENT;
	msg->res.alloc.winid = 0;
	msg->res.alloc.bitmap = NULL;
	SendMsg((struct Message *)msg);
	winid = msg->res.alloc.winid;
	bitmap = msg->res.alloc.bitmap;

	if (bitmap){
	for (y=0; y<bitmap->Width; y++){
		for (x=0; x<bitmap->Height; x++){
			union {
				ULONG val;
				struct { UBYTE r,g,b,a; } part;
			} color;
			color.part.r = x*x/128 - 2*x + 128 + y/2;
			color.part.g = (int)(sqrt((double)(x*x + y*y)) / 1.41);
			color.part.b = (x+y)/2;
			EB_WritePixel(bitmap, color.val, x,y, 0);
		}
		msg->command = PTALK_UPDATE_WINDOW;
		msg->arg.update.winid = winid;
		msg->arg.update.left = 0;
		msg->arg.update.top = y;
		msg->arg.update.width = bitmap->Width;
		msg->arg.update.height = 1;
		SendMsg((struct Message *)msg);
	}

	msg->command = PTALK_RELEASE_WINDOW;
	msg->arg.winid = winid;
	SendMsg((struct Message *)msg);
	}

	FreeVec(msg);
	DeleteMsgPort(myport);
	CloseLibrary(EGSBlitBase);
}
