*=======================================================*
*	Display-engine module: latest update 27/11/95	*
*=======================================================*
*	Call all display modules in correct order	*
*=======================================================*
*     >	New 68030 module by Douglas Little.		*
*=======================================================*

lighting_effects	; enable for light & shadows

*-------------------------------------------------------*
display_engine:
*-------------------------------------------------------*
*	Calculate window base-address (centering)	*
*-------------------------------------------------------*
	move.l		ScreenLog,d0
	move.w		scrwidth,d1
	sub.w		width,d1
	lsr.w		d1
	ext.l		d1
	add.l		d1,d1
	add.l		d1,d0
	move.l		d0,screen
*-------------------------------------------------------*
*	Reset wall & floor buffers for this frame	*
*-------------------------------------------------------*
	bsr		init_wall_lists
	bsr		init_floor_lists
*-------------------------------------------------------*
	ifd		dsp
*-------------------------------------------------------*
	dspwrite.l	#clear_command
*-------------------------------------------------------*
	endc
*-------------------------------------------------------*
*	Init display variables for this frame		*
*-------------------------------------------------------*
	bsr		update_display_struct
*-------------------------------------------------------*
*	Init lighting effects for this frame		*	
*-------------------------------------------------------*	
	bsr		set_lighting
*-------------------------------------------------------*
*	Clearscreen for resizing window & debugging	*
*-------------------------------------------------------*
;	move.b		#1,refresh
	move.l		ScreenLog,a0
	bsr		clearscreen
*-------------------------------------------------------*
*	Descend BSP tree & build scenery		*
*-------------------------------------------------------*
	bsr		descend_bsp
*-------------------------------------------------------*
*	Render floors & ceilings onto framebuffer	*
*-------------------------------------------------------*
	bsr		render_flats
*-------------------------------------------------------*
*	Render walls onto framebuffer			*
*-------------------------------------------------------*
	bsr		render_walls
*-------------------------------------------------------*
	rts

*-------------------------------------------------------*
*	Init wall edgebuffer lists 			*
*-------------------------------------------------------*
init_wall_lists:
*-------------------------------------------------------*
	lea		maxy,a0
	lea		miny,a1
	lea		occlusion_list,a2
	move.w		height,d1
	moveq		#0,d2
	moveq		#-1,d3
	move.w		width,d0
	subq.w		#1,d0
.for_i:	move.w		d1,(a0)+
	move.w		d2,(a1)+
	move.b		d3,(a2)+
	dbra		d0,.for_i
	clr.w		wallruncount
	move.w		width,columns
	rts

*-------------------------------------------------------*
*	Init floor edgebuffer lists			*
*-------------------------------------------------------*
init_floor_lists:
*-------------------------------------------------------*
	lea		flatruncounts,a0
	moveq		#0,d1
	move.w		height,d0
	subq.w		#1,d0
.clr:	move.w		d1,(a0)+
	dbra		d0,.clr
	rts

*-------------------------------------------------------*
*	Set up general (engine) structures & tables	*
*-------------------------------------------------------*
init_engine:
*-------------------------------------------------------*
	lea		display_struct,a6
*-------------------------------------------------------*
*	Initialise pointers to WAD room data		*
*-------------------------------------------------------*
	move.l		PNode_Array,display_nodes(a6)
	move.l		Seg_Array,display_segs(a6)
	move.l		Sector_Array,display_sectors(a6)
	move.l		SSector_Array,display_ssectors(a6)
	move.l		Side_Array,display_sidedefs(a6)
	move.l		Line_Array,display_linedefs(a6)
	move.l		Vertex_Array,display_vertices(a6)
*-------------------------------------------------------*
*	Create transparent (glass) translation table	*
*-------------------------------------------------------*
	bsr		init_glass
	rts

*-------------------------------------------------------*
*	Update general (engine) display structure	*
*-------------------------------------------------------*
update_display_struct:
*-------------------------------------------------------*
	lea		display_struct,a6
*-------------------------------------------------------*
*	Set up player position and angle		*
*-------------------------------------------------------*
	move.l		SinPangle,d3
	move.l		CosPangle,d4
	asr.l		#2,d3
	asr.l		#2,d4
	move.w		d3,display_sina(a6)
	move.w		d4,display_cosa(a6)
	move.w		Px,display_px(a6)
	move.w		Py,display_py(a6)
*-------------------------------------------------------*
*	Calculate base address of octant routine	*
*-------------------------------------------------------*
	move.w		Pangle,d7
	moveq		#(16-3),d0
	lsr.w		d0,d7
	lea		octs,a0
	move.w		(a0,d7.w*2),display_octant(a6)
*-------------------------------------------------------*
	move.w		#1,first_ssector
*-------------------------------------------------------*
	rts
	
*-------------------------------------------------------*
*	Select new display window size			*
*-------------------------------------------------------*
select_window:
*-------------------------------------------------------*
	pushall
	moveq		#10,d1
	move.w		window_size,d2
	cmp.w		#256,d2
	bpl.s		.ok1
	move.w		#256,d2
.ok1	cmp.w		#1<<10,d2
	bmi.s		.ok2
	move.w		#1<<10,d2
.ok2:	move.w		d2,window_size
*-------------------------------------------------------*
	move.w		d2,d0
	mulu.w		#max_width,d0
	lsr.l		d1,d0
	move.w		d0,width
*-------------------------------------------------------*
	move.w		d2,d0
	mulu.w		#max_height,d0
	lsr.l		d1,d0
	move.w		d0,height
*-------------------------------------------------------*
*	Update global display structure			*
*-------------------------------------------------------*
	lea		display_struct,a6
	move.w		width,d1
	move.w		d1,display_width(a6)
	lsr.w		d1
	move.w		d1,display_shwid(a6)
*-------------------------------------------------------*
*	Update addwall structure			*
*-------------------------------------------------------*
	lea		addwall_struct,a6
	move.w		width,addwall_xsize(a6)
	move.w		height,addwall_ysize(a6)
*-------------------------------------------------------*
*	3 CLS calls required to wipe log, phys & bak	*
*-------------------------------------------------------*
	move.b		#3,refresh
*-------------------------------------------------------*
*	Recalculate video display height		*
*-------------------------------------------------------*
	move.b		#1,adjust
*-------------------------------------------------------*
*	Resize DSP rendering zone			*
*-------------------------------------------------------*
	ifd		dsp
*-------------------------------------------------------*
	dspwrite.l	#resize_command
	moveq		#0,d0
	move.w		width,d0
	dspwrite.l	d0
	move.w		height,d0
	dspwrite.l	d0
*-------------------------------------------------------*
	endc
*-------------------------------------------------------*
	popall
	rts
		
*-------------------------------------------------------*
clearscreen:
*-------------------------------------------------------*
	tst.b		refresh
	bne.s		.go
	rts
.go:	subq.b		#1,refresh
	move.w		#(320*200)/2,d0
*-------------------------------------------------------*
clearlongs:
*-------------------------------------------------------*
	moveq		#0,d1
	move.l		d1,d2
	move.l		d2,d3
	move.l		d2,d4
	move.l		d2,d5
	move.l		d2,d6
	move.l		d2,d7
	move.l		d2,a2
	move.l		d2,a3
	moveq		#128-1,d1
	and.l		d0,d1
	bra.s		.strt
.long	move.l		d2,(a0)+
.strt	dbra		d1,.long
	lsr.l		#7,d0
	move.l		d0,d1
	lsl.l		#8,d1				; *256
	lsl.l		#1,d1				; *2	=  *512
	add.l		d1,a0
	bra.s		.next
.clr	movem.l		d2-d7/a2-a3,-(a0)		; 4*8*16 = 512
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
	movem.l		d2-d7/a2-a3,-(a0)
.next	dbra		d0,.clr
	rts

*-------------------------------------------------------*
*	Doom engine subroutines				*
*-------------------------------------------------------*

*-------------------------------------------------------*
	ifd		dsp
*-------------------------------------------------------*
	include		dspwalls.s
*-------------------------------------------------------*
	elseif
*-------------------------------------------------------*
	include		addwall.s
*-------------------------------------------------------*
	endc
*-------------------------------------------------------*

	include		bspd.s
	include		render.s
	include		lighting.s
	include		timing.s
	include		tables.s
	
*-------------------------------------------------------*
		bss
*-------------------------------------------------------*

window_size:		ds.w	1

width:			ds.w	1
height:			ds.w	1

display_struct:		ds.b	display_len

*-------------------------------------------------------*
		text
*-------------------------------------------------------*
