;/*
sc mandelbrot.c DATA=FAR NMINC STRMERGE NOSTKCHK IGNORE=73 STRUCTUREEQUIVALENCE NOSTDIO
slink from LIB:c.o mandelbrot.o to //Clients/Mandelbrot LIB LIB:scm.lib LIB:sc.lib LIB:amiga.lib /lib/client.lib SC SD STRIPDEBUG NOICONS
delete mandelbrot.o
quit

 Mandelbrot 1.2  (Client for BServer)

 Copyright © 1994 Luca Viola & Stefano Reksten of 3AM - The Three Amigos!!!
 All rights reserved.
*/

#include <exec/types.h>
#include <intuition/intuition.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#include "/include/client.h"

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *BitMapBase;

struct DisplayIDInformation *dinfo;
UBYTE command;
BOOL stillBlanking;

#define XOFF		0
#define YOFF 		0

#define	RISX		240
#define	RISY		240
#define	PLANES		4
#define	MAXITER		32
#define	COLORS		(1 << PLANES)

/* function prototypes */

void Mandel( void );
void DrawMandel( void );

/*  Intuition global  structures */

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Screen *thescreen;
struct RastPort *rport;
struct ViewPort vport;
struct BitMap *bitmap;
ULONG *table;

UWORD colors[]=
{
	0x0000, 0x011F, 0x022E, 0x033D, 0x044C, 0x055B, 0x066A, 0x0779,
	0x0888, 0x0997, 0x0AA6, 0x0BB5, 0x0CC4, 0x0DD3, 0x0EE2, 0x0FF1
};


void Mandel()
{
struct Rectangle *rect = GETTXTOSCANRECT(dinfo);

if ( thescreen = (struct Screen *)OpenScreenTags( NULL,
		SA_Left, (RECTANGLEWIDTH(rect) - RISX)>>1,
		SA_DisplayID, DISPLAYID(dinfo),
		SA_Width, RISX,
		SA_Height, RISY,
		SA_Depth, PLANES,
		SA_Quiet, TRUE,
		SA_Overscan, OSCAN_TEXT,
		TAG_DONE ) )
	{
	rport=&(thescreen->RastPort);
	vport=thescreen->ViewPort;
	LoadRGB4( &vport,colors,COLORS );
	DrawMandel();
	CloseScreen( thescreen );
	}
else
	SendClientMsg( ACTION_FAILED );
}


void DrawMandel()
{
	double quadsize;
	double temp,xr,yr,d,vx,vy,x1real,y1imm,x2real,y2imm,gapx,gapy;
	UWORD loop,x,y,risx,risy;

	SpritesOff();

	srand48( (unsigned int)time( NULL ) );	
	quadsize = 3.4 * drand48();
	x1real=-2.2 * drand48();
	x2real=x1real + quadsize;
	y1imm=-1.7 * drand48();
	y2imm=y1imm + quadsize;

	risx=RISX;
	risy=RISY;
	gapx=(x2real-x1real)/risx;
	gapy=(y1imm-y2imm)/risy;
	vy=y2imm;

	stillBlanking = TRUE;

	for ( y=0; y<=RISY; y++ )
	{
		vy=vy+gapy;
		vx=x1real;
		for( x=0; x<RISX && stillBlanking; x++ )
		{
			vx=vx+gapx;
			xr=0;yr=0;
			loop=0;

			do
			{
				loop++;
				temp=((xr*xr)-(yr*yr))+(vx);
				yr=((2.0F*xr)*yr)+vy;
				xr=temp;
				d=(xr*xr)+(yr*yr);
				if (d > 4.0F)
				{
					SetAPen( &(thescreen->RastPort), loop%COLORS+1 );
					WritePixel( &(thescreen->RastPort), x, y );
				  	loop=MAXITER;
				}
			stillBlanking = STILL_BLANKING;
			}
			while(loop!=MAXITER && stillBlanking );
		}
	}

	if ( stillBlanking )
		(void)WaitServerCommand();

	SpritesOn();
}  		 


void __main( char *line )
{
if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
	{
	if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37L ) )
		{
		if ( dinfo = OpenCommunication() )
			{
			Mandel();
			CloseCommunication( dinfo );
			}
		CloseLibrary( (struct Library *)GfxBase );
		}
	CloseLibrary( (struct Library *)IntuitionBase );
	}
}
