Debian Codegraph @

example/flappy/main.go

raw symbols SCIP
1 // Copyright 2015 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 //go:build darwin || linux
6
7 // Flappy Gopher is a simple one-button game that uses the
8 // mobile framework and the experimental sprite engine.
9 package main
10
11 import (
12 "math/rand"
13 "time"
14
15 "golang.org/x/mobile/app"
16 "golang.org/x/mobile/event/key"
17 "golang.org/x/mobile/event/lifecycle"
18 "golang.org/x/mobile/event/paint"
19 "golang.org/x/mobile/event/size"
20 "golang.org/x/mobile/event/touch"
21 "golang.org/x/mobile/exp/gl/glutil"
22 "golang.org/x/mobile/exp/sprite"
23 "golang.org/x/mobile/exp/sprite/clock"
24 "golang.org/x/mobile/exp/sprite/glsprite"
25 "golang.org/x/mobile/gl"
26 )
27
28 func main() {
29 rand.Seed(time.Now().UnixNano())
30
31 app.Main(func(a app.App) {
32 var glctx gl.Context
33 var sz size.Event
34 for e := range a.Events() {
35 switch e := a.Filter(e).(type) {
36 case lifecycle.Event:
37 switch e.Crosses(lifecycle.StageVisible) {
38 case lifecycle.CrossOn:
39 glctx, _ = e.DrawContext.(gl.Context)
40 onStart(glctx)
41 a.Send(paint.Event{})
42 case lifecycle.CrossOff:
43 onStop()
44 glctx = nil
45 }
46 case size.Event:
47 sz = e
48 case paint.Event:
49 if glctx == nil || e.External {
50 continue
51 }
52 onPaint(glctx, sz)
53 a.Publish()
54 a.Send(paint.Event{}) // keep animating
55 case touch.Event:
56 if down := e.Type == touch.TypeBegin; down || e.Type == touch.TypeEnd {
57 game.Press(down)
58 }
59 case key.Event:
60 if e.Code != key.CodeSpacebar {
61 break
62 }
63 if down := e.Direction == key.DirPress; down || e.Direction == key.DirRelease {
64 game.Press(down)
65 }
66 }
67 }
68 })
69 }
70
71 var (
72 startTime = time.Now()
73 images *glutil.Images
74 eng sprite.Engine
75 scene *sprite.Node
76 game *Game
77 )
78
79 func onStart(glctx gl.Context) {
80 images = glutil.NewImages(glctx)
81 eng = glsprite.Engine(images)
82 game = NewGame()
83 scene = game.Scene(eng)
84 }
85
86 func onStop() {
87 eng.Release()
88 images.Release()
89 game = nil
90 }
91
92 func onPaint(glctx gl.Context, sz size.Event) {
93 glctx.ClearColor(1, 1, 1, 1)
94 glctx.Clear(gl.COLOR_BUFFER_BIT)
95 now := clock.Time(time.Since(startTime) * 60 / time.Second)
96 game.Update(now)
97 eng.Render(scene, now, sz)
98 }