From af8949725a00e60331cab5ac26c43e5fa7901a1d Mon Sep 17 00:00:00 2001 From: surdeus Date: Sat, 18 Feb 2023 11:11:09 +0500 Subject: [PATCH] Implemented the example for testing the engine. --- src/cmd/test/main.go | 54 ++++++++++++++++++++++++++++++++++++++------ src/gix/main.go | 3 ++- src/gx/camera.go | 13 +++++++++-- src/gx/debug.go | 12 ++++++++++ src/gx/main.go | 15 ++++++++---- src/gx/object.go | 6 +++-- src/gx/transform.go | 2 +- 7 files changed, 87 insertions(+), 18 deletions(-) create mode 100644 src/gx/debug.go diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index f67221b..5716ff4 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -3,14 +3,19 @@ package main import ( "github.com/surdeus/gox/src/gx" "github.com/hajimehoshi/ebiten/v2/examples/resources/images" + "github.com/hajimehoshi/ebiten/v2" "bytes" "log" + "strings" ) type Player struct { *gx.Sprite + MoveSpeed gx.Float } +type Debug struct{} + var ( playerImg *gx.Image ) @@ -27,23 +32,57 @@ func NewPlayer() *Player { }, Image: playerImg, }, + MoveSpeed: 90., } } -func (p *Player) Update(e *gx.Engine) { - //p.Sprite.Object.T.P.Y += 40 * e.DT() +func (p *Player) Update(e *gx.Engine) error { dt := e.DT() c := e.Camera() - //c.Object.T.P.X += 40 * dt - //c.Object.T.S.X += .01 * dt - c.Object.T.R += .2 * dt + keys := e.Keys() + + for _, v := range keys {switch v { + case ebiten.KeyArrowUp : + c.Object.T.P.Y += p.MoveSpeed * dt + case ebiten.KeyArrowLeft : + c.Object.T.P.X -= p.MoveSpeed * dt + case ebiten.KeyArrowDown : + c.Object.T.P.Y -= p.MoveSpeed * dt + case ebiten.KeyArrowRight : + c.Object.T.P.X += p.MoveSpeed * dt + case ebiten.KeyW : + p.Object.T.P.Y += p.MoveSpeed * dt + case ebiten.KeyA : + p.Object.T.P.X -= p.MoveSpeed * dt + case ebiten.KeyS : + p.Object.T.P.Y -= p.MoveSpeed * dt + case ebiten.KeyD : + p.Object.T.P.X += p.MoveSpeed * dt + case ebiten.KeyR : + c.Object.T.R += gx.Pi * .3 * dt + case ebiten.KeyT : + c.Object.T.R -= gx.Pi * .3 * dt + }} + + return nil +} + +func (d *Debug) Draw( + e *gx.Engine, + i *gx.Image, +) { + keyStrs := []string{} + for _, k := range e.Keys() { + keyStrs = append(keyStrs, k.String()) + } + e.DebugPrint(i, strings.Join(keyStrs, ", ")) } func main() { e := gx.New(&gx.WindowConfig{ Title: "Test title", - Width: 480, - Height: 320, + Width: 720, + Height: 480, }) var err error @@ -54,5 +93,6 @@ func main() { e.Add(0, NewPlayer()) + e.Add(1, &Debug{}) e.Run() } diff --git a/src/gix/main.go b/src/gix/main.go index 27f1319..fecf8b8 100644 --- a/src/gix/main.go +++ b/src/gix/main.go @@ -1,5 +1,6 @@ package gix import ( - "github.com/hajimehoshi/ebiten/v2/inpututil" ) + + diff --git a/src/gx/camera.go b/src/gx/camera.go index 9c98a13..1957af5 100644 --- a/src/gx/camera.go +++ b/src/gx/camera.go @@ -1,11 +1,19 @@ package gx +import ( + "math" +) + // Implements the camera component // for the main window. type Camera struct { *Object } +const ( + Pi = math.Pi +) + // Returns the matrix satysfying camera // position, scale and rotation to apply // it to the objects to get the real @@ -13,16 +21,17 @@ type Camera struct { func (c *Camera)Matrix(scale bool) Matrix { g := &Matrix{} + // For rotating around. g.Translate( -c.Object.T.RA.X, - -c.Object.T.RA.Y, + c.Object.T.RA.Y, ) g.Rotate(c.Object.T.R) g.Translate( -c.Object.T.P.X, - -c.Object.T.P.Y, + c.Object.T.P.Y, ) if scale { diff --git a/src/gx/debug.go b/src/gx/debug.go new file mode 100644 index 0000000..6bd41d9 --- /dev/null +++ b/src/gx/debug.go @@ -0,0 +1,12 @@ +package gx + +import ( + "github.com/hajimehoshi/ebiten/v2/ebitenutil" +) + +func (e *Engine)DebugPrint( + i *Image, + str string, +) { + ebitenutil.DebugPrint(i, str) +} diff --git a/src/gx/main.go b/src/gx/main.go index 0c16086..393d380 100644 --- a/src/gx/main.go +++ b/src/gx/main.go @@ -14,7 +14,7 @@ type Layer int type WindowConfig struct { Title string Width, Height int - FullScreen bool + FixedSize bool } type Engine struct { @@ -90,6 +90,7 @@ func (e *Engine) AddBehaver(b Behaver) { } func (e *engine) Update() error { + var err error eng := (*Engine)(e) e.keys = inpututil. @@ -97,15 +98,16 @@ func (e *engine) Update() error { e.dt = time.Since(e.lastTime).Seconds() for _, v := range eng.behavers { - v.Update(eng) - //fmt.Println(v) + err = v.Update(eng) + if err != nil { + return err + } } e.lastTime = time.Now() return nil } - func (e *engine) Draw(i *ebiten.Image) { eng := (*Engine)(e) for p := range e.layers.Vals() { @@ -116,7 +118,10 @@ func (e *engine) Draw(i *ebiten.Image) { } func (e *engine) Layout(ow, oh int) (int, int) { - return e.wcfg.Width, e.wcfg.Height + if e.wcfg.FixedSize { + return e.wcfg.Width, e.wcfg.Height + } + return ow, oh } // Return the delta time duration value. diff --git a/src/gx/object.go b/src/gx/object.go index 1db9b37..0f5d472 100644 --- a/src/gx/object.go +++ b/src/gx/object.go @@ -2,7 +2,7 @@ package gx type Behaver interface { Start(*Engine) - Update(*Engine) + Update(*Engine) error } // The object type represents @@ -13,7 +13,9 @@ type Object struct { } func (o *Object) Start(e *Engine) {} -func (o *Object) Update(e *Engine) {} +func (o *Object) Update(e *Engine) error { + return nil +} func (o *Object) GetObject() *Object { return o } diff --git a/src/gx/transform.go b/src/gx/transform.go index dd0b9e5..d9419f3 100644 --- a/src/gx/transform.go +++ b/src/gx/transform.go @@ -31,7 +31,7 @@ func (t Transform)Matrix() 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 }