dsys2

changeset 2:1705e550bd91

foo
author John Tsiombikas <nuclear@siggraph.org>
date Wed, 31 Aug 2011 05:08:54 +0300
parents 0851b4724d13
children eec499998960
files .hgignore Makefile src/dsys.c src/dsys_impl.h test.c
diffstat 5 files changed, 333 insertions(+), 24 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Sat Aug 20 07:57:00 2011 +0300
     1.2 +++ b/.hgignore	Wed Aug 31 05:08:54 2011 +0300
     1.3 @@ -1,3 +1,5 @@
     1.4  \.d$
     1.5  \.o$
     1.6 +\.a$
     1.7  \.swp$
     1.8 +^test$
     2.1 --- a/Makefile	Sat Aug 20 07:57:00 2011 +0300
     2.2 +++ b/Makefile	Wed Aug 31 05:08:54 2011 +0300
     2.3 @@ -6,7 +6,10 @@
     2.4  
     2.5  CC = gcc
     2.6  AR = ar
     2.7 -CFLAGS = -pedantic -Wall -g
     2.8 +CFLAGS = -pedantic -Wall -g -Isrc
     2.9 +
    2.10 +test: test.o $(lib_a)
    2.11 +	$(CC) $(CFLAGS) -o $@ test.o $(lib_a) -lGL -lGLU -lglut -lm
    2.12  
    2.13  $(lib_a): $(obj)
    2.14  	$(AR) rcs $@ $(obj)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/dsys.c	Wed Aug 31 05:08:54 2011 +0300
     3.3 @@ -0,0 +1,192 @@
     3.4 +#include <stdio.h>
     3.5 +#include <math.h>
     3.6 +#include <stdlib.h>
     3.7 +#include <string.h>
     3.8 +#include <errno.h>
     3.9 +#include "dsys2.h"
    3.10 +#include "dsys_impl.h"
    3.11 +
    3.12 +static float eval_step(struct dsys_event *ev, demotime_t t);
    3.13 +static float eval_lerp(struct dsys_event *ev, demotime_t t);
    3.14 +static float eval_sigmoid(struct dsys_event *ev, demotime_t t);
    3.15 +
    3.16 +static void free_event(struct dsys_event *ev);
    3.17 +
    3.18 +
    3.19 +
    3.20 +struct dsys_demo *dsys_open(const char *fname)
    3.21 +{
    3.22 +	FILE *fp;
    3.23 +	struct dsys_demo *res;
    3.24 +
    3.25 +	if(!(fp = fopen(fname, "r"))) {
    3.26 +		fprintf(stderr, "failed to open demoscript: %s: %s\n", fname, strerror(errno));
    3.27 +		return 0;
    3.28 +	}
    3.29 +
    3.30 +	res = dsys_open_stream(fp);
    3.31 +	fclose(fp);
    3.32 +	return res;
    3.33 +}
    3.34 +
    3.35 +struct dsys_demo *dsys_open_stream(FILE *fp)
    3.36 +{
    3.37 +	struct dsys_demo *demo;
    3.38 +
    3.39 +	if(!(demo = malloc(sizeof *demo))) {
    3.40 +		perror("failed to allocate memory");
    3.41 +		return 0;
    3.42 +	}
    3.43 +	memset(demo, 0, sizeof *demo);
    3.44 +
    3.45 +	demo->src_tm = demo->start_tm = -1;
    3.46 +
    3.47 +	/* TODO */
    3.48 +	return demo;
    3.49 +}
    3.50 +
    3.51 +void dsys_close(struct dsys_demo *demo)
    3.52 +{
    3.53 +	while(demo->ev) {
    3.54 +		struct dsys_event *ev = demo->ev;
    3.55 +		demo->ev = demo->ev->next;
    3.56 +		free_event(ev);
    3.57 +	}
    3.58 +
    3.59 +	free(demo);
    3.60 +}
    3.61 +
    3.62 +
    3.63 +void dsys_update(struct dsys_demo *demo, demotime_t tm)
    3.64 +{
    3.65 +	demo->src_tm = tm;
    3.66 +
    3.67 +	if(demo->start_tm == -1) {
    3.68 +		dsys_start(demo);
    3.69 +	}
    3.70 +
    3.71 +	if(demo->running) {
    3.72 +		demo->tm = tm - demo->start_tm - demo->stoppage_tm;
    3.73 +	}
    3.74 +
    3.75 +	/* TODO check the events list etc etc */
    3.76 +}
    3.77 +
    3.78 +void dsys_start(struct dsys_demo *demo)
    3.79 +{
    3.80 +	if(demo->running) {
    3.81 +		return;
    3.82 +	}
    3.83 +
    3.84 +	if(demo->start_tm == -1) {
    3.85 +		demo->start_tm = demo->src_tm;
    3.86 +	} else {
    3.87 +		demo->stoppage_tm += demo->src_tm - demo->stop_tm;
    3.88 +	}
    3.89 +
    3.90 +	demo->running = 1;
    3.91 +}
    3.92 +
    3.93 +void dsys_stop(struct dsys_demo *demo)
    3.94 +{
    3.95 +	if(!demo->running) {
    3.96 +		return;
    3.97 +	}
    3.98 +
    3.99 +	demo->stop_tm = demo->src_tm;
   3.100 +	demo->running = 0;
   3.101 +}
   3.102 +
   3.103 +int dsys_is_running(struct dsys_demo *demo)
   3.104 +{
   3.105 +	return demo->running;
   3.106 +}
   3.107 +
   3.108 +
   3.109 +demotime_t dsys_duration(struct dsys_demo *demo)
   3.110 +{
   3.111 +	return demo->duration;
   3.112 +}
   3.113 +
   3.114 +demotime_t dsys_time(struct dsys_demo *demo)
   3.115 +{
   3.116 +	return demo->tm;
   3.117 +}
   3.118 +
   3.119 +float dsys_progress(struct dsys_demo *demo)
   3.120 +{
   3.121 +	return demo->tm / demo->duration;
   3.122 +}
   3.123 +
   3.124 +/* seek without continuity */
   3.125 +void dsys_seek(struct dsys_demo *demo, demotime_t tm)
   3.126 +{
   3.127 +	/* TODO */
   3.128 +}
   3.129 +
   3.130 +void dsys_seek_norm(struct dsys_demo *demo, float t)
   3.131 +{
   3.132 +	dsys_seek(demo, t * demo->duration);
   3.133 +}
   3.134 +
   3.135 +/* seek by accelerating time */
   3.136 +void dsys_warp(struct dsys_demo *demo, demotime_t tm);
   3.137 +void dsys_warp_norm(struct dsys_demo *demo, float t);
   3.138 +
   3.139 +
   3.140 +/* events */
   3.141 +struct dsys_event *dsys_event(struct dsys_demo *demo, const char *name);
   3.142 +
   3.143 +enum dsys_evtype dsys_event_type(struct dsys_event *ev);
   3.144 +float dsys_event_value(struct dsys_event *ev);
   3.145 +
   3.146 +void dsys_event_callback(struct dsys_event *ev, void (*func)(void*), void *cls);
   3.147 +void dsys_event_link(struct dsys_event *ev, float *link);
   3.148 +
   3.149 +
   3.150 +/* time conversion */
   3.151 +demotime_t dsys_sec_to_dtime(float sec)
   3.152 +{
   3.153 +	return sec;
   3.154 +}
   3.155 +
   3.156 +demotime_t dsys_msec_to_dtime(unsigned long msec)
   3.157 +{
   3.158 +	return (demotime_t)msec / 1000.0;
   3.159 +}
   3.160 +
   3.161 +float dsys_dtime_to_sec(demotime_t tm)
   3.162 +{
   3.163 +	return tm;
   3.164 +}
   3.165 +
   3.166 +unsigned long dsys_dtime_to_msec(demotime_t tm)
   3.167 +{
   3.168 +	return (unsigned long)(tm * 1000.0);
   3.169 +}
   3.170 +
   3.171 +
   3.172 +static float eval_step(struct dsys_event *ev, demotime_t t)
   3.173 +{
   3.174 +	return t >= ev->t1 ? 1.0 : 0.0;
   3.175 +}
   3.176 +
   3.177 +static float eval_lerp(struct dsys_event *ev, demotime_t t)
   3.178 +{
   3.179 +	return (t - ev->t0) / (ev->t1 - ev->t0);
   3.180 +}
   3.181 +
   3.182 +static float eval_sigmoid(struct dsys_event *ev, demotime_t t)
   3.183 +{
   3.184 +	t = eval_lerp(ev, t);
   3.185 +	return 1.0 - (cos(t * M_PI) * 0.5 + 0.5);
   3.186 +}
   3.187 +
   3.188 +static void free_event(struct dsys_event *ev)
   3.189 +{
   3.190 +	while(ev->cblist) {
   3.191 +		struct callback *cb = ev->cblist;
   3.192 +		ev->cblist = ev->cblist->next;
   3.193 +		free(cb);
   3.194 +	}
   3.195 +}
     4.1 --- a/src/dsys_impl.h	Sat Aug 20 07:57:00 2011 +0300
     4.2 +++ b/src/dsys_impl.h	Wed Aug 31 05:08:54 2011 +0300
     4.3 @@ -3,16 +3,14 @@
     4.4  
     4.5  #include "dsys2.h"
     4.6  
     4.7 -static float eval_step(struct dsys_event *ev, demotime_t t);
     4.8 -static float eval_lerp(struct dsys_event *ev, demotime_t t);
     4.9 -static float eval_sigmoid(struct dsys_event *ev, demotime_t t);
    4.10 -
    4.11 -
    4.12  struct dsys_demo {
    4.13 -	demotime_t srctime;
    4.14 +	demotime_t tm, src_tm, start_tm, stop_tm, duration;
    4.15 +	demotime_t stoppage_tm;
    4.16  
    4.17  	struct dsys_event *ev;
    4.18  	int num_ev;
    4.19 +
    4.20 +	int running;
    4.21  };
    4.22  
    4.23  struct callback {
    4.24 @@ -32,23 +30,8 @@
    4.25  	float (*eval_func)(struct dsys_event*);
    4.26  
    4.27  	struct callback *cblist;
    4.28 +
    4.29 +	struct dsys_event *next, *prev;
    4.30  };
    4.31  
    4.32 -
    4.33 -static float eval_step(struct dsys_event *ev, demotime_t t)
    4.34 -{
    4.35 -	return t >= ev->t1 ? 1.0 : 0.0;
    4.36 -}
    4.37 -
    4.38 -static float eval_lerp(struct dsys_event *ev, demotime_t t)
    4.39 -{
    4.40 -	return (t - ev->t0) / (ev->t1 - ev->t0);
    4.41 -}
    4.42 -
    4.43 -static float eval_sigmoid(struct dsys_event *ev, demotime_t t)
    4.44 -{
    4.45 -	t = eval_lerp(ev, t);
    4.46 -	return 1.0 - (cos(t * M_PI) * 0.5 + 0.5);
    4.47 -}
    4.48 -
    4.49  #endif	/* DSYS_IMPL_H_ */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test.c	Wed Aug 31 05:08:54 2011 +0300
     5.3 @@ -0,0 +1,129 @@
     5.4 +#include <stdio.h>
     5.5 +#include <stdlib.h>
     5.6 +#include <time.h>
     5.7 +#include <sys/time.h>
     5.8 +
     5.9 +#ifndef __APPLE__
    5.10 +#include <GL/glut.h>
    5.11 +#else
    5.12 +#include <GLUT/glut.h>
    5.13 +#endif
    5.14 +
    5.15 +#include "dsys2.h"
    5.16 +
    5.17 +void disp(void);
    5.18 +void draw_teapot(float sec);
    5.19 +void reshape(int x, int y);
    5.20 +void keyb(unsigned char key, int x, int y);
    5.21 +unsigned int get_ticks(void);
    5.22 +
    5.23 +struct dsys_demo *demo;
    5.24 +
    5.25 +int main(int argc, char **argv)
    5.26 +{
    5.27 +	float lpos[] = {-100, 100, 100, 1};
    5.28 +
    5.29 +	glutInit(&argc, argv);
    5.30 +	glutInitWindowSize(800, 600);
    5.31 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    5.32 +	glutCreateWindow("foo");
    5.33 +
    5.34 +	glutDisplayFunc(disp);
    5.35 +	glutReshapeFunc(reshape);
    5.36 +	glutKeyboardFunc(keyb);
    5.37 +	glutIdleFunc(glutPostRedisplay);
    5.38 +
    5.39 +	glEnable(GL_LIGHTING);
    5.40 +	glEnable(GL_LIGHT0);
    5.41 +	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    5.42 +
    5.43 +	glEnable(GL_CULL_FACE);
    5.44 +	glEnable(GL_DEPTH_TEST);
    5.45 +
    5.46 +	if(!(demo = dsys_open("/dev/null"))) {
    5.47 +		return 1;
    5.48 +	}
    5.49 +
    5.50 +	glutMainLoop();
    5.51 +	return 0;
    5.52 +}
    5.53 +
    5.54 +
    5.55 +void disp(void)
    5.56 +{
    5.57 +	float sec;
    5.58 +
    5.59 +	dsys_update(demo, dsys_msec_to_dtime(get_ticks()));
    5.60 +	sec = dsys_dtime_to_sec(dsys_time(demo));
    5.61 +
    5.62 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    5.63 +
    5.64 +	glMatrixMode(GL_MODELVIEW);
    5.65 +	glLoadIdentity();
    5.66 +	glTranslatef(0, 0, -8);
    5.67 +	glRotatef(30.0, 1, 0, 0);
    5.68 +
    5.69 +	draw_teapot(sec);
    5.70 +
    5.71 +	glutSwapBuffers();
    5.72 +}
    5.73 +
    5.74 +void draw_teapot(float sec)
    5.75 +{
    5.76 +	float dcol[] = {0.2, 0.4, 0.8, 1.0};
    5.77 +	float scol[] = {0.8, 0.8, 0.8, 1.0};
    5.78 +
    5.79 +	glPushMatrix();
    5.80 +	glRotatef(sec * 100.0, 0, 1, 0);
    5.81 +
    5.82 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
    5.83 +	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
    5.84 +	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
    5.85 +
    5.86 +	glFrontFace(GL_CW);
    5.87 +	glutSolidTeapot(1.0);
    5.88 +	glFrontFace(GL_CCW);
    5.89 +
    5.90 +	glPopMatrix();
    5.91 +}
    5.92 +
    5.93 +void reshape(int x, int y)
    5.94 +{
    5.95 +	glViewport(0, 0, x, y);
    5.96 +
    5.97 +	glMatrixMode(GL_PROJECTION);
    5.98 +	glLoadIdentity();
    5.99 +	gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
   5.100 +}
   5.101 +
   5.102 +void keyb(unsigned char key, int x, int y)
   5.103 +{
   5.104 +	switch(key) {
   5.105 +	case 27:
   5.106 +		exit(0);
   5.107 +
   5.108 +	case ' ':
   5.109 +		if(dsys_is_running(demo)) {
   5.110 +			dsys_stop(demo);
   5.111 +		} else {
   5.112 +			dsys_start(demo);
   5.113 +		}
   5.114 +		break;
   5.115 +
   5.116 +	default:
   5.117 +		break;
   5.118 +	}
   5.119 +}
   5.120 +
   5.121 +unsigned int get_ticks(void)
   5.122 +{
   5.123 +	static struct timeval tv0;
   5.124 +	struct timeval tv;
   5.125 +
   5.126 +	gettimeofday(&tv, 0);
   5.127 +
   5.128 +	if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
   5.129 +		tv0 = tv;
   5.130 +	}
   5.131 +	return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
   5.132 +}