;	include	exec/exec.i
	include	exec/exec_lib.i
;	include	graphics/gfx.i
	include	graphics/graphics_lib.i
	include	intuition/intuition.i
	include	intuition/intuition_lib.i
	include	intuition/screens.i		

	jsr	tunnel_prec
	jsr	EnvMap_prec
	jsr	textureENVm	

** set task priority to -1
	sub.l	a1,a1
	move.l	4.w,a6
	jsr	_LVOFindTask(a6)
	move.l	d0,a1
	moveq	#-1,d0
	jsr	_LVOSetTaskPri(a6)

** open libraryes
libopen	move.l	4.w,a6
	lea	libs,a5
	lea	intbase,a4
.next	move.l	(a5)+,a1
	tst.l	a1
	beq.s	.exitlib
	moveq	#0,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,(a4)+
	beq	libexit
	bra.s	.next
.exitlib

** init bitmaps
	lea	dbufinfo1,a0
	moveq	#8,d0
	move.l	#320,d1
	move.l	#200,d2
	move.l	graphbase,a6
	jsr	_LVOInitBitMap(a6)
	lea	dbufinfo2,a0
	moveq	#8,d0
	move.l	#320,d1
	move.l	#200,d2
	move.l	graphbase,a6
	jsr	_LVOInitBitMap(a6)

** open screen	
	lea	screen_,a0
	move.l	intbase,a6
	jsr	_LVOOpenScreen(a6)
	move.l	d0,scr_struct
	beq	libexit

** open window
	lea	window_,a0
	move.l	d0,nw_Screen(a0)
	jsr	_LVOOpenWindow(a6)
	move.l	d0,win.struct
	beq	scr.close

** change palette
	move.l	scr_struct,a0
	lea	sc_ViewPort(a0),a0
	lea	palette,a1
	move.l	graphbase,a6
	jsr	_LVOLoadRGB32(a6)
	
** alloc doublebuffer structure
	move.l	scr_struct,a0
	lea	sc_ViewPort(a0),a0
	jsr	_LVOAllocDBufInfo(a6)
	move.l	d0,dbufinfo
	beq	win.close
	
***************************************
***************************************
***************************************

loop	**	wait vbl	**
	move.l	graphbase,a6
	jsr	_LVOWaitTOF(a6)

	**********************
	** double buffering **
	**********************
	lea	cur.screen,a0
	move.l	(a0),d0
	move.l	8(a0),(a0)
	move.l	d0,8(a0)
	move.l	4(a0),d0
	move.l	12(a0),4(a0)
	move.l	d0,12(a0)
	*********************
	move.l	scr_struct,a0
	lea	sc_ViewPort(a0),a0
	move.l	cur.screen,a1
	move.l	dbufinfo,a2
	jsr	_LVOChangeVPBitMap(a6)

	*******************
;	move.w	#$0,$dff106
;	move.w	#$ff,$dff180
	jsr	c2p
;	move.w	#0,$dff180
	jsr	tunnel
	*******************

	move.l	win.struct,a0
	move.l	wd_MouseY(a0),d0
	bne.s	loop

**********
** exit **
**********
freedbuf	move.l	dbufinfo,a1
	move.l	graphbase,a6
	jsr	_LVOFreeDBufInfo(a6)
win.close	move.l	win.struct,a0
	move.l	intbase,a6
	jsr	_LVOCloseWindow(a6)	
scr.close	move.l	scr_struct,a0
	jsr	_LVOCloseScreen(a6)
libexit	move.l	4.w,a6
	lea	intbase,a5
.next	move.l	(a5)+,a1
	tst.l	a1
	beq.s	.exitlib
	jsr	_LVOCloseLibrary(a6)
	bra.s	.next
.exitlib	
	moveq	#0,d0
	rts

	include	openscreen.i

	section	datas,data
dbufinfo1	dc.l	0,0
	dc.l	b1p0,b1p1,b1p2,b1p3,b1p4,b1p5,b1p6,b1p7,0
dbufinfo2	dc.l	0,0
	dc.l	b2p0,b2p1,b2p2,b2p3,b2p4,b2p5,b2p6,b2p7,0
cur.screen	dc.l	dbufinfo1,b2p0
	dc.l	dbufinfo2,b1p0
dbufinfo	dc.l	0	
	******************************************************
libs	dc.l	int
	dc.l	grap
	dc.l	dos
	dc.l	0
int	dc.b	'intuition.library',0
	even
grap	dc.b	'graphics.library',0
	even
dos	dc.b	'dos.library',0
	even
intbase	dc.l	0
graphbase	dc.l	0
dosbase	dc.l	0
	dc.l	0
	***************************
scr_struct	dc.l	0
screen_
	dc.w	0,0
	dc.w	320,200,8
	dc.b	0,0
	dc.w	0
	dc.w	CUSTOMSCREEN|CUSTOMBITMAP
	dc.l	0
	dc.l	scr_title
	dc.l	0
	dc.l	dbufinfo1
	dc.l	0	; end
scr_title	dc.b	'Shin was here :)',0
	even
	***************************
win.struct	dc.l	0
window_
	dc.w	0,0
	dc.w	320,200
	dc.b	0,0
	dc.l	0	IDCMP
	dc.l	$30800	
	dc.l	0,0,0
	dc.l	0
	dc.l	0
	dc.w	0,0,0,0
	dc.w	15
	dc.l	0,0
	***************************	
	
sinus	incbin	tabs/sinus4096.w
	incbin	tabs/sinus4096.w
palette	incbin	openscreen_rgb32x256

	section	c2pscreen,BSS
chunk	ds.l	320*201/4
tunnel.tab	ds.w	320*210
tunnel.map	ds.b	65536
BMenvmap	ds.b	65536

	section	_buffer1_,bss_c
buf_width	equ	320
buf_height	equ	200
b1p0	ds.l	buf_width*buf_height/32
b1p1	ds.l	buf_width*buf_height/32
b1p2	ds.l	buf_width*buf_height/32
b1p3	ds.l	buf_width*buf_height/32
b1p4	ds.l	buf_width*buf_height/32
b1p5	ds.l	buf_width*buf_height/32
b1p6	ds.l	buf_width*buf_height/32
b1p7	ds.l	buf_width*buf_height/32
	section	_buffer2_,bss_c
b2p0	ds.l	buf_width*buf_height/32
b2p1	ds.l	buf_width*buf_height/32
b2p2	ds.l	buf_width*buf_height/32
b2p3	ds.l	buf_width*buf_height/32
b2p4	ds.l	buf_width*buf_height/32
b2p5	ds.l	buf_width*buf_height/32
b2p6	ds.l	buf_width*buf_height/32
b2p7	ds.l	buf_width*buf_height/32
