#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include "turtle.h"

#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include "turtle.proto"

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *TurtleBase;

struct Screen *Screen;
struct Window *Window;

struct TurtleHandle *TurtleHandle;

BOOL NTSC = FALSE;

long _stack = 4000;
char *_procname = "Turtle Demo";
long _priority = 0;
long _BackGroundIO = 0;


int CXBRK()

{
	return(0);
}


void spiralen()

{
	void ClrScrn();
	void WaitKey();
	
	int szahl;
	WORD winkel;
	float dist;
	
	ClrScrn();
	
	for (winkel = 300; winkel > 0; winkel -= 120)
	{
		ResetTurtle(TurtleHandle); SetAngle(TurtleHandle, winkel); dist = 4;
		
		for (szahl =  NTSC ? 104 : 128; szahl; szahl--)
		{
			Forward(TurtleHandle,dist);
			dist += 2;
			TurnLeft(TurtleHandle,60);
		};
	};
	
	WaitKey();
}


void pyramiden()

{
	void ClrScrn();
	void WaitKey();
	
	int pkanten, pzahl;
	WORD winkel = 45;
	float dist = 10;
	
	ClrScrn();
	
	for (pzahl = NTSC ? 34 : 45; pzahl; pzahl--)
	{
		for (pkanten = 8; pkanten; pkanten--)
		{
			Forward(TurtleHandle,dist);
			TurnLeft(TurtleHandle,winkel);
		};
		
		if (winkel > 0)
			TurnRight(TurtleHandle,45);
		else
		{
			TurnLeft(TurtleHandle,45);
			dist += 4;
		};
		winkel = -winkel;
	};
	
	WaitKey();
}


void schleifen()

{
	void ClrScrn();
	void WaitKey();
	
	int szahl;
	WORD winkel = 0;
	
	ClrScrn();
	
	ShowTurtle(TurtleHandle);
	for (szahl = 721; szahl; szahl--)
	{
		Forward(TurtleHandle,(float) 15);
		
		winkel = (winkel+29) % 360;
		TurnLeft(TurtleHandle,winkel);
	};
	HideTurtle(TurtleHandle);
	
	WaitKey();
}


void flocke()

{
	void ClrScrn();
	void WaitKey();
	
	void strckzeich(int);
	
	int i;
	
	ClrScrn();
	
	SetPosition(TurtleHandle,(WORD) (NTSC ? 158 : 117),(WORD) (NTSC ? 296 : 376));
	
	for (i = 3; i; i--)
	{
		strckzeich(0);
		TurnLeft(TurtleHandle,120);
	};
}


void strckzeich (int tiefe)

{
	if (tiefe <= 3)
	{
		strckzeich(tiefe+1);
		TurnRight(TurtleHandle,60);
		strckzeich(tiefe+1);
		TurnLeft(TurtleHandle,120);
		strckzeich(tiefe+1);
		TurnRight(TurtleHandle,60);
		strckzeich(tiefe+1);
	}
	else
		Forward(TurtleHandle,(float) (NTSC ? 4 : 5));
}


void OpenAll()

{
	void CloseAll();
	
	struct NewScreen ns;
	struct NewWindow nw;
	
	if (!(IntuitionBase = OpenLibrary("intuition.library",0)))
		CloseAll();
	if (!(GfxBase = OpenLibrary("graphics.library",0)))
		CloseAll();;
	if (!(TurtleBase = OpenLibrary("turtle.library",0)))
		CloseAll();
	
	ns.LeftEdge = 0;
	ns.TopEdge = 0;
	ns.Width = 640;
	ns.Height = STDSCREENHEIGHT;
	ns.Depth = 1;
	ns.DetailPen = 0;
	ns.BlockPen = 1;
	ns.ViewModes = HIRES|LACE;
	ns.Type = CUSTOMSCREEN;
	ns.Font = NULL;
	ns.DefaultTitle = "Turtle Demo - Press Return for the next Picture";
	ns.Gadgets = NULL;
	if (!(Screen = OpenScreen(&ns)))
		CloseAll();
	if (Screen->Height == 400) NTSC = TRUE;
	
	nw.LeftEdge = 0;
	nw.TopEdge = 0;
	nw.Width = 640;
	nw.Height = Screen->Height;
	nw.DetailPen = 0;
	nw.BlockPen = 1;
	nw.IDCMPFlags = VANILLAKEY;
	nw.Flags = NOCAREREFRESH|ACTIVATE|BORDERLESS|BACKDROP|SIMPLE_REFRESH;
	nw.FirstGadget = NULL;
	nw.CheckMark = NULL;
	nw.Title = NULL;
	nw.Screen = Screen;
	nw.Type = CUSTOMSCREEN;
	if (!(Window = OpenWindow(&nw)))
		CloseAll();
	
	if (!(TurtleHandle = CreateTurtle(Window->RPort,320,(WORD) (Window->Height/2+5),0,HIDETURTLE|TCX640Y400)))
		CloseAll();
}	
	

void CloseAll()

{
	if (TurtleHandle) ReturnTurtle(TurtleHandle);
	
	if (Window) CloseWindow(Window);
	if (Screen) CloseScreen(Screen);
	
	if (TurtleBase) CloseLibrary(TurtleBase);
	if (IntuitionBase) CloseLibrary(IntuitionBase);
	if (GfxBase) CloseLibrary(GfxBase);
	
	exit();
}


void WaitKey()

{
	struct IntuiMessage *message;
	char taste;
	
	do
	{
		WaitPort(Window->UserPort);
		message = (struct IntuiMessage *) GetMsg(Window->UserPort);
		taste = message->Code;
		ReplyMsg((struct Message *) message);
	} while (taste != 13);
}


void ClrScrn()

{
	HideTurtle(TurtleHandle);
	
	Move(Window->RPort,0,0);
	ClearScreen(Window->RPort);
	
	ResetTurtle(TurtleHandle);
}


void _main()

{
	OpenAll();
	
	spiralen();
	pyramiden();
	schleifen();
	flocke();
	
	WaitKey();
	CloseAll();
}

