dsys2

view 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 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 /* TODO */
45 return demo;
46 }
48 void dsys_close(struct dsys_demo *demo)
49 {
50 while(demo->ev) {
51 struct dsys_event *ev = demo->ev;
52 demo->ev = demo->ev->next;
53 free_event(ev);
54 }
56 free(demo);
57 }
60 void dsys_update(struct dsys_demo *demo, demotime_t tm)
61 {
62 demo->src_tm = tm;
64 if(demo->start_tm == -1) {
65 dsys_start(demo);
66 }
68 if(demo->running) {
69 demo->tm = tm - demo->start_tm - demo->stoppage_tm;
70 }
72 /* TODO check the events list etc etc */
73 }
75 void dsys_start(struct dsys_demo *demo)
76 {
77 if(demo->running) {
78 return;
79 }
81 if(demo->start_tm == -1) {
82 demo->start_tm = demo->src_tm;
83 } else {
84 demo->stoppage_tm += demo->src_tm - demo->stop_tm;
85 }
87 demo->running = 1;
88 }
90 void dsys_stop(struct dsys_demo *demo)
91 {
92 if(!demo->running) {
93 return;
94 }
96 demo->stop_tm = demo->src_tm;
97 demo->running = 0;
98 }
100 int dsys_is_running(struct dsys_demo *demo)
101 {
102 return demo->running;
103 }
106 demotime_t dsys_duration(struct dsys_demo *demo)
107 {
108 return demo->duration;
109 }
111 demotime_t dsys_time(struct dsys_demo *demo)
112 {
113 return demo->tm;
114 }
116 float dsys_progress(struct dsys_demo *demo)
117 {
118 return demo->tm / demo->duration;
119 }
121 /* seek without continuity */
122 void dsys_seek(struct dsys_demo *demo, demotime_t tm)
123 {
124 /* TODO */
125 }
127 void dsys_seek_norm(struct dsys_demo *demo, float t)
128 {
129 dsys_seek(demo, t * demo->duration);
130 }
132 /* seek by accelerating time */
133 void dsys_warp(struct dsys_demo *demo, demotime_t tm);
134 void dsys_warp_norm(struct dsys_demo *demo, float t);
137 /* events */
138 struct dsys_event *dsys_event(struct dsys_demo *demo, const char *name);
140 enum dsys_evtype dsys_event_type(struct dsys_event *ev);
141 float dsys_event_value(struct dsys_event *ev);
143 void dsys_event_callback(struct dsys_event *ev, void (*func)(void*), void *cls);
144 void dsys_event_link(struct dsys_event *ev, float *link);
147 /* time conversion */
148 demotime_t dsys_sec_to_dtime(float sec)
149 {
150 return sec;
151 }
153 demotime_t dsys_msec_to_dtime(unsigned long msec)
154 {
155 return (demotime_t)msec / 1000.0;
156 }
158 float dsys_dtime_to_sec(demotime_t tm)
159 {
160 return tm;
161 }
163 unsigned long dsys_dtime_to_msec(demotime_t tm)
164 {
165 return (unsigned long)(tm * 1000.0);
166 }
169 static float eval_step(struct dsys_event *ev, demotime_t t)
170 {
171 return t >= ev->t1 ? 1.0 : 0.0;
172 }
174 static float eval_lerp(struct dsys_event *ev, demotime_t t)
175 {
176 return (t - ev->t0) / (ev->t1 - ev->t0);
177 }
179 static float eval_sigmoid(struct dsys_event *ev, demotime_t t)
180 {
181 t = eval_lerp(ev, t);
182 return 1.0 - (cos(t * M_PI) * 0.5 + 0.5);
183 }
185 static void free_event(struct dsys_event *ev)
186 {
187 while(ev->cblist) {
188 struct callback *cb = ev->cblist;
189 ev->cblist = ev->cblist->next;
190 free(cb);
191 }
192 }