dsys2
diff src/dsys.c @ 2:1705e550bd91
foo
author | John Tsiombikas <nuclear@siggraph.org> |
---|---|
date | Wed, 31 Aug 2011 05:08:54 +0300 |
parents | |
children | eec499998960 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/dsys.c Wed Aug 31 05:08:54 2011 +0300 1.3 @@ -0,0 +1,192 @@ 1.4 +#include <stdio.h> 1.5 +#include <math.h> 1.6 +#include <stdlib.h> 1.7 +#include <string.h> 1.8 +#include <errno.h> 1.9 +#include "dsys2.h" 1.10 +#include "dsys_impl.h" 1.11 + 1.12 +static float eval_step(struct dsys_event *ev, demotime_t t); 1.13 +static float eval_lerp(struct dsys_event *ev, demotime_t t); 1.14 +static float eval_sigmoid(struct dsys_event *ev, demotime_t t); 1.15 + 1.16 +static void free_event(struct dsys_event *ev); 1.17 + 1.18 + 1.19 + 1.20 +struct dsys_demo *dsys_open(const char *fname) 1.21 +{ 1.22 + FILE *fp; 1.23 + struct dsys_demo *res; 1.24 + 1.25 + if(!(fp = fopen(fname, "r"))) { 1.26 + fprintf(stderr, "failed to open demoscript: %s: %s\n", fname, strerror(errno)); 1.27 + return 0; 1.28 + } 1.29 + 1.30 + res = dsys_open_stream(fp); 1.31 + fclose(fp); 1.32 + return res; 1.33 +} 1.34 + 1.35 +struct dsys_demo *dsys_open_stream(FILE *fp) 1.36 +{ 1.37 + struct dsys_demo *demo; 1.38 + 1.39 + if(!(demo = malloc(sizeof *demo))) { 1.40 + perror("failed to allocate memory"); 1.41 + return 0; 1.42 + } 1.43 + memset(demo, 0, sizeof *demo); 1.44 + 1.45 + demo->src_tm = demo->start_tm = -1; 1.46 + 1.47 + /* TODO */ 1.48 + return demo; 1.49 +} 1.50 + 1.51 +void dsys_close(struct dsys_demo *demo) 1.52 +{ 1.53 + while(demo->ev) { 1.54 + struct dsys_event *ev = demo->ev; 1.55 + demo->ev = demo->ev->next; 1.56 + free_event(ev); 1.57 + } 1.58 + 1.59 + free(demo); 1.60 +} 1.61 + 1.62 + 1.63 +void dsys_update(struct dsys_demo *demo, demotime_t tm) 1.64 +{ 1.65 + demo->src_tm = tm; 1.66 + 1.67 + if(demo->start_tm == -1) { 1.68 + dsys_start(demo); 1.69 + } 1.70 + 1.71 + if(demo->running) { 1.72 + demo->tm = tm - demo->start_tm - demo->stoppage_tm; 1.73 + } 1.74 + 1.75 + /* TODO check the events list etc etc */ 1.76 +} 1.77 + 1.78 +void dsys_start(struct dsys_demo *demo) 1.79 +{ 1.80 + if(demo->running) { 1.81 + return; 1.82 + } 1.83 + 1.84 + if(demo->start_tm == -1) { 1.85 + demo->start_tm = demo->src_tm; 1.86 + } else { 1.87 + demo->stoppage_tm += demo->src_tm - demo->stop_tm; 1.88 + } 1.89 + 1.90 + demo->running = 1; 1.91 +} 1.92 + 1.93 +void dsys_stop(struct dsys_demo *demo) 1.94 +{ 1.95 + if(!demo->running) { 1.96 + return; 1.97 + } 1.98 + 1.99 + demo->stop_tm = demo->src_tm; 1.100 + demo->running = 0; 1.101 +} 1.102 + 1.103 +int dsys_is_running(struct dsys_demo *demo) 1.104 +{ 1.105 + return demo->running; 1.106 +} 1.107 + 1.108 + 1.109 +demotime_t dsys_duration(struct dsys_demo *demo) 1.110 +{ 1.111 + return demo->duration; 1.112 +} 1.113 + 1.114 +demotime_t dsys_time(struct dsys_demo *demo) 1.115 +{ 1.116 + return demo->tm; 1.117 +} 1.118 + 1.119 +float dsys_progress(struct dsys_demo *demo) 1.120 +{ 1.121 + return demo->tm / demo->duration; 1.122 +} 1.123 + 1.124 +/* seek without continuity */ 1.125 +void dsys_seek(struct dsys_demo *demo, demotime_t tm) 1.126 +{ 1.127 + /* TODO */ 1.128 +} 1.129 + 1.130 +void dsys_seek_norm(struct dsys_demo *demo, float t) 1.131 +{ 1.132 + dsys_seek(demo, t * demo->duration); 1.133 +} 1.134 + 1.135 +/* seek by accelerating time */ 1.136 +void dsys_warp(struct dsys_demo *demo, demotime_t tm); 1.137 +void dsys_warp_norm(struct dsys_demo *demo, float t); 1.138 + 1.139 + 1.140 +/* events */ 1.141 +struct dsys_event *dsys_event(struct dsys_demo *demo, const char *name); 1.142 + 1.143 +enum dsys_evtype dsys_event_type(struct dsys_event *ev); 1.144 +float dsys_event_value(struct dsys_event *ev); 1.145 + 1.146 +void dsys_event_callback(struct dsys_event *ev, void (*func)(void*), void *cls); 1.147 +void dsys_event_link(struct dsys_event *ev, float *link); 1.148 + 1.149 + 1.150 +/* time conversion */ 1.151 +demotime_t dsys_sec_to_dtime(float sec) 1.152 +{ 1.153 + return sec; 1.154 +} 1.155 + 1.156 +demotime_t dsys_msec_to_dtime(unsigned long msec) 1.157 +{ 1.158 + return (demotime_t)msec / 1000.0; 1.159 +} 1.160 + 1.161 +float dsys_dtime_to_sec(demotime_t tm) 1.162 +{ 1.163 + return tm; 1.164 +} 1.165 + 1.166 +unsigned long dsys_dtime_to_msec(demotime_t tm) 1.167 +{ 1.168 + return (unsigned long)(tm * 1000.0); 1.169 +} 1.170 + 1.171 + 1.172 +static float eval_step(struct dsys_event *ev, demotime_t t) 1.173 +{ 1.174 + return t >= ev->t1 ? 1.0 : 0.0; 1.175 +} 1.176 + 1.177 +static float eval_lerp(struct dsys_event *ev, demotime_t t) 1.178 +{ 1.179 + return (t - ev->t0) / (ev->t1 - ev->t0); 1.180 +} 1.181 + 1.182 +static float eval_sigmoid(struct dsys_event *ev, demotime_t t) 1.183 +{ 1.184 + t = eval_lerp(ev, t); 1.185 + return 1.0 - (cos(t * M_PI) * 0.5 + 0.5); 1.186 +} 1.187 + 1.188 +static void free_event(struct dsys_event *ev) 1.189 +{ 1.190 + while(ev->cblist) { 1.191 + struct callback *cb = ev->cblist; 1.192 + ev->cblist = ev->cblist->next; 1.193 + free(cb); 1.194 + } 1.195 +}