Implemented basic layer support by sparsex.

This commit is contained in:
Andrey Parhomenko 2023-02-17 18:40:46 +05:00
parent eb79fdeedc
commit c9f8027f81
5 changed files with 56 additions and 7 deletions

3
go.mod
View file

@ -8,7 +8,8 @@ require (
github.com/hajimehoshi/ebiten/v2 v2.4.16 // indirect github.com/hajimehoshi/ebiten/v2 v2.4.16 // indirect
github.com/hajimehoshi/file2byteslice v1.0.0 // indirect github.com/hajimehoshi/file2byteslice v1.0.0 // indirect
github.com/jezek/xgb v1.1.0 // indirect github.com/jezek/xgb v1.1.0 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect github.com/surdeus/godat v0.0.0-20230217130825-eddf2c345cb7 // indirect
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb // indirect
golang.org/x/exp/shiny v0.0.0-20230213192124-5e25df0256eb // indirect golang.org/x/exp/shiny v0.0.0-20230213192124-5e25df0256eb // indirect
golang.org/x/image v0.5.0 // indirect golang.org/x/image v0.5.0 // indirect
golang.org/x/mobile v0.0.0-20221110043201-43a038452099 // indirect golang.org/x/mobile v0.0.0-20221110043201-43a038452099 // indirect

4
go.sum
View file

@ -21,6 +21,8 @@ github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFl
github.com/jfreymuth/oggvorbis v1.0.4/go.mod h1:1U4pqWmghcoVsCJJ4fRBKv9peUJMBHixthRlBeD6uII= github.com/jfreymuth/oggvorbis v1.0.4/go.mod h1:1U4pqWmghcoVsCJJ4fRBKv9peUJMBHixthRlBeD6uII=
github.com/jfreymuth/vorbis v1.0.2/go.mod h1:DoftRo4AznKnShRl1GxiTFCseHr4zR9BN3TWXyuzrqQ= github.com/jfreymuth/vorbis v1.0.2/go.mod h1:DoftRo4AznKnShRl1GxiTFCseHr4zR9BN3TWXyuzrqQ=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/surdeus/godat v0.0.0-20230217130825-eddf2c345cb7 h1:Fmoxhb42mS6BEmLF2spRiYlzes+S1VrEw0PnbR1ktUM=
github.com/surdeus/godat v0.0.0-20230217130825-eddf2c345cb7/go.mod h1:tQGz8oe6Qig5yjYobaW1O8paXGGhzdukg8nT2bpvfes=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 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-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@ -30,6 +32,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 h1:estk1glOnSVeJ9tdEZZc5mAMDZk5lNJNyJ6DvrBkTEU= golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 h1:estk1glOnSVeJ9tdEZZc5mAMDZk5lNJNyJ6DvrBkTEU=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4= golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb h1:PaBZQdo+iSDyHT053FjUCgZQ/9uqVwPOcl7KSWhKn6w=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp/shiny v0.0.0-20230213192124-5e25df0256eb h1:gdeQX7xJSkTNF+Sw7++XNIOo4pGL0CjQv3N2Vm1Erxk= golang.org/x/exp/shiny v0.0.0-20230213192124-5e25df0256eb h1:gdeQX7xJSkTNF+Sw7++XNIOo4pGL0CjQv3N2Vm1Erxk=
golang.org/x/exp/shiny v0.0.0-20230213192124-5e25df0256eb/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0= golang.org/x/exp/shiny v0.0.0-20230213192124-5e25df0256eb/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=

View file

@ -4,6 +4,10 @@ import (
"github.com/surdeus/gox/src/gx" "github.com/surdeus/gox/src/gx"
) )
type Player struct {
gx.Object
}
func main() { func main() {
e := gx.New(&gx.WindowConfig{ e := gx.New(&gx.WindowConfig{
Title: "Test title", Title: "Test title",
@ -11,5 +15,6 @@ func main() {
Height: 320, Height: 320,
}) })
e.Add(0, Player{})
e.Run() e.Run()
} }

View file

@ -2,8 +2,13 @@ package gx
import ( import (
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/surdeus/godat/src/sparsex"
"fmt"
) )
// The type represents order of drawing.
type Layer int
type WindowConfig struct { type WindowConfig struct {
Title string Title string
Width, Height int Width, Height int
@ -11,6 +16,8 @@ type WindowConfig struct {
type Engine struct { type Engine struct {
wcfg *WindowConfig wcfg *WindowConfig
layers *sparsex.Sparse[Layer, *[]Behaver]
behavers []Behaver
} }
type engine Engine type engine Engine
@ -20,13 +27,39 @@ func New(
) *Engine { ) *Engine {
return &Engine{ return &Engine{
wcfg: cfg, wcfg: cfg,
layers: sparsex.New[
Layer,
*[]Behaver,
](true),
} }
} }
func (e *Engine) Add(l Layer, b Behaver) {
g, ok := e.layers.Get(l)
if !ok {
e.layers.Set(
l,
&[]Behaver{b},
)
} else {
set := append(*g, b)
*g = set
}
e.behavers = append(e.behavers, b)
}
func (e *engine) Update() error { func (e *engine) Update() error {
eng := (*Engine)(e)
for _, v := range eng.behavers {
v.Update(eng)
fmt.Println(v)
}
return nil return nil
} }
func (e *engine) Draw(s *ebiten.Image) { func (e *engine) Draw(s *ebiten.Image) {
} }

View file

@ -1,11 +1,16 @@
package gx package gx
type Behaver interface { type Behaver interface {
Start() Start(*Engine)
Update() Update(*Engine)
GetObject() *Object GetObject() *Object
} }
// The object that is drawn by sprite
/// based on the transform.
type Sprite struct {
}
// The object type represents // The object type represents
// basic information for interaction // basic information for interaction
// with the engine. // with the engine.
@ -13,9 +18,10 @@ type Object struct {
T Transform T Transform
} }
func (o *Object) Start() {} // The functions that
func (o *Object) Update() {} func (o Object) Start(e *Engine) {}
func (o *Object) GetObject() *Object { func (o Object) Update(e *Engine) {}
return o func (o Object) GetObject() *Object {
return &o
} }