mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
mobile/ebitenmobileview: Add EbitenViewController for iOS
This is still work in progress. Updates #863
This commit is contained in:
parent
ccacc4a0b8
commit
e6e0e61215
154
mobile/compat.go
Normal file
154
mobile/compat.go
Normal file
@ -0,0 +1,154 @@
|
||||
// Copyright 2016 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 mobile
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/mobile/ebitenmobileview"
|
||||
)
|
||||
|
||||
type game struct {
|
||||
update func(*ebiten.Image) error
|
||||
width int
|
||||
height int
|
||||
}
|
||||
|
||||
func (g *game) Update(screen *ebiten.Image) error {
|
||||
return g.update(screen)
|
||||
}
|
||||
|
||||
func (g *game) Layout(viewWidth, viewHeight int) (screenWidth, screenHeight int) {
|
||||
return g.width, g.height
|
||||
}
|
||||
|
||||
// Start starts the game and returns immediately.
|
||||
//
|
||||
// Different from ebiten.Run, this invokes only the game loop and not the main (UI) loop.
|
||||
//
|
||||
// The unit of width/height is device-independent pixel (dp on Android and point on iOS).
|
||||
//
|
||||
// Start is concurrent-safe.
|
||||
//
|
||||
// The argument title is ignored. This is for backward compatibility.
|
||||
//
|
||||
// Start always returns nil as of 1.5.0-alpha.
|
||||
func Start(f func(*ebiten.Image) error, width, height int, scale float64, title string) error {
|
||||
ebitenmobileview.SetGame(&game{
|
||||
update: f,
|
||||
width: width,
|
||||
height: height,
|
||||
})
|
||||
// As the view layout is already determined, ignore the layout calculation at ebitenmobileview.
|
||||
w := int(math.Ceil((float64(width) * scale)))
|
||||
h := int(math.Ceil((float64(height) * scale)))
|
||||
ebitenmobileview.Layout(w, h, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update updates and renders the game.
|
||||
// This should be called on every frame.
|
||||
//
|
||||
// If Update is called before Start is called, Update panics.
|
||||
//
|
||||
// On Android, this should be called at onDrawFrame of Renderer (used by GLSurfaceView).
|
||||
//
|
||||
// On iOS, this should be called at glkView:drawInRect: of GLKViewDelegate.
|
||||
//
|
||||
// Update is concurrent-safe.
|
||||
//
|
||||
// Update returns error when 1) OpenGL error happens, or 2) f in Start returns error samely as ebiten.Run.
|
||||
func Update() error {
|
||||
return ebitenmobileview.Update()
|
||||
}
|
||||
|
||||
// UpdateTouchesOnAndroid updates the touch state on Android.
|
||||
//
|
||||
// This should be called with onTouchEvent of GLSurfaceView like this:
|
||||
//
|
||||
// private double mDeviceScale = 0.0;
|
||||
//
|
||||
// // pxToDp converts an value in pixels to dp.
|
||||
// private double pxToDp(double x) {
|
||||
// if (mDeviceScale == 0.0) {
|
||||
// mDeviceScale = getResources().getDisplayMetrics().density;
|
||||
// }
|
||||
// return x / mDeviceScale;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public boolean onTouchEvent(MotionEvent e) {
|
||||
// for (int i = 0; i < e.getPointerCount(); i++) {
|
||||
// int id = e.getPointerId(i);
|
||||
// int x = (int)e.getX(i);
|
||||
// int y = (int)e.getY(i);
|
||||
// // Exported function for UpdateTouchesOnAndroid
|
||||
// YourGame.UpdateTouchesOnAndroid(e.getActionMasked(), id, (int)pxToDp(x), (int)pxToDp(y));
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// The coodinate x/y is in dp.
|
||||
//
|
||||
// UpdateTouchesOnAndroid can be called even before Start is called.
|
||||
//
|
||||
// UpdateTouchesOnAndroid is concurrent-safe.
|
||||
//
|
||||
// For more details, see https://github.com/hajimehoshi/ebiten/wiki/Android.
|
||||
func UpdateTouchesOnAndroid(action int, id int, x, y int) {
|
||||
ebitenmobileview.UpdateTouchesOnAndroid(action, id, x, y)
|
||||
}
|
||||
|
||||
// UpdateTouchesOnIOS updates the touch state on iOS.
|
||||
//
|
||||
// This should be called with touch handlers of UIViewController like this:
|
||||
//
|
||||
// - (GLKView*)glkView {
|
||||
// return (GLKView*)[self.view viewWithTag:100];
|
||||
// }
|
||||
// - (void)updateTouches:(NSSet*)touches {
|
||||
// for (UITouch* touch in touches) {
|
||||
// if (touch.view != [self glkView]) {
|
||||
// continue;
|
||||
// }
|
||||
// CGPoint location = [touch locationInView: [self glkView]];
|
||||
// // Exported function for UpdateTouchesOnIOS
|
||||
// YourGameUpdateTouchesOnIOS(touch.phase, (int64_t)touch, location.x, location.y);
|
||||
// }
|
||||
// }
|
||||
// - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
// [self updateTouches:touches];
|
||||
// }
|
||||
// - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
// [self updateTouches:touches];
|
||||
// }
|
||||
// - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
// [self updateTouches:touches];
|
||||
// }
|
||||
// - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
// [self updateTouches:touches];
|
||||
// }
|
||||
//
|
||||
// The coodinate x/y is in point.
|
||||
//
|
||||
// UpdateTouchesOnIOS can be called even before Start is called.
|
||||
//
|
||||
// UpdateTouchesOnIOS is concurrent-safe.
|
||||
//
|
||||
// For more details, see https://github.com/hajimehoshi/ebiten/wiki/iOS.
|
||||
func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) {
|
||||
ebitenmobileview.UpdateTouchesOnIOS(phase, ptr, x, y)
|
||||
}
|
113
mobile/ebitenmobileview/c.go
Normal file
113
mobile/ebitenmobileview/c.go
Normal file
@ -0,0 +1,113 @@
|
||||
// Copyright 2019 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.
|
||||
|
||||
package ebitenmobileview
|
||||
|
||||
// #cgo ios LDFLAGS: -framework UIKit -framework GLKit -framework QuartzCore -framework OpenGLES
|
||||
//
|
||||
// #include <stdint.h>
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"math"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
var theState state
|
||||
|
||||
// game is not exported since gomobile complains.
|
||||
// TODO: Report this error.
|
||||
type game interface {
|
||||
Update(*ebiten.Image) error
|
||||
Layout(viewWidth, viewHeight int) (screenWidth, screenHeight int)
|
||||
}
|
||||
|
||||
type state struct {
|
||||
game game
|
||||
|
||||
running bool
|
||||
|
||||
// m is a mutex required for each function.
|
||||
// For example, on Android, Update can be called from a different thread:
|
||||
// https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer
|
||||
m sync.Mutex
|
||||
}
|
||||
|
||||
func SetGame(game game) {
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
|
||||
theState.game = game
|
||||
}
|
||||
|
||||
//export ebitenLayout
|
||||
func ebitenLayout(viewWidth, viewHeight C.int, x, y, width, height *C.int) {
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
|
||||
if theState.game == nil {
|
||||
panic("ebitenmobileview: SetGame must be called before ebitenLayout")
|
||||
}
|
||||
|
||||
w, h := theState.game.Layout(int(viewWidth), int(viewHeight))
|
||||
scaleX := float64(viewWidth) / float64(w)
|
||||
scaleY := float64(viewHeight) / float64(h)
|
||||
scale := math.Min(scaleX, scaleY)
|
||||
|
||||
*width = C.int(math.Ceil(float64(w) * scale))
|
||||
*height = C.int(math.Ceil(float64(h) * scale))
|
||||
*x = (viewWidth - *width) / 2
|
||||
*y = (viewHeight - *height) / 2
|
||||
|
||||
if !theState.running {
|
||||
start(theState.game.Update, w, h, scale)
|
||||
theState.running = true
|
||||
}
|
||||
// TODO: call SetScreenSize
|
||||
}
|
||||
|
||||
//export ebitenUpdate
|
||||
func ebitenUpdate() *C.char {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
|
||||
if err := update(); err != nil {
|
||||
// TODO: When to free cstr?
|
||||
cstr := C.CString(err.Error())
|
||||
return cstr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//export ebitenUpdateTouchesOnIOS
|
||||
func ebitenUpdateTouchesOnIOS(phase C.int, ptr C.uintptr_t, x, y C.int) {
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
|
||||
updateTouchesOnIOSImpl(int(phase), int64(ptr), int(x), int(y))
|
||||
}
|
||||
|
||||
//export ebitenUpdateTouchesOnAndroid
|
||||
func ebitenUpdateTouchesOnAndroid(action C.int, id C.int, x, y C.int) {
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
|
||||
updateTouchesOnAndroid(int(action), int(id), int(x), int(y))
|
||||
}
|
56
mobile/ebitenmobileview/compat.go
Normal file
56
mobile/ebitenmobileview/compat.go
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright 2016 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 ebitenmobileview offers functions for OpenGL/Metal view of mobiles.
|
||||
//
|
||||
// The functions are not intended for public usages.
|
||||
// There is no guarantee of backward compatibility.
|
||||
package ebitenmobileview
|
||||
|
||||
// #include <stdint.h>
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type ViewRectSetter interface {
|
||||
SetViewRect(x, y, width, height int)
|
||||
}
|
||||
|
||||
func Layout(viewWidth, viewHeight int, viewRectSetter ViewRectSetter) {
|
||||
var x, y, width, height C.int
|
||||
ebitenLayout(C.int(viewWidth), C.int(viewHeight), &x, &y, &width, &height)
|
||||
if viewRectSetter != nil {
|
||||
viewRectSetter.SetViewRect(int(x), int(y), int(width), int(height))
|
||||
}
|
||||
}
|
||||
|
||||
func Update() error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
|
||||
return update()
|
||||
}
|
||||
|
||||
func UpdateTouchesOnAndroid(action int, id int, x, y int) {
|
||||
ebitenUpdateTouchesOnAndroid((C.int)(action), (C.int)(id), (C.int)(x), (C.int)(y))
|
||||
}
|
||||
|
||||
func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) {
|
||||
ebitenUpdateTouchesOnIOS((C.int)(phase), (C.uintptr_t)(ptr), (C.int)(x), (C.int)(y))
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
// Copyright 2016 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 ebitenmobileview offers functions for OpenGL/Metal view of mobiles.
|
||||
//
|
||||
// The functions are not intended for public usages.
|
||||
// There is no guarantee of backward compatibility.
|
||||
package ebitenmobileview
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
var theState state
|
||||
|
||||
type state struct {
|
||||
updateFunc func(*ebiten.Image) error
|
||||
screenWidth int
|
||||
screenHeight int
|
||||
|
||||
// m is a mutex required for each function.
|
||||
// For example, on Android, Update can be called from a different thread:
|
||||
// https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer
|
||||
m sync.Mutex
|
||||
}
|
||||
|
||||
func Run(scale float64) {
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
|
||||
if theState.updateFunc == nil {
|
||||
panic("ebitenmobileview: SetUpdateFunc must be called before Run")
|
||||
}
|
||||
start(theState.updateFunc, theState.screenWidth, theState.screenHeight, scale)
|
||||
}
|
||||
|
||||
func Update() error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
|
||||
return update()
|
||||
}
|
||||
|
||||
func UpdateTouchesOnAndroid(action int, id int, x, y int) {
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
|
||||
updateTouchesOnAndroid(action, id, x, y)
|
||||
}
|
||||
|
||||
func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) {
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
|
||||
updateTouchesOnIOSImpl(phase, ptr, x, y)
|
||||
}
|
||||
|
||||
func ScreenWidth() int {
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
return theState.screenWidth
|
||||
}
|
||||
|
||||
func ScreenHeight() int {
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
return theState.screenHeight
|
||||
}
|
||||
|
||||
func Set(updateFunc func(*ebiten.Image) error, screenWidth, screenHeight int) {
|
||||
theState.m.Lock()
|
||||
defer theState.m.Unlock()
|
||||
|
||||
theState.updateFunc = updateFunc
|
||||
theState.screenWidth = screenWidth
|
||||
theState.screenHeight = screenHeight
|
||||
}
|
22
mobile/ebitenmobileview/ebitenviewcontroller_ios.h
Normal file
22
mobile/ebitenmobileview/ebitenviewcontroller_ios.h
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright 2019 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.
|
||||
|
||||
// +build ios
|
||||
|
||||
// TODO: Embed this header file to .framework by an original command.
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface EbitenViewController : UIViewController
|
||||
@end
|
105
mobile/ebitenmobileview/ebitenviewcontroller_ios.m
Normal file
105
mobile/ebitenmobileview/ebitenviewcontroller_ios.m
Normal file
@ -0,0 +1,105 @@
|
||||
// Copyright 2019 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.
|
||||
|
||||
// +build ios
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <GLKit/GLkit.h>
|
||||
|
||||
#import "ebitenviewcontroller_ios.h"
|
||||
|
||||
#include "_cgo_export.h"
|
||||
|
||||
@implementation EbitenViewController {
|
||||
GLKView* glkView_;
|
||||
}
|
||||
|
||||
- (GLKView*)glkView {
|
||||
if (!glkView_) {
|
||||
glkView_ = [[GLKView alloc] init];
|
||||
glkView_.multipleTouchEnabled = YES;
|
||||
}
|
||||
return glkView_;
|
||||
}
|
||||
|
||||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
|
||||
self.glkView.delegate = (id<GLKViewDelegate>)(self);
|
||||
[self.view addSubview: self.glkView];
|
||||
|
||||
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
|
||||
[self glkView].context = context;
|
||||
|
||||
[EAGLContext setCurrentContext:context];
|
||||
|
||||
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame)];
|
||||
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
}
|
||||
|
||||
- (void)viewDidLayoutSubviews {
|
||||
[super viewDidLayoutSubviews];
|
||||
CGRect viewRect = [[self view] frame];
|
||||
|
||||
int x, y, width, height;
|
||||
ebitenLayout(viewRect.size.width, viewRect.size.height, &x, &y, &width, &height);
|
||||
|
||||
CGRect glkViewRect = CGRectMake(x, y, width, height);
|
||||
[[self glkView] setFrame:glkViewRect];
|
||||
}
|
||||
|
||||
- (void)didReceiveMemoryWarning {
|
||||
[super didReceiveMemoryWarning];
|
||||
// Dispose of any resources that can be recreated.
|
||||
// TODO: Notify this to Go world?
|
||||
}
|
||||
|
||||
- (void)drawFrame{
|
||||
[[self glkView] setNeedsDisplay];
|
||||
}
|
||||
|
||||
- (void)glkView:(GLKView*)view drawInRect:(CGRect)rect {
|
||||
const char* err = ebitenUpdate();
|
||||
if (err != nil) {
|
||||
NSLog(@"Error: %s", err);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)updateTouches:(NSSet*)touches {
|
||||
for (UITouch* touch in touches) {
|
||||
if (touch.view != [self glkView]) {
|
||||
continue;
|
||||
}
|
||||
CGPoint location = [touch locationInView:touch.view];
|
||||
ebitenUpdateTouchesOnIOS(touch.phase, (uintptr_t)touch, location.x, location.y);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
[self updateTouches:touches];
|
||||
}
|
||||
|
||||
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
[self updateTouches:touches];
|
||||
}
|
||||
|
||||
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
[self updateTouches:touches];
|
||||
}
|
||||
|
||||
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
[self updateTouches:touches];
|
||||
}
|
||||
|
||||
@end
|
125
mobile/mobile.go
125
mobile/mobile.go
@ -1,4 +1,4 @@
|
||||
// Copyright 2016 Hajime Hoshi
|
||||
// Copyright 2019 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.
|
||||
@ -25,117 +25,28 @@ import (
|
||||
"github.com/hajimehoshi/ebiten/mobile/ebitenmobileview"
|
||||
)
|
||||
|
||||
// Start starts the game and returns immediately.
|
||||
// Game defines a necessary function for a mobile game.
|
||||
type Game interface {
|
||||
// Update updates a game by one frame.
|
||||
Update(*ebiten.Image) error
|
||||
|
||||
// Layout accepts a native view size in DP (device-independent pixels) and returns the game's logical screen
|
||||
// size.
|
||||
//
|
||||
// Different from ebiten.Run, this invokes only the game loop and not the main (UI) loop.
|
||||
// The screen scale is automatically adjusted to fit the view.
|
||||
//
|
||||
// The unit of width/height is device-independent pixel (dp on Android and point on iOS).
|
||||
// Layout is called at an initialization and whenever the view size is changed.
|
||||
//
|
||||
// Start is concurrent-safe.
|
||||
//
|
||||
// The argument title is ignored. This is for backward compatibility.
|
||||
//
|
||||
// Start always returns nil as of 1.5.0-alpha.
|
||||
func Start(f func(*ebiten.Image) error, width, height int, scale float64, title string) error {
|
||||
ebitenmobileview.Set(f, width, height)
|
||||
ebitenmobileview.Run(scale)
|
||||
return nil
|
||||
// You can return a fixed screen size if you don't care, or you can also return a calculated screen size
|
||||
// adjusted with the given view size.
|
||||
Layout(viewWidth, viewHeight int) (screenWidth, screenHeight int)
|
||||
}
|
||||
|
||||
// Update updates and renders the game.
|
||||
// This should be called on every frame.
|
||||
// SetGame sets a mobile game.
|
||||
//
|
||||
// If Update is called before Start is called, Update panics.
|
||||
// SetGame is epxected to be called only once.
|
||||
//
|
||||
// On Android, this should be called at onDrawFrame of Renderer (used by GLSurfaceView).
|
||||
//
|
||||
// On iOS, this should be called at glkView:drawInRect: of GLKViewDelegate.
|
||||
//
|
||||
// Update is concurrent-safe.
|
||||
//
|
||||
// Update returns error when 1) OpenGL error happens, or 2) f in Start returns error samely as ebiten.Run.
|
||||
func Update() error {
|
||||
return ebitenmobileview.Update()
|
||||
}
|
||||
|
||||
// UpdateTouchesOnAndroid updates the touch state on Android.
|
||||
//
|
||||
// This should be called with onTouchEvent of GLSurfaceView like this:
|
||||
//
|
||||
// private double mDeviceScale = 0.0;
|
||||
//
|
||||
// // pxToDp converts an value in pixels to dp.
|
||||
// private double pxToDp(double x) {
|
||||
// if (mDeviceScale == 0.0) {
|
||||
// mDeviceScale = getResources().getDisplayMetrics().density;
|
||||
// }
|
||||
// return x / mDeviceScale;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public boolean onTouchEvent(MotionEvent e) {
|
||||
// for (int i = 0; i < e.getPointerCount(); i++) {
|
||||
// int id = e.getPointerId(i);
|
||||
// int x = (int)e.getX(i);
|
||||
// int y = (int)e.getY(i);
|
||||
// // Exported function for UpdateTouchesOnAndroid
|
||||
// YourGame.UpdateTouchesOnAndroid(e.getActionMasked(), id, (int)pxToDp(x), (int)pxToDp(y));
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// The coodinate x/y is in dp.
|
||||
//
|
||||
// UpdateTouchesOnAndroid can be called even before Start is called.
|
||||
//
|
||||
// UpdateTouchesOnAndroid is concurrent-safe.
|
||||
//
|
||||
// For more details, see https://github.com/hajimehoshi/ebiten/wiki/Android.
|
||||
func UpdateTouchesOnAndroid(action int, id int, x, y int) {
|
||||
ebitenmobileview.UpdateTouchesOnAndroid(action, id, x, y)
|
||||
}
|
||||
|
||||
// UpdateTouchesOnIOS updates the touch state on iOS.
|
||||
//
|
||||
// This should be called with touch handlers of UIViewController like this:
|
||||
//
|
||||
// - (GLKView*)glkView {
|
||||
// return (GLKView*)[self.view viewWithTag:100];
|
||||
// }
|
||||
// - (void)updateTouches:(NSSet*)touches {
|
||||
// for (UITouch* touch in touches) {
|
||||
// if (touch.view != [self glkView]) {
|
||||
// continue;
|
||||
// }
|
||||
// CGPoint location = [touch locationInView: [self glkView]];
|
||||
// // Exported function for UpdateTouchesOnIOS
|
||||
// YourGameUpdateTouchesOnIOS(touch.phase, (int64_t)touch, location.x, location.y);
|
||||
// }
|
||||
// }
|
||||
// - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
// [self updateTouches:touches];
|
||||
// }
|
||||
// - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
// [self updateTouches:touches];
|
||||
// }
|
||||
// - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
// [self updateTouches:touches];
|
||||
// }
|
||||
// - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
|
||||
// [self updateTouches:touches];
|
||||
// }
|
||||
//
|
||||
// The coodinate x/y is in point.
|
||||
//
|
||||
// UpdateTouchesOnIOS can be called even before Start is called.
|
||||
//
|
||||
// UpdateTouchesOnIOS is concurrent-safe.
|
||||
//
|
||||
// For more details, see https://github.com/hajimehoshi/ebiten/wiki/iOS.
|
||||
func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) {
|
||||
ebitenmobileview.UpdateTouchesOnIOS(phase, ptr, x, y)
|
||||
}
|
||||
|
||||
func Set(updateFunc func(*ebiten.Image) error, screenWidth, screenHeight int) {
|
||||
ebitenmobileview.Set(updateFunc, screenWidth, screenHeight)
|
||||
// SetGame is expected to be called at initialization phase like an init function.
|
||||
func SetGame(game Game) {
|
||||
ebitenmobileview.SetGame(game)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user