Guides/Window Driver/Animation/Animated Viewmat

From J Wiki
Jump to navigation Jump to search
NB. -------------------------------------------
NB. viewmat animation demo
NB.
NB. (c) 2019 by michal j wallace
NB.   http://tangentstorm.com/
NB.
NB. free for use under the MIT license.
NB.   https://opensource.org/licenses/MIT
NB. -------------------------------------------
load 'viewmat'
coinsert 'jgl2'

vmcc =: viewmatcc_jviewmat_

NB. -- animation engine ------------------------

im =: 0$0

update =: verb define
 im =: ? 16 16 $ 16b1000000
)

render =: verb define
  NB. draw the grid, avoiding infinite error messages
  if. 2 = #$ im do. 'rgb' vmcc im;'g0' end.
  
  NB. update the image on-screen
  glpaint''
)

step =: render @ update                      NB. each step, we'll call those two in sequence

NB. -- build the window ------------------------

wd 'pc w0 closeok'                           NB. parent control (window) named 'w'.
wd 'pn viewmat animation'                    NB. give it a title if you want
wd 'minwh 500 500; cc g0 isidraw'            NB. add an 'isidraw' child control named 'g'
wd 'pshow'                                   NB. show the window at the given coordinates.

sys_timer_z_ =: step_base_                   NB. set up global timer to call step
wd 'timer 100'                               NB. start timer at 100 frames per second