Subversion Repositories Games.Rick Dangerous

Rev

Rev 7 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * src/sysevt.c
  3.  *
  4.  * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved.
  5.  *
  6.  * The use and distribution terms for this software are contained in the file
  7.  * named README, which can be found in the root of this distribution. By
  8.  * using this software in any fashion, you are agreeing to be bound by the
  9.  * terms of this license.
  10.  *
  11.  * You must not remove this notice, or any other, from this software.
  12.  */
  13.  
  14. #include <SDL.h>
  15.  
  16. #include "system.h"
  17. #include "game.h"
  18.  
  19. #include "control.h"
  20. #include "draw.h"
  21.  
  22. #define SYSJOY_RANGE 3280
  23.  
  24. static SDL_Event event;
  25. extern U8 want_filter;
  26. extern U8 want_fullscreen;
  27. extern U8 enable_endkey;
  28. extern U8 recreate_screen;
  29.  
  30.  
  31. /*
  32.  * Process an event
  33.  */
  34. static void processEvent (void)
  35. {
  36.    static U8 alt_pressed = FALSE;
  37.  
  38.    U16 key = event.key.keysym.scancode;
  39.  
  40.    switch (event.type)
  41.    {
  42.    case SDL_KEYDOWN:
  43.       if (key == SDL_SCANCODE_LALT)
  44.          alt_pressed = TRUE;
  45.       else if (alt_pressed && key == SDL_SCANCODE_RETURN)
  46.       {
  47.          want_fullscreen ^= TRUE;
  48.          recreate_screen = TRUE;
  49.       }
  50.       else if (alt_pressed && key == SDL_SCANCODE_F4)
  51.       {
  52.          control_status |= CONTROL_EXIT;
  53.          control_last = CONTROL_EXIT;
  54.       }
  55.       else if (key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP)
  56.       {
  57.          control_status |= CONTROL_UP;
  58.          control_last = CONTROL_UP;
  59.       }
  60.       else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN)
  61.       {
  62.          control_status |= CONTROL_DOWN;
  63.          control_last = CONTROL_DOWN;
  64.       }
  65.       else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT)
  66.       {
  67.          control_status |= CONTROL_LEFT;
  68.          control_last = CONTROL_LEFT;
  69.       }
  70.       else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT)
  71.       {
  72.          control_status |= CONTROL_RIGHT;
  73.          control_last = CONTROL_RIGHT;
  74.       }
  75.       else if ((key == SDL_SCANCODE_PAUSE) || (key == SDL_SCANCODE_P))
  76.       {
  77.          control_status |= CONTROL_PAUSE;
  78.          control_last = CONTROL_PAUSE;
  79.       }
  80.       else if ((key == SDL_SCANCODE_END) && enable_endkey)
  81.       {
  82.          control_status |= CONTROL_END;
  83.          control_last = CONTROL_END;
  84.       }
  85.       else if (key == SDL_SCANCODE_ESCAPE)
  86.       {
  87.          control_status |= CONTROL_EXIT;
  88.          control_last = CONTROL_EXIT;
  89.       }
  90.       else if (key == SDL_SCANCODE_SPACE)
  91.       {
  92.          control_status |= CONTROL_FIRE;
  93.          control_last = CONTROL_FIRE;
  94.       }
  95.       else if (key == SDL_SCANCODE_F4)
  96.          syssnd_toggleMute();
  97.       else if (key == SDL_SCANCODE_F5)
  98.          syssnd_vol(-1);
  99.       else if (key == SDL_SCANCODE_F6)
  100.          syssnd_vol(+1);
  101.       else if (key == SDL_SCANCODE_F7)
  102.          want_filter ^= TRUE;
  103.       else if (key == SDL_SCANCODE_F10)
  104.       {
  105.          control_status &= ~CONTROL_EXIT;
  106.          control_last = CONTROL_EXIT;
  107.       }
  108.       break;
  109.    case SDL_KEYUP:
  110.       if (key == SDL_SCANCODE_LALT)
  111.          alt_pressed = FALSE;
  112.       else if (key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP)
  113.       {
  114.          control_status &= ~CONTROL_UP;
  115.          control_last = CONTROL_UP;
  116.       }
  117.       else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN)
  118.       {
  119.          control_status &= ~CONTROL_DOWN;
  120.          control_last = CONTROL_DOWN;
  121.       }
  122.       else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT)
  123.       {
  124.          control_status &= ~CONTROL_LEFT;
  125.          control_last = CONTROL_LEFT;
  126.       }
  127.       else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT)
  128.       {
  129.          control_status &= ~CONTROL_RIGHT;
  130.          control_last = CONTROL_RIGHT;
  131.       }
  132.       else if ((key == SDL_SCANCODE_PAUSE) || (key == SDL_SCANCODE_P))
  133.       {
  134.          control_status &= ~CONTROL_PAUSE;
  135.          control_last = CONTROL_PAUSE;
  136.       }
  137.       else if ((key == SDL_SCANCODE_END) && enable_endkey)
  138.       {
  139.          control_status &= ~CONTROL_END;
  140.          control_last = CONTROL_END;
  141.       }
  142.       else if (key == SDL_SCANCODE_ESCAPE)
  143.       {
  144.          control_status &= ~CONTROL_EXIT;
  145.          control_last = CONTROL_EXIT;
  146.       }
  147.       else if (key == SDL_SCANCODE_SPACE)
  148.       {
  149.          control_status &= ~CONTROL_FIRE;
  150.          control_last = CONTROL_FIRE;
  151.       }
  152.       break;
  153.    case SDL_QUIT:
  154.       /* player tries to close the window -- this is the same as pressing ESC */
  155.       control_status |= CONTROL_EXIT;
  156.       control_last = CONTROL_EXIT;
  157.       break;
  158.    case SDL_JOYAXISMOTION:
  159.       if (event.jaxis.axis == 0)
  160.       {
  161.          /* left-right */
  162.          if (event.jaxis.value < -SYSJOY_RANGE)
  163.          {   /* left */
  164.             control_status |= CONTROL_LEFT;
  165.             control_status &= ~CONTROL_RIGHT;
  166.          }
  167.          else if (event.jaxis.value > SYSJOY_RANGE)
  168.          {   /* right */
  169.             control_status |= CONTROL_RIGHT;
  170.             control_status &= ~CONTROL_LEFT;
  171.          }
  172.          else
  173.          {   /* center */
  174.             control_status &= ~CONTROL_RIGHT;
  175.             control_status &= ~CONTROL_LEFT;
  176.          }
  177.       }
  178.       if (event.jaxis.axis == 1)
  179.       {
  180.          /* up-down */
  181.          if (event.jaxis.value < -SYSJOY_RANGE)
  182.          {   /* up */
  183.             control_status |= CONTROL_UP;
  184.             control_status &= ~CONTROL_DOWN;
  185.          }
  186.          else if (event.jaxis.value > SYSJOY_RANGE)
  187.          {   /* down */
  188.             control_status |= CONTROL_DOWN;
  189.             control_status &= ~CONTROL_UP;
  190.          }
  191.          else
  192.          {   /* center */
  193.             control_status &= ~CONTROL_DOWN;
  194.             control_status &= ~CONTROL_UP;
  195.          }
  196.       }
  197.       break;
  198.    case SDL_JOYBUTTONDOWN:
  199.       control_status |= CONTROL_FIRE;
  200.       break;
  201.    case SDL_JOYBUTTONUP:
  202.       control_status &= ~CONTROL_FIRE;
  203.       break;
  204.    default:
  205.       break;
  206.    }
  207. }
  208.  
  209.  
  210. /*
  211.  * Process events, if any, then return
  212.  */
  213. void sysevt_poll (void)
  214. {
  215.    while (SDL_PollEvent (&event))
  216.       processEvent ();
  217. }
  218.  
  219.  
  220. /*
  221.  * Wait for an event, then process it and return
  222.  */
  223. void sysevt_wait (void)
  224. {
  225.    SDL_WaitEvent (&event);
  226.    processEvent ();
  227. }
  228.