dsys2

changeset 0:34d90cd9ef9b

starting a new demosystem
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 20 Aug 2011 07:48:16 +0300
parents
children 0851b4724d13
files .hgignore Makefile src/dsys2.h src/dsys_impl.h
diffstat 4 files changed, 144 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Sat Aug 20 07:48:16 2011 +0300
     1.3 @@ -0,0 +1,3 @@
     1.4 +\.d$
     1.5 +\.o$
     1.6 +\.swp$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Makefile	Sat Aug 20 07:48:16 2011 +0300
     2.3 @@ -0,0 +1,25 @@
     2.4 +src = $(wildcard src/*.c)
     2.5 +obj = $(src:.c=.o)
     2.6 +dep = $(obj:.o=.d)
     2.7 +
     2.8 +lib_a = libdsys2.a
     2.9 +
    2.10 +CC = gcc
    2.11 +AR = ar
    2.12 +CFLAGS = -pedantic -Wall -g
    2.13 +
    2.14 +$(lib_a): $(obj)
    2.15 +	$(AR) rcs $@ $(obj)
    2.16 +
    2.17 +-include $(dep)
    2.18 +
    2.19 +%.d: %.c
    2.20 +	@$(CPP) $(CFLAGS) -MM -MT $(@:.d=.o) $< >$@
    2.21 +
    2.22 +.PHONY: clean
    2.23 +clean:
    2.24 +	rm -f $(obj) $(bin)
    2.25 +
    2.26 +.PHONY: cleandep
    2.27 +cleandep:
    2.28 +	rm -f $(dep)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/dsys2.h	Sat Aug 20 07:48:16 2011 +0300
     3.3 @@ -0,0 +1,62 @@
     3.4 +#ifndef DSYS2_H_
     3.5 +#define DSYS2_H_
     3.6 +
     3.7 +#include <stdio.h>
     3.8 +
     3.9 +struct dsys_demo;
    3.10 +struct dsys_event;
    3.11 +
    3.12 +typedef float demotime_t;
    3.13 +
    3.14 +enum dsys_evtype {
    3.15 +	DSYS_SINGLE,
    3.16 +	DSYS_PERIODIC
    3.17 +};
    3.18 +
    3.19 +
    3.20 +struct dsys_demo *dsys_open(const char *fname);
    3.21 +struct dsys_demo *dsys_open_stream(FILE *fp);
    3.22 +void dsys_close(struct dsys_demo *demo);
    3.23 +
    3.24 +void dsys_update(struct dsys_demo *demo, demotime_t tm);
    3.25 +
    3.26 +
    3.27 +void dsys_start(struct dsys_demo *demo);
    3.28 +void dsys_stop(struct dsys_demo *demo);
    3.29 +int dsys_is_running(struct dsys_demo *demo);
    3.30 +
    3.31 +
    3.32 +demotime_t dsys_duration(struct dsys_demo *demo);
    3.33 +demotime_t dsys_time(struct dsys_demo *demo);
    3.34 +float dsys_progress(struct dsys_demo *demo);
    3.35 +
    3.36 +/* seek without continuity */
    3.37 +void dsys_seek(struct dsys_demo *demo, demotime_t tm);
    3.38 +void dsys_seek_norm(struct dsys_demo *demo, float t);
    3.39 +
    3.40 +/* seek by accelerating time */
    3.41 +void dsys_warp(struct dsys_demo *demo, demotime_t tm);
    3.42 +void dsys_warp_norm(struct dsys_demo *demo, float t);
    3.43 +
    3.44 +
    3.45 +/* events */
    3.46 +struct dsys_event *dsys_event(struct dsys_demo *demo, const char *name);
    3.47 +
    3.48 +enum dsys_evtype dsys_event_type(struct dsys_event *ev);
    3.49 +float dsys_event_value(struct dsys_event *ev);
    3.50 +
    3.51 +void dsys_event_callback(struct dsys_event *ev, void (*func)(void*), void *cls);
    3.52 +void dsys_event_link(struct dsys_event *ev, float *link);
    3.53 +
    3.54 +
    3.55 +/* time conversion */
    3.56 +demotime_t dsys_sec_to_dtime(float sec);
    3.57 +demotime_t dsys_msec_to_dtime(unsigned long msec);
    3.58 +demotime_t dsys_norm_to_dtime(float t);
    3.59 +
    3.60 +float dsys_dtime_to_sec(demotime_t tm);
    3.61 +unsigned long dsys_dtime_to_msec(demotime_t tm);
    3.62 +float dsys_dtime_to_norm(demotime_t tm);
    3.63 +
    3.64 +
    3.65 +#endif	/* DSYS2_H_ */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/dsys_impl.h	Sat Aug 20 07:48:16 2011 +0300
     4.3 @@ -0,0 +1,54 @@
     4.4 +#ifndef DSYS_IMPL_H_
     4.5 +#define DSYS_IMPL_H_
     4.6 +
     4.7 +#include "dsys2.h"
     4.8 +
     4.9 +static float eval_step(struct dsys_event *ev, demotime_t t);
    4.10 +static float eval_lerp(struct dsys_event *ev, demotime_t t);
    4.11 +static float eval_sigmoid(struct dsys_event *ev, demotime_t t);
    4.12 +
    4.13 +
    4.14 +struct dsys_demo {
    4.15 +	demotime_t srctime;
    4.16 +
    4.17 +	struct dsys_event *ev;
    4.18 +	int num_ev;
    4.19 +};
    4.20 +
    4.21 +struct callback {
    4.22 +	void (*func)(struct dsys_event*, void*);
    4.23 +	void *cls;
    4.24 +
    4.25 +	struct callback *next;
    4.26 +};
    4.27 +
    4.28 +
    4.29 +struct dsys_event {
    4.30 +	enum dsys_evtype type;
    4.31 +
    4.32 +	demotime_t t0, t1;
    4.33 +	float val;
    4.34 +
    4.35 +	float (*eval_func)(struct dsys_event*);
    4.36 +
    4.37 +	struct callback *cblist;
    4.38 +};
    4.39 +
    4.40 +
    4.41 +static float eval_step(struct dsys_event *ev, demotime_t t)
    4.42 +{
    4.43 +	return t >= ev->t1 ? 1.0 : 0.0;
    4.44 +}
    4.45 +
    4.46 +static float eval_lerp(struct dsys_event *ev, demotime_t t)
    4.47 +{
    4.48 +	return (t - ev->t0) / (ev->t1 - ev->t0);
    4.49 +}
    4.50 +
    4.51 +static float eval_sigmoid(struct dsys_event *ev, demotime_t t)
    4.52 +{
    4.53 +	t = eval_lerp(ev, t);
    4.54 +	return 1.0 - (cos(t * M_PI) * 0.5 + 0.5);
    4.55 +}
    4.56 +
    4.57 +#endif	/* DSYS_IMPL_H_ */