Paste: raylib + timers + threads + hot-reload

Author: CapitalEx
Mode: factor
Date: Sat, 12 Aug 2023 03:18:51
Plain Text |
USING: arrays calendar combinators.smart io kernel literals
namespaces random random.data raylib sequences threads timers
unicode vocabs.loader io.pathnames io.monitors ;
IN: hello-raylib

SYMBOL: color
SYMBOL: text
CONSTANT: COLORS $[ RED GREEN BLUE 3array ]
CONSTANT: THIS-FILE $[ "vocab:hello-raylib/hello-raylib.factor" absolute-path ]

: current-color ( -- color ) 
    color get-global ;

: current-text ( -- text )
    text get-global ;

: clear ( -- )
    RAYWHITE clear-background ;

: hello-world ( -- ) 
    current-text 50 10 64 current-color draw-text ;

: idle-frame ( -- ) 
    16 milliseconds sleep ;

: with-draw ( quote -- )
    clear begin-drawing call end-drawing idle-frame ; inline

: randomize-color ( -- )
    COLORS random color set-global ;

: ranodmize-letter ( Letter --  Letter )
    [ Letter? ] [ drop random-Letter ] smart-when ;

: randomize-text ( -- )
    text [ [ ranodmize-letter ] map ] change-global ;

: start-monitor ( -- )
    [ THIS-FILE f [ "hello-raylib" reload ] run-monitor ] with-monitors ;

: ?hot-reloading ( ? -- )
    [ [ start-monitor ] "hot reloader" spawn drop ] when ;

: main ( -- )
    t ?hot-reloading

    600 450 "Hello, World!" init-window
    
    randomize-color
    "Hello, World!" text set-global

    [ randomize-color ] 250 milliseconds every drop
    [ randomize-text  ] 125 milliseconds every drop

    [ window-should-close ] [ [ hello-world ] with-draw ] until
    
    close-window ;


MAIN: main

New Annotation

Summary:
Author:
Mode:
Body: