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 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() i.ui.m.Lock()
defer i.ui.m.Unlock() defer i.ui.m.Unlock()

View File

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

View File

@ -31,23 +31,28 @@ var (
keys = map[driver.Key]struct{}{} keys = map[driver.Key]struct{}{}
runes []rune runes []rune
touches = map[driver.TouchID]position{} 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() { func updateInput() {
ts := make([]*mobile.Touch, 0, len(touches)) touchSlice = touchSlice[:0]
for id, position := range touches { for id, position := range touches {
ts = append(ts, &mobile.Touch{ touchSlice = append(touchSlice, mobile.Touch{
ID: id, ID: id,
X: position.x, X: position.x,
Y: position.y, Y: position.y,
}) })
} }
gs := make([]mobile.Gamepad, 0, len(gamepads)) gamepadSlice = gamepadSlice[:0]
for _, g := range gamepads { 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) sdlid[15] = byte(axisMask >> 8)
id := gamepadIDFromDeviceID(deviceID) id := gamepadIDFromDeviceID(deviceID)
gamepads[id] = &mobile.Gamepad{ gamepads[id] = mobile.Gamepad{
ID: id, ID: id,
SDLID: hex.EncodeToString(sdlid[:]), SDLID: hex.EncodeToString(sdlid[:]),
Name: name, Name: name,