github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/example/basic/main.go (about) 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 // +build darwin linux 6 7 // An app that draws a green triangle on a red background. 8 // 9 // Note: This demo is an early preview of Go 1.5. In order to build this 10 // program as an Android APK using the gomobile tool. 11 // 12 // See http://godoc.org/github.com/c-darwin/mobile/cmd/gomobile to install gomobile. 13 // 14 // Get the basic example and use gomobile to build or install it on your device. 15 // 16 // $ go get -d github.com/c-darwin/mobile/example/basic 17 // $ gomobile build github.com/c-darwin/mobile/example/basic # will build an APK 18 // 19 // # plug your Android device to your computer or start an Android emulator. 20 // # if you have adb installed on your machine, use gomobile install to 21 // # build and deploy the APK to an Android target. 22 // $ gomobile install github.com/c-darwin/mobile/example/basic 23 // 24 // Switch to your device or emulator to start the Basic application from 25 // the launcher. 26 // You can also run the application on your desktop by running the command 27 // below. (Note: It currently doesn't work on Windows.) 28 // $ go install github.com/c-darwin/mobile/example/basic && basic 29 package main 30 31 import ( 32 "encoding/binary" 33 "log" 34 35 "github.com/c-darwin/mobile/app" 36 "github.com/c-darwin/mobile/event/lifecycle" 37 "github.com/c-darwin/mobile/event/paint" 38 "github.com/c-darwin/mobile/event/size" 39 "github.com/c-darwin/mobile/event/touch" 40 "github.com/c-darwin/mobile/exp/app/debug" 41 "github.com/c-darwin/mobile/exp/f32" 42 "github.com/c-darwin/mobile/exp/gl/glutil" 43 "github.com/c-darwin/mobile/gl" 44 ) 45 46 var ( 47 program gl.Program 48 position gl.Attrib 49 offset gl.Uniform 50 color gl.Uniform 51 buf gl.Buffer 52 53 green float32 54 touchX float32 55 touchY float32 56 ) 57 58 func main() { 59 app.Main(func(a app.App) { 60 var sz size.Event 61 for e := range a.Events() { 62 switch e := app.Filter(e).(type) { 63 case lifecycle.Event: 64 switch e.Crosses(lifecycle.StageVisible) { 65 case lifecycle.CrossOn: 66 onStart() 67 case lifecycle.CrossOff: 68 onStop() 69 } 70 case size.Event: 71 sz = e 72 touchX = float32(sz.WidthPx / 2) 73 touchY = float32(sz.HeightPx / 2) 74 case paint.Event: 75 onPaint(sz) 76 a.EndPaint(e) 77 case touch.Event: 78 touchX = e.X 79 touchY = e.Y 80 } 81 } 82 }) 83 } 84 85 func onStart() { 86 var err error 87 program, err = glutil.CreateProgram(vertexShader, fragmentShader) 88 if err != nil { 89 log.Printf("error creating GL program: %v", err) 90 return 91 } 92 93 buf = gl.CreateBuffer() 94 gl.BindBuffer(gl.ARRAY_BUFFER, buf) 95 gl.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW) 96 97 position = gl.GetAttribLocation(program, "position") 98 color = gl.GetUniformLocation(program, "color") 99 offset = gl.GetUniformLocation(program, "offset") 100 101 // TODO(crawshaw): the debug package needs to put GL state init here 102 // Can this be an app.RegisterFilter call now?? 103 } 104 105 func onStop() { 106 gl.DeleteProgram(program) 107 gl.DeleteBuffer(buf) 108 } 109 110 func onPaint(sz size.Event) { 111 gl.ClearColor(1, 0, 0, 1) 112 gl.Clear(gl.COLOR_BUFFER_BIT) 113 114 gl.UseProgram(program) 115 116 green += 0.01 117 if green > 1 { 118 green = 0 119 } 120 gl.Uniform4f(color, 0, green, 0, 1) 121 122 gl.Uniform2f(offset, touchX/float32(sz.WidthPx), touchY/float32(sz.HeightPx)) 123 124 gl.BindBuffer(gl.ARRAY_BUFFER, buf) 125 gl.EnableVertexAttribArray(position) 126 gl.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0) 127 gl.DrawArrays(gl.TRIANGLES, 0, vertexCount) 128 gl.DisableVertexAttribArray(position) 129 130 debug.DrawFPS(sz) 131 } 132 133 var triangleData = f32.Bytes(binary.LittleEndian, 134 0.0, 0.4, 0.0, // top left 135 0.0, 0.0, 0.0, // bottom left 136 0.4, 0.0, 0.0, // bottom right 137 ) 138 139 const ( 140 coordsPerVertex = 3 141 vertexCount = 3 142 ) 143 144 const vertexShader = `#version 100 145 uniform vec2 offset; 146 147 attribute vec4 position; 148 void main() { 149 // offset comes in with x/y values between 0 and 1. 150 // position bounds are -1 to 1. 151 vec4 offset4 = vec4(2.0*offset.x-1.0, 1.0-2.0*offset.y, 0, 0); 152 gl_Position = position + offset4; 153 }` 154 155 const fragmentShader = `#version 100 156 precision mediump float; 157 uniform vec4 color; 158 void main() { 159 gl_FragColor = color; 160 }`