mobile/ebitenmobileview: Avoid creating slices every frame

This commit is contained in:
Hajime Hoshi 2021-10-16 18:16:38 +09:00
parent bbe793af5b
commit ad3115b347
4 changed files with 17 additions and 12 deletions

View File

@ -199,7 +199,7 @@ func (i *Input) IsMouseButtonPressed(key driver.MouseButton) bool {
return false
}
func (i *Input) update(keys map[driver.Key]struct{}, runes []rune, touches []*Touch, gamepads []Gamepad) {
func (i *Input) update(keys map[driver.Key]struct{}, runes []rune, touches []Touch, gamepads []Gamepad) {
i.ui.m.Lock()
defer i.ui.m.Unlock()

View File

@ -129,7 +129,7 @@ func (u *UserInterface) appMain(a app.App) {
var glctx gl.Context
var sizeInited bool
touches := map[touch.Sequence]*Touch{}
touches := map[touch.Sequence]Touch{}
keys := map[driver.Key]struct{}{}
for e := range a.Events() {
@ -184,7 +184,7 @@ func (u *UserInterface) appMain(a app.App) {
s := deviceScale()
x, y := float64(e.X)/s, float64(e.Y)/s
// TODO: Is it ok to cast from int64 to int here?
touches[e.Sequence] = &Touch{
touches[e.Sequence] = Touch{
ID: driver.TouchID(e.Sequence),
X: int(x),
Y: int(y),
@ -214,7 +214,7 @@ func (u *UserInterface) appMain(a app.App) {
}
if updateInput {
ts := []*Touch{}
var ts []Touch
for _, t := range touches {
ts = append(ts, t)
}
@ -468,7 +468,7 @@ type Gamepad struct {
AxisNum int
}
func (u *UserInterface) UpdateInput(keys map[driver.Key]struct{}, runes []rune, touches []*Touch, gamepads []Gamepad) {
func (u *UserInterface) UpdateInput(keys map[driver.Key]struct{}, runes []rune, touches []Touch, gamepads []Gamepad) {
u.input.update(keys, runes, touches, gamepads)
if u.fpsMode == driver.FPSModeVsyncOffMinimum {
u.renderRequester.RequestRenderIfNeeded()

View File

@ -31,23 +31,28 @@ var (
keys = map[driver.Key]struct{}{}
runes []rune
touches = map[driver.TouchID]position{}
gamepads = map[driver.GamepadID]*mobile.Gamepad{}
gamepads = map[driver.GamepadID]mobile.Gamepad{}
)
var (
touchSlice []mobile.Touch
gamepadSlice []mobile.Gamepad
)
func updateInput() {
ts := make([]*mobile.Touch, 0, len(touches))
touchSlice = touchSlice[:0]
for id, position := range touches {
ts = append(ts, &mobile.Touch{
touchSlice = append(touchSlice, mobile.Touch{
ID: id,
X: position.x,
Y: position.y,
})
}
gs := make([]mobile.Gamepad, 0, len(gamepads))
gamepadSlice = gamepadSlice[:0]
for _, g := range gamepads {
gs = append(gs, *g)
gamepadSlice = append(gamepadSlice, g)
}
mobile.Get().UpdateInput(keys, runes, ts, gs)
mobile.Get().UpdateInput(keys, runes, touchSlice, gamepadSlice)
}

View File

@ -302,7 +302,7 @@ func OnGamepadAdded(deviceID int, name string, buttonNum int, axisNum int, descr
sdlid[15] = byte(axisMask >> 8)
id := gamepadIDFromDeviceID(deviceID)
gamepads[id] = &mobile.Gamepad{
gamepads[id] = mobile.Gamepad{
ID: id,
SDLID: hex.EncodeToString(sdlid[:]),
Name: name,