// Black Magic TechnoFun effect by Kaos (a.k.a. Bruno Carlos)

// If you use this code in any of your
// productions give us the proper credits...

#include <dos.h>
#include <math.h>
#include <stdio.h>
#include <process.h>
#include <alloc.h>
#include <mem.h>
#include "xmode.h"

char *Ptr(char far *ptr, unsigned int l)
{
 unsigned int seg, off;

 seg=FP_SEG(ptr);
 off=FP_OFF(ptr);

 seg+=(off/16);
 off&=0x000f;
 off+=(unsigned int)(l & 0x000fL);
 seg+=(l/16L);
 ptr=(char *)MK_FP(seg, off);

 return(ptr);
}

void technofun(void)
{
 FILE *temp;
 unsigned char *buffer1;
 int i,a,b;
 unsigned Locs1[500];
 unsigned Locs2[300];
 unsigned char pal[768];

 #define Pi 3.1415927

 for (i=0; i<500; i+=2)
     {
      Locs1[i]=(int)(40*sin((2*Pi/250)*i))+40;
      Locs1[i+1]=(int)(10*sin((2*Pi/500)*i))+10;
     }

 for (i=0; i<300; i+=2)
     {
      Locs2[i]=(int)(40*sin((2*Pi/300)*i))+40;
      Locs2[i+1]=(int)(10*sin((2*Pi/150)*i))+10;
     }

 if ((temp=fopen("circles.xcr","rb"))==NULL) exit(101);
 if ((buffer1=(unsigned char *)farmalloc(89920))==NULL) exit(101);
 fread(buffer1,64000,1,temp);
 fread((unsigned char *)MK_FP(FP_SEG(buffer1)+4000,4),1,25920,temp);
 fclose(temp);

 xinitvideo(0);
 xclrvram();

 pal[0]=0;
 pal[1]=0;
 pal[2]=0;
 pal[3]=0x20;
 pal[4]=0x20;
 pal[5]=0x20;
 pal[6]=0x30;
 pal[7]=0x30;
 pal[8]=0x30;

 xsetpalette(pal);

 for (i=0,a=0,b=0; i<800; i+=1,a+=4,b+=4)
     {
      if (a==500) a=0;
      if (b==300) b=0;
      xsetvpage(1);
      xvwait();
//      xsetborder(1);
      movewin(Ptr(buffer1+Locs1[a]*320/4+Locs1[a+1],0),Ptr(buffer1+Locs2[b]*320/4+Locs2[b+1],0),0);
//      xsetborder(0);
      xsetvpage(0);
      xvwait();
//      xsetborder(1);
      movewin(Ptr(buffer1+Locs1[a+2]*320/4+Locs1[a+3],0),Ptr(buffer1+Locs2[b+2]*320/4+Locs2[b+3],0),1);
//      xsetborder(0);
     }

 farfree(buffer1);
}

void main()
{
 technofun();
 xtextmode(25);
}
