Advertisement
Guest User

Untitled

a guest
Oct 1st, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.90 KB | None | 0 0
  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/golang.org/x/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 golang.org/x/mobile/example/basic
  17. //   $ gomobile build golang.org/x/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 golang.org/x/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 golang.org/x/mobile/example/basic && basic
  29. package hello
  30.  
  31. import (
  32.     "encoding/binary"
  33.     "log"
  34.  
  35.     "golang.org/x/mobile/app"
  36.     "golang.org/x/mobile/event/lifecycle"
  37.     "golang.org/x/mobile/event/paint"
  38.     "golang.org/x/mobile/event/size"
  39.     "golang.org/x/mobile/event/touch"
  40.     "golang.org/x/mobile/exp/app/debug"
  41.     "golang.org/x/mobile/exp/f32"
  42.     "golang.org/x/mobile/exp/gl/glutil"
  43.     "golang.org/x/mobile/gl"
  44. )
  45.  
  46. var (
  47.     images   *glutil.Images
  48.     fps      *debug.FPS
  49.     program  gl.Program
  50.     position gl.Attrib
  51.     offset   gl.Uniform
  52.     color    gl.Uniform
  53.     buf      gl.Buffer
  54.  
  55.     green  float32
  56.     touchX float32
  57.     touchY float32
  58. )
  59.  
  60. func main() {
  61.     app.Main(func(a app.App) {
  62.         var glctx gl.Context
  63.         visible, sz := false, size.Event{}
  64.         for e := range a.Events() {
  65.             switch e := a.Filter(e).(type) {
  66.             case lifecycle.Event:
  67.                 switch e.Crosses(lifecycle.StageVisible) {
  68.                 case lifecycle.CrossOn:
  69.                     visible = true
  70.                     glctx, _ = e.DrawContext.(gl.Context)
  71.                     onStart(glctx)
  72.                 case lifecycle.CrossOff:
  73.                     visible = false
  74.                     onStop(glctx)
  75.                 }
  76.             case size.Event:
  77.                 sz = e
  78.                 touchX = float32(sz.WidthPx / 2)
  79.                 touchY = float32(sz.HeightPx / 2)
  80.             case paint.Event:
  81.                 onPaint(glctx, sz)
  82.                 a.Publish()
  83.                 if visible {
  84.                     // Drive the animation by preparing to paint the next frame
  85.                     // after this one is shown.
  86.                     //
  87.                     // TODO: is paint.Event the right thing to send? Should we
  88.                     // have a dedicated publish.Event type? Should App.Publish
  89.                     // take an optional event sender and send a publish.Event?
  90.                     a.Send(paint.Event{})
  91.                 }
  92.             case touch.Event:
  93.                 touchX = e.X
  94.                 touchY = e.Y
  95.             }
  96.         }
  97.     })
  98. }
  99.  
  100. func onStart(glctx gl.Context) {
  101.     var err error
  102.     program, err = glutil.CreateProgram(glctx, vertexShader, fragmentShader)
  103.     if err != nil {
  104.         log.Printf("error creating GL program: %v", err)
  105.         return
  106.     }
  107.  
  108.     buf = glctx.CreateBuffer()
  109.     glctx.BindBuffer(gl.ARRAY_BUFFER, buf)
  110.     glctx.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW)
  111.  
  112.     position = glctx.GetAttribLocation(program, "position")
  113.     color = glctx.GetUniformLocation(program, "color")
  114.     offset = glctx.GetUniformLocation(program, "offset")
  115.  
  116.     images = glutil.NewImages(glctx)
  117.     fps = debug.NewFPS(images)
  118. }
  119.  
  120. func onStop(glctx gl.Context) {
  121.     glctx.DeleteProgram(program)
  122.     glctx.DeleteBuffer(buf)
  123.     fps.Release()
  124.     images.Release()
  125. }
  126.  
  127. func onPaint(glctx gl.Context, sz size.Event) {
  128.     glctx.ClearColor(1, 0, 0, 1)
  129.     glctx.Clear(gl.COLOR_BUFFER_BIT)
  130.  
  131.     glctx.UseProgram(program)
  132.  
  133.     green += 0.01
  134.     if green > 1 {
  135.         green = 0
  136.     }
  137.     glctx.Uniform4f(color, 0, green, 0, 1)
  138.  
  139.     glctx.Uniform2f(offset, touchX/float32(sz.WidthPx), touchY/float32(sz.HeightPx))
  140.  
  141.     glctx.BindBuffer(gl.ARRAY_BUFFER, buf)
  142.     glctx.EnableVertexAttribArray(position)
  143.     glctx.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0)
  144.     glctx.DrawArrays(gl.TRIANGLES, 0, vertexCount)
  145.     glctx.DisableVertexAttribArray(position)
  146.  
  147.     fps.Draw(sz)
  148. }
  149.  
  150. var triangleData = f32.Bytes(binary.LittleEndian,
  151.     0.0, 0.4, 0.0, // top left
  152.     0.0, 0.0, 0.0, // bottom left
  153.     0.4, 0.0, 0.0, // bottom right
  154. )
  155.  
  156. const (
  157.     coordsPerVertex = 3
  158.     vertexCount     = 3
  159. )
  160.  
  161. const vertexShader = `#version 100
  162. uniform vec2 offset;
  163.  
  164. attribute vec4 position;
  165. void main() {
  166.     // offset comes in with x/y values between 0 and 1.
  167.     // position bounds are -1 to 1.
  168.     vec4 offset4 = vec4(2.0*offset.x-1.0, 1.0-2.0*offset.y, 0, 0);
  169.     gl_Position = position + offset4;
  170. }`
  171.  
  172. const fragmentShader = `#version 100
  173. precision mediump float;
  174. uniform vec4 color;
  175. void main() {
  176.     gl_FragColor = color;
  177. }`
  178.  
  179. /*
  180. package hello
  181.  
  182. import "fmt"
  183.  
  184. func Greetings(name string) string {
  185.     return fmt.Sprintf("Hello there, %s!", name)
  186. }
  187.  
  188. func Sum(a int, b int) int {
  189.     return a + b
  190. }
  191. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement