Skip to content

Commit

Permalink
Started joystick support to engine
Browse files Browse the repository at this point in the history
Still, only using buttons from controller, no axis support yet.

Signed-off-by: Carlos Coêlho <[email protected]>
  • Loading branch information
chocoelho committed May 25, 2016
1 parent 39897bf commit dd73770
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 6 deletions.
54 changes: 54 additions & 0 deletions include/joystick_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef IJENGINE_JOYSTICK_EVENT_H
#define IJENGINE_JOYSTICK_EVENT_H

#include "event.h"

#define JOYSTICK_EVENT_ID 0x04

#include <sstream>

using std::ostringstream;

namespace ijengine {

class JoystickEvent : public Event {
public:
typedef enum {PRESSED, RELEASED} State;

typedef
enum {
BUTTON_INVALID, A, B, X, Y, BACK, GUIDE, START,
LEFTSTICK, RIGHTSTICK, LEFTSHOULDER, RIGHTSHOULDER,
DPAD_UP, DPAD_DOWN, DPAD_LEFT, DPAD_RIGHT, BUTTON_MAX
} Button;

typedef
enum {
AXIS_INVALID, LEFTX, LEFTY, RIGHTX, RIGHTY,
TRIGGERLEFT, TRIGGERRIGHT, AXIS_MAX
} Axis;

JoystickEvent(unsigned t, State s, Button b) :
Event(t), m_state(s), m_button(b) {}

State state() const { return m_state; }
Button button() const { return m_button; }
Axis axis() const { return m_axis; }

string serialize() const
{
ostringstream os;
os << JOYSTICK_EVENT_ID << "," << (int) m_state << ","
<< (int) m_button;

return os.str();
}

private:
State m_state;
Button m_button;
Axis m_axis;
};
}

#endif
8 changes: 5 additions & 3 deletions include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "mouse_event.h"
#include "system_event.h"
#include "keyboard_event.h"
#include "joystick_event.h"

#include <string>
#include <list>
Expand All @@ -30,9 +31,10 @@ namespace ijengine {
virtual void play_audio_from_path(const string& title) = 0;
virtual void stop_audio() = 0;

virtual list<MouseEvent> pending_mouse_events(unsigned now) = 0;
virtual list<SystemEvent> pending_system_events(unsigned now) = 0;
virtual list<KeyboardEvent> pending_keyboard_events(unsigned now) = 0;
virtual list<MouseEvent> pending_mouse_events(unsigned now) = 0;
virtual list<SystemEvent> pending_system_events(unsigned now) = 0;
virtual list<KeyboardEvent> pending_keyboard_events(unsigned now) = 0;
virtual list<JoystickEvent> pending_joystick_events(unsigned now) = 0;

virtual unsigned time_elapsed() = 0;
virtual void pause_timer() = 0;
Expand Down
77 changes: 74 additions & 3 deletions kernel/sdl2/sdl2kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "event.h"
#include "system_event.h"
#include "keyboard_event.h"
#include "joystick_event.h"
#include "mouse_event.h"

#include <SDL2/SDL.h>
Expand Down Expand Up @@ -179,6 +180,42 @@ SDL2Kernel::init_table()
m_key_table[SDLK_F10] = KeyboardEvent::F10;
m_key_table[SDLK_F11] = KeyboardEvent::F11;
m_key_table[SDLK_F12] = KeyboardEvent::F12;

m_button_table[SDL_CONTROLLER_BUTTON_INVALID] = JoystickEvent::BUTTON_INVALID;

m_button_table[SDL_CONTROLLER_BUTTON_A] = JoystickEvent::A;
m_button_table[SDL_CONTROLLER_BUTTON_B] = JoystickEvent::B;
m_button_table[SDL_CONTROLLER_BUTTON_X] = JoystickEvent::X;
m_button_table[SDL_CONTROLLER_BUTTON_Y] = JoystickEvent::Y;

m_button_table[SDL_CONTROLLER_BUTTON_BACK] = JoystickEvent::BACK;
m_button_table[SDL_CONTROLLER_BUTTON_GUIDE] = JoystickEvent::GUIDE;
m_button_table[SDL_CONTROLLER_BUTTON_START] = JoystickEvent::START;

m_button_table[SDL_CONTROLLER_BUTTON_LEFTSTICK] = JoystickEvent::LEFTSTICK;
m_button_table[SDL_CONTROLLER_BUTTON_RIGHTSTICK] = JoystickEvent::RIGHTSTICK;

m_button_table[SDL_CONTROLLER_BUTTON_LEFTSHOULDER] = JoystickEvent::LEFTSHOULDER;
m_button_table[SDL_CONTROLLER_BUTTON_RIGHTSHOULDER] = JoystickEvent::RIGHTSHOULDER;

m_button_table[SDL_CONTROLLER_BUTTON_DPAD_UP] = JoystickEvent::DPAD_UP;
m_button_table[SDL_CONTROLLER_BUTTON_DPAD_DOWN] = JoystickEvent::DPAD_DOWN;
m_button_table[SDL_CONTROLLER_BUTTON_DPAD_LEFT] = JoystickEvent::DPAD_LEFT;
m_button_table[SDL_CONTROLLER_BUTTON_DPAD_RIGHT] = JoystickEvent::DPAD_RIGHT;

m_button_table[SDL_CONTROLLER_BUTTON_MAX] = JoystickEvent::BUTTON_MAX;

m_axis_table[SDL_CONTROLLER_AXIS_INVALID] = JoystickEvent::AXIS_INVALID;

m_axis_table[SDL_CONTROLLER_AXIS_LEFTX] = JoystickEvent::LEFTX;
m_axis_table[SDL_CONTROLLER_AXIS_LEFTY] = JoystickEvent::LEFTY;
m_axis_table[SDL_CONTROLLER_AXIS_RIGHTX] = JoystickEvent::RIGHTX;
m_axis_table[SDL_CONTROLLER_AXIS_RIGHTY] = JoystickEvent::RIGHTY;

m_axis_table[SDL_CONTROLLER_AXIS_TRIGGERLEFT] = JoystickEvent::TRIGGERLEFT;
m_axis_table[SDL_CONTROLLER_AXIS_TRIGGERRIGHT] = JoystickEvent::TRIGGERRIGHT;

m_axis_table[SDL_CONTROLLER_AXIS_MAX] = JoystickEvent::AXIS_MAX;
}

MouseEvent::State
Expand Down Expand Up @@ -258,12 +295,46 @@ SDL2Kernel::update_pending_events(unsigned now)
m_last_update = now;
}

list<KeyboardEvent>
SDL2Kernel::pending_keyboard_events(unsigned now)
list<JoystickEvent>
SDL2Kernel::pending_joystick_events(unsigned now)
{
update_pending_events(now);
auto it = m_events.begin();

list<JoystickEvent> events;

while (it != m_events.end())
{
unsigned timestamp = it->quit.timestamp;

if (it->type == SDL_JOYBUTTONDOWN)
{
auto event = JoystickEvent(timestamp,
JoystickEvent::State::PRESSED,
m_button_table[it->jbutton.button]);

events.push_back(event);
it = m_events.erase(it);
} else if (it->type == SDL_JOYBUTTONUP)
{
auto event = JoystickEvent(timestamp,
JoystickEvent::State::RELEASED,
m_button_table[it->jbutton.button]);

events.push_back(event);
it = m_events.erase(it);
} else
++it;
}

return events;
}

list<KeyboardEvent>
SDL2Kernel::pending_keyboard_events(unsigned now) {
update_pending_events(now);
auto it = m_events.begin();

list<KeyboardEvent> events;

while (it != m_events.end())
Expand Down Expand Up @@ -292,7 +363,7 @@ SDL2Kernel::pending_keyboard_events(unsigned now)
}

return events;
}
}

list<MouseEvent>
SDL2Kernel::pending_mouse_events(unsigned now)
Expand Down
3 changes: 3 additions & 0 deletions kernel/sdl2/sdl2kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SDL2Kernel : public Kernel {
list<MouseEvent> pending_mouse_events(unsigned now);
list<SystemEvent> pending_system_events(unsigned now);
list<KeyboardEvent> pending_keyboard_events(unsigned now);
list<JoystickEvent> pending_joystick_events(unsigned now);

unsigned time_elapsed();
void pause_timer();
Expand All @@ -37,6 +38,8 @@ class SDL2Kernel : public Kernel {
unsigned m_last_update;
list<SDL_Event> m_events;
map<unsigned, KeyboardEvent::Key> m_key_table;
map<unsigned, JoystickEvent::Button> m_button_table;
map<unsigned, JoystickEvent::Axis> m_axis_table;

void init_table();
void update_pending_events(unsigned now);
Expand Down

0 comments on commit dd73770

Please sign in to comment.