Skip to content

Conversation

malashin
Copy link
Collaborator

@malashin malashin commented Feb 22, 2018

https://wiki.libsdl.org/SDL_HapticEffect

Since unions are not supported in Go, HapticEffect is now interface (iHapticEffect).

HapticConstant, HapticPeriodic, HapticCondition, HapticRamp, HapticLeftRight, HapticCustom structs now implement iHapticEffect interface.
If pointers to those structs are passed to the functions that previously used HapticEffect, new appropriate *C.SDL_HapticEffect will be created inside those functions.

This way there will be no need of creating HapticEffect and modifying it's HapticConstant, HapticPeriodic, HapticCondition, HapticRamp, HapticLeftRight, HapticCustom structs.

Also previous version had no way of modifying this data at all, because it returned copies of those stucts instead of pointers to them.

Example:

package main

import (
	"fmt"
	"time"

	"github.com/veandco/go-sdl2/sdl"
)

func main() {
	// Init SDL.
	err := sdl.Init(sdl.INIT_EVERYTHING)
	if err != nil {
		panic(err)
	}

	// Open first joystick.
	joy := sdl.JoystickOpen(0)
	defer joy.Close()

	// Print out number of available haptics and the name of the joystick.
	fmt.Println(sdl.NumHaptics())
	fmt.Println("Name:", joy.Name())

	// Open haptics for this joystick.
	h, err := sdl.HapticOpenFromJoystick(joy)
	if err != nil {
		panic(err)
	}
	defer h.Close()

	// Create HapticLeftRight to test low frequency motor.
	e := &sdl.HapticLeftRight{
		Type:           sdl.HAPTIC_LEFTRIGHT,
		Length:         3000,
		LargeMagnitude: 32767,
		SmallMagnitude: 0,
	}

	// Add this effect to haptics.
	id, err := h.NewEffect(e)
	if err != nil {
		panic(err)
	}
	// Run the effect.
	h.RunEffect(id, 1)
	// Wait 3 seconds.
	time.Sleep(3 * time.Second)
	// Destroy the effect.
	h.DestroyEffect(id)

	// Modify HapticLeftRight to test high frequency motor.
	e.LargeMagnitude = 0
	e.SmallMagnitude = 32767

	// Add this effect to haptics.
	id, err = h.NewEffect(e)
	if err != nil {
		panic(err)
	}
	// Run the effect.
	h.RunEffect(id, 1)
	// Wait 3 seconds.
	time.Sleep(3 * time.Second)
	// Destroy the effect.
	h.DestroyEffect(id)
}

@malashin malashin merged commit 99737f8 into veandco:master Feb 22, 2018
neputevshina pushed a commit to neputevshina/go-sdl2 that referenced this pull request Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant