/******************************************************************************\
**  Wave drawing demo for Secal                                               **
**  Requires Kickstart 2                                                      **
\******************************************************************************/


go main;


#-------------------------------------------------------------------------------


include "inc/libcalls/exec.inc";
include "inc/libcalls/intuition.inc";
include "inc/libcalls/graphics.inc";

include "inc/utility/tagitem.inc";
include "inc/intuition/screens.inc";
include "inc/graphics/modeid.inc";


def SysBase=[4.w].ul;


/******************************************************************************\
************                      M A I N                           ************
\******************************************************************************/


obj IntuitionBase,GfxBase:ulong;

obj myscr,myscrrp:ulong;
obj basex,basey:word;


#-------------------------------------------------------------------------------


main:
call sysinit;
if d0 then
	call waveinit;
	call wavedraw;
	while [$dff016] and $400 do;		# DIRTY CHECK FOR RIGHT MOUSE BUTTON
	call sysdone;
;

d0.l:=0;
rts;															# MAIN





#																	D0=SUCCESS

sysinit:
OpenLibrary("graphics.library",37); GfxBase:=d0;
if GfxBase then
	OpenLibrary("intuition.library",37); IntuitionBase:=d0;
	if IntuitionBase then
		OpenScreenTagList(0,@scrtags); myscr:=d0;
		if myscr then

			a0:=myscr; myscrrp:=@Screen(a0).RastPort;
			basex:=Screen(a0).Width/2-300/2;
			d0:=Screen(a0).Height-(Screen(a0).BarHeight+1);
			d0:=d0/2+(Screen(a0).BarHeight+1); basey:=d0-188/2+128;	# 0,0 OFFSET

			d0:=-1; go end_sysinit;		# INIT SUCCESSFULL
		;

														# OTHERWISE FAILED
		CloseLibrary(IntuitionBase);
	;
	CloseLibrary(GfxBase);
;

d0:=0;

end_sysinit:
rts;															# SYSINIT



scrtags:
dc.l SA_Depth,2;
dc.l SA_Title,"Secal Wave demo";
dc.l SA_Pens,@scrpens;
dc.l TAG_DONE;							# TAGS FOR OUR SCREEN

scrpens:
dc.w -1;										# TO MAKE IT "NEW LOOK"





sysdone:
CloseScreen(myscr);						# CLOSE SCREEN

CloseLibrary(IntuitionBase);
CloseLibrary(GfxBase);				# CLOSE LIBS
rts;															# SYSDONE


/******************************************************************************\
************                      W A V E                           ************
\******************************************************************************/


obj hiarray[300],loarray[300]:word;			# EDGES OF THE SURFACE


#-------------------------------------------------------------------------------


waveinit:
a0:=@hiarray; a1:=@loarray;
for d0:=299 downto 0 do [a0+]:=-9999; [a1+]:=9999;;	# LO AND HI ARRAYS
rts;															# WAVEINIT





wavedraw:
push d2\d3\d4\d5;

for d5:=0 upto 196 step 2 do
	for d4:=199 downto 0 do
		d2:=d4+d5 lsr 1; d3:=(d4 lsr 1-d5) asr 1;
		d0:=d4-100; d1:=d5-100; call waveheight;
		d3:=d3-d0;								# CALC (U,V) ON THE DISPLAY

		a0:=@hiarray+d2.w lsl 1;
		if d3>[a0] then
			[a0]:=d3;
			SetAPen(myscrrp,3);
			WritePixel(myscrrp,basex+d2.w,basey+d3.w);
		;													# DRAW HIGH PIXEL
		a0:=@loarray+d2.w lsl 1;
		if d3<[a0] then
			[a0]:=d3;
			SetAPen(myscrrp,2);
			WritePixel(myscrrp,basex+d2.w,basey+d3.w);
		;													# DRAW LOW PIXEL
	;														# X LOOP
;															# Y LOOP

pop d2\d3\d4\d5;
rts;															# WAVEDRAW


#-------------------------------------------------------------------------------


# D0=X, D1=Y											D0=HEIGHT

waveheight:
push d2\d3;
d2:=d0; d3:=d1;

d0:=d3*40; sin(d0); d1:=d0;
d0:=d2*40; cos(d0);
d0.l:=(d0.w*d1.w) asr 8;			# COS(X)*SIN(Y)

d1:=d2;
if d1<0 then d1:=-d1;;
if d1<d3 then d1:=d3;
elif d1<-d3 then d1:=-d3;;			# DISTANCE

d0.l:=d0*(100-d1);

d0.l:=d0 asr 8;								# FIXPOINT OFF

pop d2\d3;
rts;															# WAVEHEIGHT





#																	D0=RESULT

pattern sin(source) is
	push a0;

	move		source,d7;
	and			$fff,d7;
	lsl			1,d7;
	lea			[@sincostable],a0;
	move		[a0+d7.w],d0;

	pop a0;
endp;															# SIN



#																	D0=RESULT

pattern cos(source) is
	push a0;

	move		source,d7;
	and			$fff,d7;
	lsl			1,d7;
	lea			[@sincostable+$800],a0;
	move		[a0+d7.w],d0;

	pop a0;
endp;															# COS





sincostable:						incbin "data/sincos.dat";
												# 1.25 SINE WAVE, 4096+1024 WORDS, 4096=1 WAVE (2*PI)


#*******************************************************************************

