home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Zone / VRZONE.ISO / mac / ZIP / MISC3D / AVRIL11.ZIP / INPUT.C < prev    next >
C/C++ Source or Header  |  1994-09-05  |  6KB  |  203 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 <math.h>
  8. #include <ctype.h>   /* isupper() */
  9. #include <alloc.h>   /* coreleft() */
  10.  
  11. #include "avril.h"
  12. #include "avrilkey.h"
  13. #include "avrildrv.h"
  14.  
  15. vrl_Object *active_object = NULL;
  16.  
  17. static int showhud = 0;
  18.  
  19. void vrl_ApplicationDrawOver(vrl_RenderStatus *stat)
  20.     {
  21.     vrl_Camera *cam = vrl_WorldGetCamera();
  22.     char buff[100];
  23.     if (vrl_ConfigGetPositionDisplay())
  24.         {
  25.         sprintf(buff, "Position: %ld,%ld",
  26.             (long) floor(scalar2float(vrl_CameraGetX(cam))),
  27.             (long) floor(scalar2float(vrl_CameraGetZ(cam))));
  28.         vrl_UserInterfaceDropText(10, 10, 15, buff);
  29.         }
  30.     if (vrl_ConfigGetFramerateDisplay())
  31.         {
  32.         sprintf(buff, "Frames/sec: %ld", vrl_SystemGetFrameRate());
  33.         vrl_UserInterfaceDropText(5, 170, 15, buff);
  34.         }
  35.     if (vrl_ConfigGetCompassDisplay())
  36.         vrl_UserInterfaceDrawCompass(cam, 250, 40, 35);
  37.     if (showhud)
  38.         {
  39.         sprintf(buff, "%c%c%c",
  40.             stat->memory ?  'M' : ' ',
  41.             stat->objects ? 'O' : ' ',
  42.             stat->facets ?  'F' : ' ');
  43.         vrl_UserInterfaceDropText(10, 20, 15, buff);
  44.         }
  45.     if (vrl_MouseGetUsage())
  46.         {
  47.         vrl_Device *dev = vrl_MouseGetPointer();
  48.         if (dev)
  49.             {
  50.             int x = vrl_DeviceGetCenter(dev, X);
  51.             int y = vrl_DeviceGetCenter(dev, Y);
  52.             int deadx = vrl_DeviceGetDeadzone(dev, X);
  53.             int deady = vrl_DeviceGetDeadzone(dev, Y);
  54.             /* white inner box */
  55.             vrl_DisplayLine(x - deadx, y - deady, x + deadx, y - deady, 15);
  56.             vrl_DisplayLine(x - deadx, y + deady, x + deadx, y + deady, 15);
  57.             vrl_DisplayLine(x - deadx, y - deady, x - deadx, y + deady, 15);
  58.             vrl_DisplayLine(x + deadx, y - deady, x + deadx, y + deady, 15);
  59.             /* black outer box */
  60.             vrl_DisplayLine(x-deadx-1, y-deady-1, x+deadx+1, y-deady-1, 0);
  61.             vrl_DisplayLine(x-deadx-1, y+deady+1, x+deadx+1, y+deady+1, 0);
  62.             vrl_DisplayLine(x-deadx-1, y-deady-1, x-deadx-1, y+deady+1, 0);
  63.             vrl_DisplayLine(x+deadx+1, y-deady-1, x+deadx+1, y+deady+1, 0);
  64.             }
  65.         }
  66.     }
  67.  
  68. static void process_key(int c)
  69.     {
  70.     vrl_Camera *cam = vrl_WorldGetCamera();
  71.     switch (c)
  72.         {
  73.         case 'o': cam->ortho = !cam->ortho; break;
  74.         case ' ': vrl_MouseSetUsage(!vrl_MouseGetUsage()); break;
  75.         case 'w': vrl_DisplaySetDrawMode(!vrl_DisplayGetDrawMode()); break;
  76.         case 'q': case 0x1B: vrl_SystemStopRunning(); break;
  77.         case 'f': vrl_ConfigToggleFramerateDisplay(); break;
  78.         case 'c': vrl_ConfigToggleCompassDisplay(); break;
  79.         case 'p': vrl_ConfigTogglePositionDisplay(); break;
  80.         case 'd': showhud = !showhud; break;
  81.         case '_': vrl_WorldToggleHorizon(); break;
  82.         case '+': vrl_CameraSetZoom(cam, vrl_CameraGetZoom(cam) * 1.1); break;
  83.         case '-': vrl_CameraSetZoom(cam, vrl_CameraGetZoom(cam) * 0.9); break;
  84.         case '=': vrl_CameraSetZoom(cam, 1.0); break;
  85.         case 'h':
  86.             {
  87.             vrl_Scalar newhither = vrl_CameraGetHither(cam) - vrl_WorldGetMovestep();
  88.             if (newhither < 1) newhither = 1;
  89.             vrl_CameraSetHither(cam, newhither);
  90.             }
  91.             break;
  92.         case 'H':
  93.             {
  94.             vrl_Scalar newhither = vrl_CameraGetHither(cam) + vrl_WorldGetMovestep();
  95.             if (newhither < 1) newhither = 1;
  96.             vrl_CameraSetHither(cam, newhither);
  97.             }
  98.             break;
  99.         default: break;
  100.         }
  101.     vrl_SystemRequestRefresh();
  102.     }
  103.     
  104. void vrl_ApplicationKey(unsigned int c)
  105.     {
  106.     static int lastkey = 0;
  107.     if (c == INSKEY)
  108.         {
  109.         int i;
  110.         for (i = 0; i < 100; ++i)
  111.             {
  112.             process_key(lastkey);
  113.             vrl_SystemRender(vrl_ObjectUpdate(vrl_WorldGetObjectTree()));
  114.             }
  115.         }
  116.     else
  117.         process_key(lastkey = c);
  118.     }
  119.  
  120. void vrl_ApplicationMouseUp(int x, int y, unsigned int buttons)
  121.     {
  122.     vrl_Object *old_active = active_object;
  123.     if ((buttons & 1) == 0)
  124.         return;
  125.     vrl_RenderMonitorInit(x, y);
  126.     vrl_SystemRender(NULL);  /* redraw screen */
  127.     if (vrl_RenderMonitorRead(&active_object, NULL, NULL))
  128.         {
  129.         if (active_object == old_active)
  130.             active_object = NULL;
  131.         else
  132.             vrl_ObjectSetHighlight(active_object, 1);
  133.         }
  134.     if (old_active)
  135.         vrl_ObjectSetHighlight(old_active, 0);
  136.     vrl_SystemRequestRefresh();
  137.     }
  138.  
  139. static int object_mover(vrl_Object *obj, vrl_Device *dev, vrl_CoordFrame frame)
  140.     {
  141.     vrl_Vector v;
  142.     if (obj == NULL || dev == NULL) return -1;
  143.     if (vrl_DeviceGetRotationMode(dev) == VRL_MOTION_ABSOLUTE)
  144.         vrl_ObjectRotReset(obj);
  145.     if (vrl_DeviceGetTranslationMode(dev) == VRL_MOTION_ABSOLUTE)
  146.         vrl_ObjectMove(obj, 0, 0, 0);
  147.     if (vrl_DeviceGetRotationMode(dev))
  148.         {
  149.         vrl_ObjectRotate(obj, vrl_DeviceGetValue(dev, YROT), Y, frame, NULL);
  150.         vrl_ObjectRotate(obj, vrl_DeviceGetValue(dev, XROT), X, frame, NULL);
  151.         vrl_ObjectRotate(obj, vrl_DeviceGetValue(dev, ZROT), Z, frame, NULL);
  152.         }
  153.     if (vrl_DeviceGetTranslationMode(dev))
  154.         {
  155.         vrl_VectorCreate(v, vrl_DeviceGetValue(dev, X), vrl_DeviceGetValue(dev, Y), vrl_DeviceGetValue(dev, Z));
  156.         vrl_ObjectTranslate(obj, v, frame, NULL);
  157.         }
  158.     vrl_SystemRequestRefresh();
  159.     return 0;
  160.     }
  161.  
  162. static int object_move_locally(vrl_Object *obj)
  163.     {
  164.     return object_mover(obj, vrl_ObjectGetApplicationData(obj), VRL_COORD_LOCAL);
  165.     }
  166.  
  167. void vrl_ApplicationInit(void)
  168.     {
  169.     vrl_Object *torso, *head;
  170.     vrl_Device *headdev = vrl_DeviceFind("head");
  171.     vrl_Device *torsodev = vrl_DeviceFind("body");
  172.  
  173.     /* if neither a head device nor a torso device was specified in the
  174.        configuration file, use the keypad as the head device */
  175.     if (headdev == NULL && torsodev == NULL)    
  176.         torsodev = vrl_DeviceOpen(vrl_KeypadDevice, 0);
  177.  
  178.     /* find the head (the object the camera is associated with) and
  179.        set it up to track the head device */
  180.     head = vrl_CameraGetObject(vrl_WorldGetCamera());
  181.     vrl_ObjectSetFunction(head, object_move_locally);
  182.     vrl_ObjectSetApplicationData(head, headdev);
  183.  
  184.     /* if the application hasn't already attached the head to a torso,
  185.        then make a copy of the head to form the torso and attach the head
  186.        to it; make sure that the torso thus created has no shape (we don't
  187.        want a head on top of a another head to be the default) */
  188.     torso = vrl_ObjectFindRoot(head);
  189.     if (torso == head)  /* no torso yet */
  190.         {
  191.         torso = vrl_ObjectCopy(head);      /* create one */
  192.         vrl_ObjectAttach(head, torso);     /* and attach the head to it */
  193.         vrl_ObjectSetShape(torso, NULL);   /* doesn't look like the head */
  194.         }
  195.  
  196.     /* let the torso track the "body" device */
  197.     if (torsodev)
  198.         {
  199.         vrl_ObjectSetFunction(torso, object_move_locally);
  200.         vrl_ObjectSetApplicationData(torso, torsodev);
  201.         }
  202.     }
  203.