mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
Add Gamepad API
This commit is contained in:
parent
9d5ab644a4
commit
a6b7eab71b
46
example/gamepad/main.go
Normal file
46
example/gamepad/main.go
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright 2015 Hajime Hoshi
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
screenWidth = 320
|
||||
screenHeight = 240
|
||||
)
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
pressed := []string{}
|
||||
for b := ebiten.GamepadButton(0); b < 16; b++ {
|
||||
if ebiten.IsGamepadButtonPressed(0, b) {
|
||||
pressed = append(pressed, strconv.Itoa(int(b)))
|
||||
}
|
||||
}
|
||||
str := "Pressed Keys: " + strings.Join(pressed, ", ")
|
||||
ebitenutil.DebugPrint(screen, str)
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Gamepad (Ebiten Demo)"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
@ -36,6 +36,14 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool {
|
||||
return ui.IsMouseButtonPressed(ui.MouseButton(mouseButton))
|
||||
}
|
||||
|
||||
func GamepadAxis(j int, dir int) float64 {
|
||||
return ui.GamepadAxis(j, dir)
|
||||
}
|
||||
|
||||
func IsGamepadButtonPressed(j int, button GamepadButton) bool {
|
||||
return ui.IsGamepadButtonPressed(j, ui.GamepadButton(button))
|
||||
}
|
||||
|
||||
// NewImage returns an empty image.
|
||||
func NewImage(width, height int, filter Filter) (*Image, error) {
|
||||
var innerImage *innerImage
|
||||
|
42
gamepad.go
Normal file
42
gamepad.go
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright 2015 Hajime Hoshi
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package ebiten
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
)
|
||||
|
||||
// A GamepadButton represents a gamepad button.
|
||||
type GamepadButton int
|
||||
|
||||
// GamepadButtons
|
||||
const (
|
||||
GamepadButton0 = GamepadButton(ui.GamepadButton0)
|
||||
GamepadButton1 = GamepadButton(ui.GamepadButton1)
|
||||
GamepadButton2 = GamepadButton(ui.GamepadButton2)
|
||||
GamepadButton3 = GamepadButton(ui.GamepadButton3)
|
||||
GamepadButton4 = GamepadButton(ui.GamepadButton4)
|
||||
GamepadButton5 = GamepadButton(ui.GamepadButton5)
|
||||
GamepadButton6 = GamepadButton(ui.GamepadButton6)
|
||||
GamepadButton7 = GamepadButton(ui.GamepadButton7)
|
||||
GamepadButton8 = GamepadButton(ui.GamepadButton8)
|
||||
GamepadButton9 = GamepadButton(ui.GamepadButton9)
|
||||
GamepadButton10 = GamepadButton(ui.GamepadButton10)
|
||||
GamepadButton11 = GamepadButton(ui.GamepadButton11)
|
||||
GamepadButton12 = GamepadButton(ui.GamepadButton12)
|
||||
GamepadButton13 = GamepadButton(ui.GamepadButton13)
|
||||
GamepadButton14 = GamepadButton(ui.GamepadButton14)
|
||||
GamepadButton15 = GamepadButton(ui.GamepadButton15)
|
||||
)
|
36
internal/ui/gamepadbutton.go
Normal file
36
internal/ui/gamepadbutton.go
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright 2015 Hajime Hoshi
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package ui
|
||||
|
||||
type GamepadButton int
|
||||
|
||||
const (
|
||||
GamepadButton0 GamepadButton = iota
|
||||
GamepadButton1
|
||||
GamepadButton2
|
||||
GamepadButton3
|
||||
GamepadButton4
|
||||
GamepadButton5
|
||||
GamepadButton6
|
||||
GamepadButton7
|
||||
GamepadButton8
|
||||
GamepadButton9
|
||||
GamepadButton10
|
||||
GamepadButton11
|
||||
GamepadButton12
|
||||
GamepadButton13
|
||||
GamepadButton14
|
||||
GamepadButton15
|
||||
)
|
@ -14,21 +14,43 @@
|
||||
|
||||
package ui
|
||||
|
||||
func IsKeyPressed(key Key) bool {
|
||||
return currentInput.keyPressed[key]
|
||||
}
|
||||
|
||||
func CursorPosition() (x, y int) {
|
||||
return currentInput.cursorX, currentInput.cursorY
|
||||
}
|
||||
|
||||
func IsMouseButtonPressed(button MouseButton) bool {
|
||||
return currentInput.mouseButtonPressed[button]
|
||||
}
|
||||
|
||||
func GamepadAxis(j int, dir int) float64 {
|
||||
if len(currentInput.gamepads) <= j {
|
||||
return 0
|
||||
}
|
||||
return currentInput.gamepads[j].axes[dir]
|
||||
}
|
||||
|
||||
func IsGamepadButtonPressed(j int, button GamepadButton) bool {
|
||||
if len(currentInput.gamepads) <= j {
|
||||
return false
|
||||
}
|
||||
return currentInput.gamepads[j].buttonPressed[button]
|
||||
}
|
||||
|
||||
var currentInput input
|
||||
|
||||
type input struct {
|
||||
keyPressed [256]bool
|
||||
mouseButtonPressed [256]bool
|
||||
cursorX int
|
||||
cursorY int
|
||||
gamepads [16]gamePad
|
||||
}
|
||||
|
||||
func (i *input) isKeyPressed(key Key) bool {
|
||||
return i.keyPressed[key]
|
||||
}
|
||||
|
||||
func (i *input) isMouseButtonPressed(button MouseButton) bool {
|
||||
return i.mouseButtonPressed[button]
|
||||
}
|
||||
|
||||
func (i *input) cursorPosition() (x, y int) {
|
||||
return i.cursorX, i.cursorY
|
||||
type gamePad struct {
|
||||
axes [2]float64
|
||||
buttonPressed [256]bool
|
||||
}
|
||||
|
@ -21,25 +21,13 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
func IsKeyPressed(key Key) bool {
|
||||
return current.input.isKeyPressed(key)
|
||||
}
|
||||
|
||||
func IsMouseButtonPressed(button MouseButton) bool {
|
||||
return current.input.isMouseButtonPressed(button)
|
||||
}
|
||||
|
||||
func CursorPosition() (x, y int) {
|
||||
return current.input.cursorPosition()
|
||||
}
|
||||
|
||||
var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{
|
||||
glfw.MouseButtonLeft: MouseButtonLeft,
|
||||
glfw.MouseButtonRight: MouseButtonRight,
|
||||
glfw.MouseButtonMiddle: MouseButtonMiddle,
|
||||
}
|
||||
|
||||
func (i *input) update(window *glfw.Window, scale int) {
|
||||
func (i *input) update(window *glfw.Window, scale int) error {
|
||||
for g, e := range glfwKeyCodeToKey {
|
||||
i.keyPressed[e] = window.GetKey(g) == glfw.Press
|
||||
}
|
||||
@ -49,4 +37,32 @@ func (i *input) update(window *glfw.Window, scale int) {
|
||||
x, y := window.GetCursorPosition()
|
||||
i.cursorX = int(math.Floor(x)) / scale
|
||||
i.cursorY = int(math.Floor(y)) / scale
|
||||
for j := glfw.Joystick(0); j < glfw.Joystick(len(i.gamepads)); j++ {
|
||||
if !glfw.JoystickPresent(j) {
|
||||
continue
|
||||
}
|
||||
axes32, err := glfw.GetJoystickAxes(j)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for a := 0; a < len(i.gamepads[j].axes); a++ {
|
||||
if len(axes32) <= a {
|
||||
i.gamepads[j].axes[a] = 0
|
||||
continue
|
||||
}
|
||||
i.gamepads[j].axes[a] = float64(axes32[a])
|
||||
}
|
||||
buttons, err := glfw.GetJoystickButtons(j)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for b := 0; b < len(i.gamepads[j].buttonPressed); b++ {
|
||||
if len(buttons) <= b {
|
||||
i.gamepads[j].buttonPressed[b] = false
|
||||
continue
|
||||
}
|
||||
i.gamepads[j].buttonPressed[b] = glfw.Action(buttons[b]) == glfw.Press
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -16,20 +16,6 @@
|
||||
|
||||
package ui
|
||||
|
||||
var currentInput input
|
||||
|
||||
func IsKeyPressed(key Key) bool {
|
||||
return currentInput.isKeyPressed(key)
|
||||
}
|
||||
|
||||
func IsMouseButtonPressed(button MouseButton) bool {
|
||||
return currentInput.isMouseButtonPressed(button)
|
||||
}
|
||||
|
||||
func CursorPosition() (x, y int) {
|
||||
return currentInput.cursorPosition()
|
||||
}
|
||||
|
||||
func (i *input) keyDown(key int) {
|
||||
k, ok := keyCodeToKey[key]
|
||||
if !ok {
|
||||
|
@ -35,8 +35,8 @@ func Use(f func(*opengl.Context)) {
|
||||
<-ch
|
||||
}
|
||||
|
||||
func DoEvents() {
|
||||
current.doEvents()
|
||||
func DoEvents() error {
|
||||
return current.doEvents()
|
||||
}
|
||||
|
||||
func Terminate() {
|
||||
@ -88,7 +88,6 @@ type ui struct {
|
||||
window *glfw.Window
|
||||
scale int
|
||||
glContext *opengl.Context
|
||||
input input
|
||||
funcs chan func()
|
||||
}
|
||||
|
||||
@ -137,17 +136,22 @@ func Start(width, height, scale int, title string) (actualScale int, err error)
|
||||
return actualScale, nil
|
||||
}
|
||||
|
||||
func (u *ui) pollEvents() {
|
||||
func (u *ui) pollEvents() error {
|
||||
glfw.PollEvents()
|
||||
u.input.update(u.window, u.scale)
|
||||
return currentInput.update(u.window, u.scale)
|
||||
}
|
||||
|
||||
func (u *ui) doEvents() {
|
||||
u.pollEvents()
|
||||
func (u *ui) doEvents() error {
|
||||
if err := u.pollEvents(); err != nil {
|
||||
return err
|
||||
}
|
||||
for current.window.GetAttribute(glfw.Focused) == 0 {
|
||||
time.Sleep(time.Second / 60)
|
||||
u.pollEvents()
|
||||
if err := u.pollEvents(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *ui) terminate() {
|
||||
|
@ -42,11 +42,12 @@ func vsync() {
|
||||
<-ch
|
||||
}
|
||||
|
||||
func DoEvents() {
|
||||
func DoEvents() error {
|
||||
vsync()
|
||||
for !shown() {
|
||||
vsync()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Terminate() {
|
||||
|
Loading…
Reference in New Issue
Block a user