File: dials.c

package info (click to toggle)
glut 3.7-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 12,360 kB
  • ctags: 39,914
  • sloc: ansic: 148,715; makefile: 28,471; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (145 lines) | stat: -rw-r--r-- 3,126 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

/* Copyright (c) Mark J. Kilgard, 1994. */

/* This program is freely distributable without licensing fees 
   and is provided without guarantee or warrantee expressed or 
   implied. This program is -not- in the public domain. */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

int *dials, *buttons;
int numdials, numbuttons;
int dw, bw;

/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

void
drawCircle(int x, int y, int r, int dir)
{
   float angle;

   glPushMatrix();
   glTranslatef(x,y,0);
   glBegin(GL_TRIANGLE_FAN);
   glVertex2f(0,0);
   for(angle = 2*M_PI; angle >= 0; angle -= M_PI/12) {
      glVertex2f(r*cos(angle),r*sin(angle));
   }
   glEnd();
   glColor3f(0,0,1);
   glBegin(GL_LINES);
   glVertex2f(0,0);
   glVertex2f(r*cos(dir*M_PI/180),r*sin(dir*M_PI/180));
   glEnd();
   glPopMatrix();
}

void
displayDials(void)
{
  int i;

  glClear(GL_COLOR_BUFFER_BIT);
  for(i=0;i<numdials;i++) {
    glColor3f(0, 1, 0);
    drawCircle(100 + (i%2) * 100, 100 + (i/2) * 100, 40, -dials[i]+90);
  }
  glutSwapBuffers();
}

void
displayButtons(void)
{
  int i, n;

  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_QUADS);
  for(i=0,n=0;i<32;i++,n++) {
    switch(n) {
    case 0:
    case 5:
    case 30:
      n++;
    }
    if(buttons[i]) {
      glColor3f(1,0,0);
    } else {
      glColor3f(1,1,1);
    }
    glVertex2f((n%6)*40+10,(n/6)*40+10);
    glVertex2f((n%6)*40+30,(n/6)*40+10);
    glVertex2f((n%6)*40+30,(n/6)*40+30);
    glVertex2f((n%6)*40+10,(n/6)*40+30);
  }
  glEnd();
  glutSwapBuffers();
}

void
reshape(int w, int h)
{
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0, w, 0, h);
  if(glutGetWindow() == bw) {
     glScalef(1, -1, 1);
     glTranslatef(0, -h, 0);
  }
  glMatrixMode(GL_MODELVIEW);
}

void
dodial(int dial, int value)
{
  dials[dial - 1] = value % 360;
  glutPostWindowRedisplay(dw);
}

void
dobutton(int button, int state)
{
  if(button <= numbuttons) {
    buttons[button-1] = (state == GLUT_DOWN);
    glutPostWindowRedisplay(bw);
  }
}

int
main(int argc, char **argv)
{
  glutInit(&argc, argv);
  numdials = glutDeviceGet(GLUT_NUM_DIALS);
  if(numdials <= 0) {
     fprintf(stderr, "dials: No dials available\n");
     exit(1);
  }
  numbuttons = glutDeviceGet(GLUT_NUM_BUTTON_BOX_BUTTONS);
  dials = (int*) calloc(numdials, sizeof(int));
  buttons = (int*) calloc(numbuttons, sizeof(int));
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowSize(300, ((numdials+1)/2) * 100 + 100);
  dw = glutCreateWindow("GLUT dials");
  glClearColor(0.5, 0.5, 0.5, 1.0);
  glLineWidth(3.0);
  glutDialsFunc(dodial);
  glutButtonBoxFunc(dobutton);
  glutDisplayFunc(displayDials);
  glutReshapeFunc(reshape);
  glutInitWindowSize(240, 240);
  bw = glutCreateWindow("GLUT button box");
  glClearColor(0.5, 0.5, 0.5, 1.0);
  glutDisplayFunc(displayButtons);
  glutReshapeFunc(reshape);
  glutDialsFunc(dodial);
  glutButtonBoxFunc(dobutton);
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}