summaryrefslogtreecommitdiff
path: root/src/libs/sig/ops_lerp.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2020-02-03 04:24:25 -0800
committerVito Caputo <vcaputo@pengaru.com>2020-02-03 04:27:38 -0800
commitdb99c38a5dfaab56e6255ed28cf6f7392c1a0a27 (patch)
tree2dbfd4b1984c39da1270170cf531baf4dd5aa0a5 /src/libs/sig/ops_lerp.c
parent503ac37cb590eb35f1891b81b2256b07a3f89592 (diff)
libs/sig: add sig_ops_lerp linear interpoplation
This takes three signals; a, b, and t t controls the weight interpolating between a and b, they all key off the same time ticks_ms.
Diffstat (limited to 'src/libs/sig/ops_lerp.c')
-rw-r--r--src/libs/sig/ops_lerp.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/libs/sig/ops_lerp.c b/src/libs/sig/ops_lerp.c
new file mode 100644
index 0000000..8002803
--- /dev/null
+++ b/src/libs/sig/ops_lerp.c
@@ -0,0 +1,68 @@
+#include <assert.h>
+
+#include "sig.h"
+
+
+typedef struct ops_lerp_ctxt_t {
+ sig_t *a, *b, *t;
+} ops_lerp_ctxt_t;
+
+
+static size_t ops_lerp_size(va_list ap)
+{
+ return sizeof(ops_lerp_ctxt_t);
+}
+
+
+/* supply two sig_t's to be multiplied, this sig_t takes
+ * ownership of them so they'll be freed by the multiplier
+ * on destroy when that sig_t is freed.
+ */
+static void ops_lerp_init(void *context, va_list ap)
+{
+ ops_lerp_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ ctxt->a = va_arg(ap, sig_t *);
+ ctxt->b = va_arg(ap, sig_t *);
+ ctxt->t = va_arg(ap, sig_t *);
+}
+
+
+static inline float lerp(float a, float b, float t)
+{
+ return (1.f - t) * a + b * t;
+}
+
+
+static float ops_lerp_output(void *context, unsigned ticks_ms)
+{
+ ops_lerp_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ return lerp(sig_output(ctxt->a, ticks_ms),
+ sig_output(ctxt->b, ticks_ms),
+ sig_output(ctxt->t, ticks_ms));
+}
+
+
+static void ops_lerp_destroy(void *context)
+{
+ ops_lerp_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ sig_free(ctxt->a);
+ sig_free(ctxt->b);
+ sig_free(ctxt->t);
+}
+
+
+sig_ops_t sig_ops_lerp = {
+ .size = ops_lerp_size,
+ .init = ops_lerp_init,
+ .destroy = ops_lerp_destroy,
+ .output = ops_lerp_output,
+};
© All Rights Reserved