From afbd1aebf15355f4e0af30974644e33025f1ca33 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 1 Jan 2023 01:16:52 +0900 Subject: [PATCH] internal/ui: use -overlay to provide the implementation for Nintendo Switch Updates #2372 --- internal/gamepad/gamepad_nintendosdk.cpp | 4 +-- internal/nintendosdk/nintendosdk.go | 28 -------------------- internal/ui/input_nintendosdk.cpp | 25 ++++++++++++++++++ internal/ui/input_nintendosdk.go | 23 ++++++++++++----- internal/ui/input_nintendosdk.h | 33 ++++++++++++++++++++++++ internal/ui/ui_nintendosdk.go | 5 +++- 6 files changed, 80 insertions(+), 38 deletions(-) create mode 100644 internal/ui/input_nintendosdk.cpp create mode 100644 internal/ui/input_nintendosdk.h diff --git a/internal/gamepad/gamepad_nintendosdk.cpp b/internal/gamepad/gamepad_nintendosdk.cpp index 0025fce07..b6ce69ea0 100644 --- a/internal/gamepad/gamepad_nintendosdk.cpp +++ b/internal/gamepad/gamepad_nintendosdk.cpp @@ -20,9 +20,7 @@ extern "C" void ebitengine_UpdateGamepads() {} -extern "C" int ebitengine_GetGamepadCount() { - return 0; -} +extern "C" int ebitengine_GetGamepadCount() { return 0; } extern "C" void ebitengine_GetGamepads(struct Gamepad *gamepads) {} diff --git a/internal/nintendosdk/nintendosdk.go b/internal/nintendosdk/nintendosdk.go index b4696e5b4..2f0000c84 100644 --- a/internal/nintendosdk/nintendosdk.go +++ b/internal/nintendosdk/nintendosdk.go @@ -32,10 +32,6 @@ package nintendosdk // void EbitenGetScreenSize(int* width, int* height); // void EbitenBeginFrame(); // void EbitenEndFrame(); -// -// // Input -// int EbitenGetTouchNum(); -// void EbitenGetTouches(struct Touch* touches); import "C" type Touch struct { @@ -61,27 +57,3 @@ func BeginFrame() { func EndFrame() { C.EbitenEndFrame() } - -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{ - ID: int(t.id), - X: int(t.x), - Y: int(t.y), - }) - } - return touches -} diff --git a/internal/ui/input_nintendosdk.cpp b/internal/ui/input_nintendosdk.cpp new file mode 100644 index 000000000..7fbe5ea2a --- /dev/null +++ b/internal/ui/input_nintendosdk.cpp @@ -0,0 +1,25 @@ +// Copyright 2023 The Ebitengine 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. + +//go:build nintendosdk + +// The actual implementaiton will be provided by -overlay. + +#include "input_nintendosdk.h" + +extern "C" void ebitengine_UpdateTouches() {} + +extern "C" int ebitengine_GetTouchCount() { return 0; } + +extern "C" void ebitengine_GetTouches(struct Touch *touches) {} diff --git a/internal/ui/input_nintendosdk.go b/internal/ui/input_nintendosdk.go index 2cc18894e..603211365 100644 --- a/internal/ui/input_nintendosdk.go +++ b/internal/ui/input_nintendosdk.go @@ -16,22 +16,33 @@ package ui -import ( - "github.com/hajimehoshi/ebiten/v2/internal/nintendosdk" -) +// #cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all +// #cgo darwin LDFLAGS: -Wl,-undefined,dynamic_lookup +// +// #include "input_nintendosdk.h" +import "C" func (u *userInterfaceImpl) updateInputState() { + C.ebitengine_UpdateTouches() + u.nativeTouches = u.nativeTouches[:0] - u.nativeTouches = nintendosdk.AppendTouches(u.nativeTouches) + if n := int(C.ebitengine_GetTouchCount()); n > 0 { + if cap(u.nativeTouches) < n { + u.nativeTouches = make([]C.struct_Touch, n) + } else { + u.nativeTouches = u.nativeTouches[:n] + } + C.ebitengine_GetTouches(&u.nativeTouches[0]) + } for i := range u.inputState.Touches { u.inputState.Touches[i].Valid = false } for i, t := range u.nativeTouches { - x, y := u.context.clientPositionToLogicalPosition(float64(t.X), float64(t.Y), deviceScaleFactor) + x, y := u.context.clientPositionToLogicalPosition(float64(t.x), float64(t.y), deviceScaleFactor) u.inputState.Touches[i] = Touch{ Valid: true, - ID: TouchID(t.ID), + ID: TouchID(t.id), X: int(x), Y: int(y), } diff --git a/internal/ui/input_nintendosdk.h b/internal/ui/input_nintendosdk.h new file mode 100644 index 000000000..72c408e1f --- /dev/null +++ b/internal/ui/input_nintendosdk.h @@ -0,0 +1,33 @@ +// Copyright 2023 The Ebitengine 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. + +//go:build nintendosdk + +struct Touch { + int id; + int x; + int y; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +void ebitengine_UpdateTouches(); +int ebitengine_GetTouchCount(); +void ebitengine_GetTouches(struct Touch* touches); + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/internal/ui/ui_nintendosdk.go b/internal/ui/ui_nintendosdk.go index f2815cc4e..a88a0cfed 100644 --- a/internal/ui/ui_nintendosdk.go +++ b/internal/ui/ui_nintendosdk.go @@ -16,6 +16,9 @@ package ui +// #include "input_nintendosdk.h" +import "C" + import ( "runtime" @@ -55,7 +58,7 @@ type userInterfaceImpl struct { context *context inputState InputState - nativeTouches []nintendosdk.Touch + nativeTouches []C.struct_Touch } func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {