' A Graphics Window Example ..
' GWS - Updated April 2018

autodefine "OFF"

WINDOW w
INT wW,wH,i,j,varW,varH,meanx,meany,sdev,c,x,y
FLOAT cstep,pi,x1,x2,ret
ISTRING a$[22]="Creative Basic is Fun"

pi = 4 * atan(1)						: ' gives an accurate value of pi (3.14 .. )
wW = 800										: ' set the window size in pixels
wH = 600

openwindow w, 0, 0, wW, wH, @SYSMENU, 0, "...and IBasic!",&msghandler

control w, @BUTTON,"Exit", (wW-70)/2, 450, 70, 30, @ctlbtnflat, 1
setcontrolcolor w, 1, 0, rgb(120,140,220)

' draw a gradient fill ..
cstep = 0.8 * 255/wH			: ' multiplier of 255 controls the gradient rate

for i = 1 to wH
	line w, 0, i, wW, i, rgb(0,0,INT(i * cstep))
next i

' create some coloured spots ...
for i = 1 to 15
	meanx = rnd(wW - 100) + 50
	meany = rnd(wH - 250) + 50
	sdev = 25 								: ' controls the spread of the pixels ...
	c = rgb(rnd(200)+55,rnd(200)+55,rnd(200)+55)
	for j = 1 to 500
		x = splot() * sdev + meanx
		y = splot() * sdev + meany
		circle w, x, y, INT(rnd(1)*1)+1, c, c
	next j
next i

' display some text ..
setfont w, "Arial", 26, 700, @sfitalic
gettextsize w, a$, varW, varH
frontpen w, rgb(130,130,250)
drawmode w, @transparent
move w, (wW-varW)/2, 80
print w, a$

waituntil iswindowclosed(w)
	end

sub msghandler(),INT
select @message
	case @idclosewindow
		closewindow w
	case @idcreate
		centerwindow w
	case @idcontrol
		if (@controlid = 1) then closewindow w
	case @idchar
		if (@code = 27) then closewindow w
		if (@code = asc("Q")) | (@code = asc("q")) then closewindow w
endselect
return 0
ENDSUB

sub splot(),FLOAT
' routine to generate a random values from a normal distribution ..
x1 = rnd(1)
x2 = rnd(1)
' catch very small values of x1 ..
if (x1 < 1.0e-5) then x1 = 1.0e-5
' the Normal curve value ..
ret = sqrt(-2.0 * log(x1)) * cos(2 * pi * x2)
return ret
ENDSUB
