2021-12-17 07:03:23 +01:00
|
|
|
// Copyright 2021 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2022-07-09 08:19:47 +02:00
|
|
|
//go:build ebitenginecbackend || ebitencbackend
|
|
|
|
// +build ebitenginecbackend ebitencbackend
|
2021-12-17 07:03:23 +01:00
|
|
|
|
|
|
|
package cbackend
|
|
|
|
|
|
|
|
// #cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all
|
|
|
|
// #cgo darwin LDFLAGS: -Wl,-undefined,dynamic_lookup
|
|
|
|
//
|
|
|
|
// #include <stdint.h>
|
|
|
|
//
|
|
|
|
// struct Gamepad {
|
|
|
|
// int id;
|
|
|
|
// char standard;
|
|
|
|
// int button_num;
|
|
|
|
// int axis_num;
|
|
|
|
// char button_pressed[32];
|
|
|
|
// float button_values[32];
|
|
|
|
// float axis_values[16];
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// struct Touch {
|
|
|
|
// int id;
|
|
|
|
// int x;
|
|
|
|
// int y;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// // UI
|
|
|
|
// void EbitenInitializeGame();
|
|
|
|
// void EbitenGetScreenSize(int* width, int* height);
|
|
|
|
// void EbitenBeginFrame();
|
|
|
|
// void EbitenEndFrame();
|
|
|
|
//
|
|
|
|
// // Input
|
|
|
|
// int EbitenGetGamepadNum();
|
|
|
|
// void EbitenGetGamepads(struct Gamepad* gamepads);
|
|
|
|
// int EbitenGetTouchNum();
|
|
|
|
// void EbitenGetTouches(struct Touch* touches);
|
2022-01-25 19:32:49 +01:00
|
|
|
// void EbitenVibrateGamepad(int id, double durationInSeconds, double strongMagnitude, double weakMagnitude);
|
2021-12-17 07:03:23 +01:00
|
|
|
//
|
|
|
|
// // Audio
|
2021-12-22 19:46:26 +01:00
|
|
|
// typedef void (*OnReadCallback)(float* buf, size_t length);
|
|
|
|
// void EbitenOpenAudio(int sample_rate, int channel_num, OnReadCallback on_read_callback);
|
2021-12-17 07:03:23 +01:00
|
|
|
// void EbitenCloseAudio();
|
|
|
|
//
|
2021-12-22 19:46:26 +01:00
|
|
|
// void EbitenAudioOnReadCallback(float* buf, size_t length);
|
|
|
|
// static void EbitenOpenAudioProxy(int sample_rate, int channel_num) {
|
|
|
|
// EbitenOpenAudio(sample_rate, channel_num, EbitenAudioOnReadCallback);
|
2021-12-17 07:03:23 +01:00
|
|
|
// }
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2021-12-22 19:46:26 +01:00
|
|
|
"reflect"
|
2022-01-25 19:32:49 +01:00
|
|
|
"time"
|
2021-12-17 07:03:23 +01:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Gamepad struct {
|
2022-02-05 10:30:17 +01:00
|
|
|
ID int
|
2021-12-17 07:03:23 +01:00
|
|
|
Standard bool
|
2022-02-05 10:30:17 +01:00
|
|
|
ButtonCount int
|
|
|
|
AxisCount int
|
2021-12-17 07:03:23 +01:00
|
|
|
ButtonPressed [32]bool
|
|
|
|
ButtonValues [32]float64
|
|
|
|
AxisValues [16]float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type Touch struct {
|
2022-02-06 11:07:17 +01:00
|
|
|
ID int
|
2021-12-17 07:03:23 +01:00
|
|
|
X int
|
|
|
|
Y int
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitializeGame() {
|
|
|
|
C.EbitenInitializeGame()
|
|
|
|
}
|
|
|
|
|
|
|
|
func ScreenSize() (int, int) {
|
|
|
|
var width, height C.int
|
|
|
|
C.EbitenGetScreenSize(&width, &height)
|
|
|
|
return int(width), int(height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BeginFrame() {
|
|
|
|
C.EbitenBeginFrame()
|
|
|
|
}
|
|
|
|
|
|
|
|
func EndFrame() {
|
|
|
|
C.EbitenEndFrame()
|
|
|
|
}
|
|
|
|
|
|
|
|
var cGamepads []C.struct_Gamepad
|
|
|
|
|
|
|
|
func AppendGamepads(gamepads []Gamepad) []Gamepad {
|
|
|
|
n := int(C.EbitenGetGamepadNum())
|
|
|
|
if cap(cGamepads) < n {
|
|
|
|
cGamepads = append(cGamepads, make([]C.struct_Gamepad, n)...)
|
|
|
|
} else {
|
|
|
|
cGamepads = cGamepads[:n]
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
C.EbitenGetGamepads(&cGamepads[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range cGamepads {
|
|
|
|
gamepad := Gamepad{
|
2022-02-05 10:30:17 +01:00
|
|
|
ID: int(g.id),
|
|
|
|
Standard: g.standard != 0,
|
|
|
|
ButtonCount: int(g.button_num),
|
|
|
|
AxisCount: int(g.axis_num),
|
2021-12-17 07:03:23 +01:00
|
|
|
}
|
2022-02-05 10:30:17 +01:00
|
|
|
for i := 0; i < gamepad.ButtonCount; i++ {
|
2021-12-17 07:03:23 +01:00
|
|
|
gamepad.ButtonPressed[i] = g.button_pressed[i] != 0
|
|
|
|
gamepad.ButtonValues[i] = float64(g.button_values[i])
|
|
|
|
}
|
2022-02-05 10:30:17 +01:00
|
|
|
for i := 0; i < gamepad.AxisCount; i++ {
|
2021-12-17 07:03:23 +01:00
|
|
|
gamepad.AxisValues[i] = float64(g.axis_values[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
gamepads = append(gamepads, gamepad)
|
|
|
|
}
|
|
|
|
return gamepads
|
|
|
|
}
|
|
|
|
|
|
|
|
var cTouches []C.struct_Touch
|
|
|
|
|
|
|
|
func AppendTouches(touches []Touch) []Touch {
|
|
|
|
n := int(C.EbitenGetTouchNum())
|
|
|
|
cTouches = cTouches[:0]
|
|
|
|
if cap(cTouches) < n {
|
|
|
|
cTouches = append(cTouches, make([]C.struct_Touch, n)...)
|
|
|
|
} else {
|
|
|
|
cTouches = cTouches[:n]
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
C.EbitenGetTouches(&cTouches[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range cTouches {
|
|
|
|
touches = append(touches, Touch{
|
2022-02-06 11:07:17 +01:00
|
|
|
ID: int(t.id),
|
2021-12-17 07:03:23 +01:00
|
|
|
X: int(t.x),
|
|
|
|
Y: int(t.y),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return touches
|
|
|
|
}
|
|
|
|
|
2022-02-05 10:30:17 +01:00
|
|
|
func VibrateGamepad(id int, duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
|
2022-01-25 19:32:49 +01:00
|
|
|
C.EbitenVibrateGamepad(C.int(id), C.double(float64(duration)/float64(time.Second)), C.double(strongMagnitude), C.double(weakMagnitude))
|
|
|
|
}
|
|
|
|
|
2021-12-22 19:46:26 +01:00
|
|
|
var onReadCallback func(buf []float32)
|
|
|
|
|
2022-07-12 19:08:38 +02:00
|
|
|
func OpenAudio(sampleRate, channelCount int, onRead func(buf []float32)) {
|
|
|
|
C.EbitenOpenAudioProxy(C.int(sampleRate), C.int(channelCount))
|
2021-12-22 19:46:26 +01:00
|
|
|
onReadCallback = onRead
|
2021-12-17 07:03:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func CloseAudio() {
|
|
|
|
C.EbitenCloseAudio()
|
|
|
|
}
|
|
|
|
|
2021-12-22 19:46:26 +01:00
|
|
|
//export EbitenAudioOnReadCallback
|
|
|
|
func EbitenAudioOnReadCallback(buf *C.float, length C.size_t) {
|
|
|
|
var s []float32
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
|
|
|
h.Data = uintptr(unsafe.Pointer(buf))
|
|
|
|
h.Len = int(length)
|
|
|
|
h.Cap = int(length)
|
|
|
|
onReadCallback(s)
|
2021-12-17 07:03:23 +01:00
|
|
|
}
|