home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / avril / input.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  8KB  |  239 lines

  1. /* Simple keyboard and mouse support for AVRIL demos */
  2.  
  3. /* Written by Bernie Roehl, April 1994 */
  4.  
  5. /* Copyright 1994 by Bernie Roehl */
  6.  
  7. #include "avril.h"
  8. #include "avrilkey.h"
  9. #include "avrildrv.h"
  10. #include <math.h>  /* floor() */
  11.  
  12. vrl_Object *active_object = NULL;
  13.  
  14. static int showhud = 0;
  15.  
  16. #define BLACKCOLOR 0x00000000L
  17. #define WHITECOLOR 0xFFFFFFFFL
  18.  
  19. void vrl_ApplicationDrawOver(vrl_RenderStatus *stat)
  20.     {
  21.     vrl_ScreenPos wleft, wtop, wright, wbottom;  /* window coordinates */
  22.     vrl_ScreenPos wwidth, wheight;   /* window dimensions */
  23.     vrl_Camera *cam = vrl_WorldGetCamera();
  24.     char buff[100];
  25.     vrl_DisplayGetWindow(&wleft, &wtop, &wright, &wbottom);
  26.     wwidth = wright - wleft + 1;
  27.     wheight = wbottom - wtop + 1;
  28.     if (vrl_ConfigGetPositionDisplay())
  29.         {
  30.         sprintf(buff, "Position: %ld,%ld",
  31.             (long) floor(scalar2float(vrl_CameraGetWorldX(cam))),
  32.             (long) floor(scalar2float(vrl_CameraGetWorldZ(cam))));
  33.         if (vrl_DisplayGetTextWidth(buff) + 10 < wwidth-1)
  34.             vrl_UserInterfaceDropText(10, 10, WHITECOLOR, buff);
  35.         }
  36.     if (vrl_ConfigGetFramerateDisplay())
  37.         {
  38.         sprintf(buff, "Frames/sec: %ld", vrl_SystemGetFrameRate());
  39.         if (vrl_DisplayGetTextWidth(buff) + 5 < wwidth-1)
  40.             vrl_UserInterfaceDropText(5,
  41.                 wheight - vrl_DisplayGetTextHeight(buff) - 10,
  42.                 WHITECOLOR, buff);
  43.         }
  44.     if (vrl_ConfigGetCompassDisplay())
  45.         vrl_UserInterfaceDrawCompass(cam, wwidth - 70, 40, 35);
  46.     if (showhud)
  47.         {
  48.         sprintf(buff, "%c%c%c",
  49.             stat->memory ?  'M' : ' ',
  50.             stat->objects ? 'O' : ' ',
  51.             stat->facets ?  'F' : ' ');
  52.         if (vrl_DisplayGetTextWidth(buff) + 10 < wwidth-1)
  53.             vrl_UserInterfaceDropText(10, 20, WHITECOLOR, buff);
  54.         }
  55.     if (vrl_MouseGetUsage())
  56.         {
  57.         vrl_Device *dev = vrl_MouseGetPointer();
  58.         if (dev)
  59.             {
  60.             int x = vrl_DeviceGetCenter(dev, X);
  61.             int y = vrl_DeviceGetCenter(dev, Y);
  62.             int deadx = vrl_DeviceGetDeadzone(dev, X);
  63.             int deady = vrl_DeviceGetDeadzone(dev, Y);
  64.             /* white inner box */
  65.             vrl_DisplayLine(x - deadx, y - deady, x + deadx, y - deady, WHITECOLOR);
  66.             vrl_DisplayLine(x - deadx, y + deady, x + deadx, y + deady, WHITECOLOR);
  67.             vrl_DisplayLine(x - deadx, y - deady, x - deadx, y + deady, WHITECOLOR);
  68.             vrl_DisplayLine(x + deadx, y - deady, x + deadx, y + deady, WHITECOLOR);
  69.             /* black outer box */
  70.             vrl_DisplayLine(x-deadx-1, y-deady-1, x+deadx+1, y-deady-1, BLACKCOLOR);
  71.             vrl_DisplayLine(x-deadx-1, y+deady+1, x+deadx+1, y+deady+1, BLACKCOLOR);
  72.             vrl_DisplayLine(x-deadx-1, y-deady-1, x-deadx-1, y+deady+1, BLACKCOLOR);
  73.             vrl_DisplayLine(x+deadx+1, y-deady-1, x+deadx+1, y+deady+1, BLACKCOLOR);
  74.             }
  75.         }
  76.     }
  77.  
  78. static void process_key(int c)
  79.     {
  80.     vrl_Camera *cam = vrl_WorldGetCamera();
  81.     switch (c)
  82.         {
  83.         case 'v':
  84.             if (vrl_DisplayStereoGetViewEye() == VRL_STEREOEYE_RIGHT)
  85.                 vrl_DisplayStereoSetViewEye(VRL_STEREOEYE_LEFT);
  86.             else
  87.                 vrl_DisplayStereoSetViewEye(VRL_STEREOEYE_RIGHT);
  88.             break;
  89.         case 's': vrl_WorldToggleStereo(); break;
  90.         case '[':
  91.             {
  92.             vrl_StereoConfiguration *conf = vrl_WorldGetStereoConfiguration();
  93.             int x = vrl_StereoGetLeftEyeShift(conf);
  94.             ++x;
  95.             vrl_StereoSetLeftEyeShift(conf, x);
  96.             vrl_StereoSetRightEyeShift(conf, x);
  97.             }
  98.             break;
  99.         case ']':
  100.             {
  101.             vrl_StereoConfiguration *conf = vrl_WorldGetStereoConfiguration();
  102.             int x = vrl_StereoGetLeftEyeShift(conf);
  103.             --x;
  104.             vrl_StereoSetLeftEyeShift(conf, x);
  105.             vrl_StereoSetRightEyeShift(conf, x);
  106.             }
  107.             break;
  108.         case 'o': cam->ortho = !cam->ortho; break;
  109.         case ' ': vrl_MouseSetUsage(!vrl_MouseGetUsage()); break;
  110.         case 'w': vrl_RenderSetDrawMode(!vrl_RenderGetDrawMode()); break;
  111.         case 'q': case 0x1B: vrl_SystemStopRunning(); break;
  112.         case 'f': vrl_ConfigToggleFramerateDisplay(); break;
  113.         case 'c': vrl_ConfigToggleCompassDisplay(); break;
  114.         case 'p': vrl_ConfigTogglePositionDisplay(); break;
  115.         case 'd': showhud = !showhud; break;
  116.         case '_': vrl_WorldToggleHorizon(); break;
  117.         case '+': vrl_CameraSetZoom(cam, vrl_CameraGetZoom(cam) * 1.1); break;
  118.         case '-': vrl_CameraSetZoom(cam, vrl_CameraGetZoom(cam) * 0.9); break;
  119.         case '=': vrl_CameraSetZoom(cam, 1.0); break;
  120.         case 'h':
  121.             {
  122.             vrl_Scalar newhither = vrl_CameraGetHither(cam) - vrl_WorldGetMovestep();
  123.             if (newhither < 1) newhither = 1;
  124.             vrl_CameraSetHither(cam, newhither);
  125.             }
  126.             break;
  127.         case 'H':
  128.             {
  129.             vrl_Scalar newhither = vrl_CameraGetHither(cam) + vrl_WorldGetMovestep();
  130.             if (newhither < 1) newhither = 1;
  131.             vrl_CameraSetHither(cam, newhither);
  132.             }
  133.             break;
  134.         default: break;
  135.         }
  136.     vrl_SystemRequestRefresh();
  137.     }
  138.     
  139. void vrl_ApplicationKey(unsigned int c)
  140.     {
  141.     static int lastkey = 0;
  142.     if (c == INSKEY)
  143.         {
  144.         int i;
  145.         for (i = 0; i < 100; ++i)
  146.             {
  147.             process_key(lastkey);
  148.             vrl_SystemRender(vrl_WorldUpdate());
  149.             }
  150.         }
  151.     else
  152.         process_key(lastkey = c);
  153.     }
  154.  
  155. void vrl_ApplicationMouseUp(int x, int y, unsigned int buttons)
  156.     {
  157.     vrl_Object *old_active = active_object;
  158.     if ((buttons & 1) == 0)
  159.         return;
  160.     vrl_RenderMonitorInit(x, y);
  161.     vrl_SystemRender(NULL);  /* redraw screen */
  162.     if (vrl_RenderMonitorRead(&active_object, NULL, NULL))
  163.         {
  164.         if (active_object == old_active)
  165.             active_object = NULL;
  166.         else
  167.             vrl_ObjectSetHighlight(active_object, 1);
  168.         }
  169.     if (old_active)
  170.         vrl_ObjectSetHighlight(old_active, 0);
  171.     vrl_SystemRequestRefresh();
  172.     }
  173.  
  174. static int object_mover(vrl_Object *obj, vrl_Device *dev, vrl_CoordFrame frame)
  175.     {
  176.     vrl_Vector v;
  177.     if (obj == NULL || dev == NULL) return -1;
  178.     if (vrl_DeviceGetRotationMode(dev) == VRL_MOTION_ABSOLUTE)
  179.         vrl_ObjectRotReset(obj);
  180.     if (vrl_DeviceGetTranslationMode(dev) == VRL_MOTION_ABSOLUTE)
  181.         vrl_ObjectMove(obj, 0, 0, 0);
  182.     if (vrl_DeviceGetRotationMode(dev))
  183.         {
  184.         vrl_ObjectRotate(obj, vrl_DeviceGetValue(dev, YROT), Y, frame, NULL);
  185.         vrl_ObjectRotate(obj, vrl_DeviceGetValue(dev, XROT), X, frame, NULL);
  186.         vrl_ObjectRotate(obj, vrl_DeviceGetValue(dev, ZROT), Z, frame, NULL);
  187.         }
  188.     if (vrl_DeviceGetTranslationMode(dev))
  189.         {
  190.         vrl_VectorCreate(v, vrl_DeviceGetValue(dev, X), vrl_DeviceGetValue(dev, Y), vrl_DeviceGetValue(dev, Z));
  191.         vrl_ObjectTranslate(obj, v, frame, NULL);
  192.         }
  193.     vrl_SystemRequestRefresh();
  194.     return 0;
  195.     }
  196.  
  197. static int object_move_locally(vrl_Object *obj)
  198.     {
  199.     return object_mover(obj, vrl_ObjectGetApplicationData(obj), VRL_COORD_LOCAL);
  200.     }
  201.  
  202. void vrl_ApplicationInit(void)
  203.     {
  204.     vrl_Object *torso, *head;
  205.     vrl_Device *headdev = vrl_DeviceFind("head");
  206.     vrl_Device *torsodev = vrl_DeviceFind("body");
  207.  
  208.     /* if neither a head device nor a torso device was specified in the
  209.        configuration file, use the keypad as the head device */
  210.     if (headdev == NULL && torsodev == NULL)    
  211.         torsodev = vrl_DeviceOpen(vrl_KeypadDevice, 0);
  212.  
  213.     /* find the head (the object the camera is associated with) and
  214.        set it up to track the head device */
  215.     head = vrl_CameraGetObject(vrl_WorldGetCamera());
  216.     vrl_ObjectSetFunction(head, object_move_locally);
  217.     vrl_ObjectSetApplicationData(head, headdev);
  218.  
  219.     /* if the application hasn't already attached the head to a torso,
  220.        then make a copy of the head to form the torso and attach the head
  221.        to it; make sure that the torso thus created has no shape (we don't
  222.        want a head on top of a another head to be the default) */
  223.     torso = vrl_ObjectFindRoot(head);
  224.     if (torso == head)  /* no torso yet */
  225.         {
  226.         torso = vrl_ObjectCopy(head);      /* create one */
  227.         vrl_ObjectAttach(head, torso);     /* and attach the head to it */
  228.         vrl_ObjectSetShape(torso, NULL);   /* doesn't look like the head */
  229.         }
  230.  
  231.     /* let the torso track the "body" device */
  232.     if (torsodev)
  233.         {
  234.         vrl_ObjectSetFunction(torso, object_move_locally);
  235.         vrl_ObjectSetApplicationData(torso, torsodev);
  236.         }
  237.     }
  238.  
  239.