#include "screendefs.h"

.text
.align 4
.globl _rotatewindow
_rotatewindow:
	pushl %ebp		!Standard entry
	movl %esp,%ebp	
	
	pushl %esi
	pushl %edi	

	pushl %eax
	pushl %ebx
	pushl %ecx
	pushl %edx

	movl 8(%ebp),%esi	!Source
	movl 12(%ebp),%edi	!Destination
	movl 16(%ebp),%eax	!Image width
	movw $SCREENHEIGHT,ycount
	subl $SCREENWIDTH,%eax	/* eax is line ajustment */
	movl %eax,xadj		 

	movl 20(%ebp),%ebx		!deltax
	movl 24(%ebp),%edx		!deltay				

	
/* eax,ebx,edx are all free for calcs in inner loop */

copyline:
	movw $320,xcount
	movl $0,fxtotal
	movl $0,fytotal
	movl $SCREENHEIGHT,%eax
	subw ycount,%ax
	movl %ebx,%ecx	
	imul %eax,%ecx
	movl %ecx,fytotal

	movl %edx,%ecx	
	imul %eax,%ecx
	movl %ecx,fxtotal

linestart:


	addl %ebx,fxtotal	/* add delta values to totals */
	addl %edx,fytotal

	movl 8(%ebp),%eax	/* place source address in eax */
	movl fxtotal,%ecx	/* place fixed point x total in ecx */
	shrl $16,%ecx		/* remove decimal part */
	addl %ecx,%eax		/* add integer part to source address */

	movl fytotal,%ecx	/* place fixed point y total in ecx */
	imul 16(%ebp),%ecx	/* multiply by image width */
	shrl $16,%ecx		/* remove decimal part */
	addl %ecx,%eax		/* add integer part to source address */

	movl %eax,%esi		/* place calulated source value in esi */

	movsb

	decw xcount 		
	jne linestart

	decw ycount
	jne copyline

	popl %edx
	popl %ecx
	popl %ebx
	popl %eax

	popl %edi		!Restore regs used
	popl %esi

	movl %ebp,%esp		!Standard exit
	popl %ebp
	ret

.comm ycount,2
.comm xcount,2
.comm xadj,4
.comm fxtotal,4
.comm fytotal,4
