// Simple sprite drawing program
// with all the relevant bits.
// Oliver Hutcheon 18-6-94 00:43am

#include <dos.h>
#include <conio.h>

#define SIZE 50000

void setgmode();
void clrgmode(int);
void cls(void);
void sprite(int,int,int,int);
void erase(int,int,int,int);
void sprite2(int,int,int,int);
void erase2(int,int,int,int);



/* spider.cpp */
const unsigned char image[1024]={
  0,0,0,0,0,0,0,0,0,0,0,0,35,35,35,35,35,35,35,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,35,35,44,44,44,44,44,44,44,35,35,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,35,35,44,44,44,44,44,44,44,44,44,44,44,35,35,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,35,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,35,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,35,44,182,182,182,44,44,44,44,44,44,44,182,182,182,44,44,44,35,0,0,0,0,0,0,0,
  0,0,0,0,0,35,44,182,182,182,182,182,44,44,44,44,44,182,182,182,182,182,44,44,44,35,0,0,0,0,0,0,
  0,0,0,0,0,35,182,182,182,182,182,182,182,44,44,44,182,182,182,182,182,182,182,44,44,35,0,0,0,0,0,0,
  0,0,0,0,35,44,182,182,15,15,182,182,182,44,44,44,182,182,15,15,182,182,182,44,44,44,35,0,0,0,0,0,
  0,0,0,0,35,44,182,182,15,15,182,182,182,44,44,44,182,182,15,15,182,182,182,44,44,44,35,0,0,0,0,0,
  0,0,0,35,44,44,44,182,182,182,182,182,44,44,44,44,44,182,182,182,182,182,44,44,44,44,44,35,0,0,0,0,
  0,0,0,35,44,44,44,44,182,182,182,44,44,44,44,44,44,44,182,182,182,44,44,44,44,77,44,35,0,0,0,0,
  0,0,0,35,44,44,44,44,44,44,44,44,44,44,39,44,44,44,44,44,44,44,44,44,44,77,77,77,0,0,0,0,
  0,0,0,35,44,44,44,44,44,44,44,44,44,39,39,39,44,44,44,44,44,44,44,44,44,44,44,35,77,77,77,0,
  0,0,0,77,77,44,44,44,44,44,44,44,39,39,39,39,39,44,44,44,44,44,44,44,44,44,44,35,0,77,0,0,
  0,0,77,77,44,44,44,44,44,44,44,44,44,39,39,39,44,44,44,44,44,44,44,44,44,44,44,35,77,0,77,0,
  0,0,77,77,44,44,44,44,44,44,44,44,44,44,39,44,44,44,44,44,44,44,44,44,44,44,44,77,0,0,77,0,
  0,77,0,77,35,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,77,0,0,0,77,0,
  0,77,0,77,35,44,44,44,44,217,217,44,44,44,44,44,44,44,44,44,217,217,44,44,44,77,77,77,77,0,77,0,
  77,77,77,77,77,77,77,44,44,44,217,217,217,217,217,217,217,217,217,217,217,44,44,44,44,77,0,0,0,77,77,0,
  0,0,77,0,0,35,44,44,44,44,44,44,217,217,217,217,217,217,217,44,44,44,44,44,44,35,0,0,0,77,77,0,
  0,0,77,0,0,0,35,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,35,0,0,0,0,77,0,77,
  0,77,77,77,77,77,77,35,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,35,0,0,0,0,0,77,0,77,
  0,77,0,0,0,0,0,0,35,35,44,44,44,44,44,44,44,44,44,44,44,35,35,0,0,0,0,0,0,77,0,77,
  0,77,0,0,0,0,0,0,0,0,35,35,44,44,44,44,44,44,44,35,35,0,0,0,0,0,0,0,0,77,0,77,
  0,77,0,0,0,0,0,0,0,0,0,0,35,35,35,35,35,35,35,0,0,0,0,0,0,0,0,0,0,77,0,77,
  0,77,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,77,77,
  77,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,77,0,
  77,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,77,0,
  77,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,77,0,
  77,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,77,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,77,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

/* boxes.cpp */
const unsigned char image2[1024]={
  171,171,171,171,171,171,171,171,171,171,171,171,171,171,171,171,171,171,171,0,0,0,0,0,0,0,0,0,0,0,0,0,
  171,0,0,0,0,0,0,0,0,0,0,0,0,0,44,44,44,44,171,44,44,44,44,44,44,44,44,44,44,44,44,44,
  171,0,0,0,0,0,0,0,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,0,0,0,0,44,
  171,0,0,0,0,0,0,0,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,0,0,0,0,44,
  171,0,0,0,0,0,0,0,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,0,0,0,0,44,
  171,0,0,0,0,0,0,0,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,0,0,0,0,44,
  171,0,0,0,0,0,0,0,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,0,0,0,0,44,
  171,0,0,0,0,0,0,0,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,0,0,0,0,44,
  171,0,0,0,0,0,0,0,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,0,0,0,0,44,
  177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,0,0,0,44,
  177,0,0,0,0,0,0,0,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,44,44,44,44,44,44,44,44,44,44,171,44,44,44,44,44,44,44,44,177,44,44,44,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,44,0,0,0,171,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,44,44,44,44,171,44,44,44,44,44,44,44,44,177,44,44,44,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,171,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,171,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,171,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,171,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,171,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,171,171,171,171,171,171,171,171,171,171,171,171,171,171,171,171,171,171,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,177,0,0,0,44,
  177,0,0,0,0,0,0,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,177,44,44,44,44,
  177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,177,0,0,0,0
};

int x,y,width,depth,width2,depth2;
float x2,y2;
char far *screen=(char far*)MK_FP(0xa000,0);
char letter;
int direction_x,direction_y,dx,dy;
unsigned char buffer[1024],buffer2[1024];
unsigned char vid_buffer[SIZE];

main(void)
{

	x=y=10;
	x2=200,y2=45;
	direction_x=direction_y=1;
	dx=-1;
	dy=1;

// initialize the graphics system

	int oldmode=*(int *)MK_FP(0x40,0x49);
	setgmode();
	cls();
	for (int frame=0;frame<1000;frame++)
		{
		sprite(x,y,32,32);
		sprite2(x2,y2,32,32);
		for (int i=y-1;i<y+33;i++) {
			for (int j=(x-1);j<(x+33);j++)
			screen[i*320+j]=vid_buffer[i*320+j] ;
			}
		for (int p=y2-1;p<y2+33;p++) {
			for (int q=(x2-1);q<(x2+33);q++)
			screen[p*320+q]=vid_buffer[p*320+q] ;
			}
//			for (int delay=0;delay<5000;delay++);
		erase2(x2,y2,32,32);
		erase(x,y,32,32);
		x=x+direction_x;
		y=y+direction_y;
		x2=x2+dx;
		y2=y2+dy;
		if (x==320-32) direction_x=-1;
		if (x==1) direction_x=1;
		if (y==(SIZE/320)-32) direction_y=-1;
		if (y==1) direction_y=1;
		if (x2==320-32) dx=-1.05;
		if (x2==1) dx=1.2;
		if (y2==(SIZE/320)-32) dy=-1.1;
		if (y2==1) dy=1.15;
		}
	clrgmode(oldmode);
	return 0;
}

void sprite(int x,int y,int width,int depth)

{
int offset,temp,index;

offset = y*320+x;
index=0;

for (int row=0; row<depth; row++) {
	for (int col=0; col<width; col++) {
		temp=image[index];
		buffer[index]=vid_buffer[offset];
		if (temp) vid_buffer[offset]=temp;
		index++;
		offset++;
		}
	offset+=(320-width);
	}
}


void erase(int x,int y,int width,int depth)

{
int offset,index;

offset = y*320+x;
index=0;

for (int row=0; row<depth; row++) {
	for (int col=0; col<width; col++) {
		vid_buffer[offset] = buffer[index];
		offset++;
		index++;
		}
		offset+=(320-width);
	}
}
void sprite2(int x,int y,int width,int depth)

{
int offset,temp,index;

offset = y2*320+x2;
index=0;

for (int row=0; row<depth; row++) {
	for (int col=0; col<width; col++) {
		temp=image2[index];
		buffer2[index]=vid_buffer[offset];
		if (temp) vid_buffer[offset]=temp;
		index++;
		offset++;
		}
	offset+=(320-width);
	}
}


void erase2(int x,int y,int width,int depth)

{
int offset,index;

offset = y2*320+x2;
index=0;

for (int row=0; row<depth; row++) {
	for (int col=0; col<width; col++) {
		vid_buffer[offset] = buffer2[index];
		offset++;
		index++;
		}
		offset+=(320-width);
	}
}

void cls()

{
for (long int i=0; i<SIZE; i++) {
	screen[i]=i;
	vid_buffer[i]=i; }

}


void setgmode()
{
asm {
		mov	ax,13h
		mov	ah,0
		int	10h

}
}

void clrgmode(int oldmode)
{
asm{
		mov	ax,oldmode
		mov	ah,0
		int	10h

}
}
