examples/gamepad: Use RunGame

Updates #1111
This commit is contained in:
Hajime Hoshi 2020-04-20 01:36:09 +09:00
parent ff271493e2
commit 29ef737413

View File

@ -32,37 +32,41 @@ const (
screenHeight = 480 screenHeight = 480
) )
var ( type Game struct {
gamepadIDs = map[int]struct{}{} gamepadIDs map[int]struct{}
) axes map[int][]string
pressedButtons map[int][]string
}
func (g *Game) Update(screen *ebiten.Image) error {
if g.gamepadIDs == nil {
g.gamepadIDs = map[int]struct{}{}
}
func update(screen *ebiten.Image) error {
// Log the gamepad connection events. // Log the gamepad connection events.
for _, id := range inpututil.JustConnectedGamepadIDs() { for _, id := range inpututil.JustConnectedGamepadIDs() {
log.Printf("gamepad connected: id: %d", id) log.Printf("gamepad connected: id: %d", id)
gamepadIDs[id] = struct{}{} g.gamepadIDs[id] = struct{}{}
} }
for id := range gamepadIDs { for id := range g.gamepadIDs {
if inpututil.IsGamepadJustDisconnected(id) { if inpututil.IsGamepadJustDisconnected(id) {
log.Printf("gamepad disconnected: id: %d", id) log.Printf("gamepad disconnected: id: %d", id)
delete(gamepadIDs, id) delete(g.gamepadIDs, id)
} }
} }
ids := ebiten.GamepadIDs() g.axes = map[int][]string{}
axes := map[int][]string{} g.pressedButtons = map[int][]string{}
pressedButtons := map[int][]string{} for id := range g.gamepadIDs {
for _, id := range ids {
maxAxis := ebiten.GamepadAxisNum(id) maxAxis := ebiten.GamepadAxisNum(id)
for a := 0; a < maxAxis; a++ { for a := 0; a < maxAxis; a++ {
v := ebiten.GamepadAxis(id, a) v := ebiten.GamepadAxis(id, a)
axes[id] = append(axes[id], fmt.Sprintf("%d:%0.2f", a, v)) g.axes[id] = append(g.axes[id], fmt.Sprintf("%d:%0.2f", a, v))
} }
maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(id)) maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(id))
for b := ebiten.GamepadButton(id); b < maxButton; b++ { for b := ebiten.GamepadButton(id); b < maxButton; b++ {
if ebiten.IsGamepadButtonPressed(id, b) { if ebiten.IsGamepadButtonPressed(id, b) {
pressedButtons[id] = append(pressedButtons[id], strconv.Itoa(int(b))) g.pressedButtons[id] = append(g.pressedButtons[id], strconv.Itoa(int(b)))
} }
// Log button events. // Log button events.
@ -74,29 +78,33 @@ func update(screen *ebiten.Image) error {
} }
} }
} }
return nil
}
if ebiten.IsDrawingSkipped() { func (g *Game) Draw(screen *ebiten.Image) {
return nil
}
// Draw the current gamepad status. // Draw the current gamepad status.
str := "" str := ""
if len(ids) > 0 { if len(g.gamepadIDs) > 0 {
for _, id := range ids { for id := range g.gamepadIDs {
str += fmt.Sprintf("Gamepad (ID: %d, SDL ID: %s):\n", id, ebiten.GamepadSDLID(id)) str += fmt.Sprintf("Gamepad (ID: %d, SDL ID: %s):\n", id, ebiten.GamepadSDLID(id))
str += fmt.Sprintf(" Axes: %s\n", strings.Join(axes[id], ", ")) str += fmt.Sprintf(" Axes: %s\n", strings.Join(g.axes[id], ", "))
str += fmt.Sprintf(" Buttons: %s\n", strings.Join(pressedButtons[id], ", ")) str += fmt.Sprintf(" Buttons: %s\n", strings.Join(g.pressedButtons[id], ", "))
str += "\n" str += "\n"
} }
} else { } else {
str = "Please connect your gamepad." str = "Please connect your gamepad."
} }
ebitenutil.DebugPrint(screen, str) ebitenutil.DebugPrint(screen, str)
return nil }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Gamepad (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Gamepad (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }