Guides/Window Driver/Animation/Animated JGL2

From J Wiki
Jump to navigation Jump to search
NB. -------------------------------------------
NB. jgl2 animation demo for j
NB. draws a timer moving around in a circle
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 'gl2'
coinsert 'jgl2'

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

im =: 1 (<5 5) } 11 11 $ 0                   NB. init to black grid with one white pixel

timestr =: verb define                       NB. return current timestamp as a string
  6!:0 'YYYY-MM-DD hh:mm:ss.ss'
)

xy =: 150 150                                NB. position at which to draw timer

update =: verb define
 NB. move the point xy around in a circle (using cosine/sine of current clock time)
 xy =: <. 150 150 + 100 * 2 1 o. 6!:1''
)

render =: verb define 

  glclear''

  NB. draw the current time at xy
  glbrush glrgb 255 255 255
  glrect xy, 140 30
  gltextxy xy + 8 6
  gltext timestr''

  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 jgl2 animation demo'                  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 10'                                NB. start timer with this many ms between ticks