Ignore:
Timestamp:
May 6, 2012, 2:49:16 AM (11 years ago)
Author:
sam
Message:

input: add core joystick support and bind the SDL input to that.

Location:
trunk/src/platform/sdl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/platform/sdl/sdlinput.cpp

    r1310 r1342  
    3535
    3636    static ivec2 GetMousePos();
     37#if defined USE_SDL
     38    Array<SDL_Joystick *, Stick *> m_joysticks;
     39#endif
    3740};
    3841
     
    4245
    4346SdlInput::SdlInput()
    44   : data(new SdlInputData())
     47  : m_data(new SdlInputData())
    4548{
    4649#if defined USE_SDL
    47     SDL_Init(SDL_INIT_TIMER);
     50    SDL_Init(SDL_INIT_TIMER | SDL_INIT_JOYSTICK);
     51
     52    /* Register all the joysticks we can find, and let the input
     53     * system decide what it wants to track. */
     54    SDL_JoystickEventState(SDL_ENABLE);
     55    for (int i = 0; i < SDL_NumJoysticks(); i++)
     56    {
     57        SDL_Joystick *sdlstick = SDL_JoystickOpen(i);
     58        Stick *stick = Input::CreateStick();
     59        stick->SetAxisCount(SDL_JoystickNumAxes(sdlstick));
     60        stick->SetButtonCount(SDL_JoystickNumButtons(sdlstick));
     61        m_data->m_joysticks.Push(sdlstick, stick);
     62    }
    4863#endif
    4964
    5065    m_gamegroup = GAMEGROUP_BEFORE;
     66}
     67
     68SdlInput::~SdlInput()
     69{
     70#if defined USE_SDL
     71    /* Unregister all the joysticks we added */
     72    while (m_data->m_joysticks.Count())
     73    {
     74        SDL_JoystickClose(m_data->m_joysticks[0].m1);
     75        Input::DestroyStick(m_data->m_joysticks[0].m2);
     76        m_data->m_joysticks.Remove(0);
     77    }
     78#endif
     79    delete m_data;
    5180}
    5281
     
    5685
    5786#if !defined _WIN32
    58     data->Tick(seconds);
     87    m_data->Tick(seconds);
    5988#endif
    6089}
     
    6594
    6695#if defined _WIN32
    67     data->Tick(seconds);
     96    m_data->Tick(seconds);
    6897#endif
    6998}
     
    102131            break;
    103132        }
     133
     134        case SDL_JOYAXISMOTION:
     135            m_joysticks[event.jaxis.which].m2->SetAxis(event.jaxis.axis, (float)event.jaxis.value / 32768.f);
     136            break;
     137
     138        case SDL_JOYBUTTONUP:
     139        case SDL_JOYBUTTONDOWN:
     140            m_joysticks[event.jbutton.which].m2->SetButton(event.jbutton.button, event.jbutton.state);
     141            break;
    104142        }
    105143    }
     
    113151#endif
    114152#endif
    115 }
    116 
    117 SdlInput::~SdlInput()
    118 {
    119     delete data;
    120153}
    121154
  • trunk/src/platform/sdl/sdlinput.h

    r1310 r1342  
    3535
    3636private:
    37     SdlInputData *data;
     37    SdlInputData *m_data;
    3838};
    3939
Note: See TracChangeset for help on using the changeset viewer.