ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
video/anim/controller.c
Go to the documentation of this file.
5#include <math.h>
6#include <string.h>
7
8#include "backends.h"
9
10static bool valid_config(animation_config_t c) {
11 return c.type >= ANIMATION_SPLASH_RAINBOW && c.type <= ANIMATION_TEST_PATTERN && c.fps > 0 && c.fps <= 1000000000 && isfinite(c.speed) && c.speed >= 0 &&
13}
14
16 if (!a || !name || !valid_config(config))
17 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid animation configuration");
18 *a = (animation_t){.config = config, .visible = true};
19 NAMED_REGISTER(a, name, "animation", NULL, NULL);
20 return ASCIICHAT_OK;
21}
22
24 if (!a)
25 return;
27 memset(a, 0, sizeof(*a));
28}
29
31 if (!a)
32 return;
33 a->seconds = 0;
34 a->started = false;
35 a->last_frame = 0;
36}
37void animation_set_paused(animation_t *a, bool paused) {
38 if (a)
39 a->paused = paused;
40}
41void animation_set_visible(animation_t *a, bool visible) {
42 if (a)
43 a->visible = visible;
44}
46 if (!a || !isfinite(speed) || speed < 0)
47 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid animation speed");
48 a->config.speed = speed;
49 return ASCIICHAT_OK;
50}
51
53 if (!out || !valid_config(config) || !isfinite(seconds) || seconds < 0 || seconds * config.fps >= (double)UINT64_MAX)
54 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid animation timestamp");
55 *out = (animation_sample_t){.type = config.type, .seconds = seconds, .frame = (uint64_t)(seconds * config.fps), .changed = true, .next_frame_ns = UINT64_MAX};
56 return ASCIICHAT_OK;
57}
58
60 if (!a || !out || !valid_config(a->config) || (a->started && now < a->last_ns))
61 return SET_ERRNO(ERROR_INVALID_PARAM, "Animation timestamps must be monotonic; reset before seeking");
62 bool running = !a->paused && a->config.speed > 0 && (a->visible || a->config.hidden_policy == ANIMATION_HIDDEN_CONTINUE);
63 double seconds = a->seconds;
64 if (a->started && running)
65 seconds += (double)(now - a->last_ns) / 1e9 * a->config.speed;
67 if (err != ASCIICHAT_OK)
68 return err;
69 out->changed = !a->started || out->frame != a->last_frame;
70 if (running) {
71 double remaining = ((double)out->frame + 1.0) / a->config.fps - seconds;
72 double delay = ceil(fmax(1.0, remaining * 1e9 / a->config.speed));
73 if (delay < (double)(UINT64_MAX - now))
74 out->next_frame_ns = now + (uint64_t)delay;
75 }
76 a->seconds = seconds;
77 a->last_ns = now;
78 a->last_frame = out->frame;
79 a->started = true;
80 return ASCIICHAT_OK;
81}
82
84 if (!s || !t || !isfinite(s->seconds) || s->seconds < 0)
85 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid animation sample or target");
86 switch (s->type) {
88 if (t->type != ANIMATION_TARGET_COLOR || !t->color.out || !isfinite(t->color.position))
89 break;
90 static const rgb_pixel_t colors[] = {{255, 0, 0}, {255, 165, 0}, {255, 255, 0}, {0, 255, 0}, {0, 255, 255}, {0, 0, 255}, {255, 0, 255}};
91 double position = fmod(t->color.position, 1.0) + fmod(s->seconds, 2.5) * 0.4;
92 double phase = (position - floor(position)) * 6;
93 int i = (int)phase;
94 double blend = phase - i;
95 rgb_pixel_t a = colors[i], b = colors[i + 1];
96 *t->color.out = (rgb_pixel_t){(uint8_t)(a.r * (1 - blend) + b.r * blend), (uint8_t)(a.g * (1 - blend) + b.g * blend), (uint8_t)(a.b * (1 - blend) + b.b * blend)};
97 return ASCIICHAT_OK;
98 }
100 if (t->type == ANIMATION_TARGET_COLOR && t->color.out) {
101 color_filter_calculate_rainbow((float)fmod(s->seconds, 3.5), &t->color.out->r, &t->color.out->g, &t->color.out->b);
102 return ASCIICHAT_OK;
103 }
104 if (t->type != ANIMATION_TARGET_ANSI || !t->ansi.input || !t->ansi.out)
105 break;
106 *t->ansi.out = rainbow_render_ansi_at(t->ansi.input, (float)fmod(s->seconds, 3.5));
107 return *t->ansi.out ? ASCIICHAT_OK : SET_ERRNO(ERROR_MEMORY, "Rainbow output allocation failed");
109 if (t->type != ANIMATION_TARGET_ANSI || !t->ansi.input || !t->ansi.out || !t->ansi.rain)
110 break;
111 *t->ansi.out = digital_rain_render_at(t->ansi.rain, t->ansi.input, (float)s->seconds);
112 return *t->ansi.out ? ASCIICHAT_OK : SET_ERRNO(ERROR_MEMORY, "Digital rain output allocation failed");
115 break;
117 }
118 return SET_ERRNO(ERROR_INVALID_PARAM, "Animation type does not support this target");
119}
#define SET_ERRNO(code,...)
char * rainbow_render_ansi_at(const char *frame, float seconds)
asciichat_error_t test_pattern_render_at(struct test_pattern *pattern, int index, double time_ms, bool cadence)
char * digital_rain_render_at(digital_rain_t *rain, const char *frame, float seconds)
Named object registry for debugging — log identifiable resource names.
Matrix-style digital rain effect for ASCII frames.
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
unsigned long long uint64_t
unsigned char uint8_t
#define NAMED_REGISTER(ptr, name, type, fmt, parent_ptr)
Register any pointer with base name, type, format spec, and location (auto-suffix).
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
void color_filter_calculate_rainbow(float time, uint8_t *r, uint8_t *g, uint8_t *b)
Calculate rainbow color from time (3.5s cycle).
🔢 Mathematical Utility Functions
static bool size_t gotel_query_t * out
Definition queries.h:10
🔤 String Manipulation and Shell Escaping Utilities
animation_hidden_policy_t hidden_policy
For animation config, this is the live record that ties type, fps, speed, hidden_policy together....
animation_config_t config
struct animation_target_t::@105141136260326341273057264343342151154253073027::@021005305237221242221055207105224353364212233103 test_pattern
struct animation_target_t::@105141136260326341273057264343342151154253073027::@072247246125252004245152136267001044326224304340 color
struct animation_target_t::@105141136260326341273057264343342151154253073027::@256117116162340230264137326272113315112047155330 ansi
struct test_pattern * context
struct digital_rain * rain
animation_target_type_t type
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).
void animation_set_paused(animation_t *a, bool paused)
void animation_destroy(animation_t *a)
asciichat_error_t animation_update(animation_t *a, uint64_t now, animation_sample_t *out)
void animation_reset(animation_t *a)
asciichat_error_t animation_init(animation_t *a, const char *name, animation_config_t config)
asciichat_error_t animation_set_speed(animation_t *a, double speed)
asciichat_error_t animation_apply(const animation_sample_t *s, animation_target_t *t)
asciichat_error_t animation_sample_at(animation_config_t config, double seconds, animation_sample_t *out)
void animation_set_visible(animation_t *a, bool visible)
@ ANIMATION_HIDDEN_PAUSE
@ ANIMATION_HIDDEN_CONTINUE
@ ANIMATION_RAINBOW_FILTER
@ ANIMATION_DIGITAL_RAIN
@ ANIMATION_TEST_PATTERN
@ ANIMATION_SPLASH_RAINBOW
@ ANIMATION_TARGET_COLOR
@ ANIMATION_TARGET_TEST_PATTERN
@ ANIMATION_TARGET_ANSI