From 37b0f0956b03cfec9111dc3ebb8130817133c128 Mon Sep 17 00:00:00 2001 From: faiface Date: Fri, 14 Apr 2017 16:03:07 +0200 Subject: [PATCH] add code for part 05 of the guide --- .../05_drawing_efficiently_with_batch/main.go | 111 ++++++++++++++++++ .../trees.png | Bin 0 -> 2541 bytes 2 files changed, 111 insertions(+) create mode 100644 examples/guide/05_drawing_efficiently_with_batch/main.go create mode 100644 examples/guide/05_drawing_efficiently_with_batch/trees.png diff --git a/examples/guide/05_drawing_efficiently_with_batch/main.go b/examples/guide/05_drawing_efficiently_with_batch/main.go new file mode 100644 index 0000000..ff913d0 --- /dev/null +++ b/examples/guide/05_drawing_efficiently_with_batch/main.go @@ -0,0 +1,111 @@ +package main + +import ( + "fmt" + "image" + "math" + "math/rand" + "os" + "time" + + _ "image/png" + + "github.com/faiface/pixel" + "github.com/faiface/pixel/pixelgl" + "golang.org/x/image/colornames" +) + +func loadPicture(path string) (pixel.Picture, error) { + file, err := os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + img, _, err := image.Decode(file) + if err != nil { + return nil, err + } + return pixel.PictureDataFromImage(img), nil +} + +func run() { + cfg := pixelgl.WindowConfig{ + Title: "Pixel Rocks!", + Bounds: pixel.R(0, 0, 1024, 768), + } + win, err := pixelgl.NewWindow(cfg) + if err != nil { + panic(err) + } + + spritesheet, err := loadPicture("trees.png") + if err != nil { + panic(err) + } + + batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet) + + var treesFrames []pixel.Rect + for x := spritesheet.Bounds().Min.X(); x < spritesheet.Bounds().Max.X(); x += 32 { + for y := spritesheet.Bounds().Min.Y(); y < spritesheet.Bounds().Max.Y(); y += 32 { + treesFrames = append(treesFrames, pixel.R(x, y, x+32, y+32)) + } + } + + var ( + camPos = pixel.V(0, 0) + camSpeed = 500.0 + camZoom = 1.0 + camZoomSpeed = 1.2 + ) + + var ( + frames = 0 + second = time.Tick(time.Second) + ) + + last := time.Now() + for !win.Closed() { + dt := time.Since(last).Seconds() + last = time.Now() + + cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center() - camPos) + win.SetMatrix(cam) + + if win.Pressed(pixelgl.MouseButtonLeft) { + tree := pixel.NewSprite(spritesheet, treesFrames[rand.Intn(len(treesFrames))]) + mouse := cam.Unproject(win.MousePosition()) + tree.SetMatrix(pixel.IM.Scaled(0, 4).Moved(mouse)) + tree.Draw(batch) + } + if win.Pressed(pixelgl.KeyLeft) { + camPos -= pixel.X(camSpeed * dt) + } + if win.Pressed(pixelgl.KeyRight) { + camPos += pixel.X(camSpeed * dt) + } + if win.Pressed(pixelgl.KeyDown) { + camPos -= pixel.Y(camSpeed * dt) + } + if win.Pressed(pixelgl.KeyUp) { + camPos += pixel.Y(camSpeed * dt) + } + camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y()) + + win.Clear(colornames.Forestgreen) + batch.Draw(win) + win.Update() + + frames++ + select { + case <-second: + win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames)) + frames = 0 + default: + } + } +} + +func main() { + pixelgl.Run(run) +} diff --git a/examples/guide/05_drawing_efficiently_with_batch/trees.png b/examples/guide/05_drawing_efficiently_with_batch/trees.png new file mode 100644 index 0000000000000000000000000000000000000000..73a6beca00db1b82bf2443eb116873b0414229b9 GIT binary patch literal 2541 zcmV{+P)}rLG?7$+}9Gnqa{J_F5@Bu_bq$?tRX+L9bu=k9Q zkE_Qs{g^h3OB%^umW70? zmr3>Tc3odrFO&K@n4k9!#KHW$h~7qdzCWv8pYD>Az`!d32lMl4I2%>N*{D}AhO<$1 z@i^Tq0-SR$H30w|9>>7r0s(z_YHhwk0W)1?`hOYf4rKHXKXPj_BrJ_5Ruz@RetY%p>H6NA5rw{%r-@7>GOr5V8E z%AvQGb>Q>gr6d5MhrQkkNx6Xl;JcTn)y3melaYl0fXU!< zf`r!{%}?sjc|jISz`^|78*YZP(MpcsDXZp!;F*x&Y!pO*Z^sk&*KmVs>EZ2qWtAFgKawo& zgM`bV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