added an example for a gomobile (gomobile build) implementation

This commit is contained in:
Thomas Friedel 2018-10-31 12:43:05 +01:00
parent a01a6799f1
commit 4debf145d2
2 changed files with 60 additions and 1 deletions

View file

@ -22,7 +22,7 @@ The sdlcanvas and glfwcanvas subpackages provide a very simple way to get starte
- Android
- iOS
Unfortunately using full Go apps using gomobile doesn't work since gomobile does not seem to create a GL view with a stencil buffer, and the canvas package makes heavy use of the stencil buffer. Therefore the ```gomobile bind``` command has to be used together with platform specific projects.
Using gomobile to build a full Go app using ```gomobile build``` now works by using an offscreen texture to render to and then rendering that to the screen. See the example in examples/gomobile. The offscreen texture is necessary since gomobile automatically creates a GL context without a stencil buffer, which this library requires.
# Example

View file

@ -0,0 +1,59 @@
package main
import (
"math"
"time"
"github.com/tfriedel6/canvas"
"github.com/tfriedel6/canvas/glimpl/xmobile"
"golang.org/x/mobile/app"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
"golang.org/x/mobile/gl"
)
func main() {
app.Main(func(a app.App) {
var cv, painter *canvas.Canvas
var w, h int
var glctx gl.Context
for e := range a.Events() {
switch e := a.Filter(e).(type) {
case lifecycle.Event:
switch e.Crosses(lifecycle.StageVisible) {
case lifecycle.CrossOn:
glctx, _ = e.DrawContext.(gl.Context)
canvas.LoadGL(glimplxmobile.New(glctx))
cv = canvas.NewOffscreen(0, 0)
painter = canvas.New(0, 0, 0, 0)
a.Send(paint.Event{})
case lifecycle.CrossOff:
glctx = nil
}
case size.Event:
w, h = e.WidthPx, e.HeightPx
case paint.Event:
if glctx != nil {
glctx.ClearColor(0, 0, 0, 0)
glctx.Clear(gl.COLOR_BUFFER_BIT)
cv.SetBounds(0, 0, w, h)
painter.SetBounds(0, 0, w, h)
fw, fh := float64(w), float64(h)
color := math.Sin(float64(time.Now().UnixNano())*0.000000002)*0.3 + 0.7
cv.SetFillStyle(color*0.2, color*0.2, color*0.8)
cv.FillRect(fw*0.25, fh*0.25, fw*0.5, fh*0.5)
painter.DrawImage(cv)
a.Publish()
a.Send(paint.Event{})
}
}
}
})
}