	
	.text
	.align	4
	.arm
	.include	"obj_kollision.i"

;****************************
;*
;* check_obj_coll
;*
;* r9 = &collreturnstruct
;* r0 = &&obj_Struct
;* r1 = count
;*

	.global		check_obj_coll
	.type		check_obj_coll,function

check_obj_coll

	stmfd	sp!,{r0,r1,r9,lr}


;
; (x) durchläufe
; (2)
;

	;r1 = ges
	
	mov	r3,#0			;x=0
	
oloop_for				;von x bis ges
	
	add	r2,r3,#1		;y=x+1
	ldr	r4,[r0,r3,lsl #2]	;get adr at r0[s]
	
iloop_for				;von x bis y

	cmp	r2,r1
	bge	iloop_end	
	
	ldr	r5,[r0,r2, lsl #2]	;get adr at r0[y]
	
	stmfd	sp!,{r9}
	addr	r8,check_obj_th+1
	mov	lr,pc
	bx	r8
	ldmfd	sp!,{r9}
	cmp	r7,#1			;kollision ???
	
	moveq	r8,r3,lsl #16
	orreq	r8,r8,r2
	streq	r8,[r9],#4		;(r9)=obj1:obj2
	
	;x -> y ;r3 -> r2

	add	r2,r2,#1
	b	iloop_for	
iloop_end

	add	r3,r3,#1
	
	sub	r4,r1,#1
	cmp	r3,r4			;x!=(ges-1) ?
	bne	oloop_for
	
	mvn	r8,#0			;end=0xffffffff
	str	r8,[r9]

	ldmfd	sp!,{r0,r1,r9,lr}
	bx	lr

;****************************
;*
;* recalc obj
;*
;* berechnet rotation und zoom, mittelpunkt und gesamtradius des objektes
;*
;*
;* r6 = &obj_struct
;*
;*

	.global		recalc_obj
	.type		recalc_obj,function

recalc_obj

	stmfd	sp!,{r0-r12,lr}

	;**** object rotieren und zoomen

	ldr	r3,[r6,#OBJ_STRUCT_ROT]		;rotation
	ldr	r2,[r6,#OBJ_STRUCT_ZOOM]		;zoom
	ldr	r4,[r6,#OBJ_STRUCT_ECKEN]	;&ecken
	ldr	r0,[r6,#OBJ_STRUCT_PO]		;punkteorg
	ldr	r1,[r6,#OBJ_STRUCT_PT]		;punkte
	
	ldr	r9,[r6,#OBJ_STRUCT_X]		;addx
	ldr	r10,[r6,#OBJ_STRUCT_Y]		;addy
	
	ldr	r8,[r4]				;Ecken Anzahl
	
	addr	r11,rotPointArray2d_th+1	
	mov	lr,pc
	bx	r11				;save r2-r6 r8-r12
	
	;**** get polymitte
	
	mov	r7,r6				;base = r7
	
	ldr	r0,[r4]				;Ecken Anzahl
	ldr	r1,[r6,#OBJ_STRUCT_PT]		;punkte
	
	addr	r2,get_poly_middle_th+1		;save r0-r4,r7
	mov	lr,pc
	bx	r2
	
	str	r5,[r7,#OBJ_STRUCT_MX]		;store mittelx
	str	r6,[r7,#OBJ_STRUCT_MY]		;store mittely
	
	;**** get polyradius
		
	mov	r2,r5
	mov	r3,r6
	
	mov	r6,r7				;base = r6
	
	addr	r5,get_poly_radius_th+1		;save r1-r6
	mov	lr,pc
	bx	r5
		
	addr	r1,isqrt
	mov	r0,r7
	mov	lr,pc
	bx	r1				
	str	r0,[r6,#OBJ_STRUCT_RAD]	

	ldmfd	sp!,{r0-r12,lr}
	bx	lr


	
;****************************
;*
;* obj_line_test (gehe über alle linien von obj1 und teste ob diese in obj2 linien schneiden)
;*
;* ! kein bx
;*
;* r4 = obj1
;* r5 = obj2
;*
;* return:
;*
;* r7 = 0 = no koll / 1 = koll

	.global		obj_line_test
	.type		obj_line_test,function

obj_line_test

	stmfd	sp!,{lr}

	ldr	r0,[r4,#OBJ_STRUCT_ECKEN]	
	ldr	r11,[r4,#OBJ_STRUCT_PT]		;&pt obj1
	ldr	r12,[r0]				;anzahl ecken obj1


	ldr	r0,[r5,#OBJ_STRUCT_ECKEN]
	ldr	r8,[r5,#OBJ_STRUCT_PT]		;&pt obj2
	ldr	r9,[r0]				;anzahl ecken obj2
	
	addr	r10,fdivs_m			;divisionsfunktion
	
	ldr	r5,[r11],#-4			;punkt 0
	add	r11,r11,r12, lsl #2		;endpunkt=adr+ecken*4
	
obj1lines

	ldr	r6,[r11],#-4			;punkt 3 - 2 - 1 - 0 

	mov	r4,r8				;obj
	ldr	r0,[r4],#-4			;punkt 0
	add	r4,r4,r9, lsl #2			;endpunkt=adr+ecken*4
	mov	r3,r9				;counter

	;--
	
	stmfd	sp!,{r6,r8,r9,r11,r12}
	
obj2lines

	ldr	r1,[r4],#-4			;punkt 3 - 2 - 1 - 0 
	

	;** check 2 lines
	
	stmfd	sp!,{r3,r4,r5,r6,r11}
	
	;----------------------------------------
		
	mov	r11,#0				;keine schnittpunkt berrechnen	
	addr	r12,line_x_line_overhead+1
	mov	lr,pc
	bx	r12
	ldmfd	sp!,{r3,r4,r5,r6,r11}
	
	cmp	r7,#1
	addeq	sp,sp,#4*5			;remove r6,r8,r9,r11,r12 from stack
	beq	line_collision
	
	;**

	;mov	r0,r1
	ldr	r0,[r4,#4]		;spare push/pop von register (r1)
	subs	r3,r3,#1
	bne	obj2lines
	
	;--
	
	ldmfd	sp!,{r6,r8,r9,r11,r12}
	
	mov	r5,r6				;old=this
	subs	r12,r12,#1
	bne	obj1lines		
	
	mov	r7,#0

line_collision

	ldmfd	sp!,{r0}
	
	bx	r0	
	
	.pool 
	
	
	.section    	.iwram,"ax"
	
;****************************+
;*
;* point_in_poly
;*
;* r0 = anzahl punkte von Polygon
;* r1 = &punkte von Polygon (punkte immer 16Bit)
;* r2 = x:y
;*
;* return:
;*
;* r7 = 0 nicht in Polygon, 1 in Polygon
;*

	.global		point_in_poly
	.type		point_in_poly,function

point_in_poly

	stmfd	sp!,{r0-r6,r8-r12,lr}
	
    	;r0=npoints-1
    	;r1=&punkte
    	
    	;r2=xt:yt
    	;r3=xold:yold
    	;r4=xnew:ynew
    	;r5=x1:y1
    	;r6=x2:y2
    	
    	;r7=inside
    	;r8=i
    	
    	sub	r0,r0,#1	 ;npoints-1
	ldr	r3,[r1,r0,lsl #2] ;xold:yold	(punkte(4*(npoints-1)))
  
    	mov	r7,#0		;inside=0;
    	mov	r8,#0		;i=0;
   
inp_loop	   	

	cmp	r8,r0
	bgt	inp_loop_stop	;For i=0 To npoints-1
	
	ldr	r4,[r1,r8,lsl #2] ;xnew(i):ynew(i)
	
	mov	r9,r4, asr #16	;xnew:32

	cmp	r9,r3, asr #16	;xnew>xold
	movgt	r5,r3		;x1=xold	y1=yold
	movgt	r6,r4		;x2=xnew	y2=ynew
				;else
	movle	r5,r4		;x1=xnew	y1=ynew;	
	movle	r6,r3		;x2=xold	y2=yold;

	;( (xt<=xold) = ((xnew<xt) )	
	
	mov	r9,r2, lsr #16		;xt

	cmp	r9, r3, asr #16		;xt,  xold
	movgt	r11, #0
	movle	r11, #1
	cmp	r9, r4, asr #16		;xnew,  xt
	eorle	r11, r11, #1
	cmp	r11, #0
	beq	no_mu

	;( ((yt-y1)*(x2-x1)) < ((y2-y1)*(xt-x1))))

	mov	r10,r5, asr #16	;x1
	
	mov	r11,r2 ,lsl #16
	mov	r11,r11, asr #16	;yt
	mov	r12,r5, lsl #16
	
	rsb	r14,r10,r6, asr #16	;x2(not negative, so no signed extension)-x1
	sub	r11,r11,r12, asr #16	;yt-y1(signed extended)
	
	mul	r9,r14,r11	;(x2-x1)*(yt-y1)
	
	;((y2-?y1)*(xt-?x1))))
	
	mov	r14,r6, lsl #16		;y2
	mov	r14,r14, asr #16		;ext
	
	sub	r14,r14,r12, asr #16	;y2-y1
	rsb	r11,r10,r2, lsr #16	;xt-x1
	
	mul	r10,r11,r14
	
	cmp	r10,r9	
	eorlt	r7,r7,#1		;inside=inside Xor 1
	
no_mu

	mov	r3,r4	;xold=xnew	yold=ynew;

	add	r8,r8,#1
	b	inp_loop
		
inp_loop_stop

	ldmfd	sp!,{r0-r6,r8-r12,lr}
	bx	lr
	
	.end
	
	