dsys2

view src/dsys.c @ 3:eec499998960

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 31 Aug 2011 23:23:38 +0300
parents 1705e550bd91
children 95f010f7eadc
line source
1 #include <stdio.h>
2 #include <math.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <errno.h>
6 #include "dsys2.h"
7 #include "dsys_impl.h"
9 static float eval_step(struct dsys_event *ev, demotime_t t);
10 static float eval_lerp(struct dsys_event *ev, demotime_t t);
11 static float eval_sigmoid(struct dsys_event *ev, demotime_t t);
13 static void free_event(struct dsys_event *ev);
17 struct dsys_demo *dsys_open(const char *fname)
18 {
19 FILE *fp;
20 struct dsys_demo *res;
22 if(!(fp = fopen(fname, "r"))) {
23 fprintf(stderr, "failed to open demoscript: %s: %s\n", fname, strerror(errno));
24 return 0;
25 }
27 res = dsys_open_stream(fp);
28 fclose(fp);
29 return res;
30 }
32 struct dsys_demo *dsys_open_stream(FILE *fp)
33 {
34 struct dsys_demo *demo;
36 if(!(demo = malloc(sizeof *demo))) {
37 perror("failed to allocate memory");
38 return 0;
39 }
40 memset(demo, 0, sizeof *demo);
42 demo->src_tm = demo->start_tm = -1;
44 if(read_script(demo, fp) == -1) {
45 free(demo);
46 return 0;
47 }
49 return demo;
50 }
52 void dsys_close(struct dsys_demo *demo)
53 {
54 while(demo->ev) {
55 struct dsys_event *ev = demo->ev;
56 demo->ev = demo->ev->next;
57 free_event(ev);
58 }
60 free(demo);
61 }
65 static int read_script(struct dsys_demo *demo, FILE *fp)
66 {
67 int nline = 0;
68 char buf[512], *line, tok;
69 int timestamp;
70 struct dsys_event *ev;
72 while(fgets(buf, sizeof buf, fp)) {
73 nline++;
75 line = buf;/*strip_ws(buf);*/
77 if(!line || !*line) {
78 continue;
79 }
81 if(!(tok = strtok(line, SEP)) || !isdigit(*tok)) {
82 fprintf(stderr, "error reading script at line: %d: expected timestamp\n", nline);
83 return -1;
84 }
85 timestamp = atoi(tok);
87 if(!(tok = strtok(0, SEP))) {
88 fprintf(stderr, "error reading script at line: %d: expected event name\n", nline);
89 return -1;
90 }
92 if(!(ev = malloc(sizeof *ev))) {
93 perror("read_script: failed to allocate memory for an event\n");
94 return -1;
95 }
96 ev->t0 = t0;
97 ev->t1 = t1;
99 if(!(ev->name = malloc(strlen(tok) + 1))) {
100 free(ev);
101 sprintf("read_script: failed to allocate memory for the event name: %s\n", tok);
102 return -1;
103 }
104 }
106 return 0;
107 }
110 void dsys_update(struct dsys_demo *demo, demotime_t tm)
111 {
112 demo->src_tm = tm;
114 if(demo->start_tm == -1) {
115 dsys_start(demo);
116 }
118 if(demo->running) {
119 demo->tm = tm - demo->start_tm - demo->stoppage_tm;
120 }
122 /* TODO check the events list etc etc */
123 }
125 void dsys_start(struct dsys_demo *demo)
126 {
127 if(demo->running) {
128 return;
129 }
131 if(demo->start_tm == -1) {
132 demo->start_tm = demo->src_tm;
133 } else {
134 demo->stoppage_tm += demo->src_tm - demo->stop_tm;
135 }
137 demo->running = 1;
138 }
140 void dsys_stop(struct dsys_demo *demo)
141 {
142 if(!demo->running) {
143 return;
144 }
146 demo->stop_tm = demo->src_tm;
147 demo->running = 0;
148 }
150 int dsys_is_running(struct dsys_demo *demo)
151 {
152 return demo->running;
153 }
156 demotime_t dsys_duration(struct dsys_demo *demo)
157 {
158 return demo->duration;
159 }
161 demotime_t dsys_time(struct dsys_demo *demo)
162 {
163 return demo->tm;
164 }
166 float dsys_progress(struct dsys_demo *demo)
167 {
168 return demo->tm / demo->duration;
169 }
171 /* seek without continuity */
172 void dsys_seek(struct dsys_demo *demo, demotime_t tm)
173 {
174 /* TODO */
175 }
177 void dsys_seek_norm(struct dsys_demo *demo, float t)
178 {
179 dsys_seek(demo, t * demo->duration);
180 }
182 /* seek by accelerating time */
183 void dsys_warp(struct dsys_demo *demo, demotime_t tm);
184 void dsys_warp_norm(struct dsys_demo *demo, float t);
187 /* events */
188 struct dsys_event *dsys_event(struct dsys_demo *demo, const char *name);
190 enum dsys_evtype dsys_event_type(struct dsys_event *ev);
191 float dsys_event_value(struct dsys_event *ev);
193 void dsys_event_callback(struct dsys_event *ev, void (*func)(void*), void *cls);
194 void dsys_event_link(struct dsys_event *ev, float *link);
197 /* time conversion */
198 demotime_t dsys_sec_to_dtime(float sec)
199 {
200 return sec;
201 }
203 demotime_t dsys_msec_to_dtime(unsigned long msec)
204 {
205 return (demotime_t)msec / 1000.0;
206 }
208 float dsys_dtime_to_sec(demotime_t tm)
209 {
210 return tm;
211 }
213 unsigned long dsys_dtime_to_msec(demotime_t tm)
214 {
215 return (unsigned long)(tm * 1000.0);
216 }
219 static float eval_step(struct dsys_event *ev, demotime_t t)
220 {
221 return t >= ev->t1 ? 1.0 : 0.0;
222 }
224 static float eval_lerp(struct dsys_event *ev, demotime_t t)
225 {
226 return (t - ev->t0) / (ev->t1 - ev->t0);
227 }
229 static float eval_sigmoid(struct dsys_event *ev, demotime_t t)
230 {
231 t = eval_lerp(ev, t);
232 return 1.0 - (cos(t * M_PI) * 0.5 + 0.5);
233 }
235 static void free_event(struct dsys_event *ev)
236 {
237 while(ev->cblist) {
238 struct callback *cb = ev->cblist;
239 ev->cblist = ev->cblist->next;
240 free(cb);
241 }
242 }