Skip to content

Commit

Permalink
Remove dependency to Ebitengine (#122)
Browse files Browse the repository at this point in the history
* chore: remove dependency to ebitengine

* add .idea to .gitignore

* update examples for bumping up dependencies

* update readme
  • Loading branch information
yohamta authored Feb 22, 2024
1 parent db59a18 commit ba79e83
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 242 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
# VSCode
.vscode

# Goland
.idea

# Mac
.DS_Store

vendor
vendor
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<img src="https://github.com/yohamta/donburi/actions/workflows/test.yaml/badge.svg" /> [![Go Reference](https://pkg.go.dev/badge/github.com/yohamta/donburi.svg)](https://pkg.go.dev/github.com/yohamta/donburi)

Donburi is just another Entity Component System library for Ebitengine inspired by [legion](https://github.com/amethyst/legion).
Donburi is an Entity Component System library for Go / Ebitengine inspired by [legion](https://github.com/amethyst/legion).

It aims to be a feature rich and high performance [ECS](https://en.wikipedia.org/wiki/Entity_component_system) Library.
It aims to be a feature rich and high-performance [ECS](https://en.wikipedia.org/wiki/Entity_component_system) Library.

## Contents

Expand Down Expand Up @@ -34,6 +34,7 @@ It aims to be a feature rich and high performance [ECS](https://en.wikipedia.org
- It avoids reflection for performance.
- Ability to dynamically add or remove components from an entity.
- Type-safe APIs powered by Generics
- Zero dependencies
- Provides [Features](#features) that are common in game dev (e.g., `math`, `transform`, `hieralchy`, `events`, etc) built on top of the ECS architecture.

## Examples
Expand Down
17 changes: 6 additions & 11 deletions ecs/ecs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ecs

import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/yohamta/donburi"
"github.com/yohamta/donburi/filter"
)
Expand Down Expand Up @@ -51,7 +50,8 @@ func (ecs *ECS) AddSystem(s System) *ECS {
return ecs
}

func (ecs *ECS) AddRenderer(l LayerID, r Renderer) *ECS {
// AddRenderer adds a renderer.
func (ecs *ECS) AddRenderer(l LayerID, r any) *ECS {
ecs.getLayer(l).addRenderer(r)
return ecs
}
Expand All @@ -65,17 +65,17 @@ func (ecs *ECS) Update() {
}

// DrawLayer executes all draw systems of the specified layer.
func (ecs *ECS) DrawLayer(l LayerID, screen *ebiten.Image) {
ecs.getLayer(l).draw(ecs, screen)
func (ecs *ECS) DrawLayer(l LayerID, arg any) {
ecs.getLayer(l).draw(ecs, arg)
}

// Draw executes all draw systems.
func (ecs *ECS) Draw(screen *ebiten.Image) {
func (ecs *ECS) Draw(arg any) {
for _, l := range ecs.layers {
if l == nil {
continue
}
l.draw(ecs, screen)
l.draw(ecs, arg)
}
}

Expand Down Expand Up @@ -116,8 +116,3 @@ func (ecs *ECS) getLayer(layerID LayerID) *Layer {
}
return ecs.layers[layerID]
}

func (ecs *ECS) addRenderer(l LayerID, r Renderer) *ECS {
ecs.getLayer(l).addRenderer(r)
return ecs
}
19 changes: 9 additions & 10 deletions ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ecs
import (
"testing"

"github.com/hajimehoshi/ebiten/v2"
"github.com/yohamta/donburi"
"github.com/yohamta/donburi/filter"
)
Expand All @@ -23,7 +22,7 @@ func TestECS(t *testing.T) {

for _, sys := range systems {
ecs.AddSystem(sys.system.Update)
ecs.addRenderer(sys.layer, sys.system.Draw)
ecs.AddRenderer(sys.layer, sys.system.Draw)
}

ecs.Update()
Expand All @@ -48,7 +47,7 @@ func TestECS(t *testing.T) {
}
}

ecs.Draw(ebiten.NewImage(1, 1))
ecs.Draw("test")

drawTests := []struct {
system *testSystem
Expand Down Expand Up @@ -99,10 +98,10 @@ func TestECSLayer(t *testing.T) {

for _, sys := range systems {
ecs.AddSystem(sys.system.Update)
ecs.addRenderer(sys.layer, sys.system.Draw)
ecs.AddRenderer(sys.layer, sys.system.Draw)
}

ecs.DrawLayer(0, ebiten.NewImage(1, 1))
ecs.DrawLayer(0, "test")

if systems[0].system.QueryCountDraw != 1 {
t.Errorf("expected query count draw %d, got %d", 1, systems[0].system.QueryCountDraw)
Expand All @@ -118,9 +117,9 @@ func TestEmptyDefaultLayer(t *testing.T) {

TestLayer := LayerID(1)

ecs.AddRenderer(TestLayer, func(ecs *ECS, image *ebiten.Image) {})
ecs.AddRenderer(TestLayer, func(ecs *ECS, arg string) {})

ecs.Draw(ebiten.NewImage(1, 1))
ecs.Draw("test")
}

var (
Expand All @@ -131,7 +130,7 @@ var (
type testSystem struct {
UpdatedIndex int
DrawedIndex int
DrawImage *ebiten.Image
DrawArg string
UpdateCount int
DrawCount int
Query *donburi.Query
Expand All @@ -150,9 +149,9 @@ func (ts *testSystem) Update(ecs *ECS) {
}
}

func (ts *testSystem) Draw(ecs *ECS, image *ebiten.Image) {
func (ts *testSystem) Draw(ecs *ECS, arg string) {
ts.DrawedIndex = testDrawedIndex
ts.DrawImage = image
ts.DrawArg = arg
ts.DrawCount++

testDrawedIndex++
Expand Down
43 changes: 34 additions & 9 deletions ecs/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,54 @@ package ecs

import (
"fmt"
"reflect"

"github.com/hajimehoshi/ebiten/v2"
"github.com/yohamta/donburi"
)

type Layer struct {
*layer
renderers []Renderer
renderers map[string][]any
}

func newLayer(l *layer) *Layer {
return &Layer{l, []Renderer{}}
return &Layer{l, make(map[string][]any)}
}

func (l *Layer) draw(e *ECS, i *ebiten.Image) {
screen := i
for _, s := range l.renderers {
s(e, screen)
func keyForType(typ reflect.Type) string {
return fmt.Sprintf("%s/%s", typ.PkgPath(), typ.Name())
}

func invoke(fn any, e *ECS, arg any) {
v := reflect.ValueOf(fn)
v.Call([]reflect.Value{reflect.ValueOf(e), reflect.ValueOf(arg)})
}

func (l *Layer) draw(e *ECS, arg any) {
key := keyForType(reflect.TypeOf(arg))
for _, fn := range l.renderers[key] {
invoke(fn, e, arg)
}
}

func (l *Layer) addRenderer(r Renderer) {
l.renderers = append(l.renderers, r)
func (l *Layer) addRenderer(r any) {
// check renderer type is func(*ECS, any)
typ := reflect.TypeOf(r)
if typ.Kind() != reflect.Func {
panic("renderer must be a function")
}
if typ.NumIn() != 2 {
panic("renderer must have 2 arguments")
}
if typ.In(0) != reflect.TypeOf(&ECS{}) {
panic("first argument must be *ECS")
}
if typ.NumOut() != 0 {
panic("renderer must not have return values")
}
// add renderer
key := keyForType(typ.In(1))
l.renderers[key] = append(l.renderers[key], r)
}

var (
Expand Down
9 changes: 4 additions & 5 deletions ecs/system.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package ecs

import (
"github.com/hajimehoshi/ebiten/v2"
)

// UpdateSystem is a system that updates the world.
type System func(ecs *ECS)

// DrawSystem is a system that draws the world.
type Renderer func(ecs *ECS, screen *ebiten.Image)
type RendererWithArg[T Arg] func(ecs *ECS, arg *T)

// Arg is an argument of the renderer.
type Arg interface{}
14 changes: 7 additions & 7 deletions examples/bunnymark/go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module github.com/yohamta/donburi/examples/bunnymark

go 1.19
go 1.21

replace github.com/yohamta/donburi => ../../

require (
github.com/hajimehoshi/ebiten/v2 v2.5.3
github.com/hajimehoshi/ebiten/v2 v2.6.6
github.com/jaypipes/ghw v0.10.0
github.com/yohamta/donburi v0.0.0-00010101000000-000000000000
golang.org/x/image v0.7.0
golang.org/x/image v0.12.0
)

require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/ebitengine/purego v0.3.0 // indirect
github.com/ebitengine/purego v0.6.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
Expand All @@ -23,9 +23,9 @@ require (
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
howett.net/plist v1.0.0 // indirect
)
13 changes: 13 additions & 0 deletions examples/bunnymark/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/ebitengine/purego v0.3.0 h1:BDv9pD98k6AuGNQf3IF41dDppGBOe0F4AofvhFtBXF4=
github.com/ebitengine/purego v0.3.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/ebitengine/purego v0.6.0 h1:Yo9uBc1x+ETQbfEaf6wcBsjrQfCEnh/gaGUg7lguEJY=
github.com/ebitengine/purego v0.6.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b h1:GgabKamyOYguHqHjSkDACcgoPIz3w0Dis/zJ1wyHHHU=
Expand All @@ -15,6 +17,8 @@ github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiU
github.com/hajimehoshi/bitmapfont/v2 v2.2.3 h1:jmq/TMNj352V062Tr5e3hAoipkoxCbY1JWTzor0zNps=
github.com/hajimehoshi/ebiten/v2 v2.5.3 h1:jizHI6ig5YnNP+wyERJvhDabz4lkhJn06bhIgHWJwUo=
github.com/hajimehoshi/ebiten/v2 v2.5.3/go.mod h1:mnHSOVysTr/nUZrN1lBTRqhK4NG+T9NR3JsJP2rCppk=
github.com/hajimehoshi/ebiten/v2 v2.6.6 h1:E5X87Or4VwKZIKjeC9+Vr4ComhZAz9h839myF4Q21kc=
github.com/hajimehoshi/ebiten/v2 v2.6.6/go.mod h1:gKgQI26zfoSb6j5QbrEz2L6nuHMbAYwrsXa5qsGrQKo=
github.com/jaypipes/ghw v0.10.0 h1:UHu9UX08Py315iPojADFPOkmjTsNzHj4g4adsNKKteY=
github.com/jaypipes/ghw v0.10.0/go.mod h1:jeJGbkRB2lL3/gxYzNYzEDETV1ZJ56OKr+CSeSEym+g=
github.com/jaypipes/pcidb v1.0.0 h1:vtZIfkiCUE42oYbJS0TAq9XSfSmcsgo9IdxSm9qzYU8=
Expand All @@ -40,9 +44,13 @@ golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9t
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.7.0 h1:gzS29xtG1J5ybQlv0PuyfE3nmc6R4qB73m6LUUmvFuw=
golang.org/x/image v0.7.0/go.mod h1:nd/q4ef1AKKYl/4kft7g+6UyGbdiqWqTP1ZAbRoV7Rg=
golang.org/x/image v0.12.0 h1:w13vZbU4o5rKOFFR8y7M+c4A5jXDC0uXTdHYRP8X2DQ=
golang.org/x/image v0.12.0/go.mod h1:Lu90jvHG7GfemOIcldsh9A2hS01ocl6oNO7ype5mEnk=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c h1:Gk61ECugwEHL6IiyyNLXNzmu8XslmRP2dS0xjIYhbb4=
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c/go.mod h1:aAjjkJNdrh3PMckS4B10TGS2nag27cbKR1y2BpUxsiY=
golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57 h1:Q6NT8ckDYNcwmi/bmxe+XbiDMXqMRW1xFBtJ+bIpie4=
golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57/go.mod h1:wEyOn6VvNW7tcf+bW/wBz1sehi2s2BZ4TimyR7qZen4=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
Expand All @@ -56,6 +64,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -66,6 +76,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand All @@ -75,6 +87,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand Down
15 changes: 7 additions & 8 deletions examples/bunnymark_ecs/go.mod
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
module github.com/yohamta/donburi/examples/bunnymark_ecs

go 1.19
go 1.21

replace github.com/yohamta/donburi => ../../

require (
github.com/hajimehoshi/ebiten/v2 v2.5.3
github.com/hajimehoshi/ebiten/v2 v2.6.6
github.com/jaypipes/ghw v0.10.0
github.com/yohamta/donburi v0.0.0-00010101000000-000000000000
golang.org/x/image v0.7.0
golang.org/x/image v0.12.0
)

require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/ebitengine/purego v0.3.0 // indirect
github.com/ebitengine/purego v0.6.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/jaypipes/pcidb v1.0.0 // indirect
github.com/jezek/xgb v1.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
howett.net/plist v1.0.0 // indirect
)
Loading

0 comments on commit ba79e83

Please sign in to comment.