program tekenprogramma;
 uses crt,graph,mouse;

 type gegtek = record
                 kl : byte;
                 dik : integer;
                 lx,ly : integer;
                 {bx,by : integer;}
                 ok    : boolean;

                end;


 var oldx,oldy,x,y,mx,my,mknop : integer;
     dikte,kleur : byte;
     gd,gm : integer;
        t  : longint;
        ch : char;
     o_mx,o_my,o_y,o_x : integer;
     f_open,f,f_save : file of gegtek;
     r : gegtek;


 procedure initscreen;
  begin
   setcolor(green);
   setfillstyle(0,green);
   repeat
    PieSlice(300, 200, 0, t, t);
    delay(0);
    inc(t,4);
   until t>360;
   readkey;
   kleur:=0;
   t:=100;
   setfillstyle(1,white);
   bar(0,0,640,477);

   repeat
    setfillstyle(1,blue);
    bar(t,450,t+20,476);
    setfillstyle(1,kleur);
    bar(t,453,t+20,473);
    inc(kleur);
    inc(t,20);
   until kleur>15;
   t:=0;
  end;


  procedure laadsave;
  begin
   assign(f_open,'save1.tek');
   reset(f_open);
   seek(f_open,0);
   hidemousecursor;
   read(f_open,r);
   moveto(r.lx,r.ly);
   setfillstyle(1,white);
   bar(0,0,640,447);

   repeat
    read(f_open,r);
    if r.ok then lineto(r.lx,r.ly);
    moveto(r.lx,r.ly);
    setcolor(r.kl)
   until (eof(f_open));
   close(f_open);
  end;


  procedure savegeg;
   begin
    assign(f_save,'save1.tek');
    rewrite(f_save);
    seek(f,0);
    repeat
     read(f,r);
     write(f_save,r);
    until eof(f);
    close(f_save);
    sound(100);delay(10);nosound;
   end;

 procedure leesgeg;
  begin
   seek(f,0);
   hidemousecursor;
{   cleardevice;}
   read(f,r);
   moveto(r.lx,r.ly);
   repeat
    read(f,r);
    if r.ok then lineto(r.lx,r.ly);
    moveto(r.lx,r.ly);
    setcolor(r.kl)

   until (eof(f));
   rewrite(f);
   Showmousecursor;
  end;


 procedure startprog;
  begin
   assign(f,'geg.tek');
   rewrite(f);
   x:=100;
   y:=100;
   o_y:=100;
   o_x:=100;
   kleur:=1;
   setcolor(kleur);
   showmousecursor;
   r.ok:=False;
   dikte:=2;
   repeat
    oldx:=mx;
    oldy:=my;
    getmouse(mx,my,mknop);
    ch:=#0;
    if keypressed  then
     begin
     ch:=readkey;

     if ch='r' then leesgeg;
     if ch='s' then Savegeg;
     if ch='o' then LaadSave;

     if (o_x<>x) or (o_y<>y) then
                             begin
                              o_x:=x;
                              o_y:=y;
                              lineto(x,y);

                             end;
     end;

    r.ok:=False;
    if mknop=2 then begin inc(kleur); end;
    if mknop=1 then
                begin
                 lineto(mx,my);
                 r.ok:=true;
                end;

    if kleur=10 then kleur:=1;
    if mknop<>1 then moveto(mx,my);

    setcolor(kleur);
    moveto(x,y);
    moveto(mx,my);

    x:=mx;
    y:=my;
    r.lx:=x;
    r.ly:=y;
    r.kl:=kleur;
    r.dik:=dikte;
    write(f,r);

   until (mknop=3) or (ch=#27);
   close(f);
  end;

 begin
  gd:=detect;
  initgraph(gd,gm,'');
  initscreen;
  startprog;
  closegraph;
 end.