Subversion Repositories Games.Rick Dangerous

Rev

Rev 10 | 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 <SDL2/SDL.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17.  
  18. #include "system.h"
  19. #include "game.h"
  20.  
  21. #include "control.h"
  22. #include "draw.h"
  23.  
  24. #define SYSJOY_RANGE 3280
  25.  
  26. static char character_ringbuffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  27. static int character_ringbuffer_index = 0;
  28. extern U8 want_infinitelives;
  29. extern U8 want_infiniteammo;
  30.  
  31. static SDL_Event event;
  32. extern U8 want_filter;
  33. extern U8 want_fullscreen;
  34. extern U8 enable_endkey;
  35. extern U8 recreate_screen;
  36.  
  37.  
  38. static U8 has_accumulated (const char *str)
  39. {
  40.    int len = (int) strlen (str);
  41.    for (int i = 0; i < len; i++)
  42.       if (character_ringbuffer[(sizeof (character_ringbuffer) + character_ringbuffer_index - len + i) % 16] != str[i])
  43.          return 0;
  44.    return 1;
  45. }
  46.  
  47.  
  48. /*
  49.  * Process an event
  50.  */
  51. static void processEvent (void)
  52. {
  53.    static U8 alt_pressed = FALSE;
  54.  
  55.    U16 key = event.key.keysym.scancode;
  56.  
  57.    switch (event.type)
  58.    {
  59.    case SDL_KEYDOWN:
  60.       if (key == SDL_SCANCODE_LALT)
  61.          alt_pressed = TRUE;
  62.       else if (alt_pressed && key == SDL_SCANCODE_RETURN)
  63.       {
  64.          want_fullscreen ^= TRUE;
  65.          recreate_screen = TRUE;
  66.       }
  67.       else if (alt_pressed && key == SDL_SCANCODE_F4)
  68.       {
  69.          control_status |= CONTROL_EXIT;
  70.          control_last = CONTROL_EXIT;
  71.       }
  72.       else if ((key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP) && !has_accumulated ("MAT") && !has_accumulated ("MATOSGRAT") && !has_accumulated (";QT") && !has_accumulated (";QTOSGRQT"))
  73.       {
  74.          control_status |= CONTROL_UP;
  75.          control_last = CONTROL_UP;
  76.       }
  77.       else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN)
  78.       {
  79.          control_status |= CONTROL_DOWN;
  80.          control_last = CONTROL_DOWN;
  81.       }
  82.       else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT)
  83.       {
  84.          control_status |= CONTROL_LEFT;
  85.          control_last = CONTROL_LEFT;
  86.       }
  87.       else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT)
  88.       {
  89.          control_status |= CONTROL_RIGHT;
  90.          control_last = CONTROL_RIGHT;
  91.       }
  92.       else if (((key == SDL_SCANCODE_PAUSE) || (key == SDL_SCANCODE_P)) && !has_accumulated ("MEME") && !has_accumulated (";E;E"))
  93.       {
  94.          control_status |= CONTROL_PAUSE;
  95.          control_last = CONTROL_PAUSE;
  96.       }
  97.       else if ((key == SDL_SCANCODE_END) && enable_endkey)
  98.       {
  99.          control_status |= CONTROL_END;
  100.          control_last = CONTROL_END;
  101.       }
  102.       else if (key == SDL_SCANCODE_ESCAPE)
  103.       {
  104.          control_status |= CONTROL_EXIT;
  105.          control_last = CONTROL_EXIT;
  106.       }
  107.       else if (key == SDL_SCANCODE_SPACE)
  108.       {
  109.          control_status |= CONTROL_FIRE;
  110.          control_last = CONTROL_FIRE;
  111.       }
  112.       else if (key == SDL_SCANCODE_F4)
  113.          syssnd_toggleMute();
  114.       else if (key == SDL_SCANCODE_F5)
  115.          syssnd_vol(-1);
  116.       else if (key == SDL_SCANCODE_F6)
  117.          syssnd_vol(+1);
  118.       else if (key == SDL_SCANCODE_F7)
  119.          want_filter ^= TRUE;
  120.       else if (key == SDL_SCANCODE_F10)
  121.       {
  122.          control_status &= ~CONTROL_EXIT;
  123.          control_last = CONTROL_EXIT;
  124.       }
  125.       break;
  126.    case SDL_KEYUP:
  127.       if (key == SDL_SCANCODE_LALT)
  128.          alt_pressed = FALSE;
  129.       else if (key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP)
  130.       {
  131.          control_status &= ~CONTROL_UP;
  132.          control_last = CONTROL_UP;
  133.       }
  134.       else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN)
  135.       {
  136.          control_status &= ~CONTROL_DOWN;
  137.          control_last = CONTROL_DOWN;
  138.       }
  139.       else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT)
  140.       {
  141.          control_status &= ~CONTROL_LEFT;
  142.          control_last = CONTROL_LEFT;
  143.       }
  144.       else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT)
  145.       {
  146.          control_status &= ~CONTROL_RIGHT;
  147.          control_last = CONTROL_RIGHT;
  148.       }
  149.       else if ((key == SDL_SCANCODE_PAUSE) || (key == SDL_SCANCODE_P))
  150.       {
  151.          control_status &= ~CONTROL_PAUSE;
  152.          control_last = CONTROL_PAUSE;
  153.       }
  154.       else if ((key == SDL_SCANCODE_END) && enable_endkey)
  155.       {
  156.          control_status &= ~CONTROL_END;
  157.          control_last = CONTROL_END;
  158.       }
  159.       else if (key == SDL_SCANCODE_ESCAPE)
  160.       {
  161.          control_status &= ~CONTROL_EXIT;
  162.          control_last = CONTROL_EXIT;
  163.       }
  164.       else if (key == SDL_SCANCODE_SPACE)
  165.       {
  166.          control_status &= ~CONTROL_FIRE;
  167.          control_last = CONTROL_FIRE;
  168.       }
  169.       // cheat codes:
  170.       // 'MEMEPASMAL' -> infinite lives
  171.       // 'MATOSGRATOS' -> infinite ammo
  172.       {
  173.          const char *keyname = SDL_GetScancodeName (key);
  174.          character_ringbuffer[character_ringbuffer_index] = toupper (keyname[0]);
  175.          character_ringbuffer_index = (character_ringbuffer_index + 1) % 16;
  176.          if (has_accumulated ("MEMEPASMAL") || has_accumulated (";E;EPQS;QL"))
  177.          {
  178.             want_infinitelives = TRUE;
  179.             game_lives = 6;
  180.             syssnd_play (WAV_DIE, 1);
  181.          }
  182.          else if (has_accumulated ("MATOSGRATOS") || has_accumulated (";QTOSGRQTOS"))
  183.          {
  184.             want_infiniteammo = TRUE;
  185.             game_bullets = 6;
  186.             game_bombs = 6;
  187.             syssnd_play (WAV_DIE, 1);
  188.          }
  189.       }
  190.       break;
  191.    case SDL_QUIT:
  192.       /* player tries to close the window -- this is the same as pressing ESC */
  193.       control_status |= CONTROL_EXIT;
  194.       control_last = CONTROL_EXIT;
  195.       break;
  196.    case SDL_JOYAXISMOTION:
  197.       if (event.jaxis.axis == 0)
  198.       {
  199.          /* left-right */
  200.          if (event.jaxis.value < -SYSJOY_RANGE)
  201.          {   /* left */
  202.             control_status |= CONTROL_LEFT;
  203.             control_status &= ~CONTROL_RIGHT;
  204.          }
  205.          else if (event.jaxis.value > SYSJOY_RANGE)
  206.          {   /* right */
  207.             control_status |= CONTROL_RIGHT;
  208.             control_status &= ~CONTROL_LEFT;
  209.          }
  210.          else
  211.          {   /* center */
  212.             control_status &= ~CONTROL_RIGHT;
  213.             control_status &= ~CONTROL_LEFT;
  214.          }
  215.       }
  216.       if (event.jaxis.axis == 1)
  217.       {
  218.          /* up-down */
  219.          if (event.jaxis.value < -SYSJOY_RANGE)
  220.          {   /* up */
  221.             control_status |= CONTROL_UP;
  222.             control_status &= ~CONTROL_DOWN;
  223.          }
  224.          else if (event.jaxis.value > SYSJOY_RANGE)
  225.          {   /* down */
  226.             control_status |= CONTROL_DOWN;
  227.             control_status &= ~CONTROL_UP;
  228.          }
  229.          else
  230.          {   /* center */
  231.             control_status &= ~CONTROL_DOWN;
  232.             control_status &= ~CONTROL_UP;
  233.          }
  234.       }
  235.       break;
  236.    case SDL_JOYBUTTONDOWN:
  237.       control_status |= CONTROL_FIRE;
  238.       break;
  239.    case SDL_JOYBUTTONUP:
  240.       control_status &= ~CONTROL_FIRE;
  241.       break;
  242.    default:
  243.       break;
  244.    }
  245. }
  246.  
  247.  
  248. /*
  249.  * Process events, if any, then return
  250.  */
  251. void sysevt_poll (void)
  252. {
  253.    while (SDL_PollEvent (&event))
  254.       processEvent ();
  255. }
  256.  
  257.  
  258. /*
  259.  * Wait for an event, then process it and return
  260.  */
  261. void sysevt_wait (void)
  262. {
  263.    SDL_WaitEvent (&event);
  264.    processEvent ();
  265. }
  266.