Subversion Repositories Games.Rick Dangerous

Rev

Rev 1 | Rev 10 | 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 recreate_screen;
  28.  
  29.  
  30. /*
  31.  * Process an event
  32.  */
  33. static void processEvent (void)
  34. {
  35.    static U8 alt_pressed = FALSE;
  36.  
  37.    U16 key = event.key.keysym.scancode;
  38.  
  39.    switch (event.type)
  40.    {
  41.    case SDL_KEYDOWN:
  42.       if (key == SDL_SCANCODE_LALT)
  43.          alt_pressed = TRUE;
  44.       else if (alt_pressed && key == SDL_SCANCODE_RETURN)
  45.       {
  46.          want_fullscreen ^= TRUE;
  47.          recreate_screen = TRUE;
  48.       }
  49.       else if (alt_pressed && key == SDL_SCANCODE_F4)
  50.       {
  51.          control_status |= CONTROL_EXIT;
  52.          control_last = CONTROL_EXIT;
  53.       }
  54.       else if (key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP)
  55.       {
  56.          control_status |= CONTROL_UP;
  57.          control_last = CONTROL_UP;
  58.       }
  59.       else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN)
  60.       {
  61.          control_status |= CONTROL_DOWN;
  62.          control_last = CONTROL_DOWN;
  63.       }
  64.       else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT)
  65.       {
  66.          control_status |= CONTROL_LEFT;
  67.          control_last = CONTROL_LEFT;
  68.       }
  69.       else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT)
  70.       {
  71.          control_status |= CONTROL_RIGHT;
  72.          control_last = CONTROL_RIGHT;
  73.       }
  74.       else if (key == SDL_SCANCODE_PAUSE)
  75.       {
  76.          control_status |= CONTROL_PAUSE;
  77.          control_last = CONTROL_PAUSE;
  78.       }
  79.       else if (key == SDL_SCANCODE_END)
  80.       {
  81.          control_status |= CONTROL_END;
  82.          control_last = CONTROL_END;
  83.       }
  84.       else if (key == SDL_SCANCODE_ESCAPE)
  85.       {
  86.          control_status |= CONTROL_EXIT;
  87.          control_last = CONTROL_EXIT;
  88.       }
  89.       else if (key == SDL_SCANCODE_SPACE)
  90.       {
  91.          control_status |= CONTROL_FIRE;
  92.          control_last = CONTROL_FIRE;
  93.       }
  94.       else if (key == SDL_SCANCODE_F4)
  95.          syssnd_toggleMute();
  96.       else if (key == SDL_SCANCODE_F5)
  97.          syssnd_vol(-1);
  98.       else if (key == SDL_SCANCODE_F6)
  99.          syssnd_vol(+1);
  100.       else if (key == SDL_SCANCODE_F7)
  101.          want_filter ^= TRUE;
  102.       else if (key == SDL_SCANCODE_F10)
  103.       {
  104.          control_status &= ~CONTROL_EXIT;
  105.          control_last = CONTROL_EXIT;
  106.       }
  107.       break;
  108.    case SDL_KEYUP:
  109.       if (key == SDL_SCANCODE_LALT)
  110.          alt_pressed = FALSE;
  111.       else if (key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP)
  112.       {
  113.          control_status &= ~CONTROL_UP;
  114.          control_last = CONTROL_UP;
  115.       }
  116.       else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN)
  117.       {
  118.          control_status &= ~CONTROL_DOWN;
  119.          control_last = CONTROL_DOWN;
  120.       }
  121.       else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT)
  122.       {
  123.          control_status &= ~CONTROL_LEFT;
  124.          control_last = CONTROL_LEFT;
  125.       }
  126.       else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT)
  127.       {
  128.          control_status &= ~CONTROL_RIGHT;
  129.          control_last = CONTROL_RIGHT;
  130.       }
  131.       else if (key == SDL_SCANCODE_PAUSE)
  132.       {
  133.          control_status &= ~CONTROL_PAUSE;
  134.          control_last = CONTROL_PAUSE;
  135.       }
  136.       else if (key == SDL_SCANCODE_END)
  137.       {
  138.          control_status &= ~CONTROL_END;
  139.          control_last = CONTROL_END;
  140.       }
  141.       else if (key == SDL_SCANCODE_ESCAPE)
  142.       {
  143.          control_status &= ~CONTROL_EXIT;
  144.          control_last = CONTROL_EXIT;
  145.       }
  146.       else if (key == SDL_SCANCODE_SPACE)
  147.       {
  148.          control_status &= ~CONTROL_FIRE;
  149.          control_last = CONTROL_FIRE;
  150.       }
  151.       break;
  152.    case SDL_QUIT:
  153.       /* player tries to close the window -- this is the same as pressing ESC */
  154.       control_status |= CONTROL_EXIT;
  155.       control_last = CONTROL_EXIT;
  156.       break;
  157.    case SDL_JOYAXISMOTION:
  158.       if (event.jaxis.axis == 0)
  159.       {
  160.          /* left-right */
  161.          if (event.jaxis.value < -SYSJOY_RANGE)
  162.          {   /* left */
  163.             control_status |= CONTROL_LEFT;
  164.             control_status &= ~CONTROL_RIGHT;
  165.          }
  166.          else if (event.jaxis.value > SYSJOY_RANGE)
  167.          {   /* right */
  168.             control_status |= CONTROL_RIGHT;
  169.             control_status &= ~CONTROL_LEFT;
  170.          }
  171.          else
  172.          {   /* center */
  173.             control_status &= ~CONTROL_RIGHT;
  174.             control_status &= ~CONTROL_LEFT;
  175.          }
  176.       }
  177.       if (event.jaxis.axis == 1)
  178.       {
  179.          /* up-down */
  180.          if (event.jaxis.value < -SYSJOY_RANGE)
  181.          {   /* up */
  182.             control_status |= CONTROL_UP;
  183.             control_status &= ~CONTROL_DOWN;
  184.          }
  185.          else if (event.jaxis.value > SYSJOY_RANGE)
  186.          {   /* down */
  187.             control_status |= CONTROL_DOWN;
  188.             control_status &= ~CONTROL_UP;
  189.          }
  190.          else
  191.          {   /* center */
  192.             control_status &= ~CONTROL_DOWN;
  193.             control_status &= ~CONTROL_UP;
  194.          }
  195.       }
  196.       break;
  197.    case SDL_JOYBUTTONDOWN:
  198.       control_status |= CONTROL_FIRE;
  199.       break;
  200.    case SDL_JOYBUTTONUP:
  201.       control_status &= ~CONTROL_FIRE;
  202.       break;
  203.    default:
  204.       break;
  205.    }
  206. }
  207.  
  208.  
  209. /*
  210.  * Process events, if any, then return
  211.  */
  212. void sysevt_poll (void)
  213. {
  214.    while (SDL_PollEvent (&event))
  215.       processEvent ();
  216. }
  217.  
  218.  
  219. /*
  220.  * Wait for an event, then process it and return
  221.  */
  222. void sysevt_wait (void)
  223. {
  224.    SDL_WaitEvent (&event);
  225.    processEvent ();
  226. }
  227.