home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vroom / joystick.pas < prev    next >
Pascal/Delphi Source File  |  1996-03-19  |  4KB  |  132 lines

  1. unit joystick;
  2.  
  3. interface
  4.  
  5. uses dos, vroom, fix_math;
  6.  
  7. procedure init_joystick;
  8.  
  9. procedure read_joystick_position(var ax, ay, bx, by : word);
  10.  
  11. procedure read_joystick_switch(var switch : byte);
  12.  
  13. function Joystick_Check : boolean;
  14.  
  15. implementation
  16.  
  17. var left, back, front, right, centrex, centrey : word;
  18.  
  19. procedure init_joystick;
  20. var junk1, junk2 : word;
  21. begin
  22.      writeln('Joystick Initialisation');
  23.      writeln;
  24.      writeln('Centre joystick and press Enter');
  25.      readln;
  26.      read_joystick_position(centrex, centrey, junk1, junk2);
  27.      writeln;
  28.      writeln('Pull joystick back and to the left and press Enter');
  29.      readln;
  30.      read_joystick_position(left, back, junk1, junk2);
  31.      writeln;
  32.      writeln('Push joystick forwards and to the right and press Enter');
  33.      readln;
  34.      read_joystick_position(right, front, junk1, junk2);
  35.      writeln;
  36.      writeln('Joystick initialisation complete');
  37.      left:=centrex-left; right:=right-centrex;
  38.      front:=centrey-front; back:=back-centrey;
  39. end;
  40.  
  41.  
  42. procedure read_joystick_position(var ax, ay, bx, by : word);
  43. var r : registers;
  44. begin
  45.      r.ah:=$84;
  46.      r.dx:=$01;
  47.      intr($15,r);
  48.      ax:=r.ax;
  49.      ay:=r.bx;
  50.      bx:=r.cx;
  51.      by:=r.dx;
  52. end;
  53.  
  54. procedure read_joystick_switch(var switch : byte);
  55. var r : registers;
  56. begin
  57.      r.ah:=$84;
  58.      r.dx:=$00;
  59.      intr($15,r);
  60.      switch:=r.al;
  61. end;
  62.  
  63. function Joystick_Check : boolean;
  64. var correctkey : boolean;
  65.     ax, ay, junk1, junk2 : word;
  66.     jx, jy : integer;
  67.     switch : byte;
  68.     ca,sa,cc,sc : longint;
  69. begin
  70.      correctkey:=false;
  71.      read_joystick_position(ax,ay,junk1,junk2);
  72.      read_joystick_switch(switch);
  73.      jy:=ay-centrey; jx:=ax-centrex;
  74.      if jy>(back/10) then
  75.      begin
  76.           correctkey:=true;
  77.           colatitude:=colatitude+jy/back;
  78.      end else
  79.      if jy<(-front/10) then
  80.      begin
  81.           correctkey:=true;
  82.           colatitude:=colatitude+jy/front;
  83.      end;
  84.      if jx<(-left/10) then
  85.      begin
  86.           correctkey:=true;
  87.           azimuth:=azimuth-jx/left;
  88.      end else
  89.      if jx>(right/10) then
  90.      begin
  91.           correctkey:=true;
  92.           azimuth:=azimuth-jx/right;
  93.      end;
  94.      if switch=224 then
  95.                 begin
  96.                      correctkey:=true;
  97.                      CosSin(round(-colatitude*10),cc,sc);
  98.                      CosSin(round(azimuth*10),ca,sa);
  99.                      viewpoint.x:=viewpoint.x-(FixedMul(speed,FixedMul(cc,ca)));
  100.                      viewpoint.y:=viewpoint.y-(FixedMul(speed,FixedMul(cc,sa)));
  101.                      viewpoint.z:=viewpoint.z-(FixedMul(speed,sc));
  102.                      x_pos:=viewpoint.x/65536;
  103.                      y_pos:=viewpoint.y/65536;
  104.                      z_pos:=viewpoint.z/65536;
  105.                 end
  106.      else
  107.      if switch=208 then
  108.                 begin
  109.                      correctkey:=true;
  110.                      CosSin(round(-colatitude*10),cc,sc);
  111.                      CosSin(round(azimuth*10),ca,sa);
  112.                      viewpoint.x:=viewpoint.x+(FixedMul(speed,FixedMul(cc,ca)));
  113.                      viewpoint.y:=viewpoint.y+(FixedMul(speed,FixedMul(cc,sa)));
  114.                      viewpoint.z:=viewpoint.z+(FixedMul(speed,sc));
  115.                      x_pos:=viewpoint.x/65536;
  116.                      y_pos:=viewpoint.y/65536;
  117.                      z_pos:=viewpoint.z/65536;
  118.                 end;
  119.      if correctkey then
  120.      begin
  121.           Set_World_to_View(viewpoint.x,viewpoint.y,viewpoint.z,azimuth,colatitude,0);
  122.           main_matrix:=w2v;
  123.      end;
  124.      Joystick_Check:=correctkey;
  125. end;
  126.  
  127. end.
  128.  
  129.  
  130.  
  131.  
  132.