Exemplo n.º 1
0
func TestSubsystemConcurrency(t *testing.T) {
	const N = 10
	numc := make(chan int)
	sysA := sysFunc(func(ctx *scene.Context) {
		count := 0
		for ctx.Step() {
			count++
			numc <- count
		}
	})
	sysB := sysFunc(func(ctx *scene.Context) {
		count := 0
		for ctx.Step() {
			count = <-numc
		}
		if count != N {
			panic("mismatch")
		}
	})
	world, err := scene.New(sysA, sysB)
	if err != nil {
		t.Fatal(err)
	}
	for i := 0; i < N; i++ {
		world.Step()
	}
	world.Close()
}
Exemplo n.º 2
0
func TestCycleOrder(t *testing.T) {
	type clockColumn struct {
		scene.Column
		clock [3]int
	}
	sysA := sysFunc(func(ctx *scene.Context) {
		ctx.Declare(clockColumn{
			Column: scene.DumbColumn(0),
		})
		var c clockColumn
		ctx.Require(&c)
		for ctx.Step() {
			c.clock[0]++
			switch {
			case c.clock[0] != c.clock[1]+1,
				c.clock[0] != c.clock[2]+1:
				panic("system out of stage")
			}
		}
	})
	sysB := sysFunc(func(ctx *scene.Context) {
		var c clockColumn
		ctx.Stage(1)
		ctx.Require(&c)
		for ctx.Step() {
			c.clock[1]++
			switch {
			case c.clock[1] != c.clock[0],
				c.clock[1] != c.clock[2]+1:
				panic("system out of stage")
			}
		}
	})
	sysC := sysFunc(func(ctx *scene.Context) {
		var c clockColumn
		ctx.Stage(2)
		ctx.Require(&c)
		for ctx.Step() {
			c.clock[2]++
			switch {
			case c.clock[2] != c.clock[0],
				c.clock[2] != c.clock[1]:
				panic("system out of stage")
			}
		}
	})
	world, err := scene.New(sysA, sysB, sysC)
	if err != nil {
		t.Fatal(err)
	}
	for i := 0; i < 10; i++ {
		world.Step()
	}
	world.Close()
}
Exemplo n.º 3
0
// Open a new window using the specified environment.
func Open(env Environment) (*Window, error) {
	viewport := &graphics.Viewport{}
	world, err := scene.New(&graphics.System{
		Context:  env.Graphics(),
		Viewport: viewport,
	}, &controlsys{})
	if err != nil {
		return nil, err
	}
	w := &Window{
		world:    world,
		logics:   map[reflect.Type]void{},
		viewport: viewport,
	}
	w.init()
	go w.run(env)
	return w, nil
}