Skip to content

Commit

Permalink
[vc] Almost converting animayion-node-player
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeodrippe committed Jan 29, 2025
1 parent eaed559 commit cf5bce4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 26 deletions.
33 changes: 26 additions & 7 deletions src/vybe/c.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,11 @@ signal(SIGSEGV, sighandler);
(str/join ";\n\n"))))

:if
(let [{:keys [then else] t :test} v]
(let [{:keys [then else] t :test} v
;; For supporting the `:else` from a `cond` macro.
t (if (= (:form t) :else)
(analyze true)
t)]
(if (:form else)
(format "( %s ? \n ({%s;}) : \n ({%s;}) )"
(emit t)
Expand Down Expand Up @@ -1223,9 +1227,12 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
"
(if (seq components)
(str (->> components
(mapv (fn [c]
(mapcat (fn [c]
(let [n (-adapt-type c)]
(format " struct %s: \"%s\"" n (vp/comp-name c)))))
;; Support component value and component pointer.
[(format " struct %s: \"%s\"" n (vp/comp-name c))
;; Component pointer not for now.
#_(format " struct %s*: \"__*__%s\"" n (vp/comp-name c))])))
(str/join ", \\\n")))
"")))
#_ (-typename-schemas [VybeHooks])
Expand Down Expand Up @@ -1820,7 +1827,6 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
(defn -adapt-vybe-c-obj
[v]
(let [{:keys [type size data]} v
data (vp/reinterpret data size)

primitive-layout (case type
("int" "unsigned int")
Expand Down Expand Up @@ -1859,10 +1865,23 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
nil)
[res c] (if primitive-layout
[(vp/p->value data primitive-layout) nil]
(let [c (eval (symbol type))]
[(vp/p->value (vp/reinterpret data size) primitive-layout)
nil]
;; __*__ identifies that the component is a pointer.
(let [pointer? (str/starts-with? type "__*__")
type (if pointer?
(subs type 5)
type)
c (eval (symbol type))]
[(vp/clone
(vp/as data c))
(vp/as (vp/reinterpret data size) c)
#_(if pointer?
(do (def aaa
(first (vp/arr (vp/reinterpret data (.byteSize (vp/type->layout :*)))
1
c)))
aaa)
(vp/as (vp/reinterpret data size) c)))
c]))]
{:res res
:c c}))
Expand Down
8 changes: 4 additions & 4 deletions src/vybe/flecs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2338,9 +2338,9 @@
(if type
;; Component branch.
(let [form (if (contains? flags :maybe)
`(if (= ~k vp/null)
(vp/as vp/null [:* ~type])
(vp/& (nth ~k ~i)))
`(if ~k
(vp/& (nth ~k ~i))
(vp/as vp/null [:* ~type]))
`(vp/& (nth ~k ~i)))
res-internal-sym (symbol (str "res-internal--" idx))]
(if (map? binding-form)
Expand All @@ -2361,7 +2361,7 @@
~@body))))
(with-meta (meta &form))))

;; Defined system builder.
;; Define system builder.
(defn ~sys-name
[w#]
#_ (def ~'w w#)
Expand Down
67 changes: 55 additions & 12 deletions src/vybe/game/system.clj
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,14 @@
c)))
nil))

(vf/defsystem-c animation-node-player-2 _w
[[_ node] [:vg.anim/target-node :*]
(vf/defsystem-c ^:debug animation-node-player-2 w
[_ [:vg.anim/target-node '?node]
translation [:src '?node vt/Translation]
scale [:src '?node vt/Scale]
rotation [:src '?node vt/Rotation]
;; TODO Maybe [:vg.anim/target-component [:vf/component'?c]] ?
_ [:vg.anim/target-component '?c]
{:keys [id]} [:src '?c vf/VybeComponentId]
node-ref vf/Ref
{:keys [kind timeline_count values timeline]} vt/AnimationChannel
player [:meta {:flags #{:up :cascade}
:inout :mut}
Expand All @@ -281,18 +283,59 @@
(vp/arr values timeline_count vt/Translation)
(vp/arr values timeline_count vt/Scale))
(vp/as [:* :void]))
;; TODO Should this create a slice? (support for `count`, `first`, `last`, `nth`, iteration)
timeline* (vp/arr timeline timeline_count :float)
;; TODO OPTM We could also leverage the previous index.
idx* (vc/bs_lower_bound timeline* timeline_count (:current_time @player))
idx (int (max (dec (or idx* timeline_count)) 0))
;; TODO idx* won't be `nil`
;; TODO We should probably use the same truthy semantics as Clojure
t (when idx*
(/ (- (:current_time @player)
(nth timeline* idx))
(- (nth timeline* (inc idx))
(nth timeline* idx))))]
idx (cond
(= idx* 0)
0

(>= idx* timeline_count)
-1

:else
(dec idx*))

#_ #_my-ref (-> (vf.c/ecs-ref-get-id (:w @node-ref)
(vp/& (:flecs_ref @node-ref))
(:id (:flecs_ref @node-ref)))
#_(vp/p->map c))]
#_(tap> (long (vp/& (:flecs_ref @node-ref))))

#_(when tttt
(tap> @tttt))

;; TODO We could use some `:void` metadata to indicate that
;; a form does not return anything.
(if (>= idx 0)
(let [v (/ (- (:current_time @player)
(nth timeline* idx))
(- (nth timeline* (inc idx))
(nth timeline* idx)))]
^:void
(cond
(= kind 0)
(do (merge @translation
(nth (vp/arr values timeline_count vt/Translation) idx))
nil)

(= kind 2)
(do (merge @rotation
(nth (vp/arr values timeline_count vt/Rotation) idx))
nil)

#_(= kind 1)
#_(nth (vp/arr values timeline_count vt/Scale) idx)

#_(= kind 2)
#_(nth (vp/arr values timeline_count vt/Rotation) idx))

#_(merge @node-ref (nth values idx)
#_(if t
(lerp-p (nth values idx)
(nth values (inc idx))
t)
(nth values idx)))))

#_(when (< idx* 0)
(conj parent-e :vg.anim/stop)
Expand Down
12 changes: 9 additions & 3 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,16 +306,22 @@
- [x] mapping from entity id to component type
- [x] don't use `comp-id`
- [-] create cond automatically from it?
- [-] how to represent `nil` properly?
- [ ] fix `idx`
- [ ] fix `t`
- [x] fix `idx`
- [x] fix `t`
- [ ] lerp
- [ ] just idx for translation
- [ ] scale
- [ ] rotation
- [ ] use lerp
- [ ] :vf/entity should return a struct?
- [ ] consider making systems `always` by default
- [ ] `def` to a var from C?
- [x] show line/column error correctly for an error before calling the compiler
- [ ] Remove variables starting with a `_` in a `let` (just do the side-effect)
- [ ] optimization (-O3)?
- [ ] how to represent `nil` properly?
- [ ] maybe with nullable
- [ ] should have slices? (support for `count`, `first`, `last`, `nth`, iteration)
- [ ] built-in models
- [x] minimal
- [ ] more complex
Expand Down

0 comments on commit cf5bce4

Please sign in to comment.