! Copyright (C) 2009 Jon Harper. ! See http://factorcode.org/license.txt for BSD license. USING: accessors arrays colors.constants combinators fry kernel locals math math.matrices opengl random sequences ui.pens ui.gadgets ui calendar alarms io ; IN: brian2 [ unclip drop f suffix ] bi@ [ 2array ] 2map [ swap prefix ] 2map [ sift ] map ; : neighbours ( m -- m ) [ aggregate ] map flip [ aggregate ] map [ concat ] mmap flip ; : step-cell ( state neighbours -- state' ) [ on = ] filter length swap { { on [ drop dying ] } { dying [ drop off ] } { off [ 2 = [ on ] [ off ] if ] } } case ; : step-world ( m -- m' ) dup neighbours [ step-cell ] 2mmap ; ! !!!!!!!!!!!!!!!!!!!!!!! ! ! Words to draw the world ! ! !!!!!!!!!!!!!!!!!!!!!!! ! : color ( cell -- color ) { { on [ COLOR: red ] } { off [ COLOR: black ] } { dying [ COLOR: blue ] } } case ; : cell-loc ( x y cell-dim -- cell-loc ) [ 2array ] dip [ * ] 2map ; :: draw-cell ( cell j i cell-dim -- ) cell color gl-color j i cell-dim [ cell-loc ] keep gl-fill-rect ; : (draw) ( world cell-dim -- ) [ draw-cell ] curry meach-index ; :: draw ( gadget-dim world -- ) [let* | world-dim [ world [ first length ] [ length ] bi 2array ] cell-dim [ gadget-dim world-dim [ / ] 2map ] | world cell-dim (draw) ] ; TUPLE: world-pen world ; M: world-pen draw-interior [ pref-dim>> ] [ world>> ] bi* draw ; ! !!!!!!!!!!!!!!!!!!!!!!!!!! ! Words to open the window ! ! !!!!!!!!!!!!!!!!!!!!!!!!!! : ( world -- gadget ) world-pen boa swap >>interior { 800 800 } >>pref-dim ; : open-brian-window ( world -- gadget ) dup [ "Brian's Brain" open-window ] curry with-ui ; : step-window ( gadget -- gadget ) dup interior>> dup world>> step-world >>world >>interior dup relayout-1 ; PRIVATE> ! !!!!!!!!!!!! ! ! Public words ! ! !!!!!!!!!!!! ! ! Call cancel-alarm on the alarm to stop the rendering :: animate ( dim frequency -- alarm ) dim random-world open-brian-window [ step-window ] curry now frequency milliseconds add-alarm ; : example ( -- alarm ) { 80 80 } 100 animate ;