forked from EngoEngine/engo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engo_mobile_bind.go
182 lines (151 loc) · 4.04 KB
/
engo_mobile_bind.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
//+build mobilebind
package engo
import (
"errors"
"io"
"runtime"
"time"
"engo.io/gl"
mgl "golang.org/x/mobile/gl"
)
var (
// Gl is the current OpenGL context
Gl *gl.Context
worker mgl.Worker
ticker *time.Ticker
msaaPreference int
drawEvent = make(chan struct{})
drawDone = make(chan struct{})
initalized = false
)
// 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 int(windowWidth), int(windowHeight)
}
// 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 is the current scale of the canvas from the original window size
func CanvasScale() float32 {
return CanvasWidth() / WindowWidth()
}
// DestroyWindow destroies the window.
func DestroyWindow() { /* nothing to do here? */ }
func runLoop(defaultScene Scene, headless bool) {
go func() {
for {
mobileDraw(defaultScene)
}
}()
}
// 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) {
windowWidth = float32(opts.MobileWidth)
canvasWidth = float32(opts.MobileWidth)
windowHeight = float32(opts.MobileHeight)
canvasHeight = float32(opts.MobileHeight)
ResizeXOffset = gameWidth - canvasWidth
ResizeYOffset = gameHeight - canvasHeight
Gl.Viewport(0, 0, opts.MobileWidth, opts.MobileHeight)
Time = NewClock()
SetScene(defaultScene, false)
}
// RunIteration runs every time android calls to update the screen
func RunIteration() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
select {
case drawEvent <- struct{}{}:
for {
select {
case <-worker.WorkAvailable():
worker.DoWork()
case <-drawDone:
return
}
}
case <-time.After(500 * time.Millisecond):
return
}
}
// 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) {
return nil, errors.New("binding does not open files this way. utilize go-bindata instead")
}
// mobileDraw runs once per frame. RunIteration for the other backends
func mobileDraw(defaultScene Scene) {
if !initalized {
var ctx mgl.Context
ctx, worker = mgl.NewContext()
Gl = gl.NewContext(ctx)
}
<-drawEvent
defer func() {
drawDone <- struct{}{}
}()
if !initalized {
RunPreparation(defaultScene)
ticker = time.NewTicker(time.Duration(int(time.Second) / opts.FPSLimit))
initalized = true
}
select {
case <-ticker.C:
case <-resetLoopTicker:
ticker.Stop()
ticker = time.NewTicker(time.Duration(int(time.Second) / opts.FPSLimit))
}
Time.Tick()
if !opts.HeadlessMode {
Input.update()
}
// Then update the world and all Systems
currentUpdater.Update(Time.Delta())
Input.Mouse.Action = Neutral
}
//MobileStop handles when the game is closed
func MobileStop() {
closeEvent()
ticker.Stop()
Gl = nil
worker = nil
}
// IsAndroidChrome tells if the browser is Chrome for android
func IsAndroidChrome() bool {
return false
}