home *** CD-ROM | disk | FTP | other *** search
/ ring.yamanashi.ac.jp/pub/pc/freem/action/ / action.zip / a7xpg0_11.zip / a7xpg / src / abagames / util / sdl / Input.d next >
Text File  |  2003-09-20  |  2KB  |  90 lines

  1. /*
  2.  * $Id: Input.d,v 1.1.1.1 2003/09/19 14:55:49 kenta Exp $
  3.  *
  4.  * Copyright 2003 Kenta Cho. All rights reserved.
  5.  */
  6. module abagames.util.sdl.Input;
  7.  
  8. import string;
  9. import SDL;
  10. import abagames.util.sdl.SDLInitFailedException;
  11.  
  12. /**
  13.  * Joystick and keyboard input.
  14.  */
  15. public class Input {
  16.  public:
  17.   static const int PAD_UP = 1;
  18.   static const int PAD_DOWN = 2;
  19.   static const int PAD_LEFT = 4;
  20.   static const int PAD_RIGHT = 8;
  21.   static const int PAD_BUTTON1 = 16;
  22.   static const int PAD_BUTTON2 = 32;
  23.   Uint8 *keys;
  24.   bool buttonReversed = false;
  25.  
  26.  private:
  27.   SDL_Joystick *stick = null;
  28.   const int JOYSTICK_AXIS = 16384;
  29.  
  30.   public void openJoystick() {
  31.     if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
  32.       throw new SDLInitFailedException(
  33.     "Unable to init SDL joystick: " ~ string.toString(SDL_GetError()));
  34.     }
  35.     stick = SDL_JoystickOpen(0);
  36.   }
  37.  
  38.   public void handleEvent(SDL_Event *event) {
  39.     keys = SDL_GetKeyState(null);
  40.   }
  41.   
  42.   // Joystick and keyboard handler.
  43.  
  44.   public int getPadState() {
  45.     int x = 0, y = 0;
  46.     int pad = 0;
  47.     if (stick) {
  48.       x = SDL_JoystickGetAxis(stick, 0);
  49.       y = SDL_JoystickGetAxis(stick, 1);
  50.     }
  51.     if (keys[SDLK_RIGHT] == SDL_PRESSED || keys[SDLK_KP6] == SDL_PRESSED || x > JOYSTICK_AXIS) {
  52.       pad |= PAD_RIGHT;
  53.     }
  54.     if (keys[SDLK_LEFT] == SDL_PRESSED || keys[SDLK_KP4] == SDL_PRESSED || x < -JOYSTICK_AXIS) {
  55.       pad |= PAD_LEFT;
  56.     }
  57.     if (keys[SDLK_DOWN] == SDL_PRESSED || keys[SDLK_KP2] == SDL_PRESSED || y > JOYSTICK_AXIS) {
  58.       pad |= PAD_DOWN;
  59.     }
  60.     if (keys[SDLK_UP] == SDL_PRESSED ||  keys[SDLK_KP8] == SDL_PRESSED || y < -JOYSTICK_AXIS) {
  61.       pad |= PAD_UP;
  62.     }
  63.     return pad;
  64.   }
  65.  
  66.   public int getButtonState() {
  67.     int btn = 0;
  68.     int btn1 = 0, btn2 = 0, btn3 = 0, btn4 = 0;
  69.     if (stick) {
  70.       btn1 = SDL_JoystickGetButton(stick, 0);
  71.       btn2 = SDL_JoystickGetButton(stick, 1);
  72.       btn3 = SDL_JoystickGetButton(stick, 2);
  73.       btn4 = SDL_JoystickGetButton(stick, 3);
  74.     }
  75.     if (keys[SDLK_z] == SDL_PRESSED || keys[SDLK_LCTRL] == SDL_PRESSED || btn1 || btn4) {
  76.       if (!buttonReversed)
  77.     btn |= PAD_BUTTON1;
  78.       else
  79.     btn |= PAD_BUTTON2;
  80.     }
  81.     if (keys[SDLK_x] == SDL_PRESSED || keys[SDLK_LALT] == SDL_PRESSED || btn2 || btn3) {
  82.       if (!buttonReversed)
  83.     btn |= PAD_BUTTON2;
  84.       else
  85.     btn |= PAD_BUTTON1;
  86.     }
  87.     return btn;
  88.   }
  89. }
  90.