*=======================================================*
*	Rendering module: latest update 12/11/95	*
*=======================================================*
*	Render walls & floors onto screenbuffer		*
*=======================================================*
*     >	New 68030 module by Douglas Little.		*
*=======================================================*
*	Some improvements made to original process:	*
*=======================================================*
*     >	Simplified program-flow, lower overheads	*
*     >	Transparency handled in the form of glass.	*
*     >	Wall runs now stored in a 'frame' chunk.	*
*      	Will be changed to work with 'sector' chunks.	*
*     >	Would benefit from instruction-towers, but	*
*      	should be avoided until tex-mapping is ready.	*
*=======================================================*

towered_walls		; use instruction-tower for walls
towered_flats		; use instruction-tower for floors

*-------------------------------------------------------*
*	Pigment coefficients for glass windows		*
*-------------------------------------------------------*

glass_red		=	3	; glass diffuse colour.
glass_green		=	5
glass_blue		=	7

glass_redfilter		=	8	; glass filter colour.
glass_greenfilter	=	16
glass_bluefilter	=	32

*-------------------------------------------------------*
	ifnd		towered_flats
*-------------------------------------------------------*
	
*-------------------------------------------------------*
*	Render the floors & ceilings			*
*-------------------------------------------------------*
render_flats:
*-------------------------------------------------------*
	lea		flatruns,a0
	lea		flatruncounts,a1
	moveq		#0,d5
	move.w		ScrWidth,d5
	add.l		d5,d5
	move.l		d5,a5
	move.w		Width,d5
	move.l		screen,a2
	move.w		Height,d7
	subq.w		#1,d7
.flats:	move.l		a0,a3
	move.w		(a1)+,d6
	subq.w		#1,d6
	bmi.s		.flat
.runs:	move.w		flat_x2(a3),d2
	move.w		flat_x1(a3),d1
	sub.w		d1,d2
	ble.s		.run
	move.w		flat_id(a3),d3
	lea		(a2,d1.w*2),a4
	subq.w		#1,d2
.dots:	move.w		d3,(a4)+
	dbra		d2,.dots
.run:	addq.l		#flat_len,a3
	dbra		d6,.runs
.flat:	lea		(flat_len*max_xint)(a0),a0
	add.l		a5,a2
	dbra		d7,.flats
	rts

*-------------------------------------------------------*
	elseif
*-------------------------------------------------------*

*-------------------------------------------------------*
*	Render the floors & ceilings			*
*-------------------------------------------------------*
render_flats:
*-------------------------------------------------------*
	lea		flatruns,a0
	lea		flatruncounts,a1
	moveq		#0,d5
	move.w		scrwidth,d5
	add.l		d5,d5
	move.l		d5,a5
	move.w		width,d5
	move.l		screen,a2
	move.w		height,d7
	subq.w		#1,d7
.flats:	move.l		a0,a3
	move.w		(a1)+,d6
	subq.w		#1,d6
	bmi.s		.flat
.runs:	move.w		flat_id(a3),d3
	move.w		flat_x2(a3),d2
	move.w		d3,d1
	swap		d3
	move.w		d1,d3
	move.w		flat_x1(a3),d1
	sub.w		d1,d2
	ble.s		.run
	lea		(a2,d1.w*2),a4
	moveq		#4-1,d1
	and.w		d2,d1
	lsr.w		#2,d2
	neg.w		d1
	jmp		.tower(pc,d1.w*2)
	move.w		d3,(a4)+
	move.w		d3,(a4)+
	move.w		d3,(a4)+
	move.w		d3,(a4)+
.tower:	dbra		d2,.dots
	addq.l		#flat_len,a3
	dbra		d6,.runs
	bra.s		.flat
.dots:	move.l		d3,(a4)+
	move.l		d3,(a4)+
	dbra		d2,.dots
.run:	addq.l		#flat_len,a3
	dbra		d6,.runs
.flat:	lea		(flat_len*max_xint)(a0),a0
	add.l		a5,a2
	dbra		d7,.flats
	rts

*-------------------------------------------------------*
	endc
*-------------------------------------------------------*

*-------------------------------------------------------*
	ifnd		towered_walls
*-------------------------------------------------------*

*-------------------------------------------------------*
*	Render the walls				*
*-------------------------------------------------------*
render_walls:
*-------------------------------------------------------*
	lea		wallruns,a1
	moveq		#0,d6
	move.w		wallruncount,d6
	moveq		#0,d3
	subq.w		#1,d6
	bmi		.err
	lea		glass,a6
	move.l		d6,d0
	lsl.l		#3,d0
	add.l		d0,a1
	move.l		screen,a4
	moveq		#0,d4
	moveq		#0,d5
	move.w		ScrWidth,d5
	add.l		d5,d5
.runs:	move.w		wall_y2(a1),d2
	moveq		#0,d1
	move.w		wall_y1(a1),d1
	sub.w		d1,d2
	ble.s		.zero
	move.w		wall_x(a1),d0
	bmi.s		.transparent_column
	subq		#1,d2
	move.w		wall_id(a1),d3
	lsl.l		#4,d1
	lea		(a4,d0.w*2),a0
	move.l		d1,d0
	lsl.l		#2,d1
	add.l		d1,d0
	lsl.l		#3,d0
	add.l		d0,a0
.dots:	move.w		d3,(a0)
	add.l		d5,a0
	dbra		d2,.dots
.zero:	subq.l		#wall_len,a1
	dbra		d6,.runs
	bra.s		.err
*-------------------------------------------------------*
*	Transparent walls are treated separately	*
*-------------------------------------------------------*
.transparent_column:
*-------------------------------------------------------*
	not.w		d0
	subq		#1,d2
	move.w		wall_id(a1),d3
	lsl.l		#4,d1
	lea		(a4,d0.w*2),a0
	move.l		d1,d0
	lsl.l		#2,d1
	add.l		d1,d0
	lsl.l		#3,d0
	add.l		d0,a0
	move.w		(a0),d3
.dots2:	move.w		(a6,d3.l*2),(a0)
	add.l		d5,a0
	move.w		(a0),d3
	dbra		d2,.dots2
	subq.l		#wall_len,a1
	dbra		d6,.runs
*-------------------------------------------------------*
.err:	rts

*-------------------------------------------------------*
	elseif
*-------------------------------------------------------*
	
*-------------------------------------------------------*
*	Render the walls				*
*-------------------------------------------------------*
render_walls:
*-------------------------------------------------------*
	lea		wallruns,a1
	moveq		#0,d6
	move.w		wallruncount,d6
	moveq		#0,d3
	subq.w		#1,d6
	bmi		.err
	lea		glass,a6
	move.l		d6,d0
	lsl.l		#3,d0
	add.l		d0,a1
	move.l		screen,a4
	moveq		#0,d4
	moveq		#0,d5
	move.w		scrwidth,d5
	add.l		d5,d5
.runs:	move.w		wall_y2(a1),d2
	moveq		#0,d1
	move.w		wall_y1(a1),d1
	sub.w		d1,d2
	ble.s		.zero
	move.w		wall_x(a1),d0
	bmi.s		.transparent_column
;	subq		#1,d2
	move.w		wall_id(a1),d3
	lsl.l		#4,d1
	lea		(a4,d0.w*2),a0
	move.l		d1,d0
	lsl.l		#2,d1
	add.l		d1,d0
	lsl.l		#3,d0
	add.l		d0,a0
	moveq		#4-1,d1
	and.w		d2,d1
	lsr.w		#2,d2
	neg.w		d1
	jmp		.tower(pc,d1.w*4)
.dots:	move.w		d3,(a0)
	add.l		d5,a0
	rept		4-1
	move.w		d3,(a0)
	add.l		d5,a0
	endr
.tower:	dbra		d2,.dots
.zero:	subq.l		#wall_len,a1
	dbra		d6,.runs
	bra.s		.err
*-------------------------------------------------------*
*	Transparent walls are treated separately	*
*-------------------------------------------------------*
.transparent_column:
*-------------------------------------------------------*
	not.w		d0
	subq		#1,d2
	move.w		wall_id(a1),d3
	lsl.l		#4,d1
	lea		(a4,d0.w*2),a0
	move.l		d1,d0
	lsl.l		#2,d1
	add.l		d1,d0
	lsl.l		#3,d0
	add.l		d0,a0
	move.w		(a0),d3
.dots2:	move.w		(a6,d3.l*2),(a0)
	add.l		d5,a0
	move.w		(a0),d3
	dbra		d2,.dots2
	subq.l		#wall_len,a1
	dbra		d6,.runs
*-------------------------------------------------------*
.err:	rts

*-------------------------------------------------------*
	endc
*-------------------------------------------------------*

*-------------------------------------------------------*
*	Create transparent translation table		*
*-------------------------------------------------------*
init_glass:
*-------------------------------------------------------*
	lea		glass,a0
	move.l		#65536-1,d0
.make:	move.l		d0,d1
	move.l		d0,d2
	move.l		d0,d3
	lsr.w		#6,d1
	lsr.w		#5,d1
	lsr.w		#5,d2
	and.w		#%11111,d1
	and.w		#%111111,d2
	and.w		#%11111,d3
	mulu		#glass_redfilter,d1
	mulu		#glass_greenfilter,d2
	mulu		#glass_bluefilter,d3
	lsr.w		#5,d1
	lsr.w		#5,d2
	lsr.w		#5,d3
	add.w		#glass_red,d1	
	add.w		#glass_green,d2
	add.w		#glass_blue,d3
	lsr.w		#1,d1	
	lsr.w		#1,d2	
	lsr.w		#1,d3	
	lsl.w		#5,d2
	or.w		d2,d3
	lsl.w		#5,d1
	lsl.w		#6,d1
	or.w		d1,d3
	move.w		d3,(a0,d0.l*2)
	dbra		d0,.make
	rts

*-------------------------------------------------------*
			bss
*-------------------------------------------------------*
			
glass:			ds.w	65536

*-------------------------------------------------------*
			text
*-------------------------------------------------------*
