/*
    Includefile TURTLEGRAPHICS.H
    
    Erleichtert die Grafikprogrammierung in C,
    Grafikanweisungen aus der LOGO - Sprachumgebung,
    besonders für Anfänger geeignet, da man sich um
    Amiga - spezifische Grafikbefehle (Screens, Windows etc.)
    nicht zu kümmern braucht.

    Das Hauptprogramm benötigt nur #include <turtlegraphics.h>,
    um alle Befehle nutzen zu können.

    Thomas Globisch
*/

#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <functions.h>
#include <graphics/gfxmacros.h>
#include <math.h>

#define TURTLE

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Screen *s;
struct Window *w;
struct RastPort *rp;
struct TmpRas TR;

struct NewScreen NewScreen = {
   0,0,0,0,
   3,
   0,1, 
   NULL,NULL,
   NULL,
   NULL,NULL,NULL 
};

struct NewWindow nw1 = {
   0,0,0,0,
   0,1,
   NULL,
   ACTIVATE|BORDERLESS,
   NULL,
   NULL, NULL, NULL, NULL,
   0,0,0,0,
   NULL
};

long x_p_O_s_i_t, y_p_O_s_i_t, p_e_n=0, P_o_i_n_t_e_r;
int w_i_n_k_e_l = 0;


Initturtle(br,ho,ti,flags)
int br,ho,ti;
long flags;
{
static short map[32] = 
{
  0x0000, 0x0FFF, 0x04f4, 0x0909, 0x00cc, 0x0f00, 0x00f0, 0x000f,
  0x0e4b, 0x01f8, 0x09ce, 0x0169, 0x0f0f, 0x0cef, 0x0fe5, 0x0134,
  0x0111, 0x0222, 0x0333, 0x0444, 0x0555, 0x0666, 0x0777, 0x0888,
  0x0999, 0x0aaa, 0x0bbb, 0x0ccc, 0x0ddd, 0x0eee, 0x0159, 0x0591
};

  IntuitionBase =(struct IntuitionBase *)OpenLibrary("intuition.library",0L);
  if (IntuitionBase == NULL) exit(FALSE);

  GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",0L);
  if (GfxBase == NULL) exit(FALSE);

  if(br < 0)br = 0;
  if(ho < 0)ho = 0;
  NewScreen.Width  = br;
  NewScreen.Height = ho;
  if( ti < 1 ) ti = 1;
  if( ti > 6 ) ti = 6;
  NewScreen.Depth  = ti;
  NewScreen.ViewModes = flags;
  if((s = (struct Screen *) OpenScreen(&NewScreen)) == NULL)exit(FALSE);

  nw1.Screen = s; 
  nw1.Width  = br; 
  nw1.Height = ho; 
  if ((w = (struct Window *) OpenWindow(&nw1)) == NULL)
  {
   CloseScreen(s);
   exit(FALSE);
  }
  rp = w->RPort;  

  LoadRGB4(&s->ViewPort,map,32L);
  x_p_O_s_i_t = br / 2;
  y_p_O_s_i_t = ho / 2;  
  w_i_n_k_e_l = 0;
  Move(rp,x_p_O_s_i_t,y_p_O_s_i_t);
  P_o_i_n_t_e_r = (long) AllocRaster((long)br,(long)ho);
  InitTmpRas(&TR,P_o_i_n_t_e_r,(long) RASSIZE((long)br,(long)ho));
  rp->TmpRas = &TR;
}

Closeturtle()
{
  if((w!=NULL)&&(s!=NULL))
  {
    CloseWindow(w);
    CloseScreen(s);
  }
}

Moveto(x,y)
int x,y;
{
 if(x < 0) x = 0;
 if(y < 0) y = 0;
 if(x > NewScreen.Width)  x = NewScreen.Width;
 if(y > NewScreen.Height) y = NewScreen.Height;
 x_p_O_s_i_t = x;
 y_p_O_s_i_t = y;
 if(p_e_n == 0) Move(rp,x_p_O_s_i_t,y_p_O_s_i_t);
 else Draw(rp,x_p_O_s_i_t,y_p_O_s_i_t);
}

Moves(step)
int step;
{
  x_p_O_s_i_t += (long) (step * cos(w_i_n_k_e_l*0.017453292));
  y_p_O_s_i_t -= (long) (step * sin(w_i_n_k_e_l*0.017453292));
  if(x_p_O_s_i_t < 0) x_p_O_s_i_t = 0;
  if(y_p_O_s_i_t < 0) y_p_O_s_i_t = 0;
  if(x_p_O_s_i_t > NewScreen.Width)  x_p_O_s_i_t = NewScreen.Width;
  if(y_p_O_s_i_t > NewScreen.Height) y_p_O_s_i_t = NewScreen.Height;

  if(p_e_n==0) Move(rp,x_p_O_s_i_t,y_p_O_s_i_t);
  else Draw(rp,x_p_O_s_i_t,y_p_O_s_i_t);
}

Pencolor(farbnr)
int farbnr;
{
 if((farbnr>=0)&&(farbnr<64))
   SetAPen(rp,(long)farbnr);
}

Setcolor(nr,r,g,b)
int nr,r,g,b;
{
 if((nr>=0)&&(nr<32))
   SetRGB4(&s->ViewPort,(long)nr,(long)r,(long)g,(long)b);
}

Penup()
{
 p_e_n = 0;
}

Pendown()
{
 p_e_n = 1;
}

Turn(win)
int win;
{
 w_i_n_k_e_l = w_i_n_k_e_l + win;
 while(w_i_n_k_e_l < 0) w_i_n_k_e_l+=360;
 while(w_i_n_k_e_l > 360) w_i_n_k_e_l-=360;
}

Turnto(win)
int win;
{
 w_i_n_k_e_l = win;
 while(w_i_n_k_e_l < 0) w_i_n_k_e_l+=360;
 while(w_i_n_k_e_l > 360) w_i_n_k_e_l-=360;
}

Turtlex()
{
 return(x_p_O_s_i_t);
}

Turtley()
{
 return(y_p_O_s_i_t);
}

Turtleang()
{
 return(w_i_n_k_e_l);
}

Screenbit()
{
 return(ReadPixel(rp,x_p_O_s_i_t,y_p_O_s_i_t));
}

Clear()
{
 Move(rp,0L,0L);
 ClearScreen(rp);
 Home();
}

Home()
{
 x_p_O_s_i_t = NewScreen.Width / 2;
 y_p_O_s_i_t = NewScreen.Width / 2;
 w_i_n_k_e_l = 0;
 Penup();
}

Plot(x,y)
int x,y;
{
 WritePixel(rp,(long)x,(long)y);
}

Plottext(x,y,string)
int x,y;
char *string;
{
 if(x < 0) x = 0;
 if(y < 0) y = 0;
 if(x > NewScreen.Width ) x = NewScreen.Width;
 if(y > NewScreen.Height) y = NewScreen.Height;

 SetDrMd(rp,0L);
 Move(rp,(long)x,(long)y);
 Text(rp,string,(long) strlen(string));
 Move(rp,x_p_O_s_i_t,y_p_O_s_i_t);
 SetDrMd(rp,1L);
}

Fill(x,y)
int x,y;
{
 if(x < 0) x = 0;
 if(y < 0) y = 0;
 if(x > NewScreen.Width ) x = NewScreen.Width;
 if(y > NewScreen.Height) y = NewScreen.Height;

 Flood(rp,1L,(long)x,(long)y);
}

Fillpattern(muster,hoehe)
int hoehe;
UWORD muster[];
{
  SetAfPt(rp,muster,(long)hoehe);
}
