Changeset 1342


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

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

Location:
trunk
Files:
3 added
8 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/deushax/debugsprite.cpp

    r1310 r1342  
    4949    Entity::TickGame(seconds);
    5050
    51     vec3 move = seconds * vec3(Input::GetAxis(0), 0.0f);
     51    vec3 move = seconds * vec3(0.0f);
    5252    data->pos += move * vec3(100.f * sqrtf(2.0f), 100.f, 100.f);
    5353}
  • trunk/src/Makefile.am

    r1307 r1342  
    77    map.cpp map.h entity.cpp entity.h ticker.cpp ticker.h lolgl.h \
    88    tileset.cpp tileset.h forge.cpp forge.h video.cpp video.h log.cpp log.h \
    9     timer.cpp timer.h bitfield.h profiler.cpp profiler.h input.h input.cpp \
     9    timer.cpp timer.h bitfield.h profiler.cpp profiler.h \
    1010    world.cpp world.h sample.cpp sample.h sampler.cpp sampler.h \
    1111    text.cpp text.h emitter.cpp emitter.h numeric.h hash.cpp hash.h \
     
    2828    \
    2929    math/vector.cpp math/real.cpp math/half.cpp math/trig.cpp math/trig.h \
     30    \
     31    input/input.cpp input/input.h \
     32    input/stick.cpp input/stick.h \
    3033    \
    3134    gpu/shader.cpp gpu/shader.h \
  • trunk/src/camera.cpp

    r1336 r1342  
    5858    WorldEntity::TickGame(seconds);
    5959
    60     int updown = Input::GetButtonState(273 /*SDLK_UP*/)
    61                - Input::GetButtonState(274 /*SDLK_DOWN*/);
    62     int rightleft = Input::GetButtonState(275 /*SDLK_RIGHT*/)
    63                   - Input::GetButtonState(276 /*SDLK_LEFT*/);
    64     int pgupdown = Input::GetButtonState(280 /*SDLK_PAGEUP*/)
    65                  - Input::GetButtonState(281 /*SDLK_PAGEDOWN*/);
     60    /* Hackish keyboard support */
     61    float updown = Input::GetButtonState(273 /*SDLK_UP*/)
     62                 - Input::GetButtonState(274 /*SDLK_DOWN*/);
     63    float rightleft = Input::GetButtonState(275 /*SDLK_RIGHT*/)
     64                    - Input::GetButtonState(276 /*SDLK_LEFT*/);
     65    float pgupdown = Input::GetButtonState(280 /*SDLK_PAGEUP*/)
     66                   - Input::GetButtonState(281 /*SDLK_PAGEDOWN*/);
     67
     68    /* Hackish stick support */
     69    static Stick *stick = NULL;
     70    if (!stick)
     71        stick = Input::TrackStick();
     72    if (stick && stick->GetAxisCount() >= 2)
     73    {
     74        rightleft = 2.f * stick->GetAxis(0) * std::abs(stick->GetAxis(0));
     75        updown = -2.f * stick->GetAxis(1) * std::abs(stick->GetAxis(1));
     76    }
    6677
    6778    m_position += vec3(rightleft, pgupdown, -updown) * 200.f * seconds;
  • trunk/src/core.h

    r1307 r1342  
    7979#include "audio.h"
    8080#include "scene.h"
    81 #include "input.h"
     81#include "input/input.h"
     82#include "input/stick.h"
    8283#include "profiler.h"
    8384
  • trunk/src/input/input.cpp

    r1341 r1342  
    22// Lol Engine
    33//
    4 // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
     4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
    55//   This program is free software; you can redistribute it and/or
    66//   modify it under the terms of the Do What The Fuck You Want To
     
    4949    int nentities;
    5050    WorldEntity *lastfocus;
     51
     52    Array<Stick *> m_sticks;
    5153}
    5254inputdata;
     
    5860 */
    5961
     62#if 0
    6063vec2 Input::GetAxis(int axis)
    6164{
     
    8184    return ret;
    8285}
     86#endif
    8387
    8488ivec2 Input::GetMousePos()
     
    195199}
    196200
     201Stick *Input::CreateStick()
     202{
     203    Stick *stick = new Stick();
     204    Ticker::Ref(stick);
     205    data->m_sticks.Push(stick);
     206    return stick;
     207}
     208
     209void Input::DestroyStick(Stick *stick)
     210{
     211    for (int i = 0; i < data->m_sticks.Count(); i++)
     212        if (data->m_sticks[i] == stick)
     213            data->m_sticks.Remove(i);
     214    Ticker::Unref(stick);
     215}
     216
     217Stick *Input::TrackStick()
     218{
     219    /* FIXME: add the possibility to choose amongst sticks */
     220    if (!data->m_sticks.Count())
     221        return NULL;
     222    Ticker::Ref(data->m_sticks[0]);
     223    return data->m_sticks[0];
     224}
     225
     226void Input::UntrackStick(Stick *stick)
     227{
     228    Ticker::Unref(stick);
     229}
     230
    197231} /* namespace lol */
    198232
  • trunk/src/input/input.h

    r1341 r1342  
    1414//
    1515
    16 #if !defined __LOL_INPUT_H__
    17 #define __LOL_INPUT_H__
     16#if !defined __LOL_INPUT_INPUT_H__
     17#define __LOL_INPUT_INPUT_H__
    1818
    1919#include "lol/math/vector.h"
     20#include "input/stick.h"
    2021
    2122namespace lol
     
    2829public:
    2930    /* These methods are general queries */
    30     static vec2 GetAxis(int axis);
    3131    static ivec2 GetMousePos();
    3232    static ivec3 GetMouseButtons();
     
    4242    static void SetMouseButton(int index);
    4343    static void UnsetMouseButton(int index);
     44
     45    /*
     46     * Joystick handling
     47     */
     48
     49    static Stick *CreateStick();
     50    static void DestroyStick(Stick *stick);
     51
     52    static Stick *TrackStick();
     53    static void UntrackStick(Stick *stick);
    4454};
    4555
    4656} /* namespace lol */
    4757
    48 #endif // __LOL_INPUT_H__
     58#endif // __LOL_INPUT_INPUT_H__
    4959
  • 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
  • trunk/win32/lolcore.vcxproj

    r1307 r1342  
    9999    <ClCompile Include="..\src\image\codec\sdl-image.cpp" />
    100100    <ClCompile Include="..\src\image\image.cpp" />
    101     <ClCompile Include="..\src\input.cpp" />
     101    <ClCompile Include="..\src\input\input.cpp" />
     102    <ClCompile Include="..\src\input\stick.cpp" />
    102103    <ClCompile Include="..\src\layer.cpp" />
    103104    <ClCompile Include="..\src\log.cpp" />
     
    150151    <ClInclude Include="..\src\image\image-private.h" />
    151152    <ClInclude Include="..\src\image\image.h" />
    152     <ClInclude Include="..\src\input.h" />
     153    <ClInclude Include="..\src\input\input.h" />
     154    <ClInclude Include="..\src\input\stick.h" />
    153155    <ClInclude Include="..\src\layer.h" />
    154156    <ClInclude Include="..\src\log.h" />
  • trunk/win32/lolcore.vcxproj.filters

    r1313 r1342  
    4242      <UniqueIdentifier>{317cb5cc-5dcc-4e14-be90-40a125a2e2ec}</UniqueIdentifier>
    4343    </Filter>
     44    <Filter Include="src\input">
     45      <UniqueIdentifier>{94992c0e-ebc5-4185-b766-323b06547dcf}</UniqueIdentifier>
     46    </Filter>
    4447  </ItemGroup>
    4548  <ItemGroup>
     
    101104      <Filter>src</Filter>
    102105    </ClCompile>
    103     <ClCompile Include="..\src\input.cpp">
    104       <Filter>src</Filter>
    105     </ClCompile>
    106106    <ClCompile Include="..\src\layer.cpp">
    107107      <Filter>src</Filter>
     
    196196    <ClCompile Include="..\src\gpu\indexbuffer.cpp">
    197197      <Filter>src\gpu</Filter>
     198    </ClCompile>
     199    <ClCompile Include="..\src\input\input.cpp">
     200      <Filter>src\input</Filter>
     201    </ClCompile>
     202    <ClCompile Include="..\src\input\stick.cpp">
     203      <Filter>src\input</Filter>
    198204    </ClCompile>
    199205  </ItemGroup>
     
    259265      <Filter>src</Filter>
    260266    </ClInclude>
    261     <ClInclude Include="..\src\input.h">
    262       <Filter>src</Filter>
    263     </ClInclude>
    264267    <ClInclude Include="..\src\layer.h">
    265268      <Filter>src</Filter>
     
    373376      <Filter>src\lol</Filter>
    374377    </ClInclude>
     378    <ClInclude Include="..\src\input\input.h">
     379      <Filter>src\input</Filter>
     380    </ClInclude>
     381    <ClInclude Include="..\src\input\stick.h">
     382      <Filter>src\input</Filter>
     383    </ClInclude>
    375384  </ItemGroup>
    376385</Project>
Note: See TracChangeset for help on using the changeset viewer.