Skip to content

Instantly share code, notes, and snippets.

@Saikyun
Saikyun / janet_stepping_poc.janet
Created Apr 24, 2021
poc for step debugging janet
View janet_stepping_poc.janet
(defmacro line-number
[]
(def [l c] (tuple/sourcemap (dyn :macro-form ())))
l)
(defn add []
(def a 10)
(+ a a))
#=> turns into
View queues.janet
(def click-queue (ev/chan 4))
(defn ev/check
[chan]
(when (pos? (ev/count chan))
(ev/take chan)))
(defn ev/push
[chan v]
(when (ev/full chan)
@Saikyun
Saikyun / list_of_ways_to_deal_with_gui.md
Last active Apr 19, 2021
Different ways to handle interaction and rendering
View list_of_ways_to_deal_with_gui.md

Different ways to handle interaction and rendering

A list of different ways to deal with interactions (mouse clicks, keyboard input, network events) and rendering of graphics (gui elements, games).

All example code is written in janet.

FRP (old elm style)

Description

Inspired by how elm used to do FRP (https://youtu.be/Ju4ICobPNfw)

View fiber_vs_function_call.janet
(defmacro timeit2
```
Time the execution of `form` using `os/clock` before and after,
and print the result to stdout. returns: result of executing `form`.
Uses `tag` (default "Elapsed time:") to tag the printout.
```
[form &opt tag]
(default tag "Elapsed time:")
(with-syms [start result end]
~(do
View messy_frp.janet
(defonce pushers @[])
(defonce pullers @[])
(array/clear pushers)
(array/clear pullers)
(defonce click-buffer @[])
(array/clear click-buffer)
(defonce move-buffer @[])
View mario_frp.janet
(def mario @{:x 0 :y 0 :vx 0 :vy 0})
(defn physics
[dt mario]
(-> mario
(update :x + (* (mario :vx) dt))
(update :y + (* (mario :vy) dt))))
(defn walk
[x mario]
View handle_input_with_context.janet
# two kinds of renders
(var mouse-stolen false)
(defn mouse-button-down?
[k]
(if ((dyn :context) :captured-mouse)
true
(and (not mouse-stolen)
(raylib/mouse-button-down? k))))
@Saikyun
Saikyun / caching.janet
Created Apr 13, 2021
Caching with hopefully nice ui.
View caching.janet
(defn invalidate!
"Invalidates the cache, i.e. forces it to rerender next `cached-render` call."
[& [c]]
(default c (dyn :cache))
(put c :valid false))
(defn update!
"Like update, but also runs invalidate!"
[& args]
(update ;args)
View Input handling
41.09 How to handle focus
Kinds of focus
- Mouse clicks
- Hover
- Drag
- Keyboard handling
- (Re)rendering
How to detect focus
View anim_using_fiber.janet
(use ./build/jaylib)
(set-config-flags
:msaa-4x-hint)
(init-window 800 600 "Chart")
(init-audio-device)
(set-target-fps 60)
(def current-animations @{})