From b74ab945da4a6e2a658ff54f911cf1e44303160c Mon Sep 17 00:00:00 2001 From: surdeus Date: Tue, 20 Jun 2023 14:04:55 +0300 Subject: [PATCH] Implemented polygons. --- src/cmd/test/main.go | 14 +++++++++++--- src/gx/polygon.go | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index 6bfedba..4f77ac3 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -24,12 +24,13 @@ type Rect struct { } type Tri struct { - *gx.DrawableTriangles + *gx.DrawablePolygon } func NewTri() *Tri { ret := &Tri{} - ret.DrawableTriangles = &gx.DrawableTriangles{} + ret.DrawablePolygon = &gx.DrawablePolygon{} + ret.Transform.S = gx.V(1, 1) ret.Triangles = gx.Triangles{ gx.Triangle{ @@ -88,6 +89,7 @@ var ( player *Player rectMove gx.Rectangle rect *Rect + tri *Tri ) func NewPlayer() *Player { @@ -190,6 +192,12 @@ func (p *Player) Update(e *gx.Engine) error { } else { c.RA.Y += gx.Pi * p.MoveSpeed * dt } + case ebiten.KeyV : + if e.KeyIsPressed(ebiten.KeyShift) { + tri.R -= gx.Pi * 0.3 * dt + } else { + tri.R += gx.Pi * 0.3 * dt + } case ebiten.KeyLeftBracket : if e.KeyIsPressed(ebiten.KeyShift) { rect.R -= gx.Pi * 0.3 * dt @@ -241,7 +249,7 @@ func main() { player = NewPlayer() rect = NewRect() - tri := NewTri() + tri = NewTri() e.Add(1, &Debug{}) e.Add(0, player) diff --git a/src/gx/polygon.go b/src/gx/polygon.go index 344d3f5..21d78e4 100644 --- a/src/gx/polygon.go +++ b/src/gx/polygon.go @@ -1,29 +1,52 @@ package gx +import ( +) + type PolygonTriangle struct { - P, S int + T, S int } +// Grouped triangles type. type Polygon struct { Transform - base Triangle - triangles []PolygonTriangle + Triangles } +// Polygon that can be drawn. type DrawablePolygon struct { Polygon + ShaderOptions + Visibility + Colority } -func NewPolygon(base Triangle) *Polygon { - ret := &Polygon{ - Transform: T(), - base: base, +func (p *Polygon) MakeTriangles() Triangles { + mv := p.Matrix() + m := &mv + + ret := make(Triangles, len(p.Triangles)) + for i, t := range p.Triangles { + ret[i] = Triangle{ + t[0].Apply(m), + t[1].Apply(m), + t[2].Apply(m), + } } + return ret } -func (p *Polygon) Triangles() Triangles { - return Triangles{} +func (p *DrawablePolygon) Draw( + e *Engine, + i *Image, +) { + (&DrawableTriangles{ + Visibility: p.Visibility, + Colority: p.Colority, + ShaderOptions: p.ShaderOptions, + Triangles: p.MakeTriangles(), + }).Draw(e, i) }