Skip to content

Instantly share code, notes, and snippets.

@saikyun
saikyun / caching.janet
Created April 13, 2021 15:59
Caching with hopefully nice ui.
(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)
# 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))))
(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]
(defonce pushers @[])
(defonce pullers @[])
(array/clear pushers)
(array/clear pullers)
(defonce click-buffer @[])
(array/clear click-buffer)
(defonce move-buffer @[])
(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
@saikyun
saikyun / list_of_ways_to_deal_with_gui.md
Last active April 19, 2021 17:23
Different ways to handle interaction and rendering

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)

(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 / janet_stepping_poc.janet
Created April 24, 2021 08:24
poc for step debugging janet
(defmacro line-number
[]
(def [l c] (tuple/sourcemap (dyn :macro-form ())))
l)
(defn add []
(def a 10)
(+ a a))
#=> turns into

Today I've worked on the layouting of Freja.

Specifically, using tuples and structs, you can now create fabulous designs.

I'm getting ahead of myself though. I should start with the problem, the reason why I started making this very nice system that I will soon tell you about.

Freja has a menu, like any old program. This is how it looks.

The problem, originally, is that when my dear friend sogaiu wanted to add some menu items, the code looked like this:

@saikyun
saikyun / hiccup-compilation-example.clj
Last active July 7, 2021 11:18
example of compilation steps of hiccup (it's in janet but using .clj for highlighting)
# step 1: define hiccup
(def hiccup [:block {} [:text {:size 20} "Hello sogaiu"]])
#=> (:block {} (:text {:size 20} "Hello sogaiu"))
# step 2: compile hiccup into an element (a table)
(def element (with-dyns [# tags map e.g. :text to relevant rendering function
:tags tags
# default font
:text/font "Poppins"]
(ch/compile hiccup)))