RMGui 0.15.0
C11 retained-mode UI library: layout kernel, style schema, widget painters, fonts, software framebuffer
Loading...
Searching...
No Matches
motion.h File Reference

motion.h — values that change over time: easing curves, tweens, damped springs, a periodic wave, smooth noise and a seeded generator. More...

#include <stdint.h>
#include "geom.h"

Data Structures

struct  rmgui_motion_tween_t
 A running curve. More...
struct  rmgui_motion_map_t
 A scalar map: input / divisor is Q16 progress, truncated toward zero before mixing the endpoints. More...
struct  rmgui_motion_wave_t
 A wave's period (sine) or lattice cell (noise), in milliseconds. More...
struct  rmgui_motion_spring_t
 A damped spring pulling value toward target: acceleration = stiffness * (target - position) - damping * velocity, integrated in 1 ms semi-implicit Euler substeps on 2^-26 px state. More...
struct  rmgui_motion_impulse_t
 A normalised spring kick: units logical pixels per full amplitude, and a maximum absolute amplitude/accumulated velocity in Q16 full amplitudes. More...
struct  rmgui_motion_rng_t
 A seeded generator (xorshift32). More...

Macros

#define RMGUI_MOTION_ONE   65536
 1.0 in the 16.16 fixed point every _q16 quantity uses.

Enumerations

enum  rmgui_motion_ease_t {
  RMGUI_MOTION_EASE_LINEAR , RMGUI_MOTION_EASE_IN_QUAD , RMGUI_MOTION_EASE_OUT_QUAD , RMGUI_MOTION_EASE_IN_OUT_QUAD ,
  RMGUI_MOTION_EASE_OUT_BACK
}
 An easing curve: progress over normalised time p in [0, 1]. More...
enum  rmgui_motion_wave_kind_t { RMGUI_MOTION_WAVE_SINE , RMGUI_MOTION_WAVE_NOISE }
 Periodic sine or clamped, nonnegative-coordinate seeded value noise. More...

Functions

int32_t rmgui_motion_ease (rmgui_motion_ease_t ease, uint32_t elapsed_ms, uint32_t duration_ms)
 Progress of ease after elapsed_ms of duration_ms, Q16.
int32_t rmgui_motion_mix (int32_t from, int32_t to, int32_t progress_q16)
 from + floor(((to - from) * progress_q16 + ONE/2) / ONE), computed in 64 bits and saturated to int32.
rmgui_color_t rmgui_motion_mix_color (rmgui_color_t from, rmgui_color_t to, int32_t progress_q16)
 Each of r, g, b, a mixed as rmgui_motion_mix, progress clamped to [0, ONE].
int rmgui_motion_tween_step (rmgui_motion_tween_t *tween, uint16_t dt_ms)
 Advances elapsed_ms by dt_ms, saturating at duration_ms.
int rmgui_motion_tween_retime (rmgui_motion_tween_t *tween, uint32_t duration_ms)
 Retimes a running envelope, rounding elapsed * new / old to nearest.
int rmgui_motion_tween_phase_step (rmgui_motion_tween_t *tween, uint32_t *phase_ms, uint16_t dt_ms)
 Advances an envelope and adds only its elapsed advance to *phase_ms, saturating at UINT32_MAX.
int32_t rmgui_motion_map (const rmgui_motion_map_t *map, int32_t input)
 Evaluates a map with extrapolation and rmgui_motion_mix rounding.
int32_t rmgui_motion_wave (const rmgui_motion_wave_t *wave, const rmgui_motion_tween_t *envelope, uint32_t phase_ms, uint32_t seed, int32_t amplitude)
 Evaluates mix(0, mix(0, amplitude, wave(phase)), 1 - envelope progress).
int rmgui_motion_spring_init (rmgui_motion_spring_t *spring, int32_t value_q16, uint16_t stiffness, uint16_t damping)
 Puts spring at rest at value_q16 (target = value) with the given constants.
int rmgui_motion_spring_step (rmgui_motion_spring_t *spring, uint16_t dt_ms)
 Advances spring by dt_ms 1 ms substeps (see the type) and refreshes value_q16.
int rmgui_motion_spring_impulse (rmgui_motion_spring_t *spring, const rmgui_motion_impulse_t *impulse, int32_t amplitude_q16)
 Adds amplitude * floor(sqrt(stiffness) * ONE) to spring velocity, rounding half away from zero in spring substep units.
int32_t rmgui_motion_sine (int32_t turns_q16)
 sin(2*pi * turns_q16 / 65536) in Q16: a 257-entry quarter-wave table of round(sin(i*pi/512) * 65536), linearly interpolated over 64 steps.
int32_t rmgui_motion_noise (uint32_t seed, int32_t x_q16)
 Smooth 1-D value noise at x_q16 for seed, Q16 in [-ONE, ONE): a hashed lattice value at every integer x, joined by the smoothstep 3f^2 - 2f^3.
void rmgui_motion_rng_seed (rmgui_motion_rng_t *rng, uint32_t seed)
 Seeds rng; seed 0 maps to a fixed nonzero state.
uint32_t rmgui_motion_rng_next (rmgui_motion_rng_t *rng)
 The next 32 random bits, advancing rng (shifts 13, 17, 5).

Detailed Description

motion.h — values that change over time: easing curves, tweens, damped springs, a periodic wave, smooth noise and a seeded generator.

Each is a pure integer function of caller-owned state and a caller-supplied time step: no clock, no allocation, no global state, the same bytes on every target.

Fixed point: a _q16 quantity is signed 16.16 (RMGUI_MOTION_ONE is 1.0). A _turns_q16 angle counts 1/65536 of a turn, positive clockwise on the y-down framebuffer, and wraps modulo 65536. Time is integer milliseconds.

Step protocol: every *_step takes uint16_t dt_ms and returns 1 when it changed the state (still moving, or came to rest during this call) and 0 when it changed nothing. Stepping by a then by b leaves exactly the state stepping by a + b leaves.

Macro Definition Documentation

◆ RMGUI_MOTION_ONE

#define RMGUI_MOTION_ONE   65536

1.0 in the 16.16 fixed point every _q16 quantity uses.

Enumeration Type Documentation

◆ rmgui_motion_ease_t

An easing curve: progress over normalised time p in [0, 1].

Enumerator
RMGUI_MOTION_EASE_LINEAR 

p

RMGUI_MOTION_EASE_IN_QUAD 

p^2

RMGUI_MOTION_EASE_OUT_QUAD 

1 - (1-p)^2

RMGUI_MOTION_EASE_IN_OUT_QUAD 

2p^2 below 1/2, 1 - 2(1-p)^2 from 1/2

RMGUI_MOTION_EASE_OUT_BACK 

1 + c3(p-1)^3 + c1(p-1)^2, c1 = 111514/65536, c3 = c1 + 1

◆ rmgui_motion_wave_kind_t

Periodic sine or clamped, nonnegative-coordinate seeded value noise.

Enumerator
RMGUI_MOTION_WAVE_SINE 
RMGUI_MOTION_WAVE_NOISE 

Function Documentation

◆ rmgui_motion_ease()

int32_t rmgui_motion_ease ( rmgui_motion_ease_t ease,
uint32_t elapsed_ms,
uint32_t duration_ms )

Progress of ease after elapsed_ms of duration_ms, Q16.

With p = floor(min(elapsed, duration) * ONE / duration), each product in the curve's formula is floored to Q16. The result is 0 at elapsed 0, and exactly RMGUI_MOTION_ONE when elapsed >= duration or duration is 0. Every curve but OUT_BACK stays in [0, ONE] and is non-decreasing in elapsed_ms; OUT_BACK overshoots above ONE before it settles. An ease outside the enum returns RMGUI_MOTION_ONE.

◆ rmgui_motion_mix()

int32_t rmgui_motion_mix ( int32_t from,
int32_t to,
int32_t progress_q16 )

from + floor(((to - from) * progress_q16 + ONE/2) / ONE), computed in 64 bits and saturated to int32.

Progress 0 is from, ONE is to; progress outside [0, ONE] extrapolates.

◆ rmgui_motion_mix_color()

rmgui_color_t rmgui_motion_mix_color ( rmgui_color_t from,
rmgui_color_t to,
int32_t progress_q16 )

Each of r, g, b, a mixed as rmgui_motion_mix, progress clamped to [0, ONE].

◆ rmgui_motion_tween_step()

int rmgui_motion_tween_step ( rmgui_motion_tween_t * tween,
uint16_t dt_ms )

Advances elapsed_ms by dt_ms, saturating at duration_ms.

Returns 1 when elapsed_ms changed, 0 otherwise (finished, dt 0, or NULL).

◆ rmgui_motion_tween_retime()

int rmgui_motion_tween_retime ( rmgui_motion_tween_t * tween,
uint32_t duration_ms )

Retimes a running envelope, rounding elapsed * new / old to nearest.

Finished envelopes stay finished. NULL returns 0; otherwise returns 1 exactly when a running envelope's duration changed.

◆ rmgui_motion_tween_phase_step()

int rmgui_motion_tween_phase_step ( rmgui_motion_tween_t * tween,
uint32_t * phase_ms,
uint16_t dt_ms )

Advances an envelope and adds only its elapsed advance to *phase_ms, saturating at UINT32_MAX.

NULL arguments change nothing and return 0.

◆ rmgui_motion_map()

int32_t rmgui_motion_map ( const rmgui_motion_map_t * map,
int32_t input )

Evaluates a map with extrapolation and rmgui_motion_mix rounding.

NULL or divisor zero returns 0.

◆ rmgui_motion_wave()

int32_t rmgui_motion_wave ( const rmgui_motion_wave_t * wave,
const rmgui_motion_tween_t * envelope,
uint32_t phase_ms,
uint32_t seed,
int32_t amplitude )

Evaluates mix(0, mix(0, amplitude, wave(phase)), 1 - envelope progress).

Each mix rounds independently in the amplitude's units. The envelope's ease and duration are live; phase is independent of envelope retiming. NULL, invalid kind, zero period or a finished envelope returns 0.

◆ rmgui_motion_spring_init()

int rmgui_motion_spring_init ( rmgui_motion_spring_t * spring,
int32_t value_q16,
uint16_t stiffness,
uint16_t damping )

Puts spring at rest at value_q16 (target = value) with the given constants.

Returns 0; -1, leaving *spring unchanged, when spring is NULL, stiffness is 0, or damping is 0. Both constants accept 1..UINT16_MAX.

◆ rmgui_motion_spring_step()

int rmgui_motion_spring_step ( rmgui_motion_spring_t * spring,
uint16_t dt_ms )

Advances spring by dt_ms 1 ms substeps (see the type) and refreshes value_q16.

Returns 1 when position or velocity changed, 0 when the spring was at rest on its target for the whole call (or NULL).

◆ rmgui_motion_spring_impulse()

int rmgui_motion_spring_impulse ( rmgui_motion_spring_t * spring,
const rmgui_motion_impulse_t * impulse,
int32_t amplitude_q16 )

Adds amplitude * floor(sqrt(stiffness) * ONE) to spring velocity, rounding half away from zero in spring substep units.

Both the kick and resulting velocity clamp to the definition's limit. Position, target, constants and damping residue are unchanged. Returns 1 when velocity changes; invalid arguments or zero stiffness return 0.

◆ rmgui_motion_sine()

int32_t rmgui_motion_sine ( int32_t turns_q16)

sin(2*pi * turns_q16 / 65536) in Q16: a 257-entry quarter-wave table of round(sin(i*pi/512) * 65536), linearly interpolated over 64 steps.

Exactly 0, ONE, 0, -ONE at 0, 1/4, 1/2 and 3/4 turn; odd; |result| <= ONE. Any int32 wraps. The cosine is rmgui_motion_sine(turns_q16 + 16384).

◆ rmgui_motion_noise()

int32_t rmgui_motion_noise ( uint32_t seed,
int32_t x_q16 )

Smooth 1-D value noise at x_q16 for seed, Q16 in [-ONE, ONE): a hashed lattice value at every integer x, joined by the smoothstep 3f^2 - 2f^3.

Continuous in x, equal to the lattice value at integers; the same seed and x give the same value.

◆ rmgui_motion_rng_seed()

void rmgui_motion_rng_seed ( rmgui_motion_rng_t * rng,
uint32_t seed )

Seeds rng; seed 0 maps to a fixed nonzero state.

NULL is ignored.

◆ rmgui_motion_rng_next()

uint32_t rmgui_motion_rng_next ( rmgui_motion_rng_t * rng)

The next 32 random bits, advancing rng (shifts 13, 17, 5).

NULL returns 0.