1 ! Copyright (C) 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 ! This example only runs in the UI listener.
6 ! Pass with-canvas a quotation calling these words:
13 ! plot-string doesn't yet work.
15 ! other GL calls can be made, but be careful.
18 USING: arrays errors freetype gadgets gadgets-labels
19 gadgets-layouts gadgets-panes gadgets-theme generic kernel math
20 namespaces opengl sequences styles ;
24 { "monospaced" plain 12 } canvas-font set-global
26 : turn-by ( angle -- ) 0 0 1 glRotated ;
28 : move-by ( distance -- ) 0 0 glTranslated ;
31 GL_POINTS [ 0 0 0 glVertex3d ] do-state ;
33 : line-to ( distance -- )
35 GL_LINES [ 0 0 0 glVertex3d 0 0 glVertex3d ] do-state
38 : plot-string ( string -- )
39 canvas-font get open-font swap draw-string ;
41 : new-pen ( quot -- ) GL_MODELVIEW swap do-matrix ; inline
43 TUPLE: canvas quot id ;
46 dup delegate>gadget [ set-canvas-quot ] keep ;
48 M: canvas add-notify* ( gadget -- )
49 dup canvas-quot GL_COMPILE [ with-scope ] make-dlist
52 M: canvas draw-gadget* ( gadget -- )
54 dup rect-dim 2 v/n gl-translate
58 : with-canvas ( size quot -- )
59 <canvas> dup solid-boundary [ set-gadget-dim ] keep gadget. ;
61 : random-walk ( n -- )
62 [ 2 random-int 1/2 - 180 * turn-by 10 line-to ] times ;
64 : regular-polygon ( sides n -- )
65 [ 360 swap / ] keep [ over line-to dup turn-by ] times 2drop ;
68 4 [ drop 255 random-int 255 /f ] map gl-color ;
75 10 turn-by [ 60 10 regular-polygon ] new-pen