From bc2b5809c57c15241be171b5c354fc820adb66e9 Mon Sep 17 00:00:00 2001 From: surdeus Date: Tue, 30 May 2023 22:35:49 +0300 Subject: [PATCH] Use Ebitens coordinate system for showing. --- src/cmd/math/main.go | 6 +++--- src/cmd/test/main.go | 18 +++++++++++------- src/gx/camera.go | 20 ++++---------------- src/gx/circle.go | 11 +---------- src/gx/{main.go => engine.go} | 6 +++++- src/gx/rect.go | 26 ++++++++++++-------------- src/gx/transform.go | 4 ++-- 7 files changed, 38 insertions(+), 53 deletions(-) rename src/gx/{main.go => engine.go} (96%) diff --git a/src/cmd/math/main.go b/src/cmd/math/main.go index c538014..9651117 100644 --- a/src/cmd/math/main.go +++ b/src/cmd/math/main.go @@ -37,11 +37,11 @@ func main() { t := gx.Rectangle{ Transform: gx.Transform{ - S: gx.Vector{1, 1}, + S: gx.Vector{100, 200}, P: gx.Point{0, 200}, + RA: gx.Point{0, 0}, + R: 0, }, - W: 100, - H: 200, } points := []gx.Point{ diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index 9cf2f81..0bdfcaf 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -26,9 +26,12 @@ type Rect struct { func NewRect() *Rect { return &Rect{&gx.DrawableRectangle{ Rectangle: gx.Rectangle{ - Transform: gx.T(), - W: 200, - H: 400, + Transform: gx.Transform{ + S: gx.Vector{ + X: 200, + Y: 400, + }, + }, }, Color: gx.Color{ gx.MaxColorV, @@ -85,9 +88,10 @@ func (p *Player) Draw(e *gx.Engine, i *gx.Image) { p.Sprite.Draw(e, i) r := &gx.DrawableRectangle{ Rectangle: gx.Rectangle{ - Transform: p.Transform, - W: 10, - H: 10, + Transform: gx.Transform{ + P: player.P, + S: gx.Vector{10, 10}, + }, }, Color: gx.Color{0, 0, gx.MaxColorV, gx.MaxColorV}, } @@ -97,7 +101,7 @@ func (p *Player) Draw(e *gx.Engine, i *gx.Image) { func (p *Player) Start(e *gx.Engine, v ...any) { fmt.Println("starting") c := e.Camera() - c.RA = gx.V(360, -240) + c.RA = gx.V(360, 240) } func (p *Player) Update(e *gx.Engine) error { diff --git a/src/gx/camera.go b/src/gx/camera.go index 58d070b..8159d4d 100644 --- a/src/gx/camera.go +++ b/src/gx/camera.go @@ -16,22 +16,10 @@ func (c *Camera)RealMatrix( e *Engine, ) Matrix { g := &Matrix{} - - g.Scale( - c.S.X, - c.S.Y, - ) - - g.Translate( - -c.P.X, - c.P.Y, - ) - g.Rotate(-c.R) - - g.Translate( - c.RA.X, - -c.RA.Y, - ) + g.Scale(c.S.X, c.S.Y) + g.Translate(-c.P.X, -c.P.Y) + g.Rotate(c.R) + g.Translate(c.RA.X, c.RA.Y) return *g } diff --git a/src/gx/circle.go b/src/gx/circle.go index 8918435..968980c 100644 --- a/src/gx/circle.go +++ b/src/gx/circle.go @@ -2,16 +2,7 @@ package gx // The structure represents any circles. type Circle struct { - // Position. - P Point - // Radius. - R Float + Transform } -func (c Circle) ColliderSimplify() Rectangle { - return Rectangle{ - W: c.R * 2, - H: c.R * 2, - } -} diff --git a/src/gx/main.go b/src/gx/engine.go similarity index 96% rename from src/gx/main.go rename to src/gx/engine.go index bde8e22..f85892e 100644 --- a/src/gx/main.go +++ b/src/gx/engine.go @@ -5,7 +5,7 @@ import ( "github.com/hajimehoshi/ebiten/v2/inpututil" "github.com/surdeus/godat/src/sparsex" "github.com/surdeus/godat/src/poolx" - //"fmt" + "fmt" "time" ) @@ -56,6 +56,9 @@ func (e *Engine) Keys() []Key { func New( cfg *WindowConfig, ) *Engine { + w := Float(cfg.Width) + h := Float(cfg.Height) + fmt.Println("res:", w, h) return &Engine{ wcfg: cfg, layers: sparsex.New[ @@ -65,6 +68,7 @@ func New( camera: &Camera{ Transform: Transform{ S: Vector{1, 1}, + RA: V(w/2, h/2), }, }, updaters: poolx.New[Updater](), diff --git a/src/gx/rect.go b/src/gx/rect.go index c1caf63..da2b5c8 100644 --- a/src/gx/rect.go +++ b/src/gx/rect.go @@ -12,10 +12,8 @@ type Rectangle struct { // Position of up left corner // and the point to // rotate around(relatively of position, not absolute). + // Scale represent width and height. Transform - // Width and height. - W, H Float - } // The type describes rectangle that can be drawn. @@ -40,10 +38,13 @@ func (r Rectangle) Corners() []Point { // Get 2 triangles that the rectangle consists of. func (r Rectangle) Triangles() Triangles { m := r.Matrix() - p1 := r.P.Apply(&m) - p2 := r.P.Add(Vector{r.W, 0}).Apply(&m) - p3 := r.P.Add(Vector{r.W, -r.H}).Apply(&m) - p4 := r.P.Add(Vector{0, -r.H}).Apply(&m) + + p1 := V(0, 0).Apply(&m) + p2 := V(1, 0).Apply(&m) + p3 := V(1, -1).Apply(&m) + p4 := V(0, -1).Apply(&m) + + //fmt.Println("in:", p1, p2, p3, p4) return Triangles{ Triangle{p1, p2, p3}, @@ -72,9 +73,6 @@ func (r *DrawableRectangle) Draw( img := NewImage(1, 1) img.Set(0, 0, r.Color) - t.S.X *= r.W - t.S.Y *= r.H - m := t.Matrix() rm := e.Camera().RealMatrix(e) @@ -89,21 +87,21 @@ func (r *DrawableRectangle) Draw( // Use the Color as base image if no is provided. - var did bool + //var did bool if r.Images[0] == nil { r.Images[0] = NewImage(1, 1) r.Images[0].Set(0, 0, r.Color) - did = true + //did = true } w, h := r.Images[0].Size() - if !did { + /*if !did { t.S.X /= Float(w) t.S.Y /= Float(h) t.S.X *= r.W t.S.Y *= r.H - } + }*/ rm := e.Camera().RealMatrix(e) diff --git a/src/gx/transform.go b/src/gx/transform.go index a8f51bb..4d212f0 100644 --- a/src/gx/transform.go +++ b/src/gx/transform.go @@ -21,14 +21,14 @@ func T() Transform { } // Returns the GeoM with corresponding -// to the transfrom transformation +// to the transfrom transformation. func (t Transform)Matrix() Matrix { g := &Matrix{} g.Scale(t.S.X, t.S.Y) g.Translate(-t.RA.X, -t.RA.Y) g.Rotate(t.R) - g.Translate(t.P.X, -t.P.Y) + g.Translate(t.P.X, t.P.Y) return *g }