feat: added the grid to the cmd/test.

This commit is contained in:
Andrey Parhomenko 2024-01-24 19:14:07 +03:00
parent 86ae2fbde9
commit e78a0d9881
2 changed files with 77 additions and 0 deletions

3
bg.go Normal file
View file

@ -0,0 +1,3 @@
package gg

View file

@ -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)