Add Gamepad API

This commit is contained in:
Hajime Hoshi 2015-01-12 01:54:18 +09:00
parent 9d5ab644a4
commit a6b7eab71b
10 changed files with 210 additions and 47 deletions

46
example/gamepad/main.go Normal file
View 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)
}
}

View File

@ -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
View 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)
)

View 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
)

View File

@ -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
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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() {

View File

@ -42,11 +42,12 @@ func vsync() {
<-ch
}
func DoEvents() {
func DoEvents() error {
vsync()
for !shown() {
vsync()
}
return nil
}
func Terminate() {

4
run.go
View File

@ -54,7 +54,9 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
frames := 0
t := time.Now().UnixNano()
for {
ui.DoEvents()
if err := ui.DoEvents(); err != nil {
return err
}
if ui.IsClosed() {
return nil
}