# HG changeset patch # User John Tsiombikas # Date 1314756534 -10800 # Node ID 1705e550bd91937751f9fab86a42b6c41bef79a4 # Parent 0851b4724d13c0b5b717d234bcbf047d66699831 foo diff -r 0851b4724d13 -r 1705e550bd91 .hgignore --- a/.hgignore Sat Aug 20 07:57:00 2011 +0300 +++ b/.hgignore Wed Aug 31 05:08:54 2011 +0300 @@ -1,3 +1,5 @@ \.d$ \.o$ +\.a$ \.swp$ +^test$ diff -r 0851b4724d13 -r 1705e550bd91 Makefile --- a/Makefile Sat Aug 20 07:57:00 2011 +0300 +++ b/Makefile Wed Aug 31 05:08:54 2011 +0300 @@ -6,7 +6,10 @@ CC = gcc AR = ar -CFLAGS = -pedantic -Wall -g +CFLAGS = -pedantic -Wall -g -Isrc + +test: test.o $(lib_a) + $(CC) $(CFLAGS) -o $@ test.o $(lib_a) -lGL -lGLU -lglut -lm $(lib_a): $(obj) $(AR) rcs $@ $(obj) diff -r 0851b4724d13 -r 1705e550bd91 src/dsys.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/dsys.c Wed Aug 31 05:08:54 2011 +0300 @@ -0,0 +1,192 @@ +#include +#include +#include +#include +#include +#include "dsys2.h" +#include "dsys_impl.h" + +static float eval_step(struct dsys_event *ev, demotime_t t); +static float eval_lerp(struct dsys_event *ev, demotime_t t); +static float eval_sigmoid(struct dsys_event *ev, demotime_t t); + +static void free_event(struct dsys_event *ev); + + + +struct dsys_demo *dsys_open(const char *fname) +{ + FILE *fp; + struct dsys_demo *res; + + if(!(fp = fopen(fname, "r"))) { + fprintf(stderr, "failed to open demoscript: %s: %s\n", fname, strerror(errno)); + return 0; + } + + res = dsys_open_stream(fp); + fclose(fp); + return res; +} + +struct dsys_demo *dsys_open_stream(FILE *fp) +{ + struct dsys_demo *demo; + + if(!(demo = malloc(sizeof *demo))) { + perror("failed to allocate memory"); + return 0; + } + memset(demo, 0, sizeof *demo); + + demo->src_tm = demo->start_tm = -1; + + /* TODO */ + return demo; +} + +void dsys_close(struct dsys_demo *demo) +{ + while(demo->ev) { + struct dsys_event *ev = demo->ev; + demo->ev = demo->ev->next; + free_event(ev); + } + + free(demo); +} + + +void dsys_update(struct dsys_demo *demo, demotime_t tm) +{ + demo->src_tm = tm; + + if(demo->start_tm == -1) { + dsys_start(demo); + } + + if(demo->running) { + demo->tm = tm - demo->start_tm - demo->stoppage_tm; + } + + /* TODO check the events list etc etc */ +} + +void dsys_start(struct dsys_demo *demo) +{ + if(demo->running) { + return; + } + + if(demo->start_tm == -1) { + demo->start_tm = demo->src_tm; + } else { + demo->stoppage_tm += demo->src_tm - demo->stop_tm; + } + + demo->running = 1; +} + +void dsys_stop(struct dsys_demo *demo) +{ + if(!demo->running) { + return; + } + + demo->stop_tm = demo->src_tm; + demo->running = 0; +} + +int dsys_is_running(struct dsys_demo *demo) +{ + return demo->running; +} + + +demotime_t dsys_duration(struct dsys_demo *demo) +{ + return demo->duration; +} + +demotime_t dsys_time(struct dsys_demo *demo) +{ + return demo->tm; +} + +float dsys_progress(struct dsys_demo *demo) +{ + return demo->tm / demo->duration; +} + +/* seek without continuity */ +void dsys_seek(struct dsys_demo *demo, demotime_t tm) +{ + /* TODO */ +} + +void dsys_seek_norm(struct dsys_demo *demo, float t) +{ + dsys_seek(demo, t * demo->duration); +} + +/* seek by accelerating time */ +void dsys_warp(struct dsys_demo *demo, demotime_t tm); +void dsys_warp_norm(struct dsys_demo *demo, float t); + + +/* events */ +struct dsys_event *dsys_event(struct dsys_demo *demo, const char *name); + +enum dsys_evtype dsys_event_type(struct dsys_event *ev); +float dsys_event_value(struct dsys_event *ev); + +void dsys_event_callback(struct dsys_event *ev, void (*func)(void*), void *cls); +void dsys_event_link(struct dsys_event *ev, float *link); + + +/* time conversion */ +demotime_t dsys_sec_to_dtime(float sec) +{ + return sec; +} + +demotime_t dsys_msec_to_dtime(unsigned long msec) +{ + return (demotime_t)msec / 1000.0; +} + +float dsys_dtime_to_sec(demotime_t tm) +{ + return tm; +} + +unsigned long dsys_dtime_to_msec(demotime_t tm) +{ + return (unsigned long)(tm * 1000.0); +} + + +static float eval_step(struct dsys_event *ev, demotime_t t) +{ + return t >= ev->t1 ? 1.0 : 0.0; +} + +static float eval_lerp(struct dsys_event *ev, demotime_t t) +{ + return (t - ev->t0) / (ev->t1 - ev->t0); +} + +static float eval_sigmoid(struct dsys_event *ev, demotime_t t) +{ + t = eval_lerp(ev, t); + return 1.0 - (cos(t * M_PI) * 0.5 + 0.5); +} + +static void free_event(struct dsys_event *ev) +{ + while(ev->cblist) { + struct callback *cb = ev->cblist; + ev->cblist = ev->cblist->next; + free(cb); + } +} diff -r 0851b4724d13 -r 1705e550bd91 src/dsys_impl.h --- a/src/dsys_impl.h Sat Aug 20 07:57:00 2011 +0300 +++ b/src/dsys_impl.h Wed Aug 31 05:08:54 2011 +0300 @@ -3,16 +3,14 @@ #include "dsys2.h" -static float eval_step(struct dsys_event *ev, demotime_t t); -static float eval_lerp(struct dsys_event *ev, demotime_t t); -static float eval_sigmoid(struct dsys_event *ev, demotime_t t); - - struct dsys_demo { - demotime_t srctime; + demotime_t tm, src_tm, start_tm, stop_tm, duration; + demotime_t stoppage_tm; struct dsys_event *ev; int num_ev; + + int running; }; struct callback { @@ -32,23 +30,8 @@ float (*eval_func)(struct dsys_event*); struct callback *cblist; + + struct dsys_event *next, *prev; }; - -static float eval_step(struct dsys_event *ev, demotime_t t) -{ - return t >= ev->t1 ? 1.0 : 0.0; -} - -static float eval_lerp(struct dsys_event *ev, demotime_t t) -{ - return (t - ev->t0) / (ev->t1 - ev->t0); -} - -static float eval_sigmoid(struct dsys_event *ev, demotime_t t) -{ - t = eval_lerp(ev, t); - return 1.0 - (cos(t * M_PI) * 0.5 + 0.5); -} - #endif /* DSYS_IMPL_H_ */ diff -r 0851b4724d13 -r 1705e550bd91 test.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.c Wed Aug 31 05:08:54 2011 +0300 @@ -0,0 +1,129 @@ +#include +#include +#include +#include + +#ifndef __APPLE__ +#include +#else +#include +#endif + +#include "dsys2.h" + +void disp(void); +void draw_teapot(float sec); +void reshape(int x, int y); +void keyb(unsigned char key, int x, int y); +unsigned int get_ticks(void); + +struct dsys_demo *demo; + +int main(int argc, char **argv) +{ + float lpos[] = {-100, 100, 100, 1}; + + glutInit(&argc, argv); + glutInitWindowSize(800, 600); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); + glutCreateWindow("foo"); + + glutDisplayFunc(disp); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + glutIdleFunc(glutPostRedisplay); + + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT0, GL_POSITION, lpos); + + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + + if(!(demo = dsys_open("/dev/null"))) { + return 1; + } + + glutMainLoop(); + return 0; +} + + +void disp(void) +{ + float sec; + + dsys_update(demo, dsys_msec_to_dtime(get_ticks())); + sec = dsys_dtime_to_sec(dsys_time(demo)); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0, 0, -8); + glRotatef(30.0, 1, 0, 0); + + draw_teapot(sec); + + glutSwapBuffers(); +} + +void draw_teapot(float sec) +{ + float dcol[] = {0.2, 0.4, 0.8, 1.0}; + float scol[] = {0.8, 0.8, 0.8, 1.0}; + + glPushMatrix(); + glRotatef(sec * 100.0, 0, 1, 0); + + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol); + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol); + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0); + + glFrontFace(GL_CW); + glutSolidTeapot(1.0); + glFrontFace(GL_CCW); + + glPopMatrix(); +} + +void reshape(int x, int y) +{ + glViewport(0, 0, x, y); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0); +} + +void keyb(unsigned char key, int x, int y) +{ + switch(key) { + case 27: + exit(0); + + case ' ': + if(dsys_is_running(demo)) { + dsys_stop(demo); + } else { + dsys_start(demo); + } + break; + + default: + break; + } +} + +unsigned int get_ticks(void) +{ + static struct timeval tv0; + struct timeval tv; + + gettimeofday(&tv, 0); + + if(tv0.tv_sec == 0 && tv0.tv_usec == 0) { + tv0 = tv; + } + return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; +}