| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Go Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style | |
| 3 // license that can be found in the LICENSE file. | |
| 4 | |
| 5 package main | |
| 6 | |
| 7 import ( | |
| 8 "image" | |
| 9 "log" | |
| 10 "math" | |
| 11 "time" | |
| 12 | |
| 13 _ "image/jpeg" | |
| 14 | |
| 15 "golang.org/x/mobile/app" | |
| 16 "golang.org/x/mobile/app/debug" | |
| 17 "golang.org/x/mobile/event" | |
| 18 "golang.org/x/mobile/f32" | |
| 19 "golang.org/x/mobile/gl" | |
| 20 "golang.org/x/mobile/sprite" | |
| 21 "golang.org/x/mobile/sprite/clock" | |
| 22 "golang.org/x/mobile/sprite/glsprite" | |
| 23 ) | |
| 24 | |
| 25 var ( | |
| 26 start = time.Now() | |
| 27 lastClock = clock.Time(-1) | |
| 28 | |
| 29 eng = glsprite.Engine() | |
| 30 scene *sprite.Node | |
| 31 ) | |
| 32 | |
| 33 func main() { | |
| 34 app.Run(app.Callbacks{ | |
| 35 Draw: draw, | |
| 36 Touch: touch, | |
| 37 }) | |
| 38 } | |
| 39 | |
| 40 func draw() { | |
| 41 if scene == nil { | |
| 42 loadScene() | |
| 43 } | |
| 44 | |
| 45 now := clock.Time(time.Since(start) * 60 / time.Second) | |
| 46 if now == lastClock { | |
| 47 // TODO: figure out how to limit draw callbacks to 60Hz instead
of | |
| 48 // burning the CPU as fast as possible. | |
| 49 // TODO: (relatedly??) sync to vblank? | |
| 50 return | |
| 51 } | |
| 52 lastClock = now | |
| 53 | |
| 54 gl.ClearColor(1, 1, 1, 1) | |
| 55 gl.Clear(gl.COLOR_BUFFER_BIT) | |
| 56 eng.Render(scene, now) | |
| 57 debug.DrawFPS() | |
| 58 } | |
| 59 | |
| 60 func touch(t event.Touch) { | |
| 61 } | |
| 62 | |
| 63 func newNode() *sprite.Node { | |
| 64 n := &sprite.Node{} | |
| 65 eng.Register(n) | |
| 66 scene.AppendChild(n) | |
| 67 return n | |
| 68 } | |
| 69 | |
| 70 func loadScene() { | |
| 71 texs := loadTextures() | |
| 72 scene = &sprite.Node{} | |
| 73 eng.Register(scene) | |
| 74 eng.SetTransform(scene, f32.Affine{ | |
| 75 {1, 0, 0}, | |
| 76 {0, 1, 0}, | |
| 77 }) | |
| 78 | |
| 79 var n *sprite.Node | |
| 80 | |
| 81 n = newNode() | |
| 82 eng.SetSubTex(n, texs[texBooks]) | |
| 83 eng.SetTransform(n, f32.Affine{ | |
| 84 {36, 0, 0}, | |
| 85 {0, 36, 0}, | |
| 86 }) | |
| 87 | |
| 88 n = newNode() | |
| 89 eng.SetSubTex(n, texs[texFire]) | |
| 90 eng.SetTransform(n, f32.Affine{ | |
| 91 {72, 0, 144}, | |
| 92 {0, 72, 144}, | |
| 93 }) | |
| 94 | |
| 95 n = newNode() | |
| 96 n.Arranger = arrangerFunc(func(eng sprite.Engine, n *sprite.Node, t cloc
k.Time) { | |
| 97 // TODO: use a tweening library instead of manually arranging. | |
| 98 t0 := uint32(t) % 120 | |
| 99 if t0 < 60 { | |
| 100 eng.SetSubTex(n, texs[texGopherR]) | |
| 101 } else { | |
| 102 eng.SetSubTex(n, texs[texGopherL]) | |
| 103 } | |
| 104 | |
| 105 u := float32(t0) / 120 | |
| 106 u = (1 - f32.Cos(u*2*math.Pi)) / 2 | |
| 107 | |
| 108 tx := 18 + u*48 | |
| 109 ty := 36 + u*108 | |
| 110 sx := 36 + u*36 | |
| 111 sy := 36 + u*36 | |
| 112 eng.SetTransform(n, f32.Affine{ | |
| 113 {sx, 0, tx}, | |
| 114 {0, sy, ty}, | |
| 115 }) | |
| 116 }) | |
| 117 } | |
| 118 | |
| 119 const ( | |
| 120 texBooks = iota | |
| 121 texFire | |
| 122 texGopherR | |
| 123 texGopherL | |
| 124 ) | |
| 125 | |
| 126 func loadTextures() []sprite.SubTex { | |
| 127 a, err := app.Open("waza-gophers.jpeg") | |
| 128 if err != nil { | |
| 129 log.Fatal(err) | |
| 130 } | |
| 131 defer a.Close() | |
| 132 | |
| 133 img, _, err := image.Decode(a) | |
| 134 if err != nil { | |
| 135 log.Fatal(err) | |
| 136 } | |
| 137 t, err := eng.LoadTexture(img) | |
| 138 if err != nil { | |
| 139 log.Fatal(err) | |
| 140 } | |
| 141 | |
| 142 return []sprite.SubTex{ | |
| 143 texBooks: sprite.SubTex{t, image.Rect(4, 71, 132, 182)}, | |
| 144 texFire: sprite.SubTex{t, image.Rect(330, 56, 440, 155)}, | |
| 145 texGopherR: sprite.SubTex{t, image.Rect(152, 10, 152+140, 10+90)
}, | |
| 146 texGopherL: sprite.SubTex{t, image.Rect(162, 120, 162+140, 120+9
0)}, | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 type arrangerFunc func(e sprite.Engine, n *sprite.Node, t clock.Time) | |
| 151 | |
| 152 func (a arrangerFunc) Arrange(e sprite.Engine, n *sprite.Node, t clock.Time) { a
(e, n, t) } | |
| OLD | NEW |