home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Issue 2 / Freelog_HS_3_Setp_Oct_Nov_2000_CD2.mdx / Arcade / Orbit / src / keyboard.c < prev    next >
C/C++ Source or Header  |  1999-10-02  |  12KB  |  683 lines

  1. /*
  2.  
  3. ORBIT, a freeware space combat simulator
  4. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20. */
  21.  
  22. #include "orbit.h"
  23.  
  24. char key[256], spec[256];
  25.  
  26. static void Key (unsigned char k, int x, int y)
  27. {
  28.     /* Are we reading in some text? */
  29.     if (text.yes)
  30.     {
  31.         if ( ((k == 127) || (k == 8)) && (text.index > 0) )        /* Backspace */
  32.         {
  33.             text.index--;
  34.             text.buf[text.index] = 0;
  35.             Mprint ("%s%s", text.prompt, text.buf);
  36.             return;
  37.         }
  38.         if (k == 27)    /* Escape */
  39.         {
  40.             text.yes = 0;
  41.             Mprint (" ");
  42.             return;
  43.         }
  44.         else if (k == 13)    /* Return */
  45.         {
  46.             Mprint (" ");
  47.             text.func();
  48.             text.yes = 0;
  49.             return;
  50.         }
  51.         else
  52.         {
  53.             /* Add character to buffer */
  54.             if (text.index >= (TEXTSIZE-1))
  55.             {
  56.                 text.func();
  57.                 text.yes = 0;
  58.                 return;
  59.             }
  60.             text.buf[text.index++] = k;
  61.             text.buf[text.index] = 0;
  62.             Mprint ("%s%s", text.prompt, text.buf);
  63.             return;
  64.         }
  65.     }
  66.  
  67.     if (state == STATE_DEAD1) return;
  68.  
  69.     if (state == STATE_DEAD2)
  70.     {
  71.         state = STATE_NORMAL;
  72.         Mprint (" ");
  73.         return;
  74.     }
  75.  
  76.     if (paused)
  77.     {
  78.         paused = 0;
  79.         Mprint ("");
  80.         if ( (state != STATE_LOADGAME) ||
  81.              (k < '0') ||
  82.              (k >= '0'+nsaves) )
  83.         {
  84.             if (state == STATE_LOADGAME) state = STATE_NORMAL;
  85.             return;
  86.         }
  87.     }
  88.     key[k] = 0x81;
  89. }
  90.  
  91. static void KeyUp (unsigned char k, int x, int y)
  92. {
  93.     key[k] &= 0x7f;
  94.  
  95.     if (k == 'a') key['A'] &= 0x7f;
  96.     if (k == 'z') key['Z'] &= 0x7f;
  97.     if (k == 'A') key['a'] &= 0x7f;
  98.     if (k == 'Z') key['z'] &= 0x7f;
  99. }
  100.  
  101. static void Spec (int k, int x, int y)
  102. {
  103.     if (state == STATE_DEAD1) return;
  104.  
  105.     if (state == STATE_DEAD2)
  106.     {
  107.         state = STATE_NORMAL;
  108.         return;
  109.     }
  110.  
  111.     if (paused)
  112.     {
  113.         paused = 0;
  114.         Mprint ("");
  115.         return;
  116.     }
  117.     spec[k] = 0x81;
  118. }
  119.  
  120. static void SpecUp (int k, int x, int y)
  121. {
  122.     spec[k] &= 0x7f;
  123. }
  124.  
  125. int KeyState (k)
  126. int k;
  127. /*
  128.  *  bit 7 will be set if key is down now
  129.  *  bit 0 will be set if key has been pressed since last time
  130.  *        we were asked
  131.  */
  132. {
  133.     int j;
  134.  
  135.     j = key[k];
  136.  
  137.     /* Clear since-last-time bit */
  138.     key[k] &= 0xfe;
  139.  
  140.     return (j);
  141. }
  142.  
  143. int SpecKeyState (k)
  144. int k;
  145. /*
  146.  *  bit 7 will be set if key is down now
  147.  *  bit 0 will be set if key has been pressed since last time
  148.  *        we were asked
  149.  */
  150. {
  151.     int j;
  152.  
  153.     j = spec[k];
  154.  
  155.     /* Clear since-last-time bit */
  156.     spec[k] &= 0xfe;
  157.  
  158.     return (j);
  159. }
  160.  
  161. InitKeyboard()
  162. /*
  163.  *  Initialize the keyboard
  164.  */
  165. {
  166.     int i;
  167.  
  168.     /* Clear all the key-pressed flags */
  169.     for (i=0; i<256; i++)
  170.     {
  171.         key[i] = 0;
  172.         spec[i] = 0;
  173.     }
  174.  
  175.     /* Set up call backs */
  176.     glutKeyboardFunc (Key);
  177.     glutSpecialFunc (Spec);
  178.     glutKeyboardUpFunc (KeyUp);
  179.     glutSpecialUpFunc (SpecUp);
  180. }
  181.  
  182. Keyboard()
  183. /*
  184.  *  Read the keyboard
  185.  */
  186. {
  187.     int k, i;
  188.  
  189.     /* If we're waiting for Sparky to hit a key to load a game,
  190.        check the appropriate digit keys */
  191.     if (state == STATE_LOADGAME)
  192.     {
  193.         for (k=0; k<nsaves; k++)
  194.         {
  195.             if (1 & KeyState ('0' + k))
  196.                 LoadGameByKey (k);
  197.         }
  198.     }
  199.  
  200.     if (KeyState (27))    /* Escape */
  201.     {
  202.         if (strcasecmp (gamemode, "no")) glutLeaveGameMode();
  203.  
  204.         /* Rewrite the preferences file before leaving */
  205.         Log ("Escape key, exiting...");
  206.         ShutdownNetwork();
  207.         FinishSound();
  208.         WritePrefs();
  209.         CloseLog();
  210.         exit (0);
  211.     }
  212.  
  213.     if (KeyState ('q'))
  214.     {
  215.         if (strcasecmp (gamemode, "no")) glutLeaveGameMode();
  216.         Log ("Quitting...");
  217.         ShutdownNetwork();
  218.         FinishSound();
  219.         WritePrefs();
  220.         CloseLog();
  221.         exit (0);
  222.     }
  223.  
  224.     if (KeyState ('Q'))
  225.     {
  226.         if (strcasecmp (gamemode, "no")) glutLeaveGameMode();
  227.         Log ("Quitting. no save...");
  228.         ShutdownNetwork();
  229.         FinishSound();
  230.         CloseLog();
  231.         exit (0);
  232.     }
  233.  
  234.     player.move_right =
  235.         player.move_left =
  236.         player.move_up =
  237.         player.move_down =
  238.         player.move_forward =
  239.         player.move_backward =
  240.         player.move_pitchleft =
  241.         player.move_pitchright = 0.0;
  242.     warpspeed = 0;
  243.     
  244.     if (SpecKeyState (GLUT_KEY_RIGHT))  player.move_right = 1.0;
  245.     if (SpecKeyState (GLUT_KEY_LEFT))   player.move_left = 1.0;
  246.     if (SpecKeyState (GLUT_KEY_UP))     player.move_up = 1.0;
  247.     if (SpecKeyState (GLUT_KEY_DOWN))   player.move_down = 1.0;
  248.     if (KeyState ('a'))             player.move_forward = 0.75;
  249.     if (KeyState ('z'))             player.move_backward = 0.75;
  250.     if (SpecKeyState (GLUT_KEY_INSERT)) player.move_pitchleft = 1.0;
  251.     if (KeyState (127))             player.move_pitchright = 1.0;
  252.  
  253.     if (KeyState ('A'))
  254.     {
  255.         player.move_forward = 0.75;
  256.         warpspeed = 1;
  257.     }
  258.  
  259.     if (KeyState ('Z'))
  260.     {
  261.         player.move_backward = 0.75;
  262.         warpspeed = 1;
  263.     }
  264.  
  265.     if (1 & KeyState ('h'))
  266.     {
  267.         drawhud = !drawhud;
  268.     }
  269.         
  270.     if (1 & KeyState ('s'))
  271.     {
  272.         switch (starfield)
  273.         {
  274.         case 0:    starfield = 1;
  275.                 star_list = star_list_sparse;
  276.                 Cprint ("SPARSE Starfield");
  277.                 break;
  278.  
  279.         case 1:    starfield = 2;
  280.                 star_list = star_list_dense;
  281.                 Cprint ("DENSE Starfield");
  282.                 break;
  283.  
  284.         case 2:    starfield = 0;
  285.                 Cprint ("Starfield OFF");
  286.                 break;
  287.         }
  288.     }
  289.  
  290.     if (1 & KeyState (' '))
  291.     {
  292.         if (fullstop)
  293.         {
  294.             player.vel[0] = player.vel[1] = player.vel[2] = 0.0;
  295.             player.throttle = 0.0;
  296.             if (am_client) clientme.urgent = 1;
  297.             QueuePositionReport();
  298.         }
  299.     }
  300.  
  301.     if (1 & KeyState ('c'))
  302.     {
  303.         for (i=0; i<console.next; i++) console.age[i] = 0.0;
  304.     }
  305.  
  306.     if (1 & KeyState ('g'))
  307.     {
  308.         if (!am_client)
  309.         {
  310.             gravity = !gravity;
  311.             if (gravity)
  312.             {
  313.                 Cprint ("Gravity ON");
  314.             }
  315.             else
  316.             {
  317.                 Cprint ("Gravity OFF");
  318.             }
  319.             if (am_server) SendFlags();
  320.         }
  321.     }
  322.  
  323.     if (1 & KeyState ('j'))
  324.     {
  325.         junk = !junk;
  326.         if (junk)
  327.             Cprint ("Space junk ON");
  328.         else
  329.             Cprint ("Space junk OFF");
  330.     }
  331.  
  332.     if (1 & KeyState ('T'))
  333.     {
  334.         joy_throttle = !joy_throttle;
  335.         if (joy_throttle)
  336.             Cprint ("Joystick throttle ON");
  337.         else
  338.             Cprint ("Joystick throttle OFF");
  339.     }
  340.  
  341.     if (1 & KeyState ('e'))
  342.     {
  343.         sound = !sound;
  344.         if (sound)
  345.             Cprint ("Sound effects ON");
  346.         else
  347.             Cprint ("Sound effects OFF");
  348.     }
  349.  
  350.     if (1 & KeyState ('i'))
  351.     {
  352.         if (!am_client && !am_server)
  353.         {
  354.             vulnerable = !vulnerable;
  355.             if (vulnerable)
  356.                 Cprint ("Vulnerable");
  357.             else
  358.                 Cprint ("Invulnerable");
  359.         }
  360.     }
  361.  
  362.     if (1 & KeyState ('P'))
  363.     {
  364.         ScreenShot();
  365.     }
  366.  
  367.     if (1 & KeyState ('n'))
  368.     {
  369.         show_names = !show_names;
  370.         if (show_names)
  371.             Cprint ("Names ON");
  372.         else
  373.             Cprint ("Names OFF");
  374.     }
  375.  
  376.     if (1 & KeyState ('m'))
  377.     {
  378.         if (message.age < MSG_MAXAGE)
  379.             message.age = MSG_MAXAGE + 1.0;
  380.         else
  381.             message.age = 0.0;
  382.     }
  383.  
  384.     if (1 & KeyState ('x'))
  385.     {
  386.         textures = !textures;
  387.         if (textures)
  388.             Cprint ("Textures ON");
  389.         else
  390.             Cprint ("Textures OFF");
  391.     }
  392.  
  393.     if (1 & KeyState ('r'))
  394.     {
  395.         rings = !rings;
  396.         if (rings)
  397.             Cprint ("Rings ON");
  398.         else
  399.             Cprint ("Rings OFF");
  400.     }
  401.  
  402.     if (1 & KeyState ('u')) LockNearest();
  403.  
  404.     if (1 & KeyState ('y')) LockNext();
  405.  
  406.     if (1 & KeyState ('Y')) LockPrev();
  407.  
  408.     if (1 & KeyState ('b')) 
  409.     {
  410.         if (!am_client && !am_server) Mprint (mission.briefing);
  411.     }
  412.  
  413.     if (1 & KeyState ('w'))
  414.     {
  415.         player.weapon = (player.weapon + 1) % NPLAYER_WEAPONS;
  416.     }
  417.  
  418.     if (1 & KeyState ('W'))
  419.     {
  420.         player.weapon = (player.weapon + NPLAYER_WEAPONS - 1) %
  421.                          NPLAYER_WEAPONS;
  422.     }
  423.  
  424.     if (1 & KeyState ('1')) player.weapon = 0;
  425.     if (1 & KeyState ('2')) player.weapon = 1;
  426.     if (1 & KeyState ('3')) player.weapon = 2;
  427.     if (1 & KeyState ('4')) player.weapon = 3;
  428.  
  429.     if (1 & KeyState ('p'))
  430.     {
  431.         if (!am_client && !am_server)
  432.         {
  433.             Mprint ("Paused");
  434.             paused = 1;
  435.         }
  436.     }
  437.  
  438.     /* Control-P for silent pause */
  439.     if (1 & KeyState (16))
  440.     {
  441.         if (!am_client && !am_server) paused = 1;
  442.     }
  443.  
  444.     if (1 & KeyState ('M'))
  445.     {
  446.         mouse_control = !mouse_control;
  447.         if (mouse_control)
  448.         {
  449.             Cprint ("Mouse ON");
  450.             InitMouse();
  451.         }
  452.         else
  453.         {
  454.             Cprint ("Mouse OFF");
  455.             glutSetCursor (GLUT_CURSOR_INHERIT);
  456.         }
  457.     }        
  458.  
  459.     if (KeyState ('\t')) PlayerFires();
  460.  
  461.     if (1 & KeyState ('l'))
  462.     {
  463.         lock.type = (lock.type + 1) % 3;
  464.         lock.target = -1;
  465.         LockNearest();
  466.  
  467.         switch (lock.type)
  468.         {
  469.         case LOCK_ENEMY:
  470.             Cprint ("Locking ENEMIES");
  471.             break;
  472.  
  473.         case LOCK_FRIENDLY:
  474.             Cprint ("Locking FRIENDLIES");
  475.             break;
  476.  
  477.         case LOCK_PLANET:
  478.             Cprint ("Locking PLANETS");
  479.             break;
  480.         }
  481.     }
  482.  
  483.     if (1 & KeyState ('f'))
  484.     {
  485.         if (!am_client)
  486.         {
  487.             player.flightmodel = (player.flightmodel + 1) % 2;
  488.  
  489.             if (player.flightmodel == FLIGHT_NEWTONIAN)
  490.                 Cprint ("NEWTONIAN flight model");
  491.             else
  492.                 Cprint ("ARCADE flight model");
  493.  
  494.             if (am_server) SendFlags();
  495.         }
  496.     }
  497.  
  498.     if (1 & KeyState ('L'))
  499.     {
  500.         if (!am_client && !am_server) LoadGame();
  501.     }
  502.  
  503.     if (1 & KeyState (']')) NextWaypoint();
  504.     if (1 & KeyState ('[')) PrevWaypoint();
  505.  
  506.     if (1 & KeyState ('F'))
  507.     {
  508.         showfps = !showfps;
  509.  
  510.         if (showfps)
  511.             Cprint ("Framerate ON");
  512.         else
  513.             Cprint ("Framerate OFF");
  514.     }
  515.  
  516.     if (1 & KeyState ('S'))
  517.     {
  518.         if (am_client)
  519.         {
  520.             Mprint ("Alread a client");
  521.         }
  522.         else if (am_server)
  523.         {
  524.             ShutdownServer();
  525.         }
  526.         else
  527.         {
  528.             BecomeServer();
  529.         }
  530.     }
  531.  
  532.     if (1 & KeyState ('C'))
  533.     {
  534.         if (am_client)
  535.         {
  536.             ShutdownClient();
  537.         }
  538.         else if (am_server)
  539.         {
  540.             Mprint ("Already a server");
  541.         }
  542.         else
  543.         {
  544.             GetText ("Connect to: ", DoConnect);
  545.         }
  546.     }
  547.  
  548.     if (1 & KeyState ('U')) ShowClients();
  549.     if (1 & SpecKeyState (GLUT_KEY_F1)) ShowClients();
  550.  
  551.     if (1 & KeyState ('>'))
  552.     {
  553.         if (fov < 180.0)
  554.         {
  555.             fov += 5.0;
  556.             Reshape (ScreenWidth, ScreenHeight);
  557.         }
  558.         Cprint ("Field of view %3.0lf", fov);
  559.     }
  560.  
  561.     if (1 & KeyState ('<'))
  562.     {
  563.         if (fov > 5.0)
  564.         {
  565.             fov -= 5.0;
  566.             Reshape (ScreenWidth, ScreenHeight);
  567.         }
  568.         Cprint ("Field of view %3.0lf", fov);
  569.     }
  570.  
  571.     if (1 & KeyState ('t'))
  572.     {
  573.         GetText ("Say: ", DoChat);
  574.     }
  575.  
  576.     if (1 & KeyState (4))    /* control-D */
  577.     {
  578.         if (am_server) GetText ("Drop client: ", DoDrop);
  579.     }
  580.  
  581.     if (1 & KeyState (12))    /* control-L */
  582.     {
  583.         if (!am_server && !am_client) GetText ("Load mission: ", DoLoad);
  584.     }
  585.  
  586.     if (1 & KeyState (14))    /* control-N */
  587.     {
  588.         if (!am_client && !am_server)
  589.         {
  590.             GetText ("Player name: ", DoName);
  591.         }
  592.     }
  593.  
  594.     if (1 & KeyState ('o'))
  595.     {
  596.         draw_orbits = !draw_orbits;
  597.  
  598.         if (draw_orbits)
  599.             Cprint ("Orbits ON");
  600.         else
  601.             Cprint ("Orbits OFF");
  602.     }
  603.  
  604.     if (1 & KeyState ('O'))
  605.     {
  606.         if (!am_client && !am_server)
  607.         {
  608.             orbit = !orbit;
  609.  
  610.             if (orbit)
  611.                 Cprint ("Orbiting ON");
  612.             else
  613.                 Cprint ("Orbiting OFF");
  614.         }
  615.     }
  616.  
  617.     if (1 & KeyState ('k'))
  618.     {
  619.         if (!am_client && !am_server)
  620.         {
  621.             compression *= 2.0;
  622.             PrintCompression();
  623.         }
  624.     }
  625.  
  626.     if (1 & KeyState ('K'))
  627.     {
  628.         if (!am_client && !am_server)
  629.         {
  630.             compression /= 2.0;
  631.             PrintCompression();
  632.         }
  633.     }
  634.  
  635.     if (1 & KeyState ('v'))
  636.     {
  637.         player.viewlock = !player.viewlock;
  638.  
  639.         if (player.viewlock)
  640.             Cprint ("View LOCKED");
  641.         else
  642.             Cprint ("View UNLOCKED");
  643.     }
  644.  
  645.     if (1 & KeyState ('{'))
  646.     {
  647.         clipnear *= 0.75;
  648.         Cprint ("Near = %lf", clipnear);
  649.         Reshape (ScreenWidth, ScreenHeight);
  650.     }
  651.     if (1 & KeyState ('}'))
  652.     {
  653.         clipnear *= 1.25;
  654.         Cprint ("Near = %lf", clipnear);
  655.         Reshape (ScreenWidth, ScreenHeight);
  656.     }
  657.     if (1 & KeyState ('('))
  658.     {
  659.         clipfar *= 0.75;
  660.         Cprint ("Far = %lf", clipfar);
  661.         Reshape (ScreenWidth, ScreenHeight);
  662.     }
  663.     if (1 & KeyState (')'))
  664.     {
  665.         clipfar *= 1.25;
  666.         Cprint ("Far = %lf", clipfar);
  667.         Reshape (ScreenWidth, ScreenHeight);
  668.     }
  669. }
  670.  
  671. PrintCompression()
  672. {
  673.     if (compression >= 1.0)
  674.     {
  675.         Cprint ("Compression %.0lfX", compression);
  676.     }
  677.     else
  678.     {
  679.         Cprint ("Compression %lfX", compression);
  680.     }
  681. }
  682.  
  683.