File: dials2.c

package info (click to toggle)
glut 3.6-7
  • links: PTS
  • area: main
  • in suites: slink
  • size: 9,104 kB
  • ctags: 15,230
  • sloc: ansic: 131,032; makefile: 2,261; ada: 2,012; yacc: 473; fortran: 290; lex: 131; sed: 49; csh: 38; sh: 4
file content (140 lines) | stat: -rw-r--r-- 2,825 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

/* 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>

#define NUM_DIALS 8
#define NUM_BUTTONS 32

int *dials, *buttons;

#undef PI /* Some systems may have this defined. */
#define PI            3.14159265358979323846

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*PI; angle >= 0; angle -= PI/12) {
      glVertex2f(r*cos(angle),r*sin(angle));
   }
   glEnd();
   glColor3f(0,0,1);
   glBegin(GL_LINES);
   glVertex2f(0,0);
   glVertex2f(r*cos(dir*PI/180),r*sin(dir*PI/180));
   glEnd();
   glPopMatrix();
}

void
displayDials(void)
{
  int i;

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

void
displayButtons(void)
{
  int i, n;

  glBegin(GL_QUADS);
  for(i=0,n=0;i<NUM_BUTTONS;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+250,(n/6)*40+10);
    glVertex2f((n%6)*40+270,(n/6)*40+10);
    glVertex2f((n%6)*40+270,(n/6)*40+30);
    glVertex2f((n%6)*40+250,(n/6)*40+30);
  }
  glEnd();
}

void
display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  displayDials();
  displayButtons();
  glutSwapBuffers();
}

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

void
dodial(int dial, int value)
{
  if(dial > 0 && dial <= NUM_DIALS) {
  dials[dial - 1] = value % 360;
  glutPostRedisplay();
  }
}

void
dobutton(int button, int state)
{
  if(button > 0 && button <= NUM_BUTTONS) {
    buttons[button-1] = (state == GLUT_DOWN);
    glutPostRedisplay();
  }
}

int
main(int argc, char **argv)
{
  int width, height;
  glutInit(&argc, argv);
  dials = (int*) calloc(NUM_DIALS, sizeof(int));
  buttons = (int*) calloc(NUM_BUTTONS, sizeof(int));
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  width = 240 + 240;
  height = 100*((NUM_DIALS+1)/2) + 20;
  if(height < 240) height = 240;
  glutInitWindowSize(width, height);
  glutCreateWindow("GLUT dials & buttons");
  glClearColor(0.5, 0.5, 0.5, 1.0);
  glLineWidth(3.0);
  glutDialsFunc(dodial);
  glutButtonBoxFunc(dobutton);
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutInitWindowSize(240, 240);
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}