/* picotalk test */

#include "PreInclude.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;

	EGSBlitBase = OpenLibrary("egsblit.library",0);

	myport=CreateMsgPort();

	msg=AllocVec(sizeof(struct PicoMsg), MEMF_PUBLIC|MEMF_CLEAR);
	msg->command = PTALK_ALLOC_WINDOW;
	msg->arg.alloc.width = 320;
	msg->arg.alloc.height = 256;
	msg->arg.alloc.name = "Hello World";
	SendMsg((struct Message *)msg);
	winid = msg->res.alloc.winid;
	bitmap = msg->res.alloc.bitmap;

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

	for (y=0; y<256; y++){
		for (x=0; x<256; 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);
}
