From d3784903965ae58508e6b293921cc94c7e3418ce Mon Sep 17 00:00:00 2001 From: surdeus Date: Mon, 8 Jan 2024 10:22:09 +0300 Subject: [PATCH] feat: fixed camera matrix bufferization. --- camera.go | 13 +++++++------ cmd/test/main.go | 2 ++ cmd/test/trianlge.go | 9 +++++++++ engine.go | 45 ++++++++++++++++++++++++++------------------ go.mod | 3 ++- go.sum | 2 ++ 6 files changed, 49 insertions(+), 25 deletions(-) diff --git a/camera.go b/camera.go index 855b018..bb98d9f 100644 --- a/camera.go +++ b/camera.go @@ -7,7 +7,8 @@ type Camera struct { // that the camera shows. ShaderOptions Transform - buf *Matrix + buffered bool + buf Matrix engine *Engine } @@ -24,20 +25,20 @@ func (e *Engine) NewCamera() *Camera { // transform to display on the screen. func (c *Camera)RealMatrix() Matrix { // Bufferization - if c.buf != nil { - return *(c.buf) + if c.buffered { + return c.buf } - g := &Matrix{} + g := Matrix{} g.Translate(-c.Position.X, -c.Position.Y) g.Rotate(c.Rotation) siz := c.engine.AbsWinSize() g.Translate(c.Around.X * siz.X, c.Around.Y * siz.Y) g.Scale(c.Scale.X, c.Scale.Y) - c.buf = g + c.buffered = true - return *g + return g } // The matrix to convert things into the diff --git a/cmd/test/main.go b/cmd/test/main.go index 855b25e..d2c8777 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -3,6 +3,7 @@ package main import ( "vultras.su/core/gg" "github.com/hajimehoshi/ebiten/v2/examples/resources/images" + _ "github.com/silbinarywolf/preferdiscretegpu" "bytes" "log" //"strings" @@ -34,6 +35,7 @@ func main() { Width: 720, Height: 480, VSync: true, + Fullscreen: true, }) var err error diff --git a/cmd/test/trianlge.go b/cmd/test/trianlge.go index 3bb7cce..f1a33f8 100644 --- a/cmd/test/trianlge.go +++ b/cmd/test/trianlge.go @@ -63,6 +63,15 @@ func (t *Tri) Update(c *Context) { case gg.KeyV: t.Rotation += d * gg.Pi * 0.3 * dt case gg.Key2 : + if t.Spawned { + break + } + t.Triangles = append(t.Triangles, gg.Triangle{ + gg.V(0, 10 + gg.Float(counter)), + gg.V(100 + gg.Float(counter), 0), + gg.V(0, -10 - gg.Float(counter)), + }) + case gg.Key3 : if t.Spawned { break } diff --git a/engine.go b/engine.go index 0ddcb7e..187b186 100644 --- a/engine.go +++ b/engine.go @@ -40,6 +40,10 @@ type WindowConfig struct { VSync bool } +type Objects struct { + store []Objecter +} + // The main structure that represents current state of [game] engine. type Engine struct { wcfg *WindowConfig @@ -47,7 +51,7 @@ type Engine struct { // The main holder for objects. // Uses the map structure to quickly // delete and create new objects. - Objects maps.Map[Objecter, struct{}] + Objects *Objects // The main camera to display in window. // If is set to nil then the engine will panic. @@ -103,16 +107,22 @@ func NewEngine( ret.Camera = ret.NewCamera() ret.outerEvents = make(EventChan) ret.handleEvents = make(EventChan) - ret.Objects = maps.NewOrdered[Objecter, struct{}]() + ret.Objects = &Objects{} ret.buttons = MouseButtonMap{} return ret } // Get the real window size in the current context. func (c *Engine) RealWinSize() Vector { + var w, h int + if c.wcfg.Fullscreen { + w, h = ebiten.ScreenSizeInFullscreen() + } else { + w, h = c.wcfg.Width, c.wcfg.Height + } return V( - Float(c.wcfg.Width), - Float(c.wcfg.Height), + Float(w), + Float(h), ) } @@ -127,35 +137,32 @@ func (e *Engine) EventInput() EventChan { // Add new object considering what // interfaces it implements. func (e *Engine) Spawn(b Objecter) error { - if e.Objects.Has(b) { + /*if e.Objects.Has(b) { return ObjectExistErr - } + }*/ b.Start(&Context{Engine: e}) obj := b.GetObject() obj.input = make(chan *Context) go func() { for c := range obj.input { - switch c.typ { - case updateContext: - b.Update(c) - } + b.Update(c) e.wg.Done() } }() - e.Objects.Set(b, struct{}{}) + e.Objects.store = append(e.Objects.store, b) return nil } // Delete object from Engine. func (e *Engine) Del(b Objecter) error { - if !e.Objects.Has(b) { + /*if !e.Objects.Has(b) { return ObjectNotExistErr } b.Delete(&Context{Engine: e}) - e.Objects.Del(b) + e.Objects.Del(b)*/ return nil } @@ -206,7 +213,6 @@ func (e *Engine) AbsCursorPosition() Vector { func (e *engine) Update() error { eng := (*Engine)(e) - e.dt = time.Since(e.lastTime).Seconds() // Buffering the context for faster. e.prevKeys = e.keys @@ -274,20 +280,20 @@ func (e *engine) Update() error { typ: updateContext, Events: events, } - for object := range e.Objects.KeyChan() { + for _, object := range e.Objects.store { e.wg.Add(1) object.Input() <- c } e.wg.Wait() - e.lastTime = time.Now() return nil } func (e *engine) Draw(i *ebiten.Image) { + e.dt = time.Since(e.lastTime).Seconds() eng := (*Engine)(e) m := map[Layer][]Drawer{} - for object := range eng.Objects.KeyChan() { + for _, object := range eng.Objects.store { // Skipping the ones we do not need to draw. if !object.IsVisible() { continue @@ -311,8 +317,10 @@ func (e *engine) Draw(i *ebiten.Image) { drawer.Draw(c) } } + // Empty the buff to generate it again. - eng.Camera.buf = nil + eng.Camera.buffered = false + e.lastTime = time.Now() } func (e *engine) Layout(ow, oh int) (int, int) { @@ -341,6 +349,7 @@ func (e *Engine) Run() error { ebiten.SetWindowSize(e.wcfg.Width, e.wcfg.Height) ebiten.SetWindowSizeLimits(1, 1, e.wcfg.Width, e.wcfg.Height) + ebiten.SetFullscreen(e.wcfg.Fullscreen) ebiten.SetVsyncEnabled(e.wcfg.VSync) e.lastTime = time.Now() diff --git a/go.mod b/go.mod index 401e400..9110616 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,8 @@ toolchain go1.21.3 require ( github.com/di4f/gods v0.0.0-20231214190239-d523423d8d5e github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b + github.com/silbinarywolf/preferdiscretegpu v1.0.0 + golang.org/x/image v0.7.0 ) require ( @@ -14,7 +16,6 @@ require ( github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect github.com/jezek/xgb v1.1.0 // indirect golang.org/x/exp/shiny v0.0.0-20230522175609-2e198f4a06a1 // indirect - golang.org/x/image v0.7.0 // indirect golang.org/x/mobile v0.0.0-20230427221453-e8d11dd0ba41 // indirect golang.org/x/sync v0.2.0 // indirect golang.org/x/sys v0.8.0 // indirect diff --git a/go.sum b/go.sum index 7e087e5..2916f1f 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b h1 github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b/go.mod h1:+fFI6Ag5YvbX1ivNQD2TxNhpWFDPuxEoew421TTQAxI= github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk= github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= +github.com/silbinarywolf/preferdiscretegpu v1.0.0 h1:tuvXLRCnoFMFyk74/8PFvO0B5rjDmXm0JgNTaOYAHT0= +github.com/silbinarywolf/preferdiscretegpu v1.0.0/go.mod h1:h3s2GkfAP2sWqoS7v/PxAlFOQ1azMRsZxUJNw47QhLc= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=