home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 18 / forthsup / polygons.fth < prev    next >
Encoding:
Text File  |  1986-09-18  |  2.2 KB  |  81 lines

  1. \ Sliding polygons demo.  Load this file and type   poly-demo
  2. \ Type any key to stop it.
  3. \
  4. \ The demo initially draws triangles.  By typing
  5. \    n vertices   poly-demo
  6. \ where 3 <= n < 64, you can change it to draw polygons with more vertices
  7.  
  8. needs rnd random.fth
  9. needs line-a-init linea.fth
  10. line-a-init
  11. decimal
  12.  5 xbios: _setscreen  { w.rez l.phyz l.log -- }
  13. 37 xbios: vsync { -- }  \ wait till next vertical retrace then return
  14.  
  15. scradr constant old-screen
  16. get-rez
  17. constant cury
  18. constant curx
  19.  
  20. create source-buff  256 allot
  21. create dest-buff  256 allot
  22. create spare-buff  256 allot
  23. create new-scr-buff  1024 32 * 512 + allot
  24. new-scr-buff 512 mod 512 swap - new-scr-buff + constant new-screen
  25. variable keep-going keep-going on
  26. variable #verts  3 #verts !
  27.  
  28. : set-buf  ( addr -- )  \ randomize a buffer
  29.    dup #verts @ 0
  30.    do   curx rnd  over w! 2+  cury rnd  over w! 2+   loop
  31.    over w@ over w! 2+ swap 2+ w@  swap w!
  32. ;
  33.  
  34. : move-buf  ( -- )  \ convert one buffer to another
  35.    dest-buff  source-buff  keep-going off  #verts @ 1+  2* 0
  36.    do
  37.      dup w@  2 pick w@  2dup 2dup <> -rot 1+ <> and
  38.       if   over  > 
  39.         if   2+
  40.         else 2-
  41.         then over w!  keep-going on
  42.       else 2drop
  43.       then 2+  swap  2+  swap
  44.    loop  2drop
  45. ;
  46. : draw-source ( -- )  \ draw the source polygon
  47.    source-buff #verts @  poly-line 
  48. ;
  49. : vertices  ( n -- )  \ set the number of vertices
  50.    #verts !
  51. ;
  52. : poly-demo
  53.    27 emit ascii f emit
  54.    -1 -1 new-screen _setscreen  erase-screen
  55.    -1 -1 old-screen _setscreen  erase-screen
  56.    2 _wrt_mod w!  
  57.    dest-buff set-buf  source-buff set-buf  
  58.    -1 new-screen old-screen _setscreen vsync draw-source  
  59.    -1 old-screen new-screen _setscreen vsync draw-source  
  60.    source-buff spare-buff  #verts @ 1+ 4 * cmove
  61.    begin
  62.     begin
  63.         -1 old-screen new-screen  _setscreen
  64.         spare-buff #verts @ vsync poly-line        
  65.         source-buff spare-buff  #verts @ 1+ 4 * cmove
  66.         move-buf  draw-source 
  67.  
  68.         -1 new-screen old-screen  _setscreen
  69.         spare-buff #verts @ vsync poly-line        
  70.         source-buff spare-buff  #verts @ 1+ 4 * cmove
  71.         move-buf  draw-source 
  72.         key?
  73.         if  -1 old-screen old-screen _setscreen
  74.             27 emit ascii e emit exit  
  75.         then
  76.           keep-going @ 0=
  77.     until  dest-buff set-buf 
  78.    again
  79. ;
  80.