Created
February 22, 2014 21:21
-
-
Save rlabrecque/9162607 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System; | |
using SDL2; | |
public class SDL2Gamepad : MonoBehaviour { | |
private System.IntPtr gamecontroller = System.IntPtr.Zero; | |
private int m_device = 0; | |
private void Awake() { | |
/* Enable standard application logging */ | |
SDL.SDL_LogSetPriority(SDL.SDL_LOG_CATEGORY_APPLICATION, SDL.SDL_LogPriority.SDL_LOG_PRIORITY_INFO); | |
/* Initialize SDL (Note: video is required to start event loop) */ | |
int ret = SDL.SDL_Init(SDL.SDL_INIT_JOYSTICK | SDL.SDL_INIT_GAMECONTROLLER); | |
print("SDL_Init() - " + ret); | |
if (ret < 0) { | |
//SDL.SDL_LogError(SDL.SDL_LOG_CATEGORY_APPLICATION, string.Format("Couldn't initialize SDL: {0}\n", SDL.SDL_GetError())); | |
Debug.LogError(string.Format("Couldn't initialize SDL: {0}\n", SDL.SDL_GetError())); | |
return; | |
} | |
//SDL.SDL_GameControllerAddMapping(""); //TODO | |
int nController = 0; | |
byte[] guid2 = new byte[64]; | |
string guid; | |
/* Print information about the controller */ | |
for (int i = 0; i < SDL.SDL_NumJoysticks(); ++i) { | |
string name; | |
string description; | |
SDL.SDL_JoystickGetGUIDString(SDL.SDL_JoystickGetDeviceGUID(i), guid2, guid2.Length); | |
guid = System.Text.Encoding.UTF8.GetString(guid2).Trim('\0'); | |
if (SDL.SDL_IsGameController(i) == SDL.SDL_bool.SDL_TRUE) { | |
m_device = i; | |
nController++; | |
name = SDL.SDL_GameControllerNameForIndex(i); | |
description = "Controller"; | |
} | |
else { | |
name = SDL.SDL_JoystickNameForIndex(i); | |
description = "Joystick"; | |
} | |
//SDL.SDL_Log(string.Format("{0} {1}: {2} (guid {3})\n", description, i, string.IsNullOrEmpty(name) ? name : "Unknown", guid)); | |
Debug.Log(string.Format("{0} {1}: {2} (guid {3})", description, i, string.IsNullOrEmpty(name) ? "Unknown" : name, guid)); | |
} | |
//SDL.SDL_Log(string.Format("There are {0} game controller(s) attached ({1} joystick(s))\n", nController, SDL.SDL_NumJoysticks())); | |
Debug.Log(string.Format("There are {0} game controller(s) attached ({1} joystick(s))", nController, SDL.SDL_NumJoysticks())); | |
if (nController > 0) { | |
SDL.SDL_JoystickGetGUIDString(SDL.SDL_JoystickGetDeviceGUID(m_device), guid2, guid2.Length); | |
guid = System.Text.Encoding.UTF8.GetString(guid2).Trim('\0'); | |
//SDL.SDL_Log(string.Format("Attempting to open device {0}, guid {1}\n", device, guid)); | |
Debug.Log(string.Format("Attempting to open device {0}, guid {1}\n", m_device, guid)); | |
gamecontroller = SDL.SDL_GameControllerOpen(m_device); | |
if (gamecontroller == System.IntPtr.Zero) { | |
//SDL.SDL_LogError(SDL.SDL_LOG_CATEGORY_APPLICATION, string.Format("Couldn't open gamecontroller {0}: {1}\n", m_device, SDL.SDL_GetError())); | |
Debug.LogError(string.Format("Couldn't open gamecontroller {0}: {1}\n", m_device, SDL.SDL_GetError())); | |
} | |
#if DISABLED | |
/* Create a window to display controller state */ | |
System.IntPtr window = SDL.SDL_CreateWindow("Title", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, 500, 500, 0); | |
if (window == System.IntPtr.Zero) { | |
//SDL.SDL_LogError(SDL.SDL_LOG_CATEGORY_APPLICATION, string.Format("Couldn't create window: {0}\n", SDL.SDL_GetError())); | |
Debug.Log(string.Format("Couldn't create window: {0}\n", SDL.SDL_GetError())); | |
return; | |
} | |
System.IntPtr screen = SDL.SDL_CreateRenderer(window, -1, 0); | |
if (screen == System.IntPtr.Zero) { | |
//SDL.SDL_LogError(SDL.SDL_LOG_CATEGORY_APPLICATION, string.Format("Couldn't create renderer: {0}\n", SDL.SDL_GetError())); | |
Debug.Log(string.Format("Couldn't create renderer: {0}\n", SDL.SDL_GetError())); | |
SDL.SDL_DestroyWindow(window); | |
return; | |
} | |
SDL.SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, 255); | |
SDL.SDL_RenderClear(screen); | |
SDL.SDL_RenderPresent(screen); | |
SDL.SDL_RaiseWindow(window); | |
/* scale for platforms that don't give you the window size you asked for. */ | |
SDL.SDL_RenderSetLogicalSize(screen, 500, 500); | |
#endif | |
} | |
} | |
private void OnDestroy() { | |
SDL.SDL_GameControllerClose(gamecontroller); | |
SDL.SDL_QuitSubSystem(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_JOYSTICK | SDL.SDL_INIT_GAMECONTROLLER); | |
} | |
private void Update() { | |
SDL.SDL_Event _event; | |
while (SDL.SDL_PollEvent(out _event) != 0) { | |
print(_event.type); | |
switch (_event.type) { | |
case SDL.SDL_EventType.SDL_CONTROLLERDEVICEADDED: | |
if (gamecontroller == System.IntPtr.Zero) { | |
gamecontroller = SDL.SDL_GameControllerOpen(_event.cdevice.which); | |
m_device = _event.cdevice.which; | |
} | |
break; | |
case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN: | |
Debug.Log("SDL_MOUSEBUTTONDOWN"); | |
goto case SDL.SDL_EventType.SDL_QUIT; | |
case SDL.SDL_EventType.SDL_KEYDOWN: | |
Debug.Log("SDL_KEYDOWN: " + _event.key.keysym.sym); | |
goto case SDL.SDL_EventType.SDL_QUIT; | |
/* Fall through to signal quit */ | |
case SDL.SDL_EventType.SDL_QUIT: | |
Debug.Log("SDL_QUIT"); | |
break; | |
default: | |
break; | |
} | |
} | |
if (gamecontroller == System.IntPtr.Zero) { | |
return; | |
} | |
for (int i = 0; i < (int)SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_MAX; ++i) { | |
if (SDL.SDL_GameControllerGetButton(gamecontroller, (SDL.SDL_GameControllerButton)i) == SDL.SDL_PRESSED) { | |
Debug.Log("Button " + i + " is pressed"); | |
} | |
} | |
for (int i = 0; i < (int)SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_MAX; ++i) { | |
const short deadzone = 8000; /* !!! FIXME: real deadzone */ | |
short value = SDL.SDL_GameControllerGetAxis(gamecontroller, (SDL.SDL_GameControllerAxis)(i)); | |
if (value < -deadzone || value > deadzone) { | |
Debug.Log(value); | |
} | |
} | |
if (SDL.SDL_GameControllerGetAttached(gamecontroller) != SDL.SDL_bool.SDL_TRUE) { | |
SDL.SDL_GameControllerClose(gamecontroller); | |
gamecontroller = System.IntPtr.Zero; | |
//SDL.SDL_Log("Waiting for attach\n"); | |
Debug.Log("Waiting for reattach"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment