Initial Lime Gamepad API

This commit is contained in:
Joshua Granick
2015-03-18 01:42:56 -07:00
parent f4c9880cce
commit cfbf3f4a3f
15 changed files with 460 additions and 1 deletions

View File

@@ -21,6 +21,8 @@
#include <system/System.h>
#include <text/Font.h>
#include <text/TextLayout.h>
#include <ui/Gamepad.h>
#include <ui/GamepadEvent.h>
#include <ui/KeyEvent.h>
#include <ui/Mouse.h>
#include <ui/MouseCursor.h>
@@ -342,6 +344,22 @@ namespace lime {
}
value lime_gamepad_event_manager_register (value callback, value eventObject) {
GamepadEvent::callback = new AutoGCRoot (callback);
GamepadEvent::eventObject = new AutoGCRoot (eventObject);
return alloc_null ();
}
value lime_gamepad_get_device_name (value id) {
return alloc_string (Gamepad::GetDeviceName (val_int (id)));
}
value lime_image_encode (value buffer, value type, value quality) {
ImageBuffer imageBuffer = ImageBuffer (buffer);
@@ -715,6 +733,8 @@ namespace lime {
DEFINE_PRIM (lime_font_render_glyph, 3);
DEFINE_PRIM (lime_font_render_glyphs, 3);
DEFINE_PRIM (lime_font_set_size, 2);
DEFINE_PRIM (lime_gamepad_event_manager_register, 2);
DEFINE_PRIM (lime_gamepad_get_device_name, 1);
DEFINE_PRIM (lime_image_encode, 3);
DEFINE_PRIM (lime_image_load, 1);
DEFINE_PRIM (lime_jni_getenv, 0);

View File

@@ -1,4 +1,5 @@
#include "SDLApplication.h"
#include "SDLGamepad.h"
#ifdef HX_MACOS
#include <CoreFoundation/CoreFoundation.h>
@@ -24,7 +25,7 @@ namespace lime {
SDLApplication::SDLApplication () {
SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
SDL_Init (SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_TIMER);
#ifdef EMSCRIPTEN
currentApplication = this;
@@ -38,6 +39,7 @@ namespace lime {
lastUpdate = 0;
nextUpdate = 0;
GamepadEvent gamepadEvent;
KeyEvent keyEvent;
MouseEvent mouseEvent;
RenderEvent renderEvent;
@@ -111,6 +113,15 @@ namespace lime {
RenderEvent::Dispatch (&renderEvent);
break;
case SDL_CONTROLLERAXISMOTION:
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
case SDL_CONTROLLERDEVICEADDED:
case SDL_CONTROLLERDEVICEREMOVED:
ProcessGamepadEvent (event);
break;
case SDL_JOYAXISMOTION:
case SDL_JOYBALLMOTION:
case SDL_JOYBUTTONDOWN:
@@ -191,6 +202,75 @@ namespace lime {
}
void SDLApplication::ProcessGamepadEvent (SDL_Event* event) {
if (GamepadEvent::callback) {
switch (event->type) {
case SDL_CONTROLLERAXISMOTION:
gamepadEvent.type = BUTTON_UP;
gamepadEvent.axis = event->caxis.axis;
gamepadEvent.id = event->caxis.which;
gamepadEvent.axisValue = event->caxis.value / 32768.0;
GamepadEvent::Dispatch (&gamepadEvent);
break;
case SDL_CONTROLLERBUTTONDOWN:
gamepadEvent.type = BUTTON_DOWN;
gamepadEvent.button = event->cbutton.button;
gamepadEvent.id = event->cbutton.which;
GamepadEvent::Dispatch (&gamepadEvent);
break;
case SDL_CONTROLLERBUTTONUP:
gamepadEvent.type = BUTTON_UP;
gamepadEvent.button = event->cbutton.button;
gamepadEvent.id = event->cbutton.which;
GamepadEvent::Dispatch (&gamepadEvent);
break;
case SDL_CONTROLLERDEVICEADDED:
if (SDLGamepad::Connect (event->cdevice.which)) {
gamepadEvent.type = CONNECT;
gamepadEvent.id = SDLGamepad::GetInstanceID (event->cdevice.which);
GamepadEvent::Dispatch (&gamepadEvent);
}
break;
case SDL_CONTROLLERDEVICEREMOVED: {
if (SDLGamepad::Disconnect (event->cdevice.which)) {
gamepadEvent.type = DISCONNECT;
gamepadEvent.id = event->cdevice.which;
GamepadEvent::Dispatch (&gamepadEvent);
}
break;
}
}
}
}
void SDLApplication::ProcessKeyEvent (SDL_Event* event) {
if (KeyEvent::callback) {

View File

@@ -6,6 +6,7 @@
#include <app/Application.h>
#include <app/UpdateEvent.h>
#include <graphics/RenderEvent.h>
#include <ui/GamepadEvent.h>
#include <ui/KeyEvent.h>
#include <ui/MouseEvent.h>
#include <ui/TouchEvent.h>
@@ -35,6 +36,7 @@ namespace lime {
private:
void HandleEvent (SDL_Event* event);
void ProcessGamepadEvent (SDL_Event* event);
void ProcessKeyEvent (SDL_Event* event);
void ProcessMouseEvent (SDL_Event* event);
void ProcessTouchEvent (SDL_Event* event);
@@ -43,6 +45,7 @@ namespace lime {
bool active;
Uint32 currentUpdate;
double framePeriod;
GamepadEvent gamepadEvent;
KeyEvent keyEvent;
Uint32 lastUpdate;
MouseEvent mouseEvent;

View File

@@ -0,0 +1,67 @@
#include "SDLGamepad.h"
namespace lime {
std::map<int, SDL_GameController*> gameControllers = std::map<int, SDL_GameController*> ();
std::map<int, int> gameControllerIDs = std::map<int, int> ();
bool SDLGamepad::Connect (int deviceID) {
if (SDL_IsGameController (deviceID)) {
SDL_GameController *gameController = SDL_GameControllerOpen (deviceID);
if (gameController) {
SDL_Joystick *joystick = SDL_GameControllerGetJoystick (gameController);
int id = SDL_JoystickInstanceID (joystick);
gameControllers[id] = gameController;
gameControllerIDs[deviceID] = id;
return true;
}
}
return false;
}
bool SDLGamepad::Disconnect (int id) {
if (gameControllers.find (id) != gameControllers.end ()) {
SDL_GameController *gameController = gameControllers[id];
SDL_GameControllerClose (gameController);
gameControllers.erase (id);
return true;
}
return false;
}
const char* Gamepad::GetDeviceName (int id) {
return SDL_GameControllerName (gameControllers[id]);
}
int SDLGamepad::GetInstanceID (int deviceID) {
return gameControllerIDs[deviceID];
}
}

View File

@@ -0,0 +1,27 @@
#ifndef LIME_SDL_GAMEPAD_H
#define LIME_SDL_GAMEPAD_H
#include <SDL.h>
#include <ui/Gamepad.h>
#include <map>
namespace lime {
class SDLGamepad {
public:
static bool Connect (int deviceID);
static int GetInstanceID (int deviceID);
static bool Disconnect (int id);
};
}
#endif

View File

@@ -0,0 +1,60 @@
#include <hx/CFFI.h>
#include <ui/GamepadEvent.h>
namespace lime {
AutoGCRoot* GamepadEvent::callback = 0;
AutoGCRoot* GamepadEvent::eventObject = 0;
static double id_axis;
static int id_button;
static int id_id;
static int id_type;
static int id_value;
static bool init = false;
GamepadEvent::GamepadEvent () {
axis = 0;
axisValue = 0;
button = 0;
id = 0;
type = AXIS_MOVE;
}
void GamepadEvent::Dispatch (GamepadEvent* event) {
if (GamepadEvent::callback) {
if (!init) {
id_axis = val_id ("axis");
id_button = val_id ("button");
id_id = val_id ("id");
id_type = val_id ("type");
id_value = val_id ("value");
init = true;
}
value object = (GamepadEvent::eventObject ? GamepadEvent::eventObject->get () : alloc_empty_object ());
alloc_field (object, id_axis, alloc_float (event->axis));
alloc_field (object, id_button, alloc_int (event->button));
alloc_field (object, id_id, alloc_int (event->id));
alloc_field (object, id_type, alloc_int (event->type));
alloc_field (object, id_value, alloc_float (event->axisValue));
val_call0 (GamepadEvent::callback->get ());
}
}
}