Compare commits

...

8 Commits

Author SHA1 Message Date
Hajime Hoshi
01dc4ed9b1 inpututil: refactoring: clean up gamepad implementations 2024-10-26 19:28:53 +09:00
Hajime Hoshi
e6b8ebe8a2 internal/gamepaddb: bug fix: typo
Closes #3148
2024-10-26 19:05:51 +09:00
Hajime Hoshi
9127c30223 inpututil: bug fix: AppendJustReleasedGamepadButtons and AppendJustReleasedStandardGamepadButtons returned just-pressed buttons
Closes #3147
2024-10-26 17:31:24 +09:00
Hajime Hoshi
812feec1d5 inpututil: reland: refactoring: use clear 2024-10-26 16:54:31 +09:00
Hajime Hoshi
7fe50478a7 Revert "inpututil: refactoring: use clear"
This reverts commit d6427d63ca.

Reason: wrong clear usages
2024-10-26 16:52:17 +09:00
Hajime Hoshi
d6427d63ca inpututil: refactoring: use clear 2024-10-26 16:47:43 +09:00
Hajime Hoshi
a6b278d0e5 inpututil: refactoring 2024-10-26 16:26:04 +09:00
Hajime Hoshi
c7880a5ad6 internal/atlas: reduce for-loops 2024-10-26 16:02:25 +09:00
4 changed files with 148 additions and 204 deletions

View File

@ -16,7 +16,7 @@
package inpututil package inpututil
import ( import (
"sort" "slices"
"sync" "sync"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
@ -28,21 +28,20 @@ type pos struct {
y int y int
} }
type gamepadState struct {
buttonDurations [ebiten.GamepadButtonMax + 1]int
standardButtonDurations [ebiten.StandardGamepadButtonMax + 1]int
}
type inputState struct { type inputState struct {
keyDurations []int keyDurations [ebiten.KeyMax + 1]int
prevKeyDurations []int prevKeyDurations [ebiten.KeyMax + 1]int
mouseButtonDurations map[ebiten.MouseButton]int mouseButtonDurations [ebiten.MouseButtonMax + 1]int
prevMouseButtonDurations map[ebiten.MouseButton]int prevMouseButtonDurations [ebiten.MouseButtonMax + 1]int
gamepadIDs map[ebiten.GamepadID]struct{} gamepadStates map[ebiten.GamepadID]gamepadState
prevGamepadIDs map[ebiten.GamepadID]struct{} prevGamepadStates map[ebiten.GamepadID]gamepadState
gamepadButtonDurations map[ebiten.GamepadID][]int
prevGamepadButtonDurations map[ebiten.GamepadID][]int
standardGamepadButtonDurations map[ebiten.GamepadID][]int
prevStandardGamepadButtonDurations map[ebiten.GamepadID][]int
touchIDs map[ebiten.TouchID]struct{} touchIDs map[ebiten.TouchID]struct{}
touchDurations map[ebiten.TouchID]int touchDurations map[ebiten.TouchID]int
@ -57,20 +56,8 @@ type inputState struct {
} }
var theInputState = &inputState{ var theInputState = &inputState{
keyDurations: make([]int, ebiten.KeyMax+1), gamepadStates: map[ebiten.GamepadID]gamepadState{},
prevKeyDurations: make([]int, ebiten.KeyMax+1), prevGamepadStates: map[ebiten.GamepadID]gamepadState{},
mouseButtonDurations: map[ebiten.MouseButton]int{},
prevMouseButtonDurations: map[ebiten.MouseButton]int{},
gamepadIDs: map[ebiten.GamepadID]struct{}{},
prevGamepadIDs: map[ebiten.GamepadID]struct{}{},
gamepadButtonDurations: map[ebiten.GamepadID][]int{},
prevGamepadButtonDurations: map[ebiten.GamepadID][]int{},
standardGamepadButtonDurations: map[ebiten.GamepadID][]int{},
prevStandardGamepadButtonDurations: map[ebiten.GamepadID][]int{},
touchIDs: map[ebiten.TouchID]struct{}{}, touchIDs: map[ebiten.TouchID]struct{}{},
touchDurations: map[ebiten.TouchID]int{}, touchDurations: map[ebiten.TouchID]int{},
@ -91,109 +78,83 @@ func (i *inputState) update() {
defer i.m.Unlock() defer i.m.Unlock()
// Keyboard // Keyboard
copy(i.prevKeyDurations, i.keyDurations) copy(i.prevKeyDurations[:], i.keyDurations[:])
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ { for idx := range i.keyDurations {
if ebiten.IsKeyPressed(k) { if ebiten.IsKeyPressed(ebiten.Key(idx)) {
i.keyDurations[k]++ i.keyDurations[idx]++
} else { } else {
i.keyDurations[k] = 0 i.keyDurations[idx] = 0
} }
} }
// Mouse // Mouse
for b := ebiten.MouseButton(0); b <= ebiten.MouseButtonMax; b++ { copy(i.prevMouseButtonDurations[:], i.mouseButtonDurations[:])
i.prevMouseButtonDurations[b] = i.mouseButtonDurations[b] for idx := range i.mouseButtonDurations {
if ebiten.IsMouseButtonPressed(b) { if ebiten.IsMouseButtonPressed(ebiten.MouseButton(idx)) {
i.mouseButtonDurations[b]++ i.mouseButtonDurations[idx]++
} else { } else {
i.mouseButtonDurations[b] = 0 i.mouseButtonDurations[idx] = 0
} }
} }
// Gamepads // Gamepads
// Copy the gamepad IDs. // Copy the gamepad states.
for id := range i.prevGamepadIDs { clear(i.prevGamepadStates)
delete(i.prevGamepadIDs, id) for id, s := range i.gamepadStates {
} i.prevGamepadStates[id] = s
for id := range i.gamepadIDs {
i.prevGamepadIDs[id] = struct{}{}
} }
// Copy the gamepad button durations.
for id := range i.prevGamepadButtonDurations {
delete(i.prevGamepadButtonDurations, id)
}
for id, ds := range i.gamepadButtonDurations {
i.prevGamepadButtonDurations[id] = append([]int{}, ds...)
}
for id := range i.prevStandardGamepadButtonDurations {
delete(i.prevStandardGamepadButtonDurations, id)
}
for id, ds := range i.standardGamepadButtonDurations {
i.prevStandardGamepadButtonDurations[id] = append([]int{}, ds...)
}
for id := range i.gamepadIDs {
delete(i.gamepadIDs, id)
}
i.gamepadIDsBuf = ebiten.AppendGamepadIDs(i.gamepadIDsBuf[:0]) i.gamepadIDsBuf = ebiten.AppendGamepadIDs(i.gamepadIDsBuf[:0])
for _, id := range i.gamepadIDsBuf { for _, id := range i.gamepadIDsBuf {
i.gamepadIDs[id] = struct{}{} state := i.gamepadStates[id]
if _, ok := i.gamepadButtonDurations[id]; !ok { for b := range i.gamepadStates[id].buttonDurations {
i.gamepadButtonDurations[id] = make([]int, ebiten.GamepadButtonMax+1) if ebiten.IsGamepadButtonPressed(id, ebiten.GamepadButton(b)) {
} state.buttonDurations[b]++
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
if ebiten.IsGamepadButtonPressed(id, b) {
i.gamepadButtonDurations[id][b]++
} else { } else {
i.gamepadButtonDurations[id][b] = 0 state.buttonDurations[b] = 0
} }
} }
if _, ok := i.standardGamepadButtonDurations[id]; !ok { for b := range i.gamepadStates[id].standardButtonDurations {
i.standardGamepadButtonDurations[id] = make([]int, ebiten.StandardGamepadButtonMax+1) if ebiten.IsStandardGamepadButtonPressed(id, ebiten.StandardGamepadButton(b)) {
} state.standardButtonDurations[b]++
for b := ebiten.StandardGamepadButton(0); b <= ebiten.StandardGamepadButtonMax; b++ {
if ebiten.IsStandardGamepadButtonPressed(id, b) {
i.standardGamepadButtonDurations[id][b]++
} else { } else {
i.standardGamepadButtonDurations[id][b] = 0 state.standardButtonDurations[b] = 0
} }
} }
i.gamepadStates[id] = state
} }
for id := range i.gamepadButtonDurations {
if _, ok := i.gamepadIDs[id]; !ok { // Remove disconnected gamepads.
delete(i.gamepadButtonDurations, id) for id := range i.gamepadStates {
var found bool
for _, id2 := range i.gamepadIDsBuf {
if id == id2 {
found = true
break
} }
} }
for id := range i.standardGamepadButtonDurations { if !found {
if _, ok := i.gamepadIDs[id]; !ok { delete(i.gamepadStates, id)
delete(i.standardGamepadButtonDurations, id)
} }
} }
// Touches // Touches
// Copy the touch durations and positions. // Copy the touch durations and positions.
for id := range i.prevTouchDurations { clear(i.prevTouchPositions)
delete(i.prevTouchDurations, id)
}
for id := range i.touchDurations { for id := range i.touchDurations {
i.prevTouchDurations[id] = i.touchDurations[id] i.prevTouchDurations[id] = i.touchDurations[id]
} }
for id := range i.prevTouchPositions { clear(i.prevTouchPositions)
delete(i.prevTouchPositions, id)
}
for id := range i.touchPositions { for id := range i.touchPositions {
i.prevTouchPositions[id] = i.touchPositions[id] i.prevTouchPositions[id] = i.touchPositions[id]
} }
for id := range i.touchIDs { clear(i.touchIDs)
delete(i.touchIDs, id)
}
i.touchIDsBuf = ebiten.AppendTouchIDs(i.touchIDsBuf[:0]) i.touchIDsBuf = ebiten.AppendTouchIDs(i.touchIDsBuf[:0])
for _, id := range i.touchIDsBuf { for _, id := range i.touchIDsBuf {
i.touchIDs[id] = struct{}{} i.touchIDs[id] = struct{}{}
@ -266,14 +227,14 @@ func AppendJustReleasedKeys(keys []ebiten.Key) []ebiten.Key {
theInputState.m.RLock() theInputState.m.RLock()
defer theInputState.m.RUnlock() defer theInputState.m.RUnlock()
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ { for i := range theInputState.keyDurations {
if theInputState.keyDurations[k] != 0 { if theInputState.keyDurations[i] != 0 {
continue continue
} }
if theInputState.prevKeyDurations[k] == 0 { if theInputState.prevKeyDurations[i] == 0 {
continue continue
} }
keys = append(keys, k) keys = append(keys, ebiten.Key(i))
} }
return keys return keys
} }
@ -296,9 +257,8 @@ func IsKeyJustPressed(key ebiten.Key) bool {
// IsKeyJustReleased is concurrent safe. // IsKeyJustReleased is concurrent safe.
func IsKeyJustReleased(key ebiten.Key) bool { func IsKeyJustReleased(key ebiten.Key) bool {
theInputState.m.RLock() theInputState.m.RLock()
r := theInputState.keyDurations[key] == 0 && theInputState.prevKeyDurations[key] > 0 defer theInputState.m.RUnlock()
theInputState.m.RUnlock() return theInputState.keyDurations[key] == 0 && theInputState.prevKeyDurations[key] > 0
return r
} }
// KeyPressDuration returns how long the key is pressed in ticks (Update). // KeyPressDuration returns how long the key is pressed in ticks (Update).
@ -308,9 +268,8 @@ func IsKeyJustReleased(key ebiten.Key) bool {
// KeyPressDuration is concurrent safe. // KeyPressDuration is concurrent safe.
func KeyPressDuration(key ebiten.Key) int { func KeyPressDuration(key ebiten.Key) int {
theInputState.m.RLock() theInputState.m.RLock()
s := theInputState.keyDurations[key] defer theInputState.m.RUnlock()
theInputState.m.RUnlock() return theInputState.keyDurations[key]
return s
} }
// IsMouseButtonJustPressed returns a boolean value indicating // IsMouseButtonJustPressed returns a boolean value indicating
@ -331,10 +290,8 @@ func IsMouseButtonJustPressed(button ebiten.MouseButton) bool {
// IsMouseButtonJustReleased is concurrent safe. // IsMouseButtonJustReleased is concurrent safe.
func IsMouseButtonJustReleased(button ebiten.MouseButton) bool { func IsMouseButtonJustReleased(button ebiten.MouseButton) bool {
theInputState.m.RLock() theInputState.m.RLock()
r := theInputState.mouseButtonDurations[button] == 0 && defer theInputState.m.RUnlock()
theInputState.prevMouseButtonDurations[button] > 0 return theInputState.mouseButtonDurations[button] == 0 && theInputState.prevMouseButtonDurations[button] > 0
theInputState.m.RUnlock()
return r
} }
// MouseButtonPressDuration returns how long the mouse button is pressed in ticks (Update). // MouseButtonPressDuration returns how long the mouse button is pressed in ticks (Update).
@ -344,9 +301,8 @@ func IsMouseButtonJustReleased(button ebiten.MouseButton) bool {
// MouseButtonPressDuration is concurrent safe. // MouseButtonPressDuration is concurrent safe.
func MouseButtonPressDuration(button ebiten.MouseButton) int { func MouseButtonPressDuration(button ebiten.MouseButton) int {
theInputState.m.RLock() theInputState.m.RLock()
s := theInputState.mouseButtonDurations[button] defer theInputState.m.RUnlock()
theInputState.m.RUnlock() return theInputState.mouseButtonDurations[button]
return s
} }
// AppendJustConnectedGamepadIDs appends gamepad IDs that are connected just in the current tick to gamepadIDs, // AppendJustConnectedGamepadIDs appends gamepad IDs that are connected just in the current tick to gamepadIDs,
@ -357,18 +313,17 @@ func MouseButtonPressDuration(button ebiten.MouseButton) int {
// //
// AppendJustConnectedGamepadIDs is concurrent safe. // AppendJustConnectedGamepadIDs is concurrent safe.
func AppendJustConnectedGamepadIDs(gamepadIDs []ebiten.GamepadID) []ebiten.GamepadID { func AppendJustConnectedGamepadIDs(gamepadIDs []ebiten.GamepadID) []ebiten.GamepadID {
origLen := len(gamepadIDs)
theInputState.m.RLock() theInputState.m.RLock()
for id := range theInputState.gamepadIDs { defer theInputState.m.RUnlock()
if _, ok := theInputState.prevGamepadIDs[id]; !ok {
origLen := len(gamepadIDs)
for id := range theInputState.gamepadStates {
if _, ok := theInputState.prevGamepadStates[id]; !ok {
gamepadIDs = append(gamepadIDs, id) gamepadIDs = append(gamepadIDs, id)
} }
} }
theInputState.m.RUnlock()
s := gamepadIDs[origLen:] slices.Sort(gamepadIDs[origLen:])
sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
})
return gamepadIDs return gamepadIDs
} }
@ -389,10 +344,11 @@ func JustConnectedGamepadIDs() []ebiten.GamepadID {
// IsGamepadJustDisconnected is concurrent safe. // IsGamepadJustDisconnected is concurrent safe.
func IsGamepadJustDisconnected(id ebiten.GamepadID) bool { func IsGamepadJustDisconnected(id ebiten.GamepadID) bool {
theInputState.m.RLock() theInputState.m.RLock()
_, prev := theInputState.prevGamepadIDs[id] defer theInputState.m.RUnlock()
_, current := theInputState.gamepadIDs[id]
theInputState.m.RUnlock() _, current := theInputState.gamepadStates[id]
return prev && !current _, prev := theInputState.prevGamepadStates[id]
return !current && prev
} }
// AppendPressedGamepadButtons append currently pressed gamepad buttons to buttons and returns the extended buffer. // AppendPressedGamepadButtons append currently pressed gamepad buttons to buttons and returns the extended buffer.
@ -405,11 +361,12 @@ func AppendPressedGamepadButtons(id ebiten.GamepadID, buttons []ebiten.GamepadBu
theInputState.m.RLock() theInputState.m.RLock()
defer theInputState.m.RUnlock() defer theInputState.m.RUnlock()
if _, ok := theInputState.gamepadButtonDurations[id]; !ok { state, ok := theInputState.gamepadStates[id]
if !ok {
return buttons return buttons
} }
for b, d := range theInputState.gamepadButtonDurations[id] { for b, d := range state.buttonDurations {
if d == 0 { if d == 0 {
continue continue
} }
@ -429,11 +386,12 @@ func AppendJustPressedGamepadButtons(id ebiten.GamepadID, buttons []ebiten.Gamep
theInputState.m.RLock() theInputState.m.RLock()
defer theInputState.m.RUnlock() defer theInputState.m.RUnlock()
if _, ok := theInputState.gamepadButtonDurations[id]; !ok { state, ok := theInputState.gamepadStates[id]
if !ok {
return buttons return buttons
} }
for b, d := range theInputState.gamepadButtonDurations[id] { for b, d := range state.buttonDurations {
if d != 1 { if d != 1 {
continue continue
} }
@ -453,23 +411,23 @@ func AppendJustReleasedGamepadButtons(id ebiten.GamepadID, buttons []ebiten.Game
theInputState.m.RLock() theInputState.m.RLock()
defer theInputState.m.RUnlock() defer theInputState.m.RUnlock()
if _, ok := theInputState.gamepadButtonDurations[id]; !ok { state, ok := theInputState.gamepadStates[id]
if !ok {
return buttons return buttons
} }
if _, ok := theInputState.prevGamepadButtonDurations[id]; !ok { prevState, ok := theInputState.prevGamepadStates[id]
if !ok {
return buttons return buttons
} }
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ { for b := range state.buttonDurations {
if theInputState.gamepadButtonDurations[id][b] == 0 { if state.buttonDurations[b] != 0 {
continue continue
} }
if prevState.buttonDurations[b] == 0 {
if theInputState.prevGamepadButtonDurations[id][b] > 0 {
continue continue
} }
buttons = append(buttons, ebiten.GamepadButton(b))
buttons = append(buttons, b)
} }
return buttons return buttons
@ -493,16 +451,18 @@ func IsGamepadButtonJustPressed(id ebiten.GamepadID, button ebiten.GamepadButton
// IsGamepadButtonJustReleased is concurrent safe. // IsGamepadButtonJustReleased is concurrent safe.
func IsGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.GamepadButton) bool { func IsGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.GamepadButton) bool {
theInputState.m.RLock() theInputState.m.RLock()
prev := 0 defer theInputState.m.RUnlock()
if _, ok := theInputState.prevGamepadButtonDurations[id]; ok {
prev = theInputState.prevGamepadButtonDurations[id][button] state, ok := theInputState.gamepadStates[id]
if !ok {
return false
} }
current := 0 prevState, ok := theInputState.prevGamepadStates[id]
if _, ok := theInputState.gamepadButtonDurations[id]; ok { if !ok {
current = theInputState.gamepadButtonDurations[id][button] return false
} }
theInputState.m.RUnlock()
return current == 0 && prev > 0 return state.buttonDurations[button] == 0 && prevState.buttonDurations[button] > 0
} }
// GamepadButtonPressDuration returns how long the gamepad button of the gamepad id is pressed in ticks (Update). // GamepadButtonPressDuration returns how long the gamepad button of the gamepad id is pressed in ticks (Update).
@ -512,12 +472,14 @@ func IsGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.GamepadButto
// GamepadButtonPressDuration is concurrent safe. // GamepadButtonPressDuration is concurrent safe.
func GamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.GamepadButton) int { func GamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.GamepadButton) int {
theInputState.m.RLock() theInputState.m.RLock()
s := 0 defer theInputState.m.RUnlock()
if _, ok := theInputState.gamepadButtonDurations[id]; ok {
s = theInputState.gamepadButtonDurations[id][button] state, ok := theInputState.gamepadStates[id]
if !ok {
return 0
} }
theInputState.m.RUnlock()
return s return state.buttonDurations[button]
} }
// AppendPressedStandardGamepadButtons append currently pressed standard gamepad buttons to buttons and returns the extended buffer. // AppendPressedStandardGamepadButtons append currently pressed standard gamepad buttons to buttons and returns the extended buffer.
@ -530,15 +492,16 @@ func AppendPressedStandardGamepadButtons(id ebiten.GamepadID, buttons []ebiten.S
theInputState.m.RLock() theInputState.m.RLock()
defer theInputState.m.RUnlock() defer theInputState.m.RUnlock()
if _, ok := theInputState.standardGamepadButtonDurations[id]; !ok { state, ok := theInputState.gamepadStates[id]
if !ok {
return buttons return buttons
} }
for b, d := range theInputState.standardGamepadButtonDurations[id] { for i, d := range state.standardButtonDurations {
if d == 0 { if d == 0 {
continue continue
} }
buttons = append(buttons, ebiten.StandardGamepadButton(b)) buttons = append(buttons, ebiten.StandardGamepadButton(i))
} }
return buttons return buttons
@ -554,11 +517,12 @@ func AppendJustPressedStandardGamepadButtons(id ebiten.GamepadID, buttons []ebit
theInputState.m.RLock() theInputState.m.RLock()
defer theInputState.m.RUnlock() defer theInputState.m.RUnlock()
if _, ok := theInputState.gamepadButtonDurations[id]; !ok { state, ok := theInputState.gamepadStates[id]
if !ok {
return buttons return buttons
} }
for b, d := range theInputState.standardGamepadButtonDurations[id] { for b, d := range state.standardButtonDurations {
if d != 1 { if d != 1 {
continue continue
} }
@ -578,23 +542,23 @@ func AppendJustReleasedStandardGamepadButtons(id ebiten.GamepadID, buttons []ebi
theInputState.m.RLock() theInputState.m.RLock()
defer theInputState.m.RUnlock() defer theInputState.m.RUnlock()
if _, ok := theInputState.gamepadButtonDurations[id]; !ok { state, ok := theInputState.gamepadStates[id]
if !ok {
return buttons return buttons
} }
if _, ok := theInputState.prevGamepadButtonDurations[id]; !ok { prevState, ok := theInputState.prevGamepadStates[id]
if !ok {
return buttons return buttons
} }
for b := ebiten.StandardGamepadButton(0); b <= ebiten.StandardGamepadButtonMax; b++ { for b := range state.standardButtonDurations {
if theInputState.standardGamepadButtonDurations[id][b] == 0 { if state.standardButtonDurations[b] != 0 {
continue continue
} }
if prevState.standardButtonDurations[b] == 0 {
if theInputState.prevStandardGamepadButtonDurations[id][b] > 0 {
continue continue
} }
buttons = append(buttons, ebiten.StandardGamepadButton(b))
buttons = append(buttons, b)
} }
return buttons return buttons
@ -620,15 +584,16 @@ func IsStandardGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.Stan
theInputState.m.RLock() theInputState.m.RLock()
defer theInputState.m.RUnlock() defer theInputState.m.RUnlock()
var prev int state, ok := theInputState.gamepadStates[id]
if _, ok := theInputState.prevStandardGamepadButtonDurations[id]; ok { if !ok {
prev = theInputState.prevStandardGamepadButtonDurations[id][button] return false
} }
var current int prevState, ok := theInputState.prevGamepadStates[id]
if _, ok := theInputState.standardGamepadButtonDurations[id]; ok { if !ok {
current = theInputState.standardGamepadButtonDurations[id][button] return false
} }
return current == 0 && prev > 0
return state.standardButtonDurations[button] == 0 && prevState.standardButtonDurations[button] > 0
} }
// StandardGamepadButtonPressDuration returns how long the standard gamepad button of the gamepad id is pressed in ticks (Update). // StandardGamepadButtonPressDuration returns how long the standard gamepad button of the gamepad id is pressed in ticks (Update).
@ -640,12 +605,14 @@ func StandardGamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.Stand
theInputState.m.RLock() theInputState.m.RLock()
defer theInputState.m.RUnlock() defer theInputState.m.RUnlock()
if _, ok := theInputState.standardGamepadButtonDurations[id]; ok { state, ok := theInputState.gamepadStates[id]
return theInputState.standardGamepadButtonDurations[id][button] if !ok {
}
return 0 return 0
} }
return state.standardButtonDurations[button]
}
// AppendJustPressedTouchIDs append touch IDs that are created just in the current tick to touchIDs, // AppendJustPressedTouchIDs append touch IDs that are created just in the current tick to touchIDs,
// and returns the extended buffer. // and returns the extended buffer.
// Giving a slice that already has enough capacity works efficiently. // Giving a slice that already has enough capacity works efficiently.
@ -664,11 +631,7 @@ func AppendJustPressedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
} }
} }
s := touchIDs[origLen:] slices.Sort(touchIDs[origLen:])
sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
})
return touchIDs return touchIDs
} }
@ -699,11 +662,7 @@ func AppendJustReleasedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
} }
} }
s := touchIDs[origLen:] slices.Sort(touchIDs[origLen:])
sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
})
return touchIDs return touchIDs
} }
@ -727,9 +686,8 @@ func IsTouchJustReleased(id ebiten.TouchID) bool {
// TouchPressDuration is concurrent safe. // TouchPressDuration is concurrent safe.
func TouchPressDuration(id ebiten.TouchID) int { func TouchPressDuration(id ebiten.TouchID) int {
theInputState.m.RLock() theInputState.m.RLock()
s := theInputState.touchDurations[id] defer theInputState.m.RUnlock()
theInputState.m.RUnlock() return theInputState.touchDurations[id]
return s
} }
// TouchPositionInPreviousTick returns the position in the previous tick. // TouchPositionInPreviousTick returns the position in the previous tick.

View File

@ -421,6 +421,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice
} }
} }
var imgs [graphics.ShaderSrcImageCount]*restorable.Image
for i, src := range srcs { for i, src := range srcs {
if src == nil { if src == nil {
continue continue
@ -429,33 +430,18 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice
// A source region can be deliberately empty when this is not needed in order to avoid unexpected // A source region can be deliberately empty when this is not needed in order to avoid unexpected
// performance issue (#1293). // performance issue (#1293).
// TODO: This should no longer be needed but is kept just in case. Remove this later. // TODO: This should no longer be needed but is kept just in case. Remove this later.
if srcRegions[i].Empty() { if !srcRegions[i].Empty() {
continue
}
r := src.regionWithPadding() r := src.regionWithPadding()
srcRegions[i] = srcRegions[i].Add(r.Min) srcRegions[i] = srcRegions[i].Add(r.Min)
} }
var imgs [graphics.ShaderSrcImageCount]*restorable.Image
for i, src := range srcs {
if src == nil {
continue
}
imgs[i] = src.backend.restorable imgs[i] = src.backend.restorable
}
i.backend.restorable.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader(), uniforms, fillRule, hint)
for _, src := range srcs {
if src == nil {
continue
}
if !src.isOnSourceBackend() && src.canBePutOnAtlas() { if !src.isOnSourceBackend() && src.canBePutOnAtlas() {
// src might already registered, but assigning it again is not harmful. // src might already registered, but assigning it again is not harmful.
imagesToPutOnSourceBackend.add(src) imagesToPutOnSourceBackend.add(src)
} }
} }
i.backend.restorable.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader(), uniforms, fillRule, hint)
} }
// WritePixels replaces the pixels on the image. // WritePixels replaces the pixels on the image.

View File

@ -14,7 +14,7 @@
// Code generated by gen.go using 'go generate'. DO NOT EDIT. // Code generated by gen.go using 'go generate'. DO NOT EDIT.
//go:build darwing && !ios //go:build darwin && !ios
package gamepaddb package gamepaddb

View File

@ -113,7 +113,7 @@ func run() error {
}, },
"Mac OS X": { "Mac OS X": {
filenameSuffix: "macos", filenameSuffix: "macos",
buildConstraints: "//go:build darwing && !ios", buildConstraints: "//go:build darwin && !ios",
}, },
"Linux": { "Linux": {
filenameSuffix: "linbsd", filenameSuffix: "linbsd",