1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
// Copyright 2015 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.
//go:build example
// +build example
//
// This build tag means that "go install golang.org/x/exp/shiny/..." doesn't
// install this example program. Use "go run main.go" to run it or "go install
// -tags=example" to install it.
// Tile demonstrates tiling a screen with textures.
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"log"
"sync"
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/screen"
"golang.org/x/image/font"
"golang.org/x/image/font/inconsolata"
"golang.org/x/image/math/fixed"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/mouse"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
)
func main() {
driver.Main(func(s screen.Screen) {
w, err := s.NewWindow(&screen.NewWindowOptions{
Title: "Tile Shiny Example",
})
if err != nil {
log.Fatal(err)
}
defer w.Release()
var (
pool = &tilePool{
screen: s,
drawRGBA: drawRGBA,
m: map[image.Point]*tilePoolEntry{},
}
dragging bool
paintPending bool
drag image.Point
origin image.Point
sz size.Event
)
for {
switch e := w.NextEvent().(type) {
case lifecycle.Event:
if e.To == lifecycle.StageDead {
return
}
case key.Event:
if e.Code == key.CodeEscape {
return
}
case mouse.Event:
p := image.Point{X: int(e.X), Y: int(e.Y)}
if e.Button == mouse.ButtonLeft && e.Direction != mouse.DirNone {
dragging = e.Direction == mouse.DirPress
drag = p
}
if !dragging {
break
}
origin = origin.Sub(p.Sub(drag))
drag = p
if origin.X < 0 {
origin.X = 0
}
if origin.Y < 0 {
origin.Y = 0
}
if !paintPending {
paintPending = true
w.Send(paint.Event{})
}
case paint.Event:
generation++
var wg sync.WaitGroup
for y := -(origin.Y & 0xff); y < sz.HeightPx; y += 256 {
for x := -(origin.X & 0xff); x < sz.WidthPx; x += 256 {
wg.Add(1)
go drawTile(&wg, w, pool, origin, x, y)
}
}
wg.Wait()
w.Publish()
paintPending = false
pool.releaseUnused()
case size.Event:
sz = e
case error:
log.Print(e)
}
}
})
}
func drawTile(wg *sync.WaitGroup, w screen.Window, pool *tilePool, origin image.Point, x, y int) {
defer wg.Done()
tp := image.Point{
(x + origin.X) >> 8,
(y + origin.Y) >> 8,
}
tex, err := pool.get(tp)
if err != nil {
log.Println(err)
return
}
w.Copy(image.Point{x, y}, tex, tileBounds, screen.Src, nil)
}
func drawRGBA(m *image.RGBA, tp image.Point) {
draw.Draw(m, m.Bounds(), image.White, image.Point{}, draw.Src)
for _, p := range crossPoints {
m.SetRGBA(p.X, p.Y, crossColor)
}
d := font.Drawer{
Dst: m,
Src: image.Black,
Face: inconsolata.Regular8x16,
Dot: fixed.Point26_6{
Y: inconsolata.Regular8x16.Metrics().Ascent,
},
}
d.DrawString(fmt.Sprint(tp))
}
var (
crossColor = color.RGBA{0x7f, 0x00, 0x00, 0xff}
crossPoints = []image.Point{
{0x00, 0xfe},
{0x00, 0xff},
{0xfe, 0x00},
{0xff, 0x00},
{0x00, 0x00},
{0x01, 0x00},
{0x02, 0x00},
{0x00, 0x01},
{0x00, 0x02},
{0x80, 0x7f},
{0x7f, 0x80},
{0x80, 0x80},
{0x81, 0x80},
{0x80, 0x81},
{0x80, 0x00},
{0x00, 0x80},
}
generation int
tileSize = image.Point{256, 256}
tileBounds = image.Rectangle{Max: tileSize}
)
type tilePoolEntry struct {
tex screen.Texture
gen int
}
type tilePool struct {
screen screen.Screen
drawRGBA func(*image.RGBA, image.Point)
mu sync.Mutex
m map[image.Point]*tilePoolEntry
}
func (p *tilePool) get(tp image.Point) (screen.Texture, error) {
p.mu.Lock()
v, ok := p.m[tp]
if v != nil {
v.gen = generation
}
p.mu.Unlock()
if ok {
return v.tex, nil
}
tex, err := p.screen.NewTexture(tileSize)
if err != nil {
return nil, err
}
buf, err := p.screen.NewBuffer(tileSize)
if err != nil {
tex.Release()
return nil, err
}
p.drawRGBA(buf.RGBA(), tp)
tex.Upload(image.Point{}, buf, tileBounds)
buf.Release()
p.mu.Lock()
p.m[tp] = &tilePoolEntry{
tex: tex,
gen: generation,
}
n := len(p.m)
p.mu.Unlock()
fmt.Printf("%4d textures; created %v\n", n, tp)
return tex, nil
}
func (p *tilePool) releaseUnused() {
p.mu.Lock()
defer p.mu.Unlock()
for tp, v := range p.m {
if v.gen == generation {
continue
}
v.tex.Release()
delete(p.m, tp)
fmt.Printf("%4d textures; released %v\n", len(p.m), tp)
}
}
|