dsys2

diff test.c @ 2:1705e550bd91

foo
author John Tsiombikas <nuclear@siggraph.org>
date Wed, 31 Aug 2011 05:08:54 +0300
parents
children 3258d163cfbc
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test.c	Wed Aug 31 05:08:54 2011 +0300
     1.3 @@ -0,0 +1,129 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <time.h>
     1.7 +#include <sys/time.h>
     1.8 +
     1.9 +#ifndef __APPLE__
    1.10 +#include <GL/glut.h>
    1.11 +#else
    1.12 +#include <GLUT/glut.h>
    1.13 +#endif
    1.14 +
    1.15 +#include "dsys2.h"
    1.16 +
    1.17 +void disp(void);
    1.18 +void draw_teapot(float sec);
    1.19 +void reshape(int x, int y);
    1.20 +void keyb(unsigned char key, int x, int y);
    1.21 +unsigned int get_ticks(void);
    1.22 +
    1.23 +struct dsys_demo *demo;
    1.24 +
    1.25 +int main(int argc, char **argv)
    1.26 +{
    1.27 +	float lpos[] = {-100, 100, 100, 1};
    1.28 +
    1.29 +	glutInit(&argc, argv);
    1.30 +	glutInitWindowSize(800, 600);
    1.31 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    1.32 +	glutCreateWindow("foo");
    1.33 +
    1.34 +	glutDisplayFunc(disp);
    1.35 +	glutReshapeFunc(reshape);
    1.36 +	glutKeyboardFunc(keyb);
    1.37 +	glutIdleFunc(glutPostRedisplay);
    1.38 +
    1.39 +	glEnable(GL_LIGHTING);
    1.40 +	glEnable(GL_LIGHT0);
    1.41 +	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    1.42 +
    1.43 +	glEnable(GL_CULL_FACE);
    1.44 +	glEnable(GL_DEPTH_TEST);
    1.45 +
    1.46 +	if(!(demo = dsys_open("/dev/null"))) {
    1.47 +		return 1;
    1.48 +	}
    1.49 +
    1.50 +	glutMainLoop();
    1.51 +	return 0;
    1.52 +}
    1.53 +
    1.54 +
    1.55 +void disp(void)
    1.56 +{
    1.57 +	float sec;
    1.58 +
    1.59 +	dsys_update(demo, dsys_msec_to_dtime(get_ticks()));
    1.60 +	sec = dsys_dtime_to_sec(dsys_time(demo));
    1.61 +
    1.62 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.63 +
    1.64 +	glMatrixMode(GL_MODELVIEW);
    1.65 +	glLoadIdentity();
    1.66 +	glTranslatef(0, 0, -8);
    1.67 +	glRotatef(30.0, 1, 0, 0);
    1.68 +
    1.69 +	draw_teapot(sec);
    1.70 +
    1.71 +	glutSwapBuffers();
    1.72 +}
    1.73 +
    1.74 +void draw_teapot(float sec)
    1.75 +{
    1.76 +	float dcol[] = {0.2, 0.4, 0.8, 1.0};
    1.77 +	float scol[] = {0.8, 0.8, 0.8, 1.0};
    1.78 +
    1.79 +	glPushMatrix();
    1.80 +	glRotatef(sec * 100.0, 0, 1, 0);
    1.81 +
    1.82 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
    1.83 +	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
    1.84 +	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
    1.85 +
    1.86 +	glFrontFace(GL_CW);
    1.87 +	glutSolidTeapot(1.0);
    1.88 +	glFrontFace(GL_CCW);
    1.89 +
    1.90 +	glPopMatrix();
    1.91 +}
    1.92 +
    1.93 +void reshape(int x, int y)
    1.94 +{
    1.95 +	glViewport(0, 0, x, y);
    1.96 +
    1.97 +	glMatrixMode(GL_PROJECTION);
    1.98 +	glLoadIdentity();
    1.99 +	gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
   1.100 +}
   1.101 +
   1.102 +void keyb(unsigned char key, int x, int y)
   1.103 +{
   1.104 +	switch(key) {
   1.105 +	case 27:
   1.106 +		exit(0);
   1.107 +
   1.108 +	case ' ':
   1.109 +		if(dsys_is_running(demo)) {
   1.110 +			dsys_stop(demo);
   1.111 +		} else {
   1.112 +			dsys_start(demo);
   1.113 +		}
   1.114 +		break;
   1.115 +
   1.116 +	default:
   1.117 +		break;
   1.118 +	}
   1.119 +}
   1.120 +
   1.121 +unsigned int get_ticks(void)
   1.122 +{
   1.123 +	static struct timeval tv0;
   1.124 +	struct timeval tv;
   1.125 +
   1.126 +	gettimeofday(&tv, 0);
   1.127 +
   1.128 +	if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
   1.129 +		tv0 = tv;
   1.130 +	}
   1.131 +	return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
   1.132 +}