dsys2

view test.c @ 5:94ce16dd20c0

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 02 Sep 2011 01:57:00 +0300
parents
children 3258d163cfbc
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <sys/time.h>
6 #ifndef __APPLE__
7 #include <GL/glut.h>
8 #else
9 #include <GLUT/glut.h>
10 #endif
12 #include "dsys2.h"
14 void disp(void);
15 void draw_teapot(float sec);
16 void reshape(int x, int y);
17 void keyb(unsigned char key, int x, int y);
18 unsigned int get_ticks(void);
20 struct dsys_demo *demo;
22 int main(int argc, char **argv)
23 {
24 float lpos[] = {-100, 100, 100, 1};
26 glutInit(&argc, argv);
27 glutInitWindowSize(800, 600);
28 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
29 glutCreateWindow("foo");
31 glutDisplayFunc(disp);
32 glutReshapeFunc(reshape);
33 glutKeyboardFunc(keyb);
34 glutIdleFunc(glutPostRedisplay);
36 glEnable(GL_LIGHTING);
37 glEnable(GL_LIGHT0);
38 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
40 glEnable(GL_CULL_FACE);
41 glEnable(GL_DEPTH_TEST);
43 if(!(demo = dsys_open("/dev/null"))) {
44 return 1;
45 }
47 glutMainLoop();
48 return 0;
49 }
52 void disp(void)
53 {
54 float sec;
56 dsys_update(demo, dsys_msec_to_dtime(get_ticks()));
57 sec = dsys_dtime_to_sec(dsys_time(demo));
59 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
61 glMatrixMode(GL_MODELVIEW);
62 glLoadIdentity();
63 glTranslatef(0, 0, -8);
64 glRotatef(30.0, 1, 0, 0);
66 draw_teapot(sec);
68 glutSwapBuffers();
69 }
71 void draw_teapot(float sec)
72 {
73 float dcol[] = {0.2, 0.4, 0.8, 1.0};
74 float scol[] = {0.8, 0.8, 0.8, 1.0};
76 glPushMatrix();
77 glRotatef(sec * 100.0, 0, 1, 0);
79 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
80 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
81 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
83 glFrontFace(GL_CW);
84 glutSolidTeapot(1.0);
85 glFrontFace(GL_CCW);
87 glPopMatrix();
88 }
90 void reshape(int x, int y)
91 {
92 glViewport(0, 0, x, y);
94 glMatrixMode(GL_PROJECTION);
95 glLoadIdentity();
96 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
97 }
99 void keyb(unsigned char key, int x, int y)
100 {
101 switch(key) {
102 case 27:
103 exit(0);
105 case ' ':
106 if(dsys_is_running(demo)) {
107 dsys_stop(demo);
108 } else {
109 dsys_start(demo);
110 }
111 break;
113 default:
114 break;
115 }
116 }
118 unsigned int get_ticks(void)
119 {
120 static struct timeval tv0;
121 struct timeval tv;
123 gettimeofday(&tv, 0);
125 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
126 tv0 = tv;
127 }
128 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
129 }