inpututil: Remove map/slice allocations in update

This commit is contained in:
Hajime Hoshi 2021-01-31 04:22:50 +09:00
parent da71840106
commit b859db02e5

View File

@ -36,6 +36,7 @@ type inputState struct {
gamepadButtonDurations map[ebiten.GamepadID][]int
prevGamepadButtonDurations map[ebiten.GamepadID][]int
touchIDs map[ebiten.TouchID]struct{}
touchDurations map[ebiten.TouchID]int
prevTouchDurations map[ebiten.TouchID]int
@ -55,6 +56,7 @@ var theInputState = &inputState{
gamepadButtonDurations: map[ebiten.GamepadID][]int{},
prevGamepadButtonDurations: map[ebiten.GamepadID][]int{},
touchIDs: map[ebiten.TouchID]struct{}{},
touchDurations: map[ebiten.TouchID]int{},
prevTouchDurations: map[ebiten.TouchID]int{},
}
@ -136,28 +138,28 @@ func (i *inputState) update() {
}
// Touches
ids := map[ebiten.TouchID]struct{}{}
// Copy the touch durations.
i.prevTouchDurations = map[ebiten.TouchID]int{}
for id := range i.prevTouchDurations {
delete(i.prevTouchDurations, id)
}
for id := range i.touchDurations {
i.prevTouchDurations[id] = i.touchDurations[id]
}
for id := range i.touchIDs {
delete(i.touchIDs, id)
}
for _, id := range ebiten.TouchIDs() {
ids[id] = struct{}{}
i.touchIDs[id] = struct{}{}
i.touchDurations[id]++
}
touchIDsToDelete := []ebiten.TouchID{}
for id := range i.touchDurations {
if _, ok := ids[id]; !ok {
touchIDsToDelete = append(touchIDsToDelete, id)
}
}
for _, id := range touchIDsToDelete {
if _, ok := i.touchIDs[id]; !ok {
delete(i.touchDurations, id)
}
}
}
// IsKeyJustPressed returns a boolean value indicating
// whether the given key is pressed just in the current frame.