github.com/corfe83/mobile@v0.0.0-20220928034243-9edc37f43fac/example/flappy/main.go (about)

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