Пример #1
0
func main() {
	app.Main(func(a app.App) {
		var c event.Config
		var eng *WritableEngine
		var root *sprite.Node
		startClock := time.Now()
		for e := range a.Events() {
			switch e := event.Filter(e).(type) {
			case event.Config:
				c = e
			case event.Draw:
				if eng == nil || root == nil {
					eng = NewWritableEngine(
						glsprite.Engine(),
						image.Rect(0, 0, int(c.Width.Px(c.PixelsPerPt)), int(c.Height.Px(c.PixelsPerPt))),
						color.White,
					)
					root = loadScene(eng, loadTextures(eng))
					go listen(eng, ":8080")
				}
				now := clock.Time(time.Since(startClock) * 60 / time.Second)
				gl.ClearColor(1, 1, 1, 1)
				gl.Clear(gl.COLOR_BUFFER_BIT)
				gl.Enable(gl.BLEND)
				gl.BlendEquation(gl.FUNC_ADD)
				gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
				if eng != nil && root != nil {
					eng.Render(root, now, c)
				}
				a.EndDraw()
			}
		}
	})
}
Пример #2
0
func loadScene(c event.Config) {
	gl.Enable(gl.BLEND)
	gl.BlendEquation(gl.FUNC_ADD)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	w, h := float32(c.Width), float32(c.Height)

	texs = loadTextures()
	scene = &sprite.Node{}
	eng.Register(scene)
	a := f32.Affine{
		{1, 0, 0},
		{0, 1, 0},
	}

	if h > w {
		w, h = h, w
		angle := float32(-math.Pi / 2)
		a = f32.Affine{
			{f32.Cos(angle), -f32.Sin(angle), 0},
			{f32.Sin(angle), f32.Cos(angle), w},
		}
	}
	scale = w / 1100
	eng.SetTransform(scene, a)
	x0 = w/2 - 500*scale
	y0 = h/2 - 100*scale
	//log.Printf("width:%f height:%f scale:%f x0:%f y0:%f", w, h, scale, x0, y0)

	timeNode := newTimeNode()
	scene.AppendChild(timeNode)

	startNode := newStartNode()
	scene.AppendChild(startNode)

	endNode := newEndNode()
	scene.AppendChild(endNode)

	startVisilbe, endVisible := false, false
	scene.Arranger = arrangerFunc(func(eng sprite.Engine, n *sprite.Node, t clock.Time) {
		switch state {
		case stateStart:
			if !startVisilbe {
				startVisilbe = true
				eng.SetSubTex(startNode, texs["GO"])
			}

			if endVisible {
				eng.SetSubTex(endNode, sprite.SubTex{})
				endVisible = false
			}
		case stateRunning:
			if startVisilbe {
				eng.SetSubTex(startNode, sprite.SubTex{})
				startVisilbe = false
			}
		case stateEnd:
			if !endVisible {
				endVisible = true
				eng.SetSubTex(endNode, texs["gooon"])
			}
		}
	})
}