From e78a0d9881ac94fcda7d26bcb3e5abf9b136af93 Mon Sep 17 00:00:00 2001 From: surdeus Date: Wed, 24 Jan 2024 19:14:07 +0300 Subject: [PATCH] feat: added the grid to the cmd/test. --- bg.go | 3 ++ cmd/test/main.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 bg.go diff --git a/bg.go b/bg.go new file mode 100644 index 0000000..b2a357b --- /dev/null +++ b/bg.go @@ -0,0 +1,3 @@ +package gg + + diff --git a/cmd/test/main.go b/cmd/test/main.go index e9b2124..5b3bf37 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -12,12 +12,42 @@ import ( type Context = gg.Context +type Grid struct { + gg.Sprite + Cage, Width gg.Float +} + +func (grid *Grid) Update(c *Context) { + //d := c.IsPressed(gg.KeyShift) + for _, ev := range c.Events { switch e := ev.(type){ + case *gg.KeyDown : switch e.Key { + case gg.Key9 : + grid.Cage /= 10 + grid.Width /= 5 + case gg.Key0 : + grid.Cage *= 10 + grid.Width *= 5 + }}} +} + +func (grid *Grid) Draw(c *Context) []gg.EVertex { + scale := c.Camera.Scale() + pos := c.Camera.Position() + grid.Uniforms["Zoom"] = gg.Float(scale.X) + grid.Uniforms["PosX"] = pos.X + grid.Uniforms["PosY"] = pos.Y + grid.Uniforms["Cage"] = grid.Cage + grid.Uniforms["Width"] = grid.Width + return grid.Sprite.Draw(c) +} + const ( HighestL gg.Layer = -iota DebugL TriangleL PlayerL RectL + BackgroundLayer LowestL ) @@ -76,6 +106,50 @@ func main() { circle.Visible = true*/ //e.Spawn(circle) + bgShader := gg.MustNewShader([]byte(` + package main + + //kage:unit pixels + + //var cameraScaleX, cameraScaleY + var Zoom float + var PosX, PosY float + var Cage float + var Width float + + const ( + ) + + func Fragment(dst vec4, src vec2, _ vec4) vec4 { + c := imageSrc0UnsafeAt(src) + dstSize := imageDstSize()/Zoom + absDst := vec2( + dst.x/Zoom - dstSize.x/2 - PosX, + dst.y/Zoom - dstSize.y/2 - PosY, + ) + cx := c*step(Cage-Width, mod(absDst.x-Width/2, Cage)) + cy := c*step(Cage-Width, mod(absDst.y-Width/2, Cage)) + if cx.a == 0 { + return cy + } + return cx + } + `)) + grid := &Grid{} + grid.Transform = gg.T() + grid.SetAround(gg.V2(0)) + grid.Layer = BackgroundLayer + grid.Images[0] = gg.NewImage(720, 480) + grid.Images[0].Fill(gg.Rgba(1, 1, 1, 1)) + grid.Shader = bgShader + grid.Uniforms = map[string] any{} + //bg.Floating = true + grid.Visible = true + grid.Floating = true + grid.Cage = 100 + grid.Width = 5 + e.Spawn(grid) + txt := &Text{} txt.Transform = gg.T() //txt.Position = gg.V2(400)