dsys2

annotate test.c @ 15:26e3e0359f5b

renamed dsys_demo to dsys
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 26 Jul 2016 23:16:26 +0300
parents 4ad7a01c4ff5
children
rev   line source
nuclear@2 1 #include <stdio.h>
nuclear@2 2 #include <stdlib.h>
nuclear@8 3 #include <math.h>
nuclear@2 4 #include <time.h>
nuclear@2 5 #include <sys/time.h>
nuclear@2 6
nuclear@2 7 #ifndef __APPLE__
nuclear@2 8 #include <GL/glut.h>
nuclear@2 9 #else
nuclear@2 10 #include <GLUT/glut.h>
nuclear@2 11 #endif
nuclear@2 12
nuclear@14 13 #include "dsys.h"
nuclear@2 14
nuclear@2 15 void disp(void);
nuclear@7 16 void draw_progress(float p);
nuclear@2 17 void draw_teapot(float sec);
nuclear@8 18 void flash_callback(struct dsys_event *ev, void *cls);
nuclear@2 19 void reshape(int x, int y);
nuclear@2 20 void keyb(unsigned char key, int x, int y);
nuclear@7 21 void skeyb(int key, int x, int y);
nuclear@2 22 unsigned int get_ticks(void);
nuclear@2 23
nuclear@2 24 struct dsys_demo *demo;
nuclear@2 25
nuclear@8 26 struct dsys_event *evfoo, *evfoolong, *evfooshort;
nuclear@8 27
nuclear@8 28 float teapot_color[][4] = {
nuclear@8 29 {0.9, 0.5, 0.3, 1.0},
nuclear@8 30 {0.3, 0.4, 0.8, 1.0}
nuclear@8 31 };
nuclear@8 32
nuclear@2 33 int main(int argc, char **argv)
nuclear@2 34 {
nuclear@2 35 float lpos[] = {-100, 100, 100, 1};
nuclear@2 36
nuclear@2 37 glutInit(&argc, argv);
nuclear@2 38 glutInitWindowSize(800, 600);
nuclear@2 39 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@7 40 glutCreateWindow("dsys test");
nuclear@2 41
nuclear@2 42 glutDisplayFunc(disp);
nuclear@2 43 glutReshapeFunc(reshape);
nuclear@2 44 glutKeyboardFunc(keyb);
nuclear@7 45 glutSpecialFunc(skeyb);
nuclear@2 46 glutIdleFunc(glutPostRedisplay);
nuclear@2 47
nuclear@2 48 glEnable(GL_LIGHTING);
nuclear@2 49 glEnable(GL_LIGHT0);
nuclear@2 50 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@2 51
nuclear@2 52 glEnable(GL_CULL_FACE);
nuclear@2 53 glEnable(GL_DEPTH_TEST);
nuclear@2 54
nuclear@7 55 if(!(demo = dsys_open("testscript.dsys"))) {
nuclear@2 56 return 1;
nuclear@2 57 }
nuclear@2 58
nuclear@8 59 evfoo = dsys_event(demo, "foo");
nuclear@8 60 evfoolong = dsys_event(demo, "foolong");
nuclear@8 61 evfooshort = dsys_event(demo, "fooshort");
nuclear@8 62
nuclear@8 63 dsys_event_callback(evfooshort, flash_callback, 0);
nuclear@8 64
nuclear@2 65 glutMainLoop();
nuclear@2 66 return 0;
nuclear@2 67 }
nuclear@2 68
nuclear@2 69
nuclear@2 70 void disp(void)
nuclear@2 71 {
nuclear@8 72 int i;
nuclear@2 73 float sec;
nuclear@8 74 static const float trans[][2] = {{-3, 0}, {3, 0}, {0, -3}, {0, 3}};
nuclear@2 75
nuclear@2 76 dsys_update(demo, dsys_msec_to_dtime(get_ticks()));
nuclear@2 77 sec = dsys_dtime_to_sec(dsys_time(demo));
nuclear@2 78
nuclear@2 79 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@2 80
nuclear@2 81 glMatrixMode(GL_MODELVIEW);
nuclear@2 82 glLoadIdentity();
nuclear@2 83 glTranslatef(0, 0, -8);
nuclear@2 84 glRotatef(30.0, 1, 0, 0);
nuclear@2 85
nuclear@2 86 draw_teapot(sec);
nuclear@2 87
nuclear@8 88 glEnable(GL_BLEND);
nuclear@8 89 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
nuclear@8 90
nuclear@8 91 for(i=0; i<4; i++) {
nuclear@8 92 glPushMatrix();
nuclear@8 93 glRotatef(-sec * 100.0, 0, 1, 0);
nuclear@8 94 glTranslatef(trans[i][0], 0, trans[i][1]);
nuclear@8 95 glScalef(0.7, 0.7, 0.7);
nuclear@8 96 draw_teapot(sec);
nuclear@8 97 glPopMatrix();
nuclear@8 98 }
nuclear@8 99 glDisable(GL_BLEND);
nuclear@8 100
nuclear@7 101 draw_progress(dsys_progress(demo));
nuclear@7 102
nuclear@2 103 glutSwapBuffers();
nuclear@2 104 }
nuclear@2 105
nuclear@7 106 void draw_progress(float p)
nuclear@7 107 {
nuclear@7 108 glMatrixMode(GL_MODELVIEW);
nuclear@7 109 glPushMatrix();
nuclear@7 110 glLoadIdentity();
nuclear@7 111 glMatrixMode(GL_PROJECTION);
nuclear@7 112 glPushMatrix();
nuclear@7 113 glLoadIdentity();
nuclear@7 114
nuclear@7 115 glPushAttrib(GL_ENABLE_BIT);
nuclear@7 116 glDisable(GL_LIGHTING);
nuclear@7 117 glDisable(GL_DEPTH_TEST);
nuclear@7 118
nuclear@7 119 glBegin(GL_QUADS);
nuclear@7 120 glColor3f(1, 1, 1);
nuclear@7 121 glVertex2f(-0.5, 0.9);
nuclear@7 122 glVertex2f(0.5, 0.9);
nuclear@7 123 glVertex2f(0.5, 0.98);
nuclear@7 124 glVertex2f(-0.5, 0.98);
nuclear@7 125
nuclear@7 126 glColor3f(0, 0, 0);
nuclear@7 127 glVertex2f(-0.49, 0.91);
nuclear@7 128 glVertex2f(0.49, 0.91);
nuclear@7 129 glVertex2f(0.49, 0.97);
nuclear@7 130 glVertex2f(-0.49, 0.97);
nuclear@7 131
nuclear@7 132 glColor3f(1, 0, 0);
nuclear@7 133 glVertex2f(-0.48, 0.92);
nuclear@7 134 glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.92);
nuclear@7 135 glVertex2f(-0.48 + 2.0 * 0.48 * p, 0.96);
nuclear@7 136 glVertex2f(-0.48, 0.96);
nuclear@7 137 glEnd();
nuclear@7 138
nuclear@7 139 glPopAttrib();
nuclear@7 140
nuclear@7 141 glPopMatrix();
nuclear@7 142 glMatrixMode(GL_MODELVIEW);
nuclear@7 143 glPopMatrix();
nuclear@7 144 }
nuclear@7 145
nuclear@8 146 #define LERP(a, b, t) ((a) + ((b) - (a)) * (t))
nuclear@8 147
nuclear@2 148 void draw_teapot(float sec)
nuclear@2 149 {
nuclear@8 150 float dcol[4];
nuclear@2 151 float scol[] = {0.8, 0.8, 0.8, 1.0};
nuclear@8 152 float t = dsys_event_value(evfoo);
nuclear@8 153
nuclear@8 154 dcol[0] = LERP(teapot_color[0][0], teapot_color[1][0], t);
nuclear@8 155 dcol[1] = LERP(teapot_color[0][1], teapot_color[1][1], t);
nuclear@8 156 dcol[2] = LERP(teapot_color[0][2], teapot_color[1][2], t);
nuclear@8 157 dcol[3] = dsys_event_value(evfoolong);
nuclear@2 158
nuclear@2 159 glPushMatrix();
nuclear@2 160 glRotatef(sec * 100.0, 0, 1, 0);
nuclear@2 161
nuclear@2 162 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
nuclear@2 163 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
nuclear@2 164 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
nuclear@2 165
nuclear@2 166 glFrontFace(GL_CW);
nuclear@2 167 glutSolidTeapot(1.0);
nuclear@2 168 glFrontFace(GL_CCW);
nuclear@2 169
nuclear@2 170 glPopMatrix();
nuclear@2 171 }
nuclear@2 172
nuclear@8 173 void flash_callback(struct dsys_event *ev, void *cls)
nuclear@8 174 {
nuclear@8 175 float t = sin(dsys_event_value(ev) * M_PI);
nuclear@8 176
nuclear@8 177 glClearColor(t, t, t, 1.0);
nuclear@8 178 }
nuclear@8 179
nuclear@2 180 void reshape(int x, int y)
nuclear@2 181 {
nuclear@2 182 glViewport(0, 0, x, y);
nuclear@2 183
nuclear@2 184 glMatrixMode(GL_PROJECTION);
nuclear@2 185 glLoadIdentity();
nuclear@2 186 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
nuclear@2 187 }
nuclear@2 188
nuclear@2 189 void keyb(unsigned char key, int x, int y)
nuclear@2 190 {
nuclear@2 191 switch(key) {
nuclear@2 192 case 27:
nuclear@2 193 exit(0);
nuclear@2 194
nuclear@2 195 case ' ':
nuclear@2 196 if(dsys_is_running(demo)) {
nuclear@2 197 dsys_stop(demo);
nuclear@2 198 } else {
nuclear@2 199 dsys_start(demo);
nuclear@2 200 }
nuclear@2 201 break;
nuclear@2 202
nuclear@2 203 default:
nuclear@7 204 if(key >= '1' && key <= '9') {
nuclear@7 205 float n = (float)(key - '0') / 10.0;
nuclear@7 206 printf("seek to: %f percent\n", n * 100.0);
nuclear@7 207 dsys_seek_norm(demo, n);
nuclear@7 208 }
nuclear@7 209 break;
nuclear@7 210 }
nuclear@7 211 }
nuclear@7 212
nuclear@7 213 void skeyb(int key, int x, int y)
nuclear@7 214 {
nuclear@7 215 switch(key) {
nuclear@7 216 case GLUT_KEY_LEFT:
nuclear@7 217 dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5));
nuclear@7 218 break;
nuclear@7 219
nuclear@7 220 case GLUT_KEY_RIGHT:
nuclear@7 221 dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5));
nuclear@7 222 break;
nuclear@7 223
nuclear@7 224 case GLUT_KEY_UP:
nuclear@7 225 dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(30));
nuclear@7 226 break;
nuclear@7 227
nuclear@7 228 case GLUT_KEY_DOWN:
nuclear@7 229 dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(30));
nuclear@7 230 break;
nuclear@7 231
nuclear@7 232 case GLUT_KEY_PAGE_UP:
nuclear@7 233 dsys_seek(demo, dsys_time(demo) + dsys_sec_to_dtime(5 * 60));
nuclear@7 234 break;
nuclear@7 235
nuclear@7 236 case GLUT_KEY_PAGE_DOWN:
nuclear@7 237 dsys_seek(demo, dsys_time(demo) - dsys_sec_to_dtime(5 * 60));
nuclear@7 238 break;
nuclear@7 239
nuclear@7 240 default:
nuclear@2 241 break;
nuclear@2 242 }
nuclear@2 243 }
nuclear@2 244
nuclear@2 245 unsigned int get_ticks(void)
nuclear@2 246 {
nuclear@2 247 static struct timeval tv0;
nuclear@2 248 struct timeval tv;
nuclear@2 249
nuclear@2 250 gettimeofday(&tv, 0);
nuclear@2 251
nuclear@2 252 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
nuclear@2 253 tv0 = tv;
nuclear@2 254 }
nuclear@2 255 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
nuclear@2 256 }