Files

179 lines
4.7 KiB
Go
Raw Permalink Normal View History

// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2021-02-19 18:54:49 -05:00
//go:build darwin || linux || windows
2014-10-29 09:19:38 +11:00
// An app that draws a green triangle on a red background.
//
2021-08-30 22:04:01 +02:00
// In order to build this program as an Android APK, using the gomobile tool.
//
// See http://godoc.org/github.com/netbirdio/gomobile-tvos-fork/cmd/gomobile to install gomobile.
//
// Get the basic example and use gomobile to build or install it on your device.
//
// $ go get -d github.com/netbirdio/gomobile-tvos-fork/example/basic
// $ gomobile build github.com/netbirdio/gomobile-tvos-fork/example/basic # will build an APK
//
2022-04-11 13:11:08 -04:00
// # plug your Android device to your computer or start an Android emulator.
// # if you have adb installed on your machine, use gomobile install to
// # build and deploy the APK to an Android target.
// $ gomobile install github.com/netbirdio/gomobile-tvos-fork/example/basic
//
// Switch to your device or emulator to start the Basic application from
// the launcher.
// You can also run the application on your desktop by running the command
// below. (Note: It currently doesn't work on Windows.)
2022-04-11 13:11:08 -04:00
//
// $ go install github.com/netbirdio/gomobile-tvos-fork/example/basic && basic
package main
import (
"encoding/binary"
"log"
"github.com/netbirdio/gomobile-tvos-fork/app"
"github.com/netbirdio/gomobile-tvos-fork/event/lifecycle"
"github.com/netbirdio/gomobile-tvos-fork/event/paint"
"github.com/netbirdio/gomobile-tvos-fork/event/size"
"github.com/netbirdio/gomobile-tvos-fork/event/touch"
"github.com/netbirdio/gomobile-tvos-fork/exp/app/debug"
"github.com/netbirdio/gomobile-tvos-fork/exp/f32"
"github.com/netbirdio/gomobile-tvos-fork/exp/gl/glutil"
"github.com/netbirdio/gomobile-tvos-fork/gl"
)
var (
2015-09-22 17:29:00 -04:00
images *glutil.Images
fps *debug.FPS
program gl.Program
position gl.Attrib
offset gl.Uniform
color gl.Uniform
buf gl.Buffer
green float32
touchX float32
touchY float32
)
func main() {
2015-07-15 18:24:07 -04:00
app.Main(func(a app.App) {
2015-08-08 10:18:15 -04:00
var glctx gl.Context
var sz size.Event
2015-07-15 18:24:07 -04:00
for e := range a.Events() {
switch e := a.Filter(e).(type) {
2015-07-15 18:24:07 -04:00
case lifecycle.Event:
switch e.Crosses(lifecycle.StageVisible) {
case lifecycle.CrossOn:
2015-08-08 10:18:15 -04:00
glctx, _ = e.DrawContext.(gl.Context)
onStart(glctx)
a.Send(paint.Event{})
2015-07-15 18:24:07 -04:00
case lifecycle.CrossOff:
2015-09-24 15:27:48 +10:00
onStop(glctx)
glctx = nil
2015-07-15 18:24:07 -04:00
}
2015-08-13 19:59:18 +10:00
case size.Event:
sz = e
touchX = float32(sz.WidthPx / 2)
touchY = float32(sz.HeightPx / 2)
2015-07-15 18:24:07 -04:00
case paint.Event:
if glctx == nil || e.External {
// As we are actively painting as fast as
// we can (usually 60 FPS), skip any paint
// events sent by the system.
continue
}
2015-08-08 10:18:15 -04:00
onPaint(glctx, sz)
2015-09-04 17:13:09 +10:00
a.Publish()
// Drive the animation by preparing to paint the next frame
// after this one is shown.
a.Send(paint.Event{})
2015-07-15 18:24:07 -04:00
case touch.Event:
touchX = e.X
touchY = e.Y
2015-07-15 18:24:07 -04:00
}
}
})
}
2015-08-08 10:18:15 -04:00
func onStart(glctx gl.Context) {
var err error
2015-08-08 10:18:15 -04:00
program, err = glutil.CreateProgram(glctx, vertexShader, fragmentShader)
if err != nil {
log.Printf("error creating GL program: %v", err)
return
}
2015-08-08 10:18:15 -04:00
buf = glctx.CreateBuffer()
glctx.BindBuffer(gl.ARRAY_BUFFER, buf)
glctx.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW)
2015-08-08 10:18:15 -04:00
position = glctx.GetAttribLocation(program, "position")
color = glctx.GetUniformLocation(program, "color")
offset = glctx.GetUniformLocation(program, "offset")
2014-12-19 10:28:05 -05:00
2015-08-08 10:18:15 -04:00
images = glutil.NewImages(glctx)
2015-09-22 17:29:00 -04:00
fps = debug.NewFPS(images)
2014-12-19 10:28:05 -05:00
}
2015-08-08 10:18:15 -04:00
func onStop(glctx gl.Context) {
glctx.DeleteProgram(program)
glctx.DeleteBuffer(buf)
2015-09-22 17:29:00 -04:00
fps.Release()
images.Release()
}
2015-08-08 10:18:15 -04:00
func onPaint(glctx gl.Context, sz size.Event) {
glctx.ClearColor(1, 0, 0, 1)
glctx.Clear(gl.COLOR_BUFFER_BIT)
2015-08-08 10:18:15 -04:00
glctx.UseProgram(program)
green += 0.01
if green > 1 {
green = 0
}
2015-08-08 10:18:15 -04:00
glctx.Uniform4f(color, 0, green, 0, 1)
2015-08-08 10:18:15 -04:00
glctx.Uniform2f(offset, touchX/float32(sz.WidthPx), touchY/float32(sz.HeightPx))
2015-08-08 10:18:15 -04:00
glctx.BindBuffer(gl.ARRAY_BUFFER, buf)
glctx.EnableVertexAttribArray(position)
glctx.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0)
glctx.DrawArrays(gl.TRIANGLES, 0, vertexCount)
glctx.DisableVertexAttribArray(position)
2014-09-29 05:17:34 +10:00
2015-09-22 17:29:00 -04:00
fps.Draw(sz)
}
2014-10-29 09:19:38 +11:00
var triangleData = f32.Bytes(binary.LittleEndian,
0.0, 0.4, 0.0, // top left
0.0, 0.0, 0.0, // bottom left
0.4, 0.0, 0.0, // bottom right
)
2014-10-29 09:19:38 +11:00
const (
coordsPerVertex = 3
vertexCount = 3
)
const vertexShader = `#version 100
uniform vec2 offset;
attribute vec4 position;
void main() {
// offset comes in with x/y values between 0 and 1.
// position bounds are -1 to 1.
vec4 offset4 = vec4(2.0*offset.x-1.0, 1.0-2.0*offset.y, 0, 0);
gl_Position = position + offset4;
}`
const fragmentShader = `#version 100
precision mediump float;
uniform vec4 color;
void main() {
gl_FragColor = color;
}`