summaryrefslogtreecommitdiff
path: root/src/hackvr.c
blob: f3df371187fa894126239169ff1c8e8bfa7135db (plain) (blame)
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#define _POSIX_C_SOURCE 200809L //for fileno and strdup
#include <stdio.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/select.h> //code to use select instead of non-blocking is commented out. might decide to use it later.
#include <time.h>
#include <math.h>

#include "config.h"
#include "common.h"
#include "math.h"
#ifdef GRAPHICAL
#include "graphics_c3.h"
extern struct gra_global gra_global;
#endif

//TODO: optimizations
//TODO: store caches of cs_t, c2_t, and c3_t numbers.
//TODO: don't forget to remake gophervr with this.
//TODO: XSegment or line structs to store some shit?
//TODO: line and triangle intersection for finding what object was clicked on
//if I don't do hiliting I only need to calculate that upon click.

//TODO: will have to make some pixmaps get resized when the window does.
//for now set them to be as big as you think you'll ever resize the window to.

struct global global;

//might be able to make this faster by just using fgets() and not using recursion and malloc.
/* does not return the newline. */
char *read_line_hack(FILE *fp,int len) {
 short in;
 char *t;
 switch(in=fgetc(fp)) {
  case '\n':
   t=malloc(len+1);
   t[len]=0;
   return t;
  case -1:
   return 0;
  default:
   if((t=read_line_hack(fp,len+1))) t[len]=in;
   break;
 }
 return t;
}

//warning: clobbers input
//skips leading and trailing space.
//compresses multiple spaces to one.
//returns pointer to array of strings. second argument is return by reference length of returned array.
char **line_splitter(char *line,int *rlen) {
 char **a;
 int len,i=0;
 len=1;//we're just counting how much we'll need the first loop through.
 for(i=0;line[i] && line[i] == ' ';i++);//skip leading space
 for(;line[i];len++) {
  for(;line[i] && line[i] != ' ';i++);//skip rest of data
  for(;line[i] && line[i] == ' ';i++);//skip rest of space
 }
 a=malloc(sizeof(char *) * len+1);
 a[len]=0;
 len=0;//reuse!
 for(i=0;line[i] && line[i] == ' ';i++);//skip leading space
 a[len]=line+i;
 for(;;) {
  for(;line[i] && line[i] != ' ';i++);//skip rest of data
  if(!line[i]) break;
  line[i++]=0;
  for(;line[i] && line[i] == ' ';i++);//skip rest of space
  if(!line[i]) break;
  a[++len]=line+i;
 }
 a[++len]=0;
 *rlen=len;
 return a;
}

//this function is like strcmp
//but the first argument MAY use a simple * glob ONLY at the end.
//this function may be changed to allow globs in different spots later.
int glob_match(char *a,char *b) {
  if(strchr(a,'*')) {
    return strncmp(a,b,strchr(a,'*')-a-1);//hack? sure.
  }
  return strcmp(a,b);
}

int load_stdin() {//this function returns -1 to quit, 0 to not ask for a redraw, and 1 to ask for redraw
// struct c3_shape s;
// struct c3_line l;
 c3_group_rot_t *gr;
 char *command;
 char *line=0;
 char *id;
 char **a=0;
 int len;
 int ret=0;
 int j,k,l;
 int counter;
 //struct timeval timeout;
 //fd_set master;
 //fd_set readfs;
 //FD_ZERO(&master);
 //FD_ZERO(&readfs);
 //FD_SET(0,&master);//just stdin.
 int i;//used to store the last triangle. even though I have a global for that. >_>

// fprintf(stderr,"# entering load_stdin()\n");
//#ifdef _HACKVR_USE_NONBLOCK_
 for(i=0;global.shape[i];i++) ;//hop to the end.
 fcntl(0,F_SETFL,O_NONBLOCK);
 if(feof(stdin))  {
  clearerr(stdin);
 }
//#else
/* readfs=master;
 timeout.tv_sec=0;
 timeout.tv_usec=1;
 if((j=select(1,&readfs,0,0,&timeout)) == -1) {
  perror("select");
  return 0;
 }
 if(FD_ISSET(0,&readfs)) {*/
//#endif
 counter=0;
// printf("#right before main read loop\n");
// fflush(stdout);
 for(counter=0;counter < 100 && (line=line?free(line),read_line_hack(stdin,0):read_line_hack(stdin,0));counter++) {//load as long there's something to load
  if(*line == '#') continue;
//  fprintf(stderr,"# read command: %s\n",line);
 if(a) free(a);//use a static char pointer array so I don't have to use the heap. possible optimization.
 a=line_splitter(line,&len);
//  for(i=0;i<len;i++) {
//   printf("\"%s\" ",a[i]);
//  }
//  printf("\n");
  id=a[0];
  if(len > 1) {
   command=a[1];
  }
  if(len < 2) {
   if(!strcmp(id,"help")) {
#ifdef GRAPHICAL
    fprintf(stderr,"# NOT built headless.\n");
#else
    fprintf(stderr,"# built headless.\n");
#endif
    fprintf(stderr,"# command format:\n");
    fprintf(stderr,"# group names can be globbed in some cases to operate on multiple groups\n");
    fprintf(stderr,"# groupnam* command arguments\n");
    fprintf(stderr,"# commands:\n");
    fprintf(stderr,"#   deleteallexcept grou*\n");
    fprintf(stderr,"#   deletegroup grou*\n");
    fprintf(stderr,"#   assimilate grou*\n");
    fprintf(stderr,"#   renamegroup group\n");
    fprintf(stderr,"#   control grou* [globbing this group could have fun effects]\n");
    fprintf(stderr,"#   dump\n");
    fprintf(stderr,"#   quit\n");
    fprintf(stderr,"#   set\n");
    fprintf(stderr,"#   addshape color N x1 y1 z1 ... xN yN zN\n");
    fprintf(stderr,"#   export grou*\n");
    fprintf(stderr,"# * scaleup x y z\n");
    fprintf(stderr,"# * move x y z\n");
    fprintf(stderr,"# * move forward|backward|left|right\n");
    fprintf(stderr,"# that is all.\n");
    continue;
   } else {
    //fprintf(stderr,"# ur not doing it right. '%s'\n",id);
    continue;
   }
  }
  ret=1;
  if(!strcmp(command,"deleteallexcept")) {
   if(len == 3) {
    for(j=0;global.shape[j] && j < MAXSHAPES;j++) {//mark first. compress later.
     if(glob_match(a[2],global.shape[j]->id)) {
      free(global.shape[j]->id);
      free(global.shape[j]);
      global.shape[j]=0;
     }
    }
    for(k=0;k<j;k++) {
     if(global.shape[k]) continue;
     for(l=k;global.shape[l] == 0 && l<j;l++);
     global.shape[k]=global.shape[l];
     global.shape[l]=0;
    }
    ret=1;
    //now do the same stuff but for the group_rot structs.
    for(j=1;global.group_rot[j] && j < MAXSHAPES;j++) {//start at 1 so we skip the camera.
     if(glob_match(a[2],global.group_rot[j]->id)) {
      free(global.group_rot[j]->id);
      free(global.group_rot[j]);
      global.group_rot[j]=0;
     }
    }
    for(k=0;k<j;k++) {
     if(global.group_rot[k]) continue;
     for(l=k;global.group_rot[l] == 0 && l<j;l++);
     global.group_rot[k]=global.group_rot[l];
     global.group_rot[l]=0;
    }
    continue;
   }
  }
  if(!strcmp(command,"apply")) {
   /*gr=get_group_relative(a[2]);
   if(gr) {
     for(j=0;global.shape[j] && j < MAXSHAPES;j++) {
       if(!glob_match(a[2],global.shape[j].id)) {
         for(k=0;k<global.shape[j].len+(global.shape[j].len==1);k++) {
           //apply the movement stored in this gr to the shapes themselves.
           //after taking into account the rotation.
           //this code is the same as the code in graphics_c3 but this is object specific.
           global.shape[j].p[k]=c3_add(gr->p,rotate_c3_yr(global.shape[j].p[k]);
         }
         gr->p.x=gr->p.y=gr->p.z=0;

         gr->r.x=0;
         gr->r.y=0;
         gr->r.z=0;
       }
     }
     //we don't need this gr anymore.
     //we could clean it up, but its values are already zero.
   }*/
   printf("# this group doesn't have a gr.\n");
  }
  if(!strcmp(command,"deletegroup")) {//should the grouprot get deleted too? sure...
   if(len == 3) {
    for(j=0;global.shape[j] && j < MAXSHAPES;j++) {
     if(!glob_match(a[2],global.shape[j]->id)) {
      free(global.shape[j]->id);
      free(global.shape[j]);
      global.shape[j]=0;
     }
    }
    //we now have an array that needs to be compressed.
    //max length of j.
    for(k=0;k<j;k++) {//now... we go from the beginning
     if(global.shape[k]) continue;
     for(l=k;global.shape[l] == 0 && l<j;l++);
     global.shape[k]=global.shape[l];
     global.shape[l]=0;
    }
    //now for the group_rot struct that goes with it. there should only be one, but might be a few due to bugs elsewhere. heh. let's just get all of them I guess.
    for(j=1;global.group_rot[j] && j < MAXSHAPES;j++) {//start at 1 to skip passed the camera. it isn't malloc()d and will crash if we try to free it.
     if(!glob_match(a[2],global.group_rot[j]->id)) {
      free(global.group_rot[j]->id);
      free(global.group_rot[j]);
      global.group_rot[j]=0;
     }
    }
    for(k=0;k<j;k++) {
     if(global.group_rot[k]) continue;
     for(l=k;global.group_rot[l] == 0 && l<j;l++);
     global.group_rot[k]=global.group_rot[l];
     global.group_rot[l]=0;
    }
    ret=1;
    continue;
   }
  }
  if(!strcmp(command,"assimilate")) {//um... what do we do with the group_rotation?
   if(len == 3) {
    for(j=0;global.shape[j];j++) {
     if(!glob_match(a[2],global.shape[j]->id)) {
      free(global.shape[j]->id);
      global.shape[j]->id=strdup(id);
     }
    }
   }
   ret=1;
   continue;
  }
  if(!strcmp(command,"renamegroup")) {//this command doesn't need globbing
   if(len == 4) {
    for(j=0;global.shape[j];j++) {
     if(!strcmp(a[2],global.shape[j]->id)) {
      free(global.shape[j]->id);
      global.shape[j]->id=strdup(a[3]);
     }
    }
    gr=get_group_relative(a[2]);//this shouldn't be used here.
    if(gr) {
     free(gr->id);
     gr->id=strdup(a[3]);
    }
   }
   ret=1;
   continue;
  }
  if(!strcmp(command,"status")) {
#ifdef GRAPHICAL
   fprintf(stderr,"# fps: %d\n",gra_global.fps);
   continue;
#endif
  }
  if(!strcmp(command,"dump")) {//same as debug output... and the periodic data.
   printf("%s set global.camera.p.x %f\n",global.user,global.camera.p.x);
   printf("%s set global.camera.p.y %f\n",global.user,global.camera.p.y);
   printf("%s set global.camera.p.z %f\n",global.user,global.camera.p.z);
   printf("%s set global.camera.r.x %d\n",global.user,global.camera.r.x.d);
   printf("%s set global.camera.r.y %d\n",global.user,global.camera.r.y.d);
   printf("%s set global.camera.r.z %d\n",global.user,global.camera.r.z.d);
   printf("%s set global.zoom %f\n",global.user,global.zoom);
   continue;
  }
  if(!strcmp(command,"quit")) {
   return -1;
  }
  if(!strcmp(command,"set")) { //set variable //TODO: add more things to this.
   if(len != 3 && len != 4) continue;
   if(len == 4) {
    if(0);
#ifdef GRAPHICAL
    else if(!strcmp(a[2],"camera.p.x")) global.camera.p.x=strtold(a[3],0);
    else if(!strcmp(a[2],"camera.p.y")) global.camera.p.y=strtold(a[3],0);
    else if(!strcmp(a[2],"camera.p.z")) global.camera.p.z=strtold(a[3],0);
    else if(!strcmp(a[2],"global.zoom")) global.zoom=strtold(a[3],0);
    else if(!strcmp(a[2],"camera.r.x")) global.camera.r.x.d=atoi(a[3]);
    else if(!strcmp(a[2],"camera.r.y")) global.camera.r.y.d=atoi(a[3]);
    else if(!strcmp(a[2],"camera.r.z")) global.camera.r.z.d=atoi(a[3]);
#endif
    else fprintf(stderr,"# unknown variable: %s\n",a[2]);
    ret=1;
    continue;
   }
#ifdef GRAPHICAL
   if(!strcmp(a[2],"global.beep")) global.beep=1;
   else if(!strcmp(a[2],"force_redraw")) gra_global.force_redraw^=1;
   else if(!strcmp(a[2],"red_and_blue")) { gra_global.red_and_blue^=1; set_aspect_ratio(); }
#endif
   else { fprintf(stderr,"# unknown variable: %s\n",a[2]); continue; }
   fprintf(stderr,"# %s toggled!\n",a[2]);
   ret=1;
   continue;
  }
  if(!strcmp(command,"control")) {//change what shape key commands  affect.
   if(len > 2) {
    free(global.user);
    global.user=strdup(a[2]); // :D
   }
   ret=0;//doesn't change anything yet...
   continue;
  }
  if(!strcmp(command,"addshape")) {//need to add a grouprot with this.
   if(len > 3) {
    if(len != ((strtold(a[3],0)+(strtold(a[3],0)==1))*3)+4) {
     fprintf(stderr,"# ERROR: wrong amount of parts for addshape. got: %d expected %d\n",len,((int)strtold(a[3],0)+(strtold(a[3],0)==1))*3+4);
     fprintf(stderr,"# usage: addshape color number x y z x y z repeated number of time.\n");
     continue;
    }
    for(i=0;global.shape[i];i++) { if(i>= MAXSHAPES) abort();}//just take me to the end.
    global.shape[i]=malloc(sizeof(struct c3_shape));
    global.shape[i]->len=strtold(a[3],0);
    global.shape[i]->id=strdup(id);
    global.shape[i]->attrib.col=strtold(a[2],0);
    global.shape[i]->attrib.lum=0;
    for(j=0;j < global.shape[i]->len+(global.shape[i]->len==1);j++) {
     global.shape[i]->p[j].x=strtold(a[(j*3)+4],0);//second arg is just for a return value. set to 0 if you don't want it.
     global.shape[i]->p[j].y=strtold(a[(j*3)+5],0);
     global.shape[i]->p[j].z=strtold(a[(j*3)+6],0);
    }
    i++;
    global.shapes=i;
    global.shape[i]=0;

    for(i=0;global.group_rot[i];i++) {
     if(i >= MAXSHAPES) abort();
     if(!strcmp(global.group_rot[i]->id,id)) {
      break;
     }
    }
    if(global.group_rot[i] == 0) {//we have ourselves a new grouprot!
     global.group_rot[i]=malloc(sizeof(c3_group_rot_t));
     global.group_rot[i]->id=strdup(id);
     global.group_rot[i+1]=0;
     global.group_rot[i]->p.x=0;
     global.group_rot[i]->p.y=0;
     global.group_rot[i]->p.z=0;
     global.group_rot[i]->r.x=(degrees){0};
     global.group_rot[i]->r.y=(degrees){0};
     global.group_rot[i]->r.z=(degrees){0};
    }
   }
   ret=1;
   continue;
  }
  if(!strcmp(command,"export")) {//dump shapes and group rotation for argument (or all if arg is *)
   if(len > 2) {
    for(i=0;global.shape[i];i++) {//require a[2], if not it'll segfault. derrrr, epoch.
     if(!glob_match(a[2],global.shape[i]->id)) {
      printf("%s_%s addshape %d %d",id,global.shape[i]->id,global.shape[i]->attrib.col,global.shape[i]->len);
      for(j=0;j < global.shape[i]->len+(global.shape[i]->len==1);j++) {
       printf(" %f %f %f",global.shape[i]->p[j].x,global.shape[i]->p[j].y,global.shape[i]->p[j].z);
      }//possible TODO: should I combine the string and output it all at once instead of throughout a loop?
      printf("\n");
     }
    }
    for(i=0;global.group_rot[i];i++) {
     if(!glob_match(a[2],global.group_rot[i]->id)) {
      printf("%s_%s rotate %d %d %d\n",id,a[2],global.group_rot[i]->r.x.d,global.group_rot[i]->r.y.d,global.group_rot[i]->r.z.d);
      printf("%s_%s move %f %f %f\n",id,a[2],global.group_rot[i]->p.x,global.group_rot[i]->p.y,global.group_rot[i]->p.z);
     }
    }
   }
   continue;
  }
//should scaleup even be inside hackvr? seems like something an external program could do... but it wouldn't act on hackvr's state. so nevermind.
  if(!strcmp(command,"scaleup")) {//should this scale separately so it can be a deform too?
   for(i=0;global.shape[i];i++) {
    if(!glob_match(id,global.shape[i]->id)) { //we're allowing globbing in this command I guess.
     for(j=0;j < global.shape[i]->len+(global.shape[i]->len==1);j++) {
      global.shape[i]->p[j].x*=strtold(a[2],0);
      global.shape[i]->p[j].y*=strtold(len>4?a[3]:a[2],0);
      global.shape[i]->p[j].z*=strtold(len>4?a[4]:a[2],0);
     }
    }
   }
   ret=1;
   continue;
  }
  if(!strcmp(command,"rotate")) {
   if(len > 4) {
    for(i=0;global.group_rot[i];i++) {
     if(!strcmp(global.group_rot[i]->id,id)) {
      break;
     }
    }
    if(global.group_rot[i] == 0) {//we have ourselves a new grouprot!
     global.group_rot[i]=malloc(sizeof(c3_group_rot_t));
     global.group_rot[i]->id=strdup(id);
     global.group_rot[i+1]=0;
     global.group_rot[i]->p.x=0;//only set these if new.
     global.group_rot[i]->p.y=0;
     global.group_rot[i]->p.z=0;
    }
    global.group_rot[i]->r.x=(degrees){(a[2][0]=='+'?global.group_rot[i]->r.x.d:0)+atoi(a[2]+(a[2][0]=='+'))};
    global.group_rot[i]->r.y=(degrees){(a[3][0]=='+'?global.group_rot[i]->r.y.d:0)+atoi(a[3]+(a[3][0]=='+'))};
    global.group_rot[i]->r.z=(degrees){(a[4][0]=='+'?global.group_rot[i]->r.z.d:0)+atoi(a[4]+(a[4][0]=='+'))};
    //now to sanitize them into 0 <= degrees < 360
    global.group_rot[i]->r.x.d -= (-(global.group_rot[i]->r.x.d < 0)+(global.group_rot[i]->r.x.d / 360)) * 360;
    global.group_rot[i]->r.y.d -= (-(global.group_rot[i]->r.y.d < 0)+(global.group_rot[i]->r.y.d / 360)) * 360;
    global.group_rot[i]->r.z.d -= (-(global.group_rot[i]->r.z.d < 0)+(global.group_rot[i]->r.z.d / 360)) * 360;
   }
   ret=1;
   continue;
  }
  if(!strcmp(command,"move")) {
   if(len > 2) {
    for(i=0;global.group_rot[i];i++) {
     if(!strcmp(global.group_rot[i]->id,id)) {
      break;
     }
    }
    if(global.group_rot[i] == 0) {//we have ourselves a new grouprot!
     global.group_rot[i]=malloc(sizeof(c3_group_rot_t));
     global.group_rot[i]->id=strdup(id);
     global.group_rot[i+1]=0;
     global.group_rot[i]->r.x=(degrees){0};//only set these if new.
     global.group_rot[i]->r.y=(degrees){0};
     global.group_rot[i]->r.z=(degrees){0};
    }
   }
   if(len > 4) { //if we have > 4 we're doing relative movement
    global.group_rot[i]->p.x=(a[2][0]=='+'?global.group_rot[i]->p.x:0)+strtold(a[2]+(a[2][0]=='+'),0);
    global.group_rot[i]->p.y=(a[3][0]=='+'?global.group_rot[i]->p.y:0)+strtold(a[3]+(a[3][0]=='+'),0);
    global.group_rot[i]->p.z=(a[4][0]=='+'?global.group_rot[i]->p.z:0)+strtold(a[4]+(a[4][0]=='+'),0);
   }
   else if(len > 2) {
    if(!strcmp(a[2],"forward")) {
     //global.
    } else if(!strcmp(a[2],"backward")) {

    } else if(!strcmp(a[2],"up")) {

    } else if(!strcmp(a[2],"down")) {

    } else if(!strcmp(a[2],"left")) {

    } else if(!strcmp(a[2],"right")) {

    } else {
     fprintf(stderr,"# dunno what direction you're talking about. try up, down, left, right, forward, or backward\n");
    }
   } else {
    fprintf(stderr,"# ERROR: wrong amount of parts for move. got: %d expected: 4 or 2\n",len);
   }
   ret=1;
   continue;
  }
  fprintf(stderr,"# I don't know what command you're talking about. %s\n",command);
  //I used to have free(line) here, but this place is never gotten to if a command is found so it wasn't getting released.
 }
 return ret;
}

int export_file(FILE *fp) {//not used yet. maybe export in obj optionally? no. that should be an external program
// struct c3_shape *to;
// int i;
// for(i=0;global.shape[i];i++) {
//  to=global.shape[i];
//  printf("%s addshape %f %f %f %f %f %f %f %f %f\n",to->id,to->p1.x,to->p1.y,to->p1.z,to->p2.x,to->p2.y,to->p2.z,to->p3.x,to->p3.y,to->p3.z);
// }
 return 0;
}

int main(int argc,char *argv[]) {
  char redraw;
  c3_t old_p;
  c3_rot_t old_r;
  if(argc < 2) {
   fprintf(stderr,"usage: hackvr yourname < from_others > to_others\n");
   return 1;
  } else {
   global.user=strdup(argv[1]);
  }
  fcntl(1,F_SETFL,O_NONBLOCK);//won't work
  setbuf(stdin,0);
  setbuf(stdout,0);
  global.debug=DEBUG;
  global.periodic_output=PERIODIC_OUTPUT;
  global.beep=0;
#ifdef GRAPHICAL
  graphics_init();
#endif
  fprintf(stderr,"# entering main loop\n");
  for(;;) {
    switch(redraw=load_stdin()) {
     case -1:
      return 0;
      break;
     case 0:
      break;
     default:
      break;
    }
    if(global.periodic_output == 1) {//this is the same type of thing the debug output does.
     global.periodic_output = PERIODIC_OUTPUT;
     //output any difference between current camera's state
     //and the camera state the last time we output it.
     if(memcmp(&old_p,&global.camera.p,sizeof(c3_t))) {
      old_p.x=global.camera.p.x;
      old_p.z=global.camera.p.z;
      old_p.y=global.camera.p.y;
      printf("%s move %f %f %f\n",global.user,old_p.x,old_p.y,old_p.z);
     }
     if(memcmp(&old_r,&global.camera.r,sizeof(c3_rot_t))) {
      old_r.x=global.camera.r.x;
      old_r.y=global.camera.r.y;
      old_r.z=global.camera.r.z;
      printf("%s rotate %d %d %d\n",global.user,global.camera.r.x.d,global.camera.r.y.d,global.camera.r.z.d);
     }
    }
    global.periodic_output--;
#ifdef GRAPHICAL
    //input_event_handler();//keyboard, mouse AND joysticks all at once?
    graphics_event_handler(redraw);//this thing should call draw_screen when it needs to.
#endif
   sleep(.01);
  }
  return 0;
}