nandi/jolt-nativepublic Fork 0
6a3304ddddcc7d3e9486b470fea5933a1f81f8e8
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

raster.clj · 92 lines · 3.9 KBClojure Blame HistoryRaw
Paint glimmer with nothing under it f554a01 nandi 12d ago1(ns glimmer-gfx.raster
2 "Software rasterizer over an int-array of 0xRRGGBB. Nothing here knows what
3 a window is -- and nothing here calls into Java. aget/aset/int-array/abs are
4 all clojure.core; the ^ints/^long hints are hints, not calls."
5 (:require [clojure.string :as str]))
6
7(defrecord Buf [^ints px ^long w ^long h])
8
9(defn buf [w h] (->Buf (int-array (* w h)) w h))
10
11(defn clear [{:keys [^ints px]} ^long c]
12 (let [c (int c)] (dotimes [i (alength px)] (aset px i c))))
13
14(defn px!
15 [{:keys [^ints px ^long w ^long h]} ^long x ^long y ^long c]
16 (when (and (>= x 0) (< x w) (>= y 0) (< y h))
17 (aset px (unchecked-add (unchecked-multiply y w) x) (int c))))
18
19(defn rect!
20 "Filled, clipped."
21 [{:keys [^ints px ^long w ^long h]} x0 y0 rw rh c]
22 (let [x1 (min w (+ x0 rw)) y1 (min h (+ y0 rh))
23 c (int c)]
24 (loop [y (max 0 y0)]
25 (when (< y y1)
26 (let [row (unchecked-multiply y w)]
27 (loop [x (max 0 x0)]
28 (when (< x x1)
29 (aset px (unchecked-add row x) c)
30 (recur (inc x)))))
31 (recur (inc y))))))
32
33(defn line!
34 "Bresenham. ponytail: no AA -- add Wu's if the jaggies bother you."
35 [b x0 y0 x1 y1 c]
36 (let [dx (abs (- x1 x0)) dy (- (abs (- y1 y0)))
37 sx (if (< x0 x1) 1 -1) sy (if (< y0 y1) 1 -1)]
38 (loop [x x0 y y0 err (+ dx dy)]
39 (px! b x y c)
40 (when-not (and (= x x1) (= y y1))
41 (let [e2 (* 2 err)]
42 (recur (if (>= e2 dy) (+ x sx) x)
43 (if (<= e2 dx) (+ y sy) y)
44 (cond-> err (>= e2 dy) (+ dy) (<= e2 dx) (+ dx))))))))
45
Play three games on the gfx backend e98a184 nandi 11d ago46(defn poly!
47 "Closed wireframe through [x y] points. Vector art is lines, so this is the
48 whole of it. ponytail: outline only -- a filled polygon needs a scanline
49 pass, add one when something actually has to be solid."
50 [b pts c]
51 (doseq [[[x0 y0] [x1 y1]] (map vector pts (concat (rest pts) [(first pts)]))]
52 (line! b (int x0) (int y0) (int x1) (int y1) c)))
53
Paint glimmer with nothing under it f554a01 nandi 12d ago54;; ------------------------------------------------------------------ 3x5 font
55;; Authored as strings so a glyph is readable and fixable in place.
56
57(def ^:private glyphs
58 (->> ["A.#.|#.#|###|#.#|#.#" "B##.|#.#|##.|#.#|##." "C.##|#..|#..|#..|.##"
59 "D##.|#.#|#.#|#.#|##." "E###|#..|##.|#..|###" "F###|#..|##.|#..|#.."
60 "G.##|#..|#.#|#.#|.##" "H#.#|#.#|###|#.#|#.#" "I###|.#.|.#.|.#.|###"
61 "J..#|..#|..#|#.#|.#." "K#.#|#.#|##.|#.#|#.#" "L#..|#..|#..|#..|###"
62 "M#.#|###|###|#.#|#.#" "N##.|#.#|#.#|#.#|#.#" "O.#.|#.#|#.#|#.#|.#."
63 "P##.|#.#|##.|#..|#.." "Q.#.|#.#|#.#|###|.##" "R##.|#.#|##.|#.#|#.#"
64 "S.##|#..|.#.|..#|##." "T###|.#.|.#.|.#.|.#." "U#.#|#.#|#.#|#.#|.##"
65 "V#.#|#.#|#.#|#.#|.#." "W#.#|#.#|###|###|#.#" "X#.#|#.#|.#.|#.#|#.#"
66 "Y#.#|#.#|.#.|.#.|.#." "Z###|..#|.#.|#..|###"
67 "0###|#.#|#.#|#.#|###" "1.#.|##.|.#.|.#.|###" "2##.|..#|.#.|#..|###"
68 "3##.|..#|.#.|..#|##." "4#.#|#.#|###|..#|..#" "5###|#..|##.|..#|##."
69 "6.##|#..|###|#.#|###" "7###|..#|.#.|.#.|.#." "8###|#.#|###|#.#|###"
70 "9###|#.#|###|..#|##."
71 " ...|...|...|...|..." "....|...|...|...|.#." ":...|.#.|...|.#.|..."
72 "-...|...|###|...|..." "/..#|..#|.#.|#..|#.." "%#.#|..#|.#.|#..|#.#"
73 "(.#.|#..|#..|#..|.#." ")#..|.#.|.#.|.#.|#.."]
74 (map (fn [s] [(first s) (->> (subs s 1) (remove #{\|}) (partition 3)
75 (mapv str/join))]))
76 (into {})))
77
78(def ^:const glyph-w 4) ; 3px + 1px gap
79
80(defn text!
81 "Draws s at (x,y), scaled. Unknown chars render blank."
82 ([b x y s c] (text! b x y s c 2))
83 ([b x y s c k]
84 (let [s (str/upper-case (str s))]
85 (dotimes [i (count s)]
86 (when-let [g (glyphs (nth s i))]
87 (dotimes [row 5]
88 (dotimes [col 3]
89 (when (= \# (nth (g row) col))
90 (rect! b (+ x (* k (+ (* i glyph-w) col))) (+ y (* k row)) k k c)))))))))
91
92(defn text-w ^long [s ^long k] (* k glyph-w (count s)))