forked from golang/mobile
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
195 lines (169 loc) · 4.03 KB
/
main.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin linux
// An app that makes a sound as the gopher hits the walls of the screen.
//
// Note: This demo is an early preview of Go 1.5. In order to build this
// program as an Android APK using the gomobile tool.
//
// See http://godoc.org/github.com/c-darwin/mobile/cmd/gomobile to install gomobile.
//
// Get the audio example and use gomobile to build or install it on your device.
//
// $ go get -d github.com/c-darwin/mobile/example/audio
// $ gomobile build github.com/c-darwin/mobile/example/audio # will build an APK
//
// # plug your Android device to your computer or start an Android emulator.
// # if you have adb installed on your machine, use gomobile install to
// # build and deploy the APK to an Android target.
// $ gomobile install github.com/c-darwin/mobile/example/audio
//
// Additionally, you can run the sample on your desktop environment
// by using the go tool.
//
// $ go install github.com/c-darwin/mobile/example/audio && audio
//
// On Linux, you need to install OpenAL developer library by
// running the command below.
//
// $ apt-get install libopenal-dev
package main
import (
"image"
"log"
"time"
_ "image/jpeg"
"github.com/c-darwin/mobile/app"
"github.com/c-darwin/mobile/asset"
"github.com/c-darwin/mobile/event/lifecycle"
"github.com/c-darwin/mobile/event/paint"
"github.com/c-darwin/mobile/event/size"
"github.com/c-darwin/mobile/exp/app/debug"
"github.com/c-darwin/mobile/exp/audio"
"github.com/c-darwin/mobile/exp/f32"
"github.com/c-darwin/mobile/exp/sprite"
"github.com/c-darwin/mobile/exp/sprite/clock"
"github.com/c-darwin/mobile/exp/sprite/glsprite"
"github.com/c-darwin/mobile/gl"
)
const (
width = 72
height = 60
)
var (
startTime = time.Now()
eng = glsprite.Engine()
scene *sprite.Node
player *audio.Player
sz size.Event
)
func main() {
app.Main(func(a app.App) {
for e := range a.Events() {
switch e := app.Filter(e).(type) {
case lifecycle.Event:
switch e.Crosses(lifecycle.StageVisible) {
case lifecycle.CrossOn:
onStart()
case lifecycle.CrossOff:
onStop()
}
case size.Event:
sz = e
case paint.Event:
onPaint()
a.EndPaint(e)
}
}
})
}
func onStart() {
rc, err := asset.Open("boing.wav")
if err != nil {
log.Fatal(err)
}
player, err = audio.NewPlayer(rc, 0, 0)
if err != nil {
log.Fatal(err)
}
}
func onStop() {
player.Close()
}
func onPaint() {
if scene == nil {
loadScene()
}
gl.ClearColor(1, 1, 1, 1)
gl.Clear(gl.COLOR_BUFFER_BIT)
now := clock.Time(time.Since(startTime) * 60 / time.Second)
eng.Render(scene, now, sz)
debug.DrawFPS(sz)
}
func newNode() *sprite.Node {
n := &sprite.Node{}
eng.Register(n)
scene.AppendChild(n)
return n
}
func loadScene() {
gopher := loadGopher()
scene = &sprite.Node{}
eng.Register(scene)
eng.SetTransform(scene, f32.Affine{
{1, 0, 0},
{0, 1, 0},
})
var x, y float32
dx, dy := float32(1), float32(1)
n := newNode()
// TODO: Shouldn't arranger pass the size.Event?
n.Arranger = arrangerFunc(func(eng sprite.Engine, n *sprite.Node, t clock.Time) {
eng.SetSubTex(n, gopher)
if x < 0 {
dx = 1
boing()
}
if y < 0 {
dy = 1
boing()
}
if x+width > float32(sz.WidthPt) {
dx = -1
boing()
}
if y+height > float32(sz.HeightPt) {
dy = -1
boing()
}
x += dx
y += dy
eng.SetTransform(n, f32.Affine{
{width, 0, x},
{0, height, y},
})
})
}
func boing() {
player.Seek(0)
player.Play()
}
func loadGopher() sprite.SubTex {
a, err := asset.Open("gopher.jpeg")
if err != nil {
log.Fatal(err)
}
defer a.Close()
img, _, err := image.Decode(a)
if err != nil {
log.Fatal(err)
}
t, err := eng.LoadTexture(img)
if err != nil {
log.Fatal(err)
}
return sprite.SubTex{t, image.Rect(0, 0, 360, 300)}
}
type arrangerFunc func(e sprite.Engine, n *sprite.Node, t clock.Time)
func (a arrangerFunc) Arrange(e sprite.Engine, n *sprite.Node, t clock.Time) { a(e, n, t) }