2
0
Pascal Peridont 8 жил өмнө
parent
commit
64d29dea45

+ 22 - 0
libs/sdl/sdl.c

@@ -443,6 +443,28 @@ DEFINE_PRIM(_BOOL, gctrl_get_button, TGCTRL _I32);
 DEFINE_PRIM(_I32, gctrl_get_id, TGCTRL);
 DEFINE_PRIM(_BYTES, gctrl_get_name, TGCTRL);
 
+HL_PRIM SDL_Haptic *HL_NAME(haptic_open)(SDL_GameController *controller) {
+	return SDL_HapticOpenFromJoystick(SDL_GameControllerGetJoystick(controller));
+}
+
+HL_PRIM void HL_NAME(haptic_close)(SDL_Haptic *haptic) {
+	SDL_HapticClose(haptic);
+}
+
+HL_PRIM int HL_NAME(haptic_rumble_init)(SDL_Haptic *haptic) {
+	return SDL_HapticRumbleInit(haptic);
+}
+
+HL_PRIM int HL_NAME(haptic_rumble_play)(SDL_Haptic *haptic, double strength, int length) {
+	return SDL_HapticRumblePlay(haptic, strength, length);
+}
+#define THAPTIC _ABSTRACT(sdl_haptic)
+DEFINE_PRIM(THAPTIC, haptic_open, TGCTRL);
+DEFINE_PRIM(_VOID, haptic_close, THAPTIC);
+DEFINE_PRIM(_I32, haptic_rumble_init, THAPTIC);
+DEFINE_PRIM(_I32, haptic_rumble_play, THAPTIC _F64 _I32);
+
+
 // surface
 
 

+ 35 - 0
libs/sdl/sdl/GameController.hx

@@ -1,11 +1,14 @@
 package sdl;
 
 private typedef GameControllerPtr = hl.Abstract<"sdl_gamecontroller">;
+private typedef HapticPtr = hl.Abstract<"sdl_haptic">;
 
 @:hlNative("sdl")
 class GameController {
 
 	var ptr : GameControllerPtr;
+	var haptic : HapticPtr;
+	var rumbleInitialized : Bool = false;
 
 	public var id(get,never) : Int;
 	public var name(get,never) : String;
@@ -30,7 +33,23 @@ class GameController {
 		return @:privateAccess String.fromUTF8( gctrlGetName(ptr) );
 	}
 
+	public function rumble( strength : Float, length : Int ) : Bool {
+		if( haptic == null && !rumbleInitialized ){
+			rumbleInitialized = true;
+			haptic = hapticOpen(ptr);
+			if( haptic != null && hapticRumbleInit(haptic) != 0 ){
+				hapticClose(haptic);
+				haptic = null;
+			}
+		}
+		if( haptic == null ) return false;
+		return hapticRumblePlay( haptic, strength, length ) == 0;
+	}
+
 	public function close(){
+		if( haptic != null )
+			hapticClose(haptic);
+		haptic = null;
 		gctrlClose( ptr );
 		ptr = null;
 	}
@@ -62,4 +81,20 @@ class GameController {
 		return null;
 	}
 
+	static function hapticOpen( controller : GameControllerPtr ) : HapticPtr {
+		return null;
+	}
+
+	static function hapticClose( haptic : HapticPtr ) : Void {
+	}
+
+	static function hapticRumbleInit( haptic : HapticPtr ) : Int {
+		return -1;
+	}
+	
+	static function hapticRumblePlay( haptic : HapticPtr, strength : Float, length : Int ) : Int {
+		return -1;
+	}
+
+
 }