feat(stdlib): add Anim.* + Tween.* — deterministic 2D animation & tweening (#5)
Two ECS-native, deterministic namespaces for 2D motion, driven off the fixed
frame clock so replays and lockstep netcode reproduce every frame and every
eased value exactly. Both are pure computed-inline Q16.16 / integer math (no new
runtime, no heap) — the game stores a timer on a component and calls these each
frame, exactly the way Collision.* / Grid.* are used.
Anim.* — spritesheet frame animation:
- Anim.frame(timer,fps,count) -> int looping frame index
- Anim.once(timer,fps,count) -> int one-shot, clamps on the last frame
- Anim.pingpong(timer,fps,count) -> int bounce 0..count-1..0
- Anim.finished(timer,fps,count) -> bool has a one-shot run past its end?
- Anim.duration(fps,count) -> fixed seconds for one cycle
- Anim.cell_x/cell_y(frame,cols,cell) -> int source rect on a grid sheet
Tween.* — value interpolation over a timeline:
- Tween.progress/loop/yoyo(timer,duration) -> fixed normalized amount
- Tween.done(timer,duration) -> bool
- Tween.ease(t, mode) -> fixed shape by a literal curve 0..6,
the same curves as Ease.* (now
factored into a shared ease_eval)
- Tween.number/round/point/tint(from,to,t) blend a fixed / int / Vector / color
The typed blends reuse the existing fixed / Vector / color helpers, and
Tween.ease shares Ease.*'s exact formulas via the new ease_eval(mode,t) — one
source of truth for every easing curve in the engine.
examples/library/anim.ludic asserts 34 cases (frame math, clamping, ping-pong,
cell geometry, timeline clamp/loop/yoyo, rounding, color/vector blends, and
Ease.in == Tween.ease(.,1)); wired into x test (now 62 passed). Docs: Anim +
Tween sections with 16 per-symbol pages, inventory/coverage green. Seed
reseeded; the C-free bootstrap fixpoint holds.
The stateful sugar the proposal sketches (named clips, Anim.play, fluent
Tween.chain/parallel handles, and an auto-injected advance system) is deliberately
left as a follow-up — this lands the deterministic math core both halves stand on.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
07e5a20c0e
commit
e4d1e95dcb
25 changed files with 12379 additions and 9910 deletions
7
docs/language/anim/_section.md
Normal file
7
docs/language/anim/_section.md
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
---
|
||||||
|
id: anim
|
||||||
|
title: Anim
|
||||||
|
order: 28
|
||||||
|
---
|
||||||
|
|
||||||
|
Spritesheet frame animation off the fixed frame clock. Store an elapsed <code>timer</code> (seconds, a <code>fixed</code>) on a component and each frame ask <a href="anim-frame"><code>Anim.frame</code></a> / <a href="anim-once"><code>Anim.once</code></a> / <a href="anim-pingpong"><code>Anim.pingpong</code></a> which cell to draw; <a href="anim-cell_x"><code>Anim.cell_x</code></a>/<a href="anim-cell_y"><code>Anim.cell_y</code></a> turn a frame index into a source rectangle on the sheet. Everything is integer/fixed and deterministic — the same timer reproduces the same frame every run, so replays and lockstep netcode match exactly.
|
||||||
31
docs/language/anim/anim-cell_x.md
Normal file
31
docs/language/anim/anim-cell_x.md
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
---
|
||||||
|
id: anim-cell_x
|
||||||
|
name: Anim.cell_x
|
||||||
|
category: anim
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Anim.cell_x
|
||||||
|
sig: Anim.cell_x(frame, cols, cell_w) -> int
|
||||||
|
tip: The source x (pixels) of a frame on a grid spritesheet.
|
||||||
|
order: 6
|
||||||
|
ns: Anim
|
||||||
|
member: cell_x
|
||||||
|
---
|
||||||
|
|
||||||
|
Turns a frame index into the left pixel of its cell on a spritesheet laid out as a grid of <code>cols</code> columns: <code>(frame mod cols) * cell_w</code>. Combine with <a href="anim-cell_y"><code>Anim.cell_y</code></a> to get the top-left source coordinate to blit from.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `frame` — the frame index (e.g. from <a href="anim-frame"><code>Anim.frame</code></a>)
|
||||||
|
- `cols` — columns in the sheet
|
||||||
|
- `cell_w` — cell width in pixels
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
handler Run phase Update {
|
||||||
|
let f = Anim.frame(fixed(1), 12, 8)
|
||||||
|
let sx = Anim.cell_x(f, 4, 16)
|
||||||
|
let sy = Anim.cell_y(f, 4, 16)
|
||||||
|
print(sx)
|
||||||
|
print(sy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
29
docs/language/anim/anim-cell_y.md
Normal file
29
docs/language/anim/anim-cell_y.md
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
---
|
||||||
|
id: anim-cell_y
|
||||||
|
name: Anim.cell_y
|
||||||
|
category: anim
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Anim.cell_y
|
||||||
|
sig: Anim.cell_y(frame, cols, cell_h) -> int
|
||||||
|
tip: The source y (pixels) of a frame on a grid spritesheet.
|
||||||
|
order: 7
|
||||||
|
ns: Anim
|
||||||
|
member: cell_y
|
||||||
|
---
|
||||||
|
|
||||||
|
Turns a frame index into the top pixel of its cell on a spritesheet laid out as a grid of <code>cols</code> columns: <code>(frame / cols) * cell_h</code>. Pair with <a href="anim-cell_x"><code>Anim.cell_x</code></a> for the full source rectangle of the frame.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `frame` — the frame index (e.g. from <a href="anim-frame"><code>Anim.frame</code></a>)
|
||||||
|
- `cols` — columns in the sheet
|
||||||
|
- `cell_h` — cell height in pixels
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
handler Run phase Update {
|
||||||
|
let f = Anim.frame(fixed(1), 12, 8)
|
||||||
|
let sy = Anim.cell_y(f, 4, 16)
|
||||||
|
print(sy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
27
docs/language/anim/anim-duration.md
Normal file
27
docs/language/anim/anim-duration.md
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
---
|
||||||
|
id: anim-duration
|
||||||
|
name: Anim.duration
|
||||||
|
category: anim
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Anim.duration
|
||||||
|
sig: Anim.duration(fps, count) -> fixed
|
||||||
|
tip: Seconds for one full cycle of a clip: count / fps.
|
||||||
|
order: 5
|
||||||
|
ns: Anim
|
||||||
|
member: duration
|
||||||
|
---
|
||||||
|
|
||||||
|
Returns the length of one cycle of a clip in seconds as a <code>fixed</code>: <code>count / fps</code>. Handy to schedule the next event, size a progress bar, or line a tween up with an animation.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `fps` — frames per second
|
||||||
|
- `count` — number of frames in the clip
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
handler Run phase Update {
|
||||||
|
let secs = Anim.duration(12, 4) # 4 frames / 12 fps = 0.333s
|
||||||
|
print(secs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
30
docs/language/anim/anim-finished.md
Normal file
30
docs/language/anim/anim-finished.md
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
---
|
||||||
|
id: anim-finished
|
||||||
|
name: Anim.finished
|
||||||
|
category: anim
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Anim.finished
|
||||||
|
sig: Anim.finished(timer, fps, count) -> bool
|
||||||
|
tip: True once a one-shot clip has run past its last frame.
|
||||||
|
order: 4
|
||||||
|
ns: Anim
|
||||||
|
member: finished
|
||||||
|
---
|
||||||
|
|
||||||
|
Returns <code>true</code> once <code>floor(timer * fps) >= count</code> — i.e. a one-shot clip driven by <a href="anim-once"><code>Anim.once</code></a> has played its final frame. Use it to despawn an effect, fire a follow-up, or switch back to an idle clip.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `timer` — elapsed seconds (a `fixed`)
|
||||||
|
- `fps` — frames per second
|
||||||
|
- `count` — number of frames in the clip
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
property Blast { timer: fixed = 0.0 }
|
||||||
|
model Boom { Blast }
|
||||||
|
handler Run phase Update {
|
||||||
|
Blast.timer = Blast.timer + Time.delta()
|
||||||
|
if Anim.finished(Blast.timer, 15, 6) { despawn(self()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
31
docs/language/anim/anim-frame.md
Normal file
31
docs/language/anim/anim-frame.md
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
---
|
||||||
|
id: anim-frame
|
||||||
|
name: Anim.frame
|
||||||
|
category: anim
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Anim.frame
|
||||||
|
sig: Anim.frame(timer, fps, count) -> int
|
||||||
|
tip: The looping frame index for an elapsed timer at a given fps.
|
||||||
|
order: 1
|
||||||
|
ns: Anim
|
||||||
|
member: frame
|
||||||
|
---
|
||||||
|
|
||||||
|
Returns the current frame of a looping clip: <code>floor(timer * fps)</code> reduced modulo <code>count</code>. <code>timer</code> is elapsed seconds as a <code>fixed</code>, <code>fps</code> the clip's frames per second, and <code>count</code> the number of frames. The clip wraps forever — frame <code>count-1</code> is followed by frame <code>0</code>.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `timer` — elapsed seconds (a `fixed`)
|
||||||
|
- `fps` — frames per second
|
||||||
|
- `count` — number of frames in the clip
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
property Sprite { timer: fixed = 0.0 }
|
||||||
|
model Hero { Sprite }
|
||||||
|
handler Run phase Update {
|
||||||
|
Sprite.timer = Sprite.timer + Time.delta()
|
||||||
|
let cell = Anim.frame(Sprite.timer, 12, 4) # 4-frame run at 12 fps
|
||||||
|
print(cell)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
31
docs/language/anim/anim-once.md
Normal file
31
docs/language/anim/anim-once.md
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
---
|
||||||
|
id: anim-once
|
||||||
|
name: Anim.once
|
||||||
|
category: anim
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Anim.once
|
||||||
|
sig: Anim.once(timer, fps, count) -> int
|
||||||
|
tip: A non-looping frame index that clamps on the last frame.
|
||||||
|
order: 2
|
||||||
|
ns: Anim
|
||||||
|
member: once
|
||||||
|
---
|
||||||
|
|
||||||
|
Like <a href="anim-frame"><code>Anim.frame</code></a> but for a one-shot clip: the frame is <code>min(floor(timer * fps), count - 1)</code>, so once it reaches the last frame it stays there instead of wrapping. Use it for a play-once animation like an explosion or a door opening; pair it with <a href="anim-finished"><code>Anim.finished</code></a> to know when it is done.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `timer` — elapsed seconds (a `fixed`)
|
||||||
|
- `fps` — frames per second
|
||||||
|
- `count` — number of frames in the clip
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
property Blast { timer: fixed = 0.0 }
|
||||||
|
model Boom { Blast }
|
||||||
|
handler Run phase Update {
|
||||||
|
Blast.timer = Blast.timer + Time.delta()
|
||||||
|
let cell = Anim.once(Blast.timer, 15, 6)
|
||||||
|
print(cell)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
31
docs/language/anim/anim-pingpong.md
Normal file
31
docs/language/anim/anim-pingpong.md
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
---
|
||||||
|
id: anim-pingpong
|
||||||
|
name: Anim.pingpong
|
||||||
|
category: anim
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Anim.pingpong
|
||||||
|
sig: Anim.pingpong(timer, fps, count) -> int
|
||||||
|
tip: A frame index that bounces 0..count-1..0 and repeats.
|
||||||
|
order: 3
|
||||||
|
ns: Anim
|
||||||
|
member: pingpong
|
||||||
|
---
|
||||||
|
|
||||||
|
Returns a frame index that plays forward to <code>count-1</code>, then back to <code>0</code>, then forward again — a triangle wave over the frames. Ideal for a two-way idle bob or a breathing/pulsing loop where a plain wrap would snap.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `timer` — elapsed seconds (a `fixed`)
|
||||||
|
- `fps` — frames per second
|
||||||
|
- `count` — number of frames in the clip
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
property Idle { timer: fixed = 0.0 }
|
||||||
|
model Fish { Idle }
|
||||||
|
handler Run phase Update {
|
||||||
|
Idle.timer = Idle.timer + Time.delta()
|
||||||
|
let cell = Anim.pingpong(Idle.timer, 8, 4) # 0 1 2 3 2 1 0 1 ...
|
||||||
|
print(cell)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
7
docs/language/tween/_section.md
Normal file
7
docs/language/tween/_section.md
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
---
|
||||||
|
id: tween
|
||||||
|
title: Tween
|
||||||
|
order: 29
|
||||||
|
---
|
||||||
|
|
||||||
|
Value interpolation over a timeline, off the fixed frame clock. The timeline helpers <a href="tween-progress"><code>Tween.progress</code></a>/<a href="tween-loop"><code>Tween.loop</code></a>/<a href="tween-yoyo"><code>Tween.yoyo</code></a> turn an elapsed <code>timer</code> and a <code>duration</code> into a normalized amount; <a href="tween-ease"><code>Tween.ease</code></a> shapes that amount through an easing curve (shared with <a href="ease"><code>Ease</code></a>); and the typed blends <a href="tween-number"><code>Tween.number</code></a>/<a href="tween-round"><code>Tween.round</code></a>/<a href="tween-point"><code>Tween.point</code></a>/<a href="tween-tint"><code>Tween.tint</code></a> interpolate a <code>fixed</code>, <code>int</code>, <code>Vector</code>, or color. All deterministic fixed-point, so a replay reproduces every eased value exactly.
|
||||||
29
docs/language/tween/tween-done.md
Normal file
29
docs/language/tween/tween-done.md
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
---
|
||||||
|
id: tween-done
|
||||||
|
name: Tween.done
|
||||||
|
category: tween
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Tween.done
|
||||||
|
sig: Tween.done(timer, duration) -> bool
|
||||||
|
tip: True once a one-shot tween's timer reaches its duration.
|
||||||
|
order: 4
|
||||||
|
ns: Tween
|
||||||
|
member: done
|
||||||
|
---
|
||||||
|
|
||||||
|
Returns <code>true</code> once <code>timer >= duration</code> — the companion to <a href="tween-progress"><code>Tween.progress</code></a> for ending a one-shot tween: kick off the next step, clear a component, or snap the value to its target.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `timer` — elapsed seconds (a `fixed`)
|
||||||
|
- `duration` — the tween's length in seconds (a `fixed`)
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
property Slide { timer: fixed = 0.0 }
|
||||||
|
model Menu { Slide }
|
||||||
|
handler Run phase Update {
|
||||||
|
Slide.timer = Slide.timer + Time.delta()
|
||||||
|
if Tween.done(Slide.timer, fixed(1)) { print(1) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
37
docs/language/tween/tween-ease.md
Normal file
37
docs/language/tween/tween-ease.md
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
---
|
||||||
|
id: tween-ease
|
||||||
|
name: Tween.ease
|
||||||
|
category: tween
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Tween.ease
|
||||||
|
sig: Tween.ease(t, mode) -> fixed
|
||||||
|
tip: Shape a 0..1 amount through an easing curve chosen by a literal mode.
|
||||||
|
order: 5
|
||||||
|
ns: Tween
|
||||||
|
member: ease
|
||||||
|
---
|
||||||
|
|
||||||
|
Shapes a normalized amount <code>t</code> (<code>0.0</code>..<code>1.0</code>) through one of the engine's easing curves and returns the eased <code>fixed</code>. <code>mode</code> must be a literal integer selecting the curve — the same curves as the <a href="ease"><code>Ease</code></a> namespace:
|
||||||
|
|
||||||
|
- `0` — linear
|
||||||
|
- `1` — ease-in (quadratic)
|
||||||
|
- `2` — ease-out (quadratic)
|
||||||
|
- `3` — smooth ease-in-out
|
||||||
|
- `4` — ease-in-back (overshoots below 0)
|
||||||
|
- `5` — ease-out-elastic (springy settle)
|
||||||
|
- `6` — ease-out-bounce
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `t` — the amount to shape (a `fixed` in 0..1, e.g. from <a href="tween-progress"><code>Tween.progress</code></a>)
|
||||||
|
- `mode` — a literal `int` 0..6 selecting the curve
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
handler Run phase Update {
|
||||||
|
let t = Tween.progress(fixed(1), fixed(2))
|
||||||
|
let e = Tween.ease(t, 4) # ease-in-back
|
||||||
|
let x = Tween.round(0, 200, e)
|
||||||
|
print(x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
30
docs/language/tween/tween-loop.md
Normal file
30
docs/language/tween/tween-loop.md
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
---
|
||||||
|
id: tween-loop
|
||||||
|
name: Tween.loop
|
||||||
|
category: tween
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Tween.loop
|
||||||
|
sig: Tween.loop(timer, duration) -> fixed
|
||||||
|
tip: A repeating 0..1 sawtooth amount over duration.
|
||||||
|
order: 2
|
||||||
|
ns: Tween
|
||||||
|
member: loop
|
||||||
|
---
|
||||||
|
|
||||||
|
Returns the fractional part of <code>timer / duration</code> — a sawtooth that ramps <code>0.0</code>→<code>1.0</code> then jumps back to <code>0.0</code> and repeats. Use it for a continuously cycling value: a scrolling offset, a rotating hue, a repeating pulse.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `timer` — elapsed seconds (a `fixed`)
|
||||||
|
- `duration` — seconds per cycle (a `fixed`)
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
property Spin { timer: fixed = 0.0 }
|
||||||
|
model Coin { Spin }
|
||||||
|
handler Run phase Update {
|
||||||
|
Spin.timer = Spin.timer + Time.delta()
|
||||||
|
let t = Tween.loop(Spin.timer, fixed(2)) # a 2-second cycle
|
||||||
|
print(t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
29
docs/language/tween/tween-number.md
Normal file
29
docs/language/tween/tween-number.md
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
---
|
||||||
|
id: tween-number
|
||||||
|
name: Tween.number
|
||||||
|
category: tween
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Tween.number
|
||||||
|
sig: Tween.number(from, to, t) -> fixed
|
||||||
|
tip: Linear blend of two fixeds by amount t.
|
||||||
|
order: 6
|
||||||
|
ns: Tween
|
||||||
|
member: number
|
||||||
|
---
|
||||||
|
|
||||||
|
Interpolates two <code>fixed</code> values: <code>from + (to - from) * t</code>. Feed it an eased <code>t</code> from <a href="tween-ease"><code>Tween.ease</code></a> to tween any fractional quantity — an alpha, a scale, a fixed-point position.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `from` — the value at `t = 0` (a `fixed`)
|
||||||
|
- `to` — the value at `t = 1` (a `fixed`)
|
||||||
|
- `t` — the blend amount (a `fixed`, usually 0..1)
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
handler Run phase Update {
|
||||||
|
let t = Tween.progress(fixed(1), fixed(2))
|
||||||
|
let scale = Tween.number(fixed(1), fixed(2), Tween.ease(t, 2))
|
||||||
|
print(scale)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
31
docs/language/tween/tween-point.md
Normal file
31
docs/language/tween/tween-point.md
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
---
|
||||||
|
id: tween-point
|
||||||
|
name: Tween.point
|
||||||
|
category: tween
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Tween.point
|
||||||
|
sig: Tween.point(from, to, t) -> Vector
|
||||||
|
tip: Component-wise blend of two Vectors by amount t.
|
||||||
|
order: 8
|
||||||
|
ns: Tween
|
||||||
|
member: point
|
||||||
|
---
|
||||||
|
|
||||||
|
Interpolates two <a href="vector"><code>Vector</code></a> endpoints component-wise by amount <code>t</code> — the 2D version of <a href="tween-number"><code>Tween.number</code></a>. Move an entity along a straight path, or ease a camera toward a target, in one call.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `from` — the point at `t = 0` (a `Vector`)
|
||||||
|
- `to` — the point at `t = 1` (a `Vector`)
|
||||||
|
- `t` — the blend amount (a `fixed`, usually 0..1)
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
handler Run phase Update {
|
||||||
|
let a = Vector.make(fixed(0), fixed(0))
|
||||||
|
let b = Vector.make(fixed(100), fixed(40))
|
||||||
|
let t = Tween.progress(fixed(1), fixed(2))
|
||||||
|
let p = Tween.point(a, b, Tween.ease(t, 3))
|
||||||
|
print(Vector.x(p))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
31
docs/language/tween/tween-progress.md
Normal file
31
docs/language/tween/tween-progress.md
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
---
|
||||||
|
id: tween-progress
|
||||||
|
name: Tween.progress
|
||||||
|
category: tween
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Tween.progress
|
||||||
|
sig: Tween.progress(timer, duration) -> fixed
|
||||||
|
tip: A one-shot 0..1 amount, clamped, for timer over duration.
|
||||||
|
order: 1
|
||||||
|
ns: Tween
|
||||||
|
member: progress
|
||||||
|
---
|
||||||
|
|
||||||
|
Returns <code>timer / duration</code> clamped to <code>0.0</code>..<code>1.0</code> — the normalized progress of a one-shot tween. Feed it to <a href="tween-ease"><code>Tween.ease</code></a> to shape it, then to a typed blend. Once <code>timer</code> reaches <code>duration</code> it pins at <code>1.0</code> (a non-positive <code>duration</code> also reads as done).
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `timer` — elapsed seconds (a `fixed`)
|
||||||
|
- `duration` — the tween's length in seconds (a `fixed`)
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
property Fade { timer: fixed = 0.0 }
|
||||||
|
model Panel { Fade }
|
||||||
|
handler Run phase Update {
|
||||||
|
Fade.timer = Fade.timer + Time.delta()
|
||||||
|
let t = Tween.progress(Fade.timer, fixed(1))
|
||||||
|
let a = Tween.round(0, 255, Tween.ease(t, 2))
|
||||||
|
print(a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
32
docs/language/tween/tween-round.md
Normal file
32
docs/language/tween/tween-round.md
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
---
|
||||||
|
id: tween-round
|
||||||
|
name: Tween.round
|
||||||
|
category: tween
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Tween.round
|
||||||
|
sig: Tween.round(from, to, t) -> int
|
||||||
|
tip: Linear blend of two ints by amount t, rounded to the nearest int.
|
||||||
|
order: 7
|
||||||
|
ns: Tween
|
||||||
|
member: round
|
||||||
|
---
|
||||||
|
|
||||||
|
Interpolates two <code>int</code> endpoints by amount <code>t</code> and rounds to the nearest integer (half up): <code>from + round((to - from) * t)</code>. The go-to for tweening a pixel position, a channel byte, or any integer field. Combine with <a href="tween-ease"><code>Tween.ease</code></a> for shaped motion.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `from` — the value at `t = 0` (an `int`)
|
||||||
|
- `to` — the value at `t = 1` (an `int`)
|
||||||
|
- `t` — the blend amount (a `fixed`, usually 0..1)
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
property Slide { timer: fixed = 0.0 }
|
||||||
|
model Card { Slide }
|
||||||
|
handler Run phase Update {
|
||||||
|
Slide.timer = Slide.timer + Time.delta()
|
||||||
|
let t = Tween.progress(Slide.timer, fixed(1))
|
||||||
|
let x = Tween.round(0, 320, Tween.ease(t, 2))
|
||||||
|
print(x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
29
docs/language/tween/tween-tint.md
Normal file
29
docs/language/tween/tween-tint.md
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
---
|
||||||
|
id: tween-tint
|
||||||
|
name: Tween.tint
|
||||||
|
category: tween
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Tween.tint
|
||||||
|
sig: Tween.tint(from, to, t) -> int
|
||||||
|
tip: Per-channel blend of two colors by amount t.
|
||||||
|
order: 9
|
||||||
|
ns: Tween
|
||||||
|
member: tint
|
||||||
|
---
|
||||||
|
|
||||||
|
Blends two colors (<code>0x00RRGGBB</code> ints) channel by channel by amount <code>t</code> — the color equivalent of <a href="tween-number"><code>Tween.number</code></a>. Fade a sprite to white on hit, cross-fade a sky, or pulse a UI accent. Matches <a href="color-lerp"><code>Color.lerp</code></a>, ready for an eased <code>t</code> from <a href="tween-ease"><code>Tween.ease</code></a>.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `from` — the color at `t = 0` (an `int`, `0x00RRGGBB`)
|
||||||
|
- `to` — the color at `t = 1` (an `int`, `0x00RRGGBB`)
|
||||||
|
- `t` — the blend amount (a `fixed`, usually 0..1)
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
handler Run phase Update {
|
||||||
|
let t = Tween.progress(fixed(1), fixed(2))
|
||||||
|
let c = Tween.tint(Color.rgb(0, 0, 0), Color.rgb(255, 255, 255), t)
|
||||||
|
print(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
31
docs/language/tween/tween-yoyo.md
Normal file
31
docs/language/tween/tween-yoyo.md
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
---
|
||||||
|
id: tween-yoyo
|
||||||
|
name: Tween.yoyo
|
||||||
|
category: tween
|
||||||
|
kind: namespace-method
|
||||||
|
tokens: Tween.yoyo
|
||||||
|
sig: Tween.yoyo(timer, duration) -> fixed
|
||||||
|
tip: A repeating 0..1..0 triangle amount over duration.
|
||||||
|
order: 3
|
||||||
|
ns: Tween
|
||||||
|
member: yoyo
|
||||||
|
---
|
||||||
|
|
||||||
|
Returns a triangle wave: <code>timer / duration</code> ramps <code>0.0</code>→<code>1.0</code> over the first <code>duration</code>, then <code>1.0</code>→<code>0.0</code> over the next, and repeats. The smooth back-and-forth for a hover bob, a pulsing highlight, or a breathing scale — no snap at the ends the way <a href="tween-loop"><code>Tween.loop</code></a> has.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `timer` — elapsed seconds (a `fixed`)
|
||||||
|
- `duration` — seconds for each leg (a `fixed`)
|
||||||
|
|
||||||
|
```ludic
|
||||||
|
program Demo {
|
||||||
|
property Bob { timer: fixed = 0.0 }
|
||||||
|
model Pickup { Bob }
|
||||||
|
handler Run phase Update {
|
||||||
|
Bob.timer = Bob.timer + Time.delta()
|
||||||
|
let t = Tween.yoyo(Bob.timer, fixed(1))
|
||||||
|
let y = Tween.round(100, 108, Tween.ease(t, 3))
|
||||||
|
print(y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
87
examples/library/anim.ludic
Normal file
87
examples/library/anim.ludic
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
# anim.ludic — Anim.* spritesheet frame animation and Tween.* value tweening,
|
||||||
|
# both pure and deterministic off the fixed frame clock. Each assertion that
|
||||||
|
# holds prints its number, so a full run prints:
|
||||||
|
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
||||||
|
# A `timer` is elapsed seconds as a fixed; fps / frame counts are ints; tween
|
||||||
|
# amounts are a fixed in 0..1. Timers are exact-in-fixed so floor(timer*fps)
|
||||||
|
# never lands ambiguously on a frame boundary.
|
||||||
|
program Anim {
|
||||||
|
property Tag { v: int = 0 }
|
||||||
|
model Marker { Tag }
|
||||||
|
|
||||||
|
handler Boot phase Start {
|
||||||
|
let F0 = fixed(0)
|
||||||
|
let F1 = fixed(1)
|
||||||
|
let HALF = fixed(1) / fixed(2) # 0.5
|
||||||
|
let QTR = fixed(1) / fixed(4) # 0.25
|
||||||
|
let E18 = fixed(1) / fixed(8) # 0.125
|
||||||
|
let E78 = fixed(7) / fixed(8) # 0.875
|
||||||
|
let F125 = fixed(5) / fixed(4) # 1.25
|
||||||
|
let F15 = fixed(3) / fixed(2) # 1.5
|
||||||
|
let TWO = fixed(2)
|
||||||
|
let FOUR = fixed(4)
|
||||||
|
let WHITE = 16777215 # 0xFFFFFF
|
||||||
|
let MID = 8355711 # 0x7F7F7F
|
||||||
|
|
||||||
|
# --- Anim.frame: looping frame index at 8 fps over a 4-frame clip ---
|
||||||
|
if Anim.frame(F0, 8, 4) == 0 { print(1) } # elapsed 0
|
||||||
|
if Anim.frame(QTR, 8, 4) == 2 { print(2) } # elapsed 2
|
||||||
|
if Anim.frame(HALF, 8, 4) == 0 { print(3) } # elapsed 4 -> 4%4
|
||||||
|
if Anim.frame(E78, 8, 4) == 3 { print(4) } # elapsed 7 -> 7%4
|
||||||
|
|
||||||
|
# --- Anim.once: clamped one-shot (stops on the last frame) ---
|
||||||
|
if Anim.once(HALF, 8, 4) == 3 { print(5) } # elapsed 4 -> min(4,3)
|
||||||
|
if Anim.once(QTR, 8, 4) == 2 { print(6) } # elapsed 2
|
||||||
|
|
||||||
|
# --- Anim.pingpong: bounce 0..3..0 ---
|
||||||
|
if Anim.pingpong(HALF, 8, 4) == 2 { print(7) } # elapsed 4 -> 6-4
|
||||||
|
if Anim.pingpong(E18, 8, 4) == 1 { print(8) } # elapsed 1
|
||||||
|
|
||||||
|
# --- Anim.finished: has a one-shot run past its last frame? ---
|
||||||
|
if Anim.finished(HALF, 8, 4) { print(9) } # elapsed 4 >= 4
|
||||||
|
if not Anim.finished(QTR, 8, 4) { print(10) } # elapsed 2 < 4
|
||||||
|
|
||||||
|
# --- Anim.duration + spritesheet cell geometry ---
|
||||||
|
if Anim.duration(8, 4) == HALF { print(11) } # 4/8 = 0.5s
|
||||||
|
if Anim.cell_x(5, 4, 16) == 16 { print(12) } # (5%4)*16
|
||||||
|
if Anim.cell_y(5, 4, 16) == 16 { print(13) } # (5/4)*16
|
||||||
|
if Anim.cell_x(6, 4, 16) == 32 { print(14) } # (6%4)*16
|
||||||
|
|
||||||
|
# --- Tween.progress: clamped 0..1 over a duration ---
|
||||||
|
if Tween.progress(QTR, F1) == QTR { print(15) } # 0.25/1
|
||||||
|
if Tween.progress(TWO, F1) == F1 { print(16) } # 2/1 -> clamp 1
|
||||||
|
if Tween.progress(F0 - F1, F1) == F0 { print(17) } # -1 -> clamp 0
|
||||||
|
|
||||||
|
# --- Tween.done ---
|
||||||
|
if Tween.done(F1, F1) { print(18) }
|
||||||
|
if not Tween.done(HALF, F1) { print(19) }
|
||||||
|
|
||||||
|
# --- Tween.loop / Tween.yoyo ---
|
||||||
|
if Tween.loop(F125, F1) == QTR { print(20) } # frac(1.25)
|
||||||
|
if Tween.yoyo(HALF, F1) == HALF { print(21) } # up-leg
|
||||||
|
if Tween.yoyo(F15, F1) == HALF { print(22) } # down-leg 2-1.5
|
||||||
|
|
||||||
|
# --- Tween.number / Tween.round scalar interpolation ---
|
||||||
|
if Tween.number(F0, FOUR, HALF) == TWO { print(23) } # lerp fixed
|
||||||
|
if Tween.round(0, 10, HALF) == 5 { print(24) } # round(5.0)
|
||||||
|
if Tween.round(0, 10, QTR) == 3 { print(25) } # round(2.5) half-up
|
||||||
|
|
||||||
|
# --- Tween.tint: per-channel color blend ---
|
||||||
|
if Tween.tint(0, WHITE, F0) == 0 { print(26) }
|
||||||
|
if Tween.tint(0, WHITE, F1) == WHITE { print(27) }
|
||||||
|
if Tween.tint(0, WHITE, HALF) == MID { print(28) } # 0x7F7F7F
|
||||||
|
|
||||||
|
# --- Tween.point: 2D vector interpolation ---
|
||||||
|
let p = Tween.point(Vector.make(F0, F0), Vector.make(FOUR, fixed(8)), HALF)
|
||||||
|
if Vector.x(p) == TWO { print(29) }
|
||||||
|
if Vector.y(p) == FOUR { print(30) }
|
||||||
|
|
||||||
|
# --- Tween.ease: curve selection, and consistency with Ease.* ---
|
||||||
|
if Tween.ease(HALF, 0) == HALF { print(31) } # 0 = linear
|
||||||
|
if Tween.ease(HALF, 1) == QTR { print(32) } # 1 = ease-in: 0.5^2
|
||||||
|
if Tween.ease(HALF, 1) == Ease.in(HALF) { print(33) } # same curve as Ease.in
|
||||||
|
if Tween.ease(F1, 2) == F1 { print(34) } # 2 = ease-out at 1.0
|
||||||
|
}
|
||||||
|
|
||||||
|
handler Run phase Update { quit() }
|
||||||
|
}
|
||||||
|
|
@ -23,6 +23,14 @@ function emit_ns_call(ns: pointer, meth: pointer, e: Node) -> Val {
|
||||||
if is_ease_ns(meth) { return emit_ease_ns(meth, e) }
|
if is_ease_ns(meth) { return emit_ease_ns(meth, e) }
|
||||||
perr(`unknown builtin Ease.{meth}`)
|
perr(`unknown builtin Ease.{meth}`)
|
||||||
}
|
}
|
||||||
|
if (ns == "Anim") {
|
||||||
|
if is_anim_ns(meth) { return emit_anim_ns(meth, e) }
|
||||||
|
perr(`unknown builtin Anim.{meth}`)
|
||||||
|
}
|
||||||
|
if (ns == "Tween") {
|
||||||
|
if is_tween_ns(meth) { return emit_tween_ns(meth, e) }
|
||||||
|
perr(`unknown builtin Tween.{meth}`)
|
||||||
|
}
|
||||||
if (ns == "Collision") {
|
if (ns == "Collision") {
|
||||||
if is_collide_ns(meth) { return emit_collide_ns(meth, e) }
|
if is_collide_ns(meth) { return emit_collide_ns(meth, e) }
|
||||||
perr(`unknown builtin Collision.{meth}`)
|
perr(`unknown builtin Collision.{meth}`)
|
||||||
|
|
|
||||||
174
selfhost/backend/stdlib/emit_anim.ludic
Normal file
174
selfhost/backend/stdlib/emit_anim.ludic
Normal file
|
|
@ -0,0 +1,174 @@
|
||||||
|
# emit_anim.ludic — 2D animation: the Anim.* (spritesheet frame animation) and
|
||||||
|
# Tween.* (value interpolation over a timeline) namespaces. Both are pure,
|
||||||
|
# deterministic Q16.16 / integer math driven off the game's fixed frame clock —
|
||||||
|
# the game stores a timer in a component and calls these each frame, exactly the
|
||||||
|
# way Collision.* / Grid.* are used. Same inputs -> same frame and same eased
|
||||||
|
# value on every run, so replays and lockstep netcode reproduce motion exactly.
|
||||||
|
#
|
||||||
|
# Conventions: a `timer` is elapsed seconds as a fixed (Q16.16); `fps` and frame
|
||||||
|
# counts are plain ints; interpolation amounts `t` are a fixed in 0.0..1.0.
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------ Anim.* ----
|
||||||
|
# spritesheet frame animation: turn an elapsed timer into the frame index to
|
||||||
|
# draw. floor(timer * fps) is the number of whole frames elapsed; the flavour
|
||||||
|
# (loop / once / ping-pong) decides how that maps back into 0..count-1.
|
||||||
|
|
||||||
|
function is_anim_ns(meth: pointer) -> bool {
|
||||||
|
if (meth == "frame") or (meth == "once") or (meth == "pingpong") { return true }
|
||||||
|
if (meth == "finished") or (meth == "duration") { return true }
|
||||||
|
if (meth == "cell_x") or (meth == "cell_y") { return true }
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
# floor(timer * fps) -> i32 code of whole frames elapsed (i64 intermediate so a
|
||||||
|
# long-running timer can't overflow the multiply).
|
||||||
|
function anim_elapsed(timer: pointer, fps: pointer) -> pointer {
|
||||||
|
let t64 = emit_bind(`sext i32 {timer} to i64`)
|
||||||
|
let f64 = emit_bind(`sext i32 {fps} to i64`)
|
||||||
|
let m = emit_bind(`mul i64 {t64}, {f64}`) # Q16.16 frames
|
||||||
|
let sh = emit_bind(`ashr i64 {m}, 16`)
|
||||||
|
return emit_bind(`trunc i64 {sh} to i32`)
|
||||||
|
}
|
||||||
|
|
||||||
|
# max(1, v) — guard a divisor / modulus against a zero or negative count.
|
||||||
|
function anim_atleast1(v: pointer) -> pointer {
|
||||||
|
let c = emit_bind(`icmp slt i32 {v}, 1`)
|
||||||
|
return emit_bind(`select i1 {c}, i32 1, i32 {v}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
function emit_anim_ns(meth: pointer, e: Node) -> Val {
|
||||||
|
if (meth == "frame") { # looping frame: elapsed mod count
|
||||||
|
let timer = emit_expr(e.kids[0]); let fps = emit_expr(e.kids[1]); let count = emit_expr(e.kids[2])
|
||||||
|
let el = anim_elapsed(timer.code, fps.code)
|
||||||
|
let cnt = anim_atleast1(count.code)
|
||||||
|
return val(emit_bind(`srem i32 {el}, {cnt}`), "int")
|
||||||
|
}
|
||||||
|
if (meth == "once") { # one-shot: min(elapsed, count-1)
|
||||||
|
let timer = emit_expr(e.kids[0]); let fps = emit_expr(e.kids[1]); let count = emit_expr(e.kids[2])
|
||||||
|
let el = anim_elapsed(timer.code, fps.code)
|
||||||
|
let last = emit_bind(`sub i32 {count.code}, 1`)
|
||||||
|
let c = emit_bind(`icmp slt i32 {el}, {last}`)
|
||||||
|
return val(emit_bind(`select i1 {c}, i32 {el}, i32 {last}`), "int")
|
||||||
|
}
|
||||||
|
if (meth == "pingpong") { # bounce 0..count-1..0
|
||||||
|
let timer = emit_expr(e.kids[0]); let fps = emit_expr(e.kids[1]); let count = emit_expr(e.kids[2])
|
||||||
|
let el = anim_elapsed(timer.code, fps.code)
|
||||||
|
let two = emit_bind(`mul i32 {count.code}, 2`)
|
||||||
|
let p2 = emit_bind(`sub i32 {two}, 2`) # 2*count - 2
|
||||||
|
let period = anim_atleast1(p2)
|
||||||
|
let m = emit_bind(`srem i32 {el}, {period}`)
|
||||||
|
let back = emit_bind(`sub i32 {period}, {m}`)
|
||||||
|
let c = emit_bind(`icmp slt i32 {m}, {count.code}`)
|
||||||
|
return val(emit_bind(`select i1 {c}, i32 {m}, i32 {back}`), "int")
|
||||||
|
}
|
||||||
|
if (meth == "finished") { # has a one-shot run past its last frame?
|
||||||
|
let timer = emit_expr(e.kids[0]); let fps = emit_expr(e.kids[1]); let count = emit_expr(e.kids[2])
|
||||||
|
let el = anim_elapsed(timer.code, fps.code)
|
||||||
|
let c = emit_bind(`icmp sge i32 {el}, {count.code}`)
|
||||||
|
return val(emit_bind(`zext i1 {c} to i32`), "bool")
|
||||||
|
}
|
||||||
|
if (meth == "duration") { # seconds for one cycle: count / fps -> fixed
|
||||||
|
let fps = emit_expr(e.kids[0]); let count = emit_expr(e.kids[1])
|
||||||
|
let num = emit_bind(`shl i32 {count.code}, 16`) # count as fixed
|
||||||
|
let den = anim_atleast1(fps.code)
|
||||||
|
return val(emit_bind(`sdiv i32 {num}, {den}`), "fixed")
|
||||||
|
}
|
||||||
|
if (meth == "cell_x") { # source x of a frame: (frame mod cols) * cell_w
|
||||||
|
let frame = emit_expr(e.kids[0]); let cols = emit_expr(e.kids[1]); let cw = emit_expr(e.kids[2])
|
||||||
|
let c1 = anim_atleast1(cols.code)
|
||||||
|
let col = emit_bind(`srem i32 {frame.code}, {c1}`)
|
||||||
|
return val(emit_bind(`mul i32 {col}, {cw.code}`), "int")
|
||||||
|
}
|
||||||
|
# cell_y — source y of a frame: (frame / cols) * cell_h
|
||||||
|
let frame = emit_expr(e.kids[0]); let cols = emit_expr(e.kids[1]); let ch = emit_expr(e.kids[2])
|
||||||
|
let c1 = anim_atleast1(cols.code)
|
||||||
|
let row = emit_bind(`sdiv i32 {frame.code}, {c1}`)
|
||||||
|
return val(emit_bind(`mul i32 {row}, {ch.code}`), "int")
|
||||||
|
}
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------- Tween.* ----
|
||||||
|
# value interpolation over a timeline. The timeline helpers turn (timer,
|
||||||
|
# duration) into a normalized amount with a chosen boundary behaviour; ease()
|
||||||
|
# shapes that amount through one of the engine's easing curves (shared with
|
||||||
|
# Ease.*); the typed interpolators blend two endpoints by an amount.
|
||||||
|
|
||||||
|
function is_tween_ns(meth: pointer) -> bool {
|
||||||
|
if (meth == "progress") or (meth == "loop") or (meth == "yoyo") or (meth == "done") { return true }
|
||||||
|
if (meth == "ease") or (meth == "number") or (meth == "round") { return true }
|
||||||
|
if (meth == "point") or (meth == "tint") { return true }
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
# a positive divisor for the timeline: duration if > 0, else 1.0 (65536).
|
||||||
|
function tween_den(dur: pointer) -> pointer {
|
||||||
|
let dpos = emit_bind(`icmp sgt i32 {dur}, 0`)
|
||||||
|
return emit_bind(`select i1 {dpos}, i32 {dur}, i32 65536`)
|
||||||
|
}
|
||||||
|
|
||||||
|
function emit_tween_ns(meth: pointer, e: Node) -> Val {
|
||||||
|
if (meth == "progress") { # clamp(timer / duration, 0, 1) -> fixed
|
||||||
|
let timer = emit_expr(e.kids[0]); let dur = emit_expr(e.kids[1])
|
||||||
|
let dpos = emit_bind(`icmp sgt i32 {dur.code}, 0`)
|
||||||
|
let den = emit_bind(`select i1 {dpos}, i32 {dur.code}, i32 65536`)
|
||||||
|
let r = fx_div_code(timer.code, den)
|
||||||
|
let neg = emit_bind(`icmp slt i32 {r}, 0`)
|
||||||
|
let lo = emit_bind(`select i1 {neg}, i32 0, i32 {r}`)
|
||||||
|
let over = emit_bind(`icmp sgt i32 {lo}, 65536`)
|
||||||
|
let r1 = emit_bind(`select i1 {over}, i32 65536, i32 {lo}`)
|
||||||
|
return val(emit_bind(`select i1 {dpos}, i32 {r1}, i32 65536`), "fixed") # dur<=0 -> done
|
||||||
|
}
|
||||||
|
if (meth == "loop") { # frac(timer / duration) in [0,1) -> fixed
|
||||||
|
let timer = emit_expr(e.kids[0]); let dur = emit_expr(e.kids[1])
|
||||||
|
let den = tween_den(dur.code)
|
||||||
|
let r = fx_div_code(timer.code, den)
|
||||||
|
return val(emit_bind(`and i32 {r}, 65535`), "fixed") # nonneg fractional part
|
||||||
|
}
|
||||||
|
if (meth == "yoyo") { # triangle 0..1..0 over the duration -> fixed
|
||||||
|
let timer = emit_expr(e.kids[0]); let dur = emit_expr(e.kids[1])
|
||||||
|
let den = tween_den(dur.code)
|
||||||
|
let r = fx_div_code(timer.code, den)
|
||||||
|
let u = emit_bind(`srem i32 {r}, 131072`) # mod 2.0
|
||||||
|
let back = emit_bind(`sub i32 131072, {u}`)
|
||||||
|
let c = emit_bind(`icmp sle i32 {u}, 65536`)
|
||||||
|
return val(emit_bind(`select i1 {c}, i32 {u}, i32 {back}`), "fixed")
|
||||||
|
}
|
||||||
|
if (meth == "done") { # timer >= duration -> bool
|
||||||
|
let timer = emit_expr(e.kids[0]); let dur = emit_expr(e.kids[1])
|
||||||
|
let c = emit_bind(`icmp sge i32 {timer.code}, {dur.code}`)
|
||||||
|
return val(emit_bind(`zext i1 {c} to i32`), "bool")
|
||||||
|
}
|
||||||
|
if (meth == "ease") { # ease(t, mode) -> fixed; mode is a literal 0..6
|
||||||
|
let t = emit_expr(e.kids[0])
|
||||||
|
let m = e.kids[1]
|
||||||
|
if (m.kind != E_INT) { perr("Tween.ease: the easing mode must be a literal int 0..6") }
|
||||||
|
return val(ease_eval(m.ival, t.code), "fixed")
|
||||||
|
}
|
||||||
|
if (meth == "number") { # lerp two fixeds by t -> fixed
|
||||||
|
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1]); let t = emit_expr(e.kids[2])
|
||||||
|
return val(fx_lerp_code(a.code, b.code, t.code), "fixed")
|
||||||
|
}
|
||||||
|
if (meth == "round") { # lerp two ints by t, rounded -> int
|
||||||
|
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1]); let t = emit_expr(e.kids[2])
|
||||||
|
let d = emit_bind(`sub i32 {b.code}, {a.code}`)
|
||||||
|
let d64 = emit_bind(`sext i32 {d} to i64`)
|
||||||
|
let t64 = emit_bind(`sext i32 {t.code} to i64`)
|
||||||
|
let p = emit_bind(`mul i64 {d64}, {t64}`) # Q16.16
|
||||||
|
let p2 = emit_bind(`add i64 {p}, 32768`) # + 0.5
|
||||||
|
let sh = emit_bind(`ashr i64 {p2}, 16`)
|
||||||
|
let dt = emit_bind(`trunc i64 {sh} to i32`)
|
||||||
|
return val(emit_bind(`add i32 {a.code}, {dt}`), "int")
|
||||||
|
}
|
||||||
|
if (meth == "point") { # lerp two Vectors by t -> Vector
|
||||||
|
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1]); let t = emit_expr(e.kids[2])
|
||||||
|
let ax = vec_x(a.code); let ay = vec_y(a.code); let bx = vec_x(b.code); let by = vec_y(b.code)
|
||||||
|
let lx = fx_lerp_code(ax, bx, t.code)
|
||||||
|
let ly = fx_lerp_code(ay, by, t.code)
|
||||||
|
return val(vec_pack(lx, ly), "Vector")
|
||||||
|
}
|
||||||
|
# tint — blend two colors (0x00RRGGBB) by t, per channel -> int
|
||||||
|
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1]); let t = emit_expr(e.kids[2])
|
||||||
|
let r = color_lerp_ch(color_ch(a.code, "16"), color_ch(b.code, "16"), t.code)
|
||||||
|
let g = color_lerp_ch(color_ch(a.code, "8"), color_ch(b.code, "8"), t.code)
|
||||||
|
let bl = color_lerp_ch(color_ch(a.code, "0"), color_ch(b.code, "0"), t.code)
|
||||||
|
return val(color_pack(r, g, bl), "int")
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,11 @@
|
||||||
# emit_ease.ludic — the Ease.* namespace: tween curves over a normalized amount
|
# emit_ease.ludic — the Ease.* namespace: tween curves over a normalized amount
|
||||||
# t in 0.0..1.0, returning an eased fixed. All pure Q16.16, deterministic. The
|
# t in 0.0..1.0, returning an eased fixed. All pure Q16.16, deterministic. The
|
||||||
# "juice" layer that makes motion feel good (Robert Penner's easings).
|
# "juice" layer that makes motion feel good (Robert Penner's easings).
|
||||||
|
#
|
||||||
|
# The curve math is factored into ease_eval(mode, t) so the Tween.* namespace
|
||||||
|
# (emit_anim.ludic) can pick a curve by a small integer mode and reuse the exact
|
||||||
|
# same formulas — one source of truth for every easing in the engine.
|
||||||
|
# 0 linear 1 in 2 out 3 in_out 4 back 5 elastic 6 bounce
|
||||||
|
|
||||||
function is_ease_ns(meth: pointer) -> bool {
|
function is_ease_ns(meth: pointer) -> bool {
|
||||||
if (meth == "in") or (meth == "out") or (meth == "in_out") { return true }
|
if (meth == "in") or (meth == "out") or (meth == "in_out") { return true }
|
||||||
|
|
@ -14,50 +19,69 @@ function ease_bounce_seg(u: pointer) -> pointer {
|
||||||
return fx_mul_code(uu, "495616") # 7.5625 * u*u
|
return fx_mul_code(uu, "495616") # 7.5625 * u*u
|
||||||
}
|
}
|
||||||
|
|
||||||
function emit_ease_ns(meth: pointer, e: Node) -> Val {
|
# ease-out bounce: four parabolic segments, selected by t (all computed, then
|
||||||
let t = emit_expr(e.kids[0])
|
# picked branch-free). Shifts/offsets are the standard 2.75-denominator set.
|
||||||
if (meth == "in") { # ease-in quad: t*t
|
function ease_bounce_code(t: pointer) -> pointer {
|
||||||
return val(fx_mul_code(t.code, t.code), "fixed")
|
let sA = ease_bounce_seg(t)
|
||||||
|
let uB = emit_bind(`sub i32 {t}, 35747`); let sB0 = ease_bounce_seg(uB); let sB = emit_bind(`add i32 {sB0}, 49152`)
|
||||||
|
let uC = emit_bind(`sub i32 {t}, 53620`); let sC0 = ease_bounce_seg(uC); let sC = emit_bind(`add i32 {sC0}, 61440`)
|
||||||
|
let uD = emit_bind(`sub i32 {t}, 62557`); let sD0 = ease_bounce_seg(uD); let sD = emit_bind(`add i32 {sD0}, 64512`)
|
||||||
|
let cCD = emit_bind(`icmp slt i32 {t}, 59578`)
|
||||||
|
let rCD = emit_bind(`select i1 {cCD}, i32 {sC}, i32 {sD}`)
|
||||||
|
let cB = emit_bind(`icmp slt i32 {t}, 47663`)
|
||||||
|
let rB = emit_bind(`select i1 {cB}, i32 {sB}, i32 {rCD}`)
|
||||||
|
let cA = emit_bind(`icmp slt i32 {t}, 23831`)
|
||||||
|
return emit_bind(`select i1 {cA}, i32 {sA}, i32 {rB}`)
|
||||||
}
|
}
|
||||||
if (meth == "out") { # ease-out quad: t*(2 - t)
|
|
||||||
let inv = emit_bind(`sub i32 131072, {t.code}`)
|
# evaluate easing `mode` at normalized amount `t` (a fixed code) -> fixed code.
|
||||||
return val(fx_mul_code(t.code, inv), "fixed")
|
# The single source of truth for the engine's easing curves.
|
||||||
|
function ease_eval(mode: int, t: pointer) -> pointer {
|
||||||
|
if (mode == 0) { # linear: t
|
||||||
|
return t
|
||||||
}
|
}
|
||||||
if (meth == "in_out") { # smooth ease-in-out: 3t^2 - 2t^3
|
if (mode == 1) { # ease-in quad: t*t
|
||||||
let t2 = fx_mul_code(t.code, t.code)
|
return fx_mul_code(t, t)
|
||||||
let t3 = fx_mul_code(t2, t.code)
|
}
|
||||||
|
if (mode == 2) { # ease-out quad: t*(2 - t)
|
||||||
|
let inv = emit_bind(`sub i32 131072, {t}`)
|
||||||
|
return fx_mul_code(t, inv)
|
||||||
|
}
|
||||||
|
if (mode == 3) { # smooth ease-in-out: 3t^2 - 2t^3
|
||||||
|
let t2 = fx_mul_code(t, t)
|
||||||
|
let t3 = fx_mul_code(t2, t)
|
||||||
let three = emit_bind(`mul i32 {t2}, 3`)
|
let three = emit_bind(`mul i32 {t2}, 3`)
|
||||||
let two = emit_bind(`mul i32 {t3}, 2`)
|
let two = emit_bind(`mul i32 {t3}, 2`)
|
||||||
return val(emit_bind(`sub i32 {three}, {two}`), "fixed")
|
return emit_bind(`sub i32 {three}, {two}`)
|
||||||
}
|
}
|
||||||
if (meth == "back") { # ease-in-back (overshoots below 0)
|
if (mode == 4) { # ease-in-back (overshoots below 0)
|
||||||
let t2 = fx_mul_code(t.code, t.code)
|
let t2 = fx_mul_code(t, t)
|
||||||
let t3 = fx_mul_code(t2, t.code)
|
let t3 = fx_mul_code(t2, t)
|
||||||
let a = fx_mul_code(t3, "177051") # 2.70158 * t^3
|
let a = fx_mul_code(t3, "177051") # 2.70158 * t^3
|
||||||
let b = fx_mul_code(t2, "111515") # 1.70158 * t^2
|
let b = fx_mul_code(t2, "111515") # 1.70158 * t^2
|
||||||
return val(emit_bind(`sub i32 {a}, {b}`), "fixed")
|
return emit_bind(`sub i32 {a}, {b}`)
|
||||||
}
|
}
|
||||||
if (meth == "elastic") { # ease-out elastic: springy overshoot that settles
|
if (mode == 5) { # ease-out elastic: springy overshoot that settles
|
||||||
g_uses_mathrt = true # 2^(-10t) * sin((10t - 0.75) * 2pi/3) + 1
|
g_uses_mathrt = true # 2^(-10t) * sin((10t - 0.75) * 2pi/3) + 1
|
||||||
let tt = emit_bind(`mul i32 {t.code}, 10`) # 10t
|
let tt = emit_bind(`mul i32 {t}, 10`) # 10t
|
||||||
let ntt = emit_bind(`sub i32 0, {tt}`) # -10t (exp2 exponent, Q16.16)
|
let ntt = emit_bind(`sub i32 0, {tt}`) # -10t (exp2 exponent, Q16.16)
|
||||||
let decay = emit_bind(`call i32 @fn_fx_exp2(i32 {ntt})`)
|
let decay = emit_bind(`call i32 @fn_fx_exp2(i32 {ntt})`)
|
||||||
let ph = emit_bind(`sub i32 {tt}, 49152`) # 10t - 0.75
|
let ph = emit_bind(`sub i32 {tt}, 49152`) # 10t - 0.75
|
||||||
let ang = fx_mul_code(ph, "137258") # * (2pi/3), 2pi/3 = 137258 fixed
|
let ang = fx_mul_code(ph, "137258") # * (2pi/3), 2pi/3 = 137258 fixed
|
||||||
let s = emit_bind(`call i32 @fn_fx_sin(i32 {ang})`)
|
let s = emit_bind(`call i32 @fn_fx_sin(i32 {ang})`)
|
||||||
let osc = fx_mul_code(decay, s)
|
let osc = fx_mul_code(decay, s)
|
||||||
return val(emit_bind(`add i32 {osc}, 65536`), "fixed")
|
return emit_bind(`add i32 {osc}, 65536`)
|
||||||
}
|
}
|
||||||
# ease-out bounce: four parabolic segments, selected by t (all computed, then
|
# mode == 6 — ease-out bounce
|
||||||
# picked branch-free). Shifts/offsets are the standard 2.75-denominator set.
|
return ease_bounce_code(t)
|
||||||
let sA = ease_bounce_seg(t.code)
|
}
|
||||||
let uB = emit_bind(`sub i32 {t.code}, 35747`); let sB0 = ease_bounce_seg(uB); let sB = emit_bind(`add i32 {sB0}, 49152`)
|
|
||||||
let uC = emit_bind(`sub i32 {t.code}, 53620`); let sC0 = ease_bounce_seg(uC); let sC = emit_bind(`add i32 {sC0}, 61440`)
|
function emit_ease_ns(meth: pointer, e: Node) -> Val {
|
||||||
let uD = emit_bind(`sub i32 {t.code}, 62557`); let sD0 = ease_bounce_seg(uD); let sD = emit_bind(`add i32 {sD0}, 64512`)
|
let t = emit_expr(e.kids[0])
|
||||||
let cCD = emit_bind(`icmp slt i32 {t.code}, 59578`)
|
if (meth == "in") { return val(ease_eval(1, t.code), "fixed") }
|
||||||
let rCD = emit_bind(`select i1 {cCD}, i32 {sC}, i32 {sD}`)
|
if (meth == "out") { return val(ease_eval(2, t.code), "fixed") }
|
||||||
let cB = emit_bind(`icmp slt i32 {t.code}, 47663`)
|
if (meth == "in_out") { return val(ease_eval(3, t.code), "fixed") }
|
||||||
let rB = emit_bind(`select i1 {cB}, i32 {sB}, i32 {rCD}`)
|
if (meth == "back") { return val(ease_eval(4, t.code), "fixed") }
|
||||||
let cA = emit_bind(`icmp slt i32 {t.code}, 23831`)
|
if (meth == "elastic"){ return val(ease_eval(5, t.code), "fixed") }
|
||||||
return val(emit_bind(`select i1 {cA}, i32 {sA}, i32 {rB}`), "fixed")
|
return val(ease_eval(6, t.code), "fixed") # bounce
|
||||||
}
|
}
|
||||||
|
|
|
||||||
21431
selfhost/ludicc.seed.ll
21431
selfhost/ludicc.seed.ll
File diff suppressed because it is too large
Load diff
|
|
@ -38,6 +38,7 @@ function selfhost_frags() -> []pointer {
|
||||||
push(f, "selfhost/backend/stdlib/emit_fs.ludic")
|
push(f, "selfhost/backend/stdlib/emit_fs.ludic")
|
||||||
push(f, "selfhost/backend/stdlib/emit_list.ludic")
|
push(f, "selfhost/backend/stdlib/emit_list.ludic")
|
||||||
push(f, "selfhost/backend/stdlib/emit_ease.ludic")
|
push(f, "selfhost/backend/stdlib/emit_ease.ludic")
|
||||||
|
push(f, "selfhost/backend/stdlib/emit_anim.ludic")
|
||||||
push(f, "selfhost/backend/game/emit_collide.ludic")
|
push(f, "selfhost/backend/game/emit_collide.ludic")
|
||||||
push(f, "selfhost/backend/emit_mem.ludic")
|
push(f, "selfhost/backend/emit_mem.ludic")
|
||||||
push(f, "selfhost/backend/stdlib/emit_time.ludic")
|
push(f, "selfhost/backend/stdlib/emit_time.ludic")
|
||||||
|
|
|
||||||
|
|
@ -104,6 +104,7 @@ function cmd_test() -> int {
|
||||||
feat_case("library/noise", "", "1 2 3 4 5 6 7 8 9 10 11", "noise.ludic (Noise value/perlin/simplex/fbm/cellular determinism + range)")
|
feat_case("library/noise", "", "1 2 3 4 5 6 7 8 9 10 11", "noise.ludic (Noise value/perlin/simplex/fbm/cellular determinism + range)")
|
||||||
feat_case("library/regex", "", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18", "regex.ludic (Regex match/find/groups/classes/quantifiers/replace + linear-time safety)")
|
feat_case("library/regex", "", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18", "regex.ludic (Regex match/find/groups/classes/quantifiers/replace + linear-time safety)")
|
||||||
feat_case("library/grid", "", "1 2 3 4 5 6 7 8 9 10 11 12 13", "grid.ludic (Grid line/flood/line_of_sight + A* pathfinding over the tilemap)")
|
feat_case("library/grid", "", "1 2 3 4 5 6 7 8 9 10 11 12 13", "grid.ludic (Grid line/flood/line_of_sight + A* pathfinding over the tilemap)")
|
||||||
|
feat_case("library/anim", "", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34", "anim.ludic (Anim frame/once/pingpong/cell + Tween progress/loop/yoyo/ease/number/round/point/tint)")
|
||||||
feat_case("library/logging", "", "0 5 2 1", "logging.ludic (Log levels, set_level/level threshold, structured fields)")
|
feat_case("library/logging", "", "0 5 2 1", "logging.ludic (Log levels, set_level/level threshold, structured fields)")
|
||||||
# Os known-folders/arch and Fs.list read the BSD utsname/dirent layout, so
|
# Os known-folders/arch and Fs.list read the BSD utsname/dirent layout, so
|
||||||
# their asserted values are macOS-specific; skip off Darwin (see is_darwin).
|
# their asserted values are macOS-specific; skip off Darwin (see is_darwin).
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue