-
Notifications
You must be signed in to change notification settings - Fork 136
/
engo_mobile.go
222 lines (188 loc) · 5.4 KB
/
engo_mobile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//go:build (android || ios) && !mobilebind
// +build android ios
// +build !mobilebind
package engo
import (
"io"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/EngoEngine/gl"
"golang.org/x/mobile/app"
"golang.org/x/mobile/asset"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
"golang.org/x/mobile/event/touch"
)
var (
// Gl is the current OpenGL context
Gl *gl.Context
sz size.Event
msaaPreference int
)
// CreateWindow creates a window with the specified parameters
func CreateWindow(title string, width, height int, fullscreen bool, msaa int) {
CurrentBackEnd = BackEndMobile
gameWidth = float32(width)
gameHeight = float32(height)
msaaPreference = msaa
}
// WindowSize returns the width and height of the current window
func WindowSize() (w, h int) {
return sz.WidthPx, sz.HeightPx
}
// CursorPos returns the current cursor position
func CursorPos() (x, y float32) {
notImplemented("CursorPos")
return 0, 0
}
// WindowWidth returns the current window width
func WindowWidth() float32 {
return windowWidth
}
// WindowHeight returns the current window height
func WindowHeight() float32 {
return windowHeight
}
// CanvasWidth returns the current canvas width
func CanvasWidth() float32 {
return canvasWidth
}
// CanvasHeight returns the current canvas height
func CanvasHeight() float32 {
return canvasHeight
}
// CanvasScale returns the current scale of the canvas from the original window
func CanvasScale() float32 {
return CanvasWidth() / WindowWidth()
}
// DestroyWindow handles destroying the window
func DestroyWindow() { /* nothing to do here? */ }
func runLoop(defaultScene Scene, headless bool) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
closeEvent()
}()
app.Main(func(a app.App) {
var (
ticker *time.Ticker
)
for e := range a.Events() {
switch e := a.Filter(e).(type) {
case lifecycle.Event:
switch e.Crosses(lifecycle.StageVisible) {
case lifecycle.CrossOn:
Gl = gl.NewContext(e.DrawContext)
RunPreparation(defaultScene)
ticker = time.NewTicker(time.Duration(int(time.Second) / opts.FPSLimit))
// Start tick, minimize the delta
Time.Tick()
// Let the device know we want to start painting :-)
a.Send(paint.Event{})
case lifecycle.CrossOff:
closeEvent()
ticker.Stop()
Gl = nil
}
case size.Event:
sz = e
windowWidth = float32(sz.WidthPx)
windowHeight = float32(sz.HeightPx)
canvasWidth = float32(sz.WidthPx)
canvasHeight = float32(sz.HeightPx)
Gl.Viewport(0, 0, sz.WidthPx, sz.HeightPx)
ResizeXOffset = (gameWidth - canvasWidth)
ResizeYOffset = (gameHeight - canvasHeight)
case paint.Event:
if e.External {
// As we are actively painting as fast as
// we can (usually 60 FPS), skip any paint
// events sent by the system.
continue
}
select {
case <-ticker.C:
RunIteration()
case <-resetLoopTicker:
ticker.Stop()
ticker = time.NewTicker(time.Duration(int(time.Second) / opts.FPSLimit))
}
Input.Mouse.Action = Neutral
a.Publish() // same as SwapBuffers
// Drive the animation by preparing to paint the next frame
// after this one is shown. - FPS is ignored here!
a.Send(paint.Event{})
case touch.Event:
Input.Mouse.X = e.X / opts.GlobalScale.X
Input.Mouse.Y = e.Y / opts.GlobalScale.Y
id := int(e.Sequence)
switch e.Type {
case touch.TypeBegin:
Input.Mouse.Action = Press
Input.Touches[id] = Point{
X: float32(e.X) / opts.GlobalScale.X,
Y: float32(e.Y) / opts.GlobalScale.Y,
}
case touch.TypeMove:
Input.Mouse.Action = Move
Input.Touches[id] = Point{
X: float32(e.X) / opts.GlobalScale.X,
Y: float32(e.Y) / opts.GlobalScale.Y,
}
case touch.TypeEnd:
Input.Mouse.Action = Release
delete(Input.Touches, id)
}
}
}
})
}
// RunPreparation is called only once, and is called automatically when calling Open
// It is only here for benchmarking in combination with OpenHeadlessNoRun
func RunPreparation(defaultScene Scene) {
Time = NewClock()
SetScene(defaultScene, false)
}
// RunIteration runs one iteration / frame
func RunIteration() {
Time.Tick()
if !opts.HeadlessMode {
Input.update()
}
// Then update the world and all Systems
currentUpdater.Update(Time.Delta())
}
// SetCursor changes the cursor - not yet implemented
func SetCursor(Cursor) {
notImplemented("SetCursor")
}
//SetCursorVisibility sets the visibility of the cursor.
//If true the cursor is visible, if false the cursor is not.
//Does nothing in mobile since there's no visible cursor to begin with
func SetCursorVisibility(visible bool) {}
// SetTitle has no effect on mobile
func SetTitle(title string) {}
// openFile is the mobile-specific way of opening a file
func openFile(url string) (io.ReadCloser, error) {
usedUrl := url
if strings.HasPrefix(url, "assets/") {
usedUrl = usedUrl[7:]
}
return asset.Open(usedUrl)
}
// IsAndroidChrome tells if the browser is Chrome for android
func IsAndroidChrome() bool {
return false
}
// GetKeyName returns the string returned from the given Key. So you can write "Press W to move forward"
// and get a W for QWERTY and Z for AZERTY
// Always returns the empty string on mobile since keyboard access is rare and takes a lot of effort
func GetKeyName(k Key) string {
return ""
}