File: hanoi2.c

package info (click to toggle)
glut 3.7-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,556 kB
  • ctags: 45,170
  • sloc: ansic: 148,716; makefile: 35,208; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (444 lines) | stat: -rw-r--r-- 10,052 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <GL/glut.h>

#define MAX_DISKS 6

#define DISK_HEIGHT 0.2
#define CYL_RADIUS 0.1
#define CYL_HEIGHT 1.8
#define OVER_CLICKS 120
#define UP_CLICKS 120

enum {X=0, Y, Z};
enum {DL_POLE=1, DL_FLOOR, DL_DISK};		       /* disk must be last */

/* motion states */
enum {ST_UP=1, ST_TO_1, ST_TO_2, ST_DOWN, ST_READNEXT, ST_IDLE};

int motion = 1;
int spinning = 1;
int click = 0;
int delay = 0;
int direction;
int state=ST_READNEXT;
int c_pole;
int old_pole;
int c_disk;
int engine_fd;
int engine_pid;

float pole_offset[3][2] = { { 0, -0.575 },
			    { 1.15, 0.575 },
                            { -1.15, 0.575 } };
float disk_offset[MAX_DISKS][3] = {{ 0, -0.575, 0},
				   { 0, -0.575, 0},   
				   { 0, -0.575, 0},   
				   { 0, -0.575, 0},   
				   { 0, -0.575, 0},   
				   { 0, -0.575, 0}};
float disk_incr[3] = { 0, 0, 0 };

int num_disks;
struct {
  int num_disks;
  int disks[MAX_DISKS];
} disks_on_poles[3];

/*
 * create display list for disk of outside radius orad
 */
void
diskdlist(int dlist, float orad)
{
    GLUquadricObj *obj;

    obj = gluNewQuadric();
    if (obj == 0) {
	perror("can't alloc quadric");
	exit(1);
    }
    glNewList(dlist, GL_COMPILE);
    glPushMatrix();
      glColor4ub(205, 67, 100, 200);
      gluQuadricDrawStyle(obj, GLU_FILL);

      gluQuadricOrientation(obj, GLU_OUTSIDE);
      gluCylinder(obj, orad, orad, DISK_HEIGHT, 20, 4);

      gluQuadricOrientation(obj, GLU_INSIDE);
      gluCylinder(obj, CYL_RADIUS, CYL_RADIUS, DISK_HEIGHT, 20, 4);

      gluQuadricOrientation(obj, GLU_INSIDE);
      gluDisk(obj, CYL_RADIUS, orad, 20, 4);

      gluQuadricOrientation(obj, GLU_OUTSIDE);
      glPushMatrix();
        glTranslatef(0.0, 0.0, DISK_HEIGHT);
        gluDisk(obj, CYL_RADIUS, orad, 20, 4);
      glPopMatrix();
    glPopMatrix();
    glEndList();
}

/*
 * create display list for pole
 */
void
poledlist(int dlist)
{
    GLUquadricObj *obj;

    obj = gluNewQuadric();
    if (obj == 0) {
	perror("can't alloc quadric");
	exit(1);
    }

    glNewList(dlist, GL_COMPILE);
    glPushMatrix();
      glColor3ub(67, 205, 128);
      gluQuadricDrawStyle(obj, GLU_FILL);
      gluQuadricOrientation(obj, GLU_OUTSIDE);

      gluCylinder(obj, CYL_RADIUS, CYL_RADIUS, CYL_HEIGHT, 12, 3);

      gluQuadricOrientation(obj, GLU_INSIDE);
      gluDisk(obj, 0.0, CYL_RADIUS, 12, 3);

      gluQuadricOrientation(obj, GLU_OUTSIDE);
      glPushMatrix();
        glTranslatef(0.0, 0.0, CYL_HEIGHT);
        gluDisk(obj, 0.0, CYL_RADIUS, 12, 3);
      glPopMatrix();
    glPopMatrix();
    glEndList();

}

/*
 * create display list for floor
 */
void
floordlist(int dlist)
{
    glNewList(dlist, GL_COMPILE);
    glPushMatrix();
      glColor4ub(90, 100, 230,100);

      /* top/bottom */
      glBegin(GL_TRIANGLE_STRIP);
      glNormal3f(0.0, 0.0, 1.0);
      glVertex3f(-2.0, -2.0, 0);
      glVertex3f(2.0, -2.0, 0);
      glVertex3f(-2.0, 2.0, 0);
      glVertex3f(2.0, 2.0, 0);
      glEnd();
      glPushMatrix();
        glTranslatef(0, 0, -0.2);
        glBegin(GL_TRIANGLE_STRIP);
        glNormal3f(0.0, 0.0, -1.0);
        glVertex3f(2.0, -2.0, 0);
        glVertex3f(-2.0, -2.0, 0);
        glVertex3f(2.0, 2.0, 0);
        glVertex3f(-2.0, 2.0, 0);
        glEnd();
      glPopMatrix();

      /* edges */
      glBegin(GL_TRIANGLE_STRIP);
      glNormal3f(-1.0, 0.0, 0.0);
      glVertex3f(-2.0, -2.0, -0.2);
      glVertex3f(-2.0, -2.0, 0);
      glVertex3f(-2.0, 2.0, -0.2);
      glVertex3f(-2.0, 2.0, 0);
      glNormal3f(0.0, 1.0, 0.0);
      glVertex3f(2.0, 2.0, -0.2);
      glVertex3f(2.0, 2.0, 0);
      glNormal3f(1.0, 0.0, 0.0);
      glVertex3f(2.0, -2.0, -0.2);
      glVertex3f(2.0, -2.0, 0);
      glNormal3f(0.0, -1.0, 0.0);
      glVertex3f(-2.0, -2.0, -0.2);
      glVertex3f(-2.0, -2.0, 0);
      glEnd();
    glPopMatrix();
    glEndList();
}

/*
 * motion state machine -- idle loop
 */
void
idle(void)
{
  static int over_clicks;
  int rc;
  char next_move[3];

  if (spinning)
    click++;

  if (motion) {
      switch(state) {
	case ST_READNEXT:
	  /*
	   * read an instruction from the hanoi engine
	   */
	  rc = read(engine_fd, next_move, 3);
	  if (rc == 3 && next_move[0] == 'M') {
	      /* choose poles/disks to move */
 	      old_pole = next_move[1];
	      c_pole = next_move[2];
	      c_disk = disks_on_poles[old_pole].disks[disks_on_poles[old_pole].num_disks - 1];
	      state = ST_UP;
	      disk_incr[Z] = CYL_HEIGHT / (float)UP_CLICKS;
	  }
	  else if (rc == 3 && next_move[0] == 'D') {
	      state = ST_IDLE;
	  }
	  else if (rc != 0) {
	      fprintf(stderr,"bad read; %d, [%d%d%d]\n",
		      rc, next_move[0], next_move[1], next_move[2]);
	      exit(1);
	  }
	  /* if rc == 0, do nothing this frame */
	  break;

	case ST_UP:
	  disk_offset[c_disk][Z] += disk_incr[Z];
	  if (disk_offset[c_disk][Z] >= (CYL_HEIGHT+0.1)) {
	      state = ST_TO_1;
	      over_clicks = OVER_CLICKS;
	      disk_incr[X] = (pole_offset[c_pole][X] 
			      - pole_offset[old_pole][X]) / (float)(over_clicks-1);
	      disk_incr[Y] = (pole_offset[c_pole][Y] 
			      - pole_offset[old_pole][Y]) / (float)(over_clicks-1);
	      disk_incr[Z] = 0.0;
	  }
	  break;

	case ST_DOWN:
	  disk_offset[c_disk][Z] -= disk_incr[Z];
	  if (disk_offset[c_disk][Z] <= (disks_on_poles[c_pole].num_disks*0.2+disk_incr[Z])) {
	      disk_offset[c_disk][Z] = disks_on_poles[c_pole].num_disks*0.2;
	      disks_on_poles[old_pole].num_disks --;
	      disks_on_poles[c_pole].disks[disks_on_poles[c_pole].num_disks ++] = c_disk;
	      state = ST_READNEXT;
	  }
	  break;

	case ST_TO_1:
	case ST_TO_2:
	  disk_offset[c_disk][X] += disk_incr[X];
	  disk_offset[c_disk][Y] += disk_incr[Y];
	  over_clicks --;
	  if (over_clicks == 0) {
	      state = ST_DOWN;
	      disk_incr[X] = 0.0;
	      disk_incr[Y] = 0.0;
	      disk_incr[Z] = CYL_HEIGHT / (float)UP_CLICKS;
	      disk_offset[c_disk][X] = pole_offset[c_pole][X]; /* paranoia */
	      disk_offset[c_disk][Y] = pole_offset[c_pole][Y];
	  }
	  break;

	case ST_IDLE:
	  break;
      }
  }
  glutPostRedisplay();
}

void
draw_scene(void)
{
  int i;
  glPushMatrix();
    glRotatef(click, 0, 0, 1);
    glRotatef(click/5.0, 1, 0, 0);
    for (i=0; i<3; i++) {
      glPushMatrix();
	glTranslatef(pole_offset[i][X], pole_offset[i][Y], 0);
        glCallList(DL_POLE);
      glPopMatrix();
    }
    for(i=0; i<num_disks; i++) {
      glPushMatrix();
	glTranslatef(disk_offset[i][X], disk_offset[i][Y], disk_offset[i][Z]);
	glCallList(DL_DISK+i);
      glPopMatrix();
    }
#ifndef TOOSLOW
    glCallList(DL_FLOOR);
#endif
  glPopMatrix();
}

void
display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  draw_scene();

  glutSwapBuffers();
}

void
decide_animation(void)
{
  if(motion || spinning) {
    glutIdleFunc(idle);
  } else {
    glutIdleFunc(NULL);
  }
}

void
visible(int state)
{
  if (state == GLUT_VISIBLE) {
    decide_animation();
  } else {
    glutIdleFunc(NULL);
  }
}

void
menu(int value)
{
  switch (value) {
  case 2:
    motion = 1 - motion;
    decide_animation();
    break;
  case 3:
    spinning = 1 - spinning;
    decide_animation();
    break;
  case 666:
    kill(engine_pid, SIGTERM);
    exit(0);
  }
}

int
main(int argc, char *argv[])
{
  int c;
  int i;

  GLfloat mat_specular[] = { 0.7, 0.7, 0.7, 1.0 };
  GLfloat mat_shininess[] = { 40.0 };
  GLfloat light_position[] = { 4.5, 0.0, 4.5, 0.0 };

  glutInit(&argc, argv);

  num_disks = MAX_DISKS;
  while((c = getopt(argc, argv, "n:s:m:")) != -1) {
      switch (c) {
	case 'n':
	  num_disks = atoi(optarg);
	  if (num_disks < 1 || num_disks > MAX_DISKS) {
	      num_disks = MAX_DISKS;
	  }
	  break;
	case 's':
	  spinning = atoi(optarg) ? 1 : 0;
	  break;
	case 'm':
	  motion = atoi(optarg) ? 1 : 0;
	  break;
	default:
	  break;
      }
  }

  glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  glutCreateWindow("Hanoi");
  glutDisplayFunc(display);
  glutVisibilityFunc(visible);
  glMatrixMode(GL_PROJECTION);
  gluPerspective(40.0, 1.0, 0.1, 10.0);
  glMatrixMode(GL_MODELVIEW);
  gluLookAt(0, 5.5, 3.5,
    0, 0, 0,
    0, 0, 1);
  glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
#ifndef TOOSLOW
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  glEnable(GL_COLOR_MATERIAL);
#endif
#ifndef TOOSLOW
  glShadeModel(GL_SMOOTH);
#endif
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE);
#ifndef TOOSLOW
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
#endif
  glDepthFunc(GL_LEQUAL);
  glClearColor(0.3, 0.3, 0.3, 0.0);
#ifndef TOOSLOW
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
#endif

  glPolygonMode(GL_FRONT, GL_FILL);

  glutCreateMenu(menu);
  glutAddMenuEntry("Toggle motion", 2);
  glutAddMenuEntry("Toggle spinning", 3);
  glutAddMenuEntry("Quit", 666);
  glutAttachMenu(GLUT_RIGHT_BUTTON);
#if defined(GL_POLYGON_OFFSET_EXT)
  if (glutExtensionSupported("GL_EXT_polygon_offset")) {
    glPolygonOffsetEXT(0.5, 0.0);
    glEnable(GL_POLYGON_OFFSET_EXT);
  }
#endif

  poledlist(DL_POLE);
  floordlist(DL_FLOOR);
  
  disks_on_poles[0].num_disks = num_disks;
  for (i=0; i<num_disks; i++) {
      diskdlist(DL_DISK+i, 0.3 + i*0.1);
      disks_on_poles[0].disks[num_disks-i-1] = i;
      disk_offset[i][Z] = 0.2*(num_disks-i-1);
  }

  /*
   * start hanoi instruction engine
   */
  {
      int engine_args[2];
      extern void engine(int *);
      int p[2];

      prctl(PR_SETEXITSIG, SIGTERM);
      if (-1 == pipe(p)) {
	  perror("can't pipe");
	  exit(1);
      }
      engine_args[0] = num_disks;
      engine_args[1] = p[1];
      engine_fd = p[0];
      engine_pid = sproc((void(*)(void *))engine, PR_SALL, (void *)engine_args);
      if (engine_pid == -1) {
	  perror("can't sproc");
	  exit(1);
      }
  }

  glutMainLoop();
  /*NOTREACHED*/
  return 0;             /* ANSI C requires main to return int. */
}