4.3BSD/usr/contrib/X/xdemo/web.clu

% from Steve Ward

webdemo = proc ()
    ai = array[int]
    minord = 7
    maxord = 16
    minweb = 10
    maxweb = 45
    bwidth: int := int$parse(xdemo_default("web", "BorderWidth"))
       except when not_found, overflow, bad_format: bwidth := 2 end
    back: x_pixmap := x_display$white()
    bdr: x_pixmap := x_display$black()
    linepix: int := BlackPixel
    if x_display$cells() > 2
       then begin
	    r, g, b: int := x_parse_color(xdemo_default("web", "Background"))
	    back := x_pixmap$tile(x_display$alloc_color(r, g, b))
	    end except when not_found: end
	    begin
	    r, g, b: int := x_parse_color(xdemo_default("web", "Border"))
	    bdr := x_pixmap$tile(x_display$alloc_color(r, g, b))
	    end except when not_found: end
	    linepix := x_display$alloc_cell()
       end
    w: x_window, wid0, hgt0: int := x_cons("web", back, bdr,
					   xdemo_geometry(), "=400x400+1+1",
					   40, 40, bwidth)
    w.name := "web"
    w.input := UnmapWindow
    x_window$map(w)
    w.input := ExposeWindow + UnmapWindow
    ev: event := x_input$empty_event()
    while true do
	sx, sy, width, height, bw, ms, wk: int, iw: x_window := x_window$query(w)
	if width <= 30  cor  height <= 30
	   then x_window$destroy(w)
		return
	   end
	height := height - 1
	width := width - 1
	pvec: ai := ai$fill(0, maxord * 2, 0)
	x: ai := ai$fill(0, maxord, 0)
	y: ai := ai$fill(0, maxord, 0)
	dx: ai := ai$fill(0, maxord, 0)
	dy: ai := ai$fill(0, maxord, 0)
	vlist: x_vlist := x_vlist$create(maxord * 2 * 2)
	while ~x_input$pending() do
	    x_window$clear(w)
	    if linepix ~= BlackPixel
	       then random_color(linepix) end
	    order: int := minord + random$next(maxord - minord + 1)
	    sweb: int := minweb + (random$next(maxweb) * 2**random$next(4)) / 8
	    for i: int in int$from_to_by(0, 2 * order - 2, 2) do
		pvec[i] := random$next(width)
		pvec[i + 1] := random$next(height)
		end
	    for i: int in int$from_to(0, order - 1) do
		x[i] := 32 * pvec[2 * i]
		y[i] := 32 * pvec[2 * i + 1]
		if i ~= 0
		   then dx[i - 1] := ((x[i] - x[i - 1]) * 32) / (sweb * 32)
		        dy[i - 1] := ((y[i] - y[i - 1]) * 32) / (sweb * 32)
		   end
		end
	    order := order - 1
	    for j: int in int$from_to_by(0, sweb, 2) do
		xx: int := int$max(0, int$min(x[0] / 32, width))
		yy: int := int$max(0, int$min(y[0] / 32, height))
		x[0] := x[0] + dx[0]
		y[0] := y[0] + dy[0]
		idx: int := 1
		for i: int in int$from_to(1, order - 1) do
		    nx: int := int$max(0, int$min(x[i] / 32, width))
		    ny: int := int$max(0, int$min(y[i] / 32, height))
		    x_vlist$store(vlist, idx, xx, yy, VertexDontDraw)
		    x_vlist$store(vlist, idx + 1, nx, ny, VertexDrawLastPoint)
		    idx := idx + 2
		    xx := nx
		    yy := ny
		    x[i] := x[i] + dx[i]
		    y[i] := y[i] + dy[i]
		    end
		j := j + 1
		if j > sweb
		   then x_window$draw(w, vlist, idx - 1, linepix, 1, 1,
				      GXcopy, -1)
			break
		   end
		i: int := order - 1
		xx := int$max(0, int$min(x[i] / 32, width))
		yy := int$max(0, int$min(y[i] / 32, height))
		x[i] := x[i] + dx[i]
		y[i] := y[i] + dy[i]
		for i in int$from_to_by(i - 1, 0, -1) do
		    nx: int := int$max(0, int$min(x[i] / 32, width))
		    ny: int := int$max(0, int$min(y[i] / 32, height))
		    x_vlist$store(vlist, idx, xx, yy, VertexDontDraw)
		    x_vlist$store(vlist, idx + 1, nx, ny, VertexDrawLastPoint)
		    idx := idx + 2
		    xx := nx
		    yy := ny
		    x[i] := x[i] + dx[i]
		    y[i] := y[i] + dy[i]
		    end
		x_window$draw(w, vlist, idx - 1, linepix, 1, 1, GXcopy, -1)
		end
	    x_flush()
	    sleep(2)
	    end
	x_input$deq(ev)
	if ev.kind = UnmapWindow
	   then x_input$deq(ev) end
	end
    end webdemo