#pragma inline
#include <stdio.h>
#include <io.h>
#include <mem.h>
#include <malloc.h>
#include <alloc.h>
#include <dos.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <bios.h>
#include <conio.h>
#include <time.h>

#include "e:\travail\ma_lib\co_si.h"
#include "e:\travail\ma_lib\graphics.h"
#include "e:\travail\ma_lib\classes.h"

IMG IMAGE;

unsigned int x,y;
unsigned int cx,cy;
unsigned int x1,y1;
unsigned int cx1,cy1;

// char far tampon[320*100];
unsigned char far *tampon;
unsigned char far *techno;                   // Image des cercles.
unsigned char far *circle;                   // Image des cercles.
unsigned char far *dancer;                   // Image de la danseuse


void update()
{
 asm {
 push ds
 mov ax,0xA000
 mov es,ax
 xor di,di
// mov ax,seg tampon
// mov ds,ax
// mov si,offset tampon
 lds si, tampon
 mov cx,2000
 }
 LP:
 asm{
	mov eax,ds:[si]
	mov es:[di],eax
	mov eax,ds:[si+4]
	mov es:[di+4],eax
	mov eax,ds:[si+8]
	mov es:[di+8],eax
	mov eax,ds:[si+12]
	mov es:[di+12],eax
	add si,16
	add di,16
	dec cx
	jnz LP
	pop ds
 }
}

void put_image2(unsigned int x,unsigned int y,char couleur,unsigned int x2,unsigned int y2,char couleur2)
{
 asm {
 .386
	push ds
//      mov ax,seg tampon
//      mov es,ax
//      mov di,offset tampon
	les di,tampon
	lds si,circle
	push si
	mov ax,y
	add ax,50
	shl ax,8
	add si,ax
	shr ax,2
	add si,ax
	add si,x
	add si,80
	mov bx,si       // SI est copi‚ dans bx
	shl ebx,16      // EBX = SI : BX
	pop si
	mov ax,y2
	add ax,50
	shl ax,8
	add si,ax
	shr ax,2
	add si,ax
	add si,x2
	add si,80
	mov bx,si       // EBX = SI1 : SI2
	mov dx,100
 }
 LOOP_1:
 asm   {
	mov cx,80
 }
 LOOP_2:
 asm {

	mov al,ds:[bx]
	mov ah,al
	mov si,ax
	inc bx
	rol ebx,16
	mov al,ds:[bx]
	shl al,1
	mov ah,al
	add ax,si
	mov es:[di],ax
	inc bx
	rol ebx,16
	mov al,ds:[bx]
	mov ah,al
	mov si,ax
	inc bx
	rol ebx,16
	mov al,ds:[bx]
	shl al,1
	mov ah,al
	add ax,si
	mov es:[di+2],ax
	inc bx
	rol ebx,16
	add di,4
	dec cx
	jne LOOP_2
	add bx,160
	rol ebx,16
	add bx,160
	rol ebx,16
	dec dx
	jne LOOP_1
	pop ds
 }
}

void disp_sp(int lx,int ly)
{
 lx=lx<<6;
 ly=ly<<6;
 lx=lx+(lx>>2);
 ly=ly+(ly>>2);
 asm {
	push ds
//      mov ax,seg tampon //0xA000
//      mov es,ax
//      mov di,offset tampon //3280
	les di, tampon
	add di,3280
	lds si, techno
	mov ax,ly
	shl ax,8
	add si,ax
	shr ax,2
	add si,ax
	add si,lx
	mov bx,80
 }
 LOOP_1:
 asm    mov cx,20
 LOOP_2:
 asm {
	mov eax,ds:[si]
	shl al,4
	add es:[di],al
	add es:[di+1],al
	shl ah,4
	add es:[di+2],ah
	add es:[di+3],ah
	ror eax,16
	shl al,4
	add es:[di+4],al
	add es:[di+5],al
	shl ah,4
	add es:[di+6],ah
	add es:[di+7],ah
	add di,8
	add si,4
	dec cx
	jne LOOP_2
	add si,240
	add di,160
	dec bx
	jne LOOP_1
	pop ds
 }


}

void disp_danceleft(int ly)
{
 ly=ly*50;
 asm {
	push ds
//      mov ax,seg tampon //0xA000
//      mov es,ax
//      mov di,offset tampon //3280
	les di, tampon
	add di,19200
	lds si, dancer
	mov ax,ly
	shl ax,8
	add si,ax
	shr ax,2
	add si,ax
	mov bx,50
 }
 LOOP_1:
 asm    mov cx,26
 LOOP_2:
 asm {
	mov eax,ds:[si]
	shl al,3
	add es:[di],al
	shl ah,3
	add es:[di+1],ah
	ror eax,16
	shl al,3
	add es:[di+2],al
	shl ah,3
	add es:[di+3],ah
	add di,4
	add si,4
	dec cx
	jne LOOP_2
	add si,216
	add di,216
	dec bx
	jne LOOP_1
	pop ds
 }
}

void disp_danceright(int ly)
{
 ly=ly*50;
 asm {
	push ds
//      mov ax,seg tampon //0xA000
//      mov es,ax
//      mov di,offset tampon //3280
	les di,tampon
	add di,19416
	lds si, dancer
	mov ax,ly
	shl ax,8
	add si,ax
	shr ax,2
	add si,ax
	add si,100
	mov bx,50
 }
 LOOP_1:
 asm    mov cx,26
 LOOP_2:
 asm {
	mov eax,ds:[si]
	shl al,3
	add es:[di+3],al
	shl ah,3
	add es:[di+2],ah
	ror eax,16
	shl al,3
	add es:[di+1],al
	shl ah,3
	add es:[di],ah
	add di,4
	sbb si,4
	dec cx
	jne LOOP_2
	add si,424
	add di,216
	dec bx
	jne LOOP_1
	pop ds
 }
}

void disp_subblim()
{
 asm {
	push ds
	mov ax,0xA000
	mov es,ax
	mov di,6796
	lds si, dancer
	add si,104
	mov bx,58
 }
 LOOP_1:
 asm    mov cx,21
 LOOP_2:
 asm {
	mov eax,ds:[si]
	add es:[di],al
	add es:[di+1],al
	add es:[di+2],ah
	add es:[di+3],ah
	ror eax,16
	add es:[di+4],al
	add es:[di+5],al
	add es:[di+6],ah
	add es:[di+7],ah
	add di,8
	add si,4
	dec cx
	jne LOOP_2
	add si,236
	add di,152
	dec bx
	jne LOOP_1
	pop ds
 }
}

void main()
{
 unsigned int port;
 unsigned char dev;
 unsigned char irq;
 unsigned char dma;
 int f;
 int randch;
 int randsub;
 int randlov;
 int chg=0;
 int chg2=0;
 int chg3=0;
 int chg4=0;
 int av_scroll;
 int lgo,clo;
 int pastxt;
 int lg,cl;
 int dan1,dan2;
 int av1,av2;
 randsub=rand()%5;
 randch=3+rand()%6;
 randlov=rand()%6;
 av_scroll=0;pastxt=0;cx=0;cy=0;cx1=0;cy1=0;lgo=0;clo=0;
 dan1=0;dan2=3;av1=1;av2=-1;f=0;
 techno=(unsigned char far *)farmalloc(64500U);
 circle=(unsigned char far *)farmalloc(64500U);
 dancer=(unsigned char far *)farmalloc(64500U);
 tampon=(unsigned char far *)farmalloc(32000U);
 if((circle==NULL)||(techno==NULL)||(dancer==NULL)||(tampon==NULL))
 { printf("\nNot enough memory...");exit(0); }
 IMAGE.Decompresse("techno.mcp",(unsigned char far *)techno);
 IMAGE.Decompresse("dancer.mcp",(unsigned char far *)dancer);
 IMAGE.Decompresse("circle.mcp",(unsigned char far *)circle);
 asm mov ax,0x13                        // initialise le nouveau mode
 asm int 0x10                           // d'affichage video !!!
 asm mov dx,0x3D4
 asm mov al,0x09
 asm out dx,al
 asm inc dl
 asm mov al,3
 asm out dx,al
 asm      in   al,21h
 asm      or   al,01111010b         // Mask off all unneccesary interrupts.
 asm      out  21h,Al
 asm      in   al,0a1h
 asm      or   al,01100010b
  asm      out  0a1h,al
 IMAGE.Genere_couleurs();IMAGE.Affiche_couleurs();
 while( (inportb(0x60)!=1))
 {
  cx+=9;if(cx>380) cx-=380;
  cy+=11;if(cy>380) cy-=380;
 cx1+=7;if(cx1>380) cx1-=380;
 cy1+=9;if(cy1>380) cy1-=380;
 /*
 if(!f)
 {
  put_image2((unsigned)((si[cx])/25.6),(unsigned)((co[cy])/51.2),1,
	     (unsigned)((co[cx1])/73.14),(unsigned)((si[cy1])/85.33),2);
 }
 else
 {
  put_image2((unsigned)((co[cx1])/73.14),(unsigned)((si[cy1])/85.33),2,
	     (unsigned)((si[cx])/25.6),(unsigned)((co[cy])/51.2),1);
 }
 */
 if(!f)
 {
  put_image2((unsigned)((si[cx])/25),(unsigned)((co[cy])/51),1,
	     (unsigned)((co[cx1])/73),(unsigned)((si[cy1])/85),2);
 }
 else
 {
  put_image2((unsigned)((co[cx1])/73),(unsigned)((si[cy1])/85),2,
	     (unsigned)((si[cx])/25),(unsigned)((co[cy])/51),1);
 }
 f++;if(f>1) f=0;
 if(!chg) { lg=rand()%4; cl=rand()%2; }
 chg++;
 if(chg>randsub) {  randsub=rand()%4;chg=0; }
 if(!chg2)
 {
  if(dan1==3) { av1=-1; } 
  if(dan1==0) { av1=1; }
  if(dan2==3) { av2=-1; }
  if(dan2==0) { av2=1; }
  dan1=dan1+av1;dan2=dan2+av2;
  disp_danceleft(dan1);disp_danceright(dan2);
 }

 chg2++;if(chg2>1) chg2=0;
 disp_sp(lg,cl);
 chg3++;if(chg3>randch) {randch=2+rand()%4;chg3=0;}
 asm mov dx,0x3DA
 SYNCH1:
 asm in al,dx
 asm test al,8
 asm jnz SYNCH1
 SYNCH2:
 asm in al,dx
 asm test al,8
 asm jz SYNCH2
 if(!chg3)
 {
  asm {
	xor ax,ax
	mov dx,0x3C8
	out dx,al
	inc dl
	mov cx,256
	mov al,63
  }
  LP:
  asm { out dx,al
	out dx,al
	out dx,al
	dec cx
	jnz LP
   }
  }
  if(chg3==1) { IMAGE.Affiche_couleurs(); }
  chg4++; if(chg4>randlov)
  {
   chg4=0;randlov=rand()%6;disp_subblim();
   asm mov dx,0x3DA
   lps:
   asm in al,dx
   asm test al,8
   asm jz lps
  }
  update();
 }
 farfree((unsigned char far *)techno);
 farfree((unsigned char far *)circle);
 farfree((unsigned char far *)dancer);
 asm mov ax,0x3
 asm int 0x10
 asm mov al,0
 asm out 0x21,al
 asm out 0xa1,al
 while(kbhit()) getch();
 printf("\nCoding P.E. BAROILLER (c) 1995 Venom Software");
 exit(0);
}
