浏览代码

Added GLFW Joystick.

Brucey 5 年之前
父节点
当前提交
af2065956a
共有 2 个文件被更改,包括 186 次插入0 次删除
  1. 54 0
      glfwjoystick.mod/common.bmx
  2. 132 0
      glfwjoystick.mod/glfwjoystick.bmx

+ 54 - 0
glfwjoystick.mod/common.bmx

@@ -0,0 +1,54 @@
+' Copyright (c) 2020 Bruce A Henderson
+'
+' This software is provided 'as-is', without any express or implied
+' warranty. In no event will the authors be held liable for any damages
+' arising from the use of this software.
+'
+' Permission is granted to anyone to use this software for any purpose,
+' including commercial applications, and to alter it and redistribute it
+' freely, subject to the following restrictions:
+'
+'    1. The origin of this software must not be misrepresented; you must not
+'    claim that you wrote the original software. If you use this software
+'    in a product, an acknowledgment in the product documentation would be
+'    appreciated but is not required.
+'
+'    2. Altered source versions must be plainly marked as such, and must not be
+'    misrepresented as being the original software.
+'
+'    3. This notice may not be removed or altered from any source
+'    distribution.
+'
+SuperStrict
+
+Import GLFW.GLFW
+
+Extern
+	Function bmx_glfw_glfwJoystickPresent:Int(id:Int)="glfwJoystickPresent"
+	Function bmx_glfw_glfwGetJoystickAxes:Float Ptr(id:Int, count:Int Var)="glfwGetJoystickAxes"
+	Function bmx_glfw_glfwGetJoystickButtons:Byte Ptr(id:Int, count:Int Var)="glfwGetJoystickButtons"
+	Function bmx_glfw_glfwGetJoystickHats:Byte Ptr(id:Int, count:Int Var)="glfwGetJoystickHats"
+	Function bmx_glfw_glfwGetJoystickName:Byte Ptr(id:Int)="glfwGetJoystickName"
+	Function bmx_glfw_glfwGetJoystickGUID:Byte Ptr(id:Int)="glfwGetJoystickGUID"
+	Function bmx_glfw_glfwJoystickIsGamepad:Int(id:Int)="glfwJoystickIsGamepad"
+	Function bmx_glfw_glfwSetJoystickCallback(func(id:Int, event:Int))="glfwSetJoystickCallback"
+End Extern
+
+Const GLFW_JOYSTICK_1:Int = 0
+Const GLFW_JOYSTICK_2:Int = 1
+Const GLFW_JOYSTICK_3:Int = 2
+Const GLFW_JOYSTICK_4:Int = 3
+Const GLFW_JOYSTICK_5:Int = 4
+Const GLFW_JOYSTICK_6:Int = 5
+Const GLFW_JOYSTICK_7:Int = 6
+Const GLFW_JOYSTICK_8:Int = 7
+Const GLFW_JOYSTICK_9:Int = 8
+Const GLFW_JOYSTICK_10:Int = 9
+Const GLFW_JOYSTICK_11:Int = 10
+Const GLFW_JOYSTICK_12:Int = 11
+Const GLFW_JOYSTICK_13:Int = 12
+Const GLFW_JOYSTICK_14:Int = 13
+Const GLFW_JOYSTICK_15:Int = 14
+Const GLFW_JOYSTICK_16:Int = 15
+Const GLFW_JOYSTICK_LAST:Int = GLFW_JOYSTICK_16
+

+ 132 - 0
glfwjoystick.mod/glfwjoystick.bmx

@@ -0,0 +1,132 @@
+' Copyright (c) 2020 Bruce A Henderson
+'
+' This software is provided 'as-is', without any express or implied
+' warranty. In no event will the authors be held liable for any damages
+' arising from the use of this software.
+'
+' Permission is granted to anyone to use this software for any purpose,
+' including commercial applications, and to alter it and redistribute it
+' freely, subject to the following restrictions:
+'
+'    1. The origin of this software must not be misrepresented; you must not
+'    claim that you wrote the original software. If you use this software
+'    in a product, an acknowledgment in the product documentation would be
+'    appreciated but is not required.
+'
+'    2. Altered source versions must be plainly marked as such, and must not be
+'    misrepresented as being the original software.
+'
+'    3. This notice may not be removed or altered from any source
+'    distribution.
+'
+SuperStrict
+
+Rem
+bbdoc: GLFW Joystick
+End Rem
+Module GLFW.GLFWJoystick
+
+Import "common.bmx"
+
+Rem
+bbdoc: Returns whether the specified joystick is present.
+returns : #True if the joystick is present, or #False otherwise.
+about: There is no need to call this function before other functions that accept a joystick ID, as they all check for presence before performing any other work.
+End Rem
+Function JoystickPresent:Int(id:Int)
+	Return bmx_glfw_glfwJoystickPresent(id)
+End Function
+
+Rem
+bbdoc: Returns the values of all axes of the specified joystick.
+about: Each element in the array is a value between -1.0 and 1.0.
+about: If the specified joystick is not present this function will return #Null but will not generate an error.
+This can be used instead of first calling #JoystickPresent.
+End Rem
+Function GetJoystickAxes:Float Ptr(id:Int, count:Int Var)
+	Return bmx_glfw_glfwGetJoystickAxes(id, count)
+End Function
+
+Rem
+bbdoc: Returns the state of all buttons of the specified joystick.
+about: Each element in the array is either 1 or 0.
+
+For backward compatibility with earlier versions that did not have #GetJoystickHats, the button array also includes all hats, each represented as four buttons.
+The hats are in the same order as returned by #GetJoystickHats and are in the order up, right, down and left. To disable these extra buttons, set
+the #GLFW_JOYSTICK_HAT_BUTTONS init hint before initialization.
+
+If the specified joystick is not present this function will return #Null but will not generate an error.
+This can be used instead of first calling #JoystickPresent.
+End Rem
+Function GetJoystickButtons:Byte Ptr(id:Int, count:Int Var)
+	Return bmx_glfw_glfwGetJoystickButtons(id, count)
+End Function
+
+Rem
+bbdoc: returns the state of all hats of the specified joystick. Each element in the array is one of the following values: 
+GLFW_HAT_CENTERED, #GLFW_HAT_UP, #GLFW_HAT_RIGHT, #GLFW_HAT_DOWN, #GLFW_HAT_LEFT, #GLFW_HAT_RIGHT_UP, #GLFW_HAT_RIGHT_DOWN, #GLFW_HAT_LEFT_UP and GLFW_HAT_LEFT_DOWN
+
+The diagonal directions are bitwise combinations of the primary (up, right, down and left) directions and you can test for these individually by
+ANDing it with the corresponding direction.
+
+If the specified joystick is not present this function will return #Null but will not generate an error.
+This can be used instead of first calling #JoystickPresent.
+End Rem
+Function GetJoystickHats:Byte Ptr(id:Int, count:Int Var)
+	Return bmx_glfw_glfwGetJoystickHats(id, count)
+End Function
+
+Rem
+bbdoc: This function returns the name of the specified joystick.
+about: The returned string is allocated and freed by GLFW. You should not free it yourself.
+
+If the specified joystick is not present this function will return #Null but will not generate an error.
+This can be used instead of first calling #JoystickPresent.
+End Rem
+Function GetJoystickName:String(id:Int)
+	Local n:Byte Ptr = bmx_glfw_glfwGetJoystickName(id)
+	If n Then
+		Return String.FromUTF8String(n)
+	End If
+End Function
+
+Rem
+bbdoc: This function returns the SDL compatible GUID, as a hexadecimal #String, of the specified joystick.
+about: The GUID is what connects a joystick to a gamepad mapping. A connected joystick will always have a GUID even if there is no
+gamepad mapping assigned to it.
+
+If the specified joystick is not present this function will return #Null but will not generate an error.
+This can be used instead of first calling #JoystickPresent.
+
+The GUID uses the format introduced in SDL 2.0.5. This GUID tries to uniquely identify the make and model of a joystick
+but does not identify a specific unit, e.g. all wired Xbox 360 controllers will have the same GUID on that platform.
+The GUID for a unit may vary between platforms depending on what hardware information the platform specific APIs provide.
+End Rem
+Function GetJoystickGUID:String(id:Int)
+	Local g:Byte Ptr = bmx_glfw_glfwGetJoystickGUID(id)
+	If g Then
+		Return String.FromUTF8String(g)
+	End If
+End Function
+
+Rem
+bbdoc: This function returns whether the specified joystick is both present and has a gamepad mapping.
+returns: #True if a joystick is both present and has a gamepad mapping, or #False otherwise.
+about: If the specified joystick is present but does not have a gamepad mapping this function will return #False but will not generate an error.
+Call #JoystickPresent to check if a joystick is present regardless of whether it has a mapping.
+End Rem
+Function JoystickIsGamepad:Int(id:Int)
+	Return bmx_glfw_glfwJoystickIsGamepad(id)
+End Function
+
+Rem
+bbdoc: This function sets the joystick configuration callback, or removes the currently set callback.
+about: This is called when a joystick is connected to or disconnected from the system.
+
+For joystick connection and disconnection events to be delivered on all platforms, you need to call one of the event processing functions.
+Joystick disconnection may also be detected and the callback called by joystick functions. The function will then return whatever it returns
+if the joystick is not present.
+End Rem
+Function SetJoystickCallback(func(id:Int, event:Int))
+	bmx_glfw_glfwSetJoystickCallback(func)
+End Function