Esempio n. 1
0
func NewTextMapping(sheet *sprites.Sheet, defaultSprite string) (out *TextMapping, err error) {
	var (
		sprite *sprites.Sprite
	)
	out = &TextMapping{
		sheet:   sheet,
		mapping: map[rune]int{},
	}
	if sprite, err = sheet.Sprite(defaultSprite); err != nil {
		return
	}
	out.defaultSprite = sprite.Index()
	return

}
Esempio n. 2
0
func main() {
	flag.Parse()

	const (
		WinTitle              = "uniform-texture-coords"
		WinWidth              = 640
		WinHeight             = 480
		PixelsPerUnit float32 = 100
	)

	var (
		context         *core.Context
		sheet           *sprites.Sheet
		camera          *core.Camera
		framerate       *util.Framerate
		font            *text.FontFace
		fg              = color.RGBA{255, 255, 255, 255}
		bg              = color.RGBA{0, 0, 0, 255}
		err             error
		inst            *render.Instance
		rot             int = 0
		textMapping     *loaders.TextMapping
		batchData       *render.Geometry
		renderer        *render.Renderer
		spriteInstances *sprites.SpriteInstanceList
		textInstances   *text.TextInstanceList
		batchInstances  *render.InstanceList
		square          *render.Geometry
	)
	if context, err = core.NewContext(); err != nil {
		panic(err)
	}
	if err = context.CreateWindow(WinWidth, WinHeight, WinTitle); err != nil {
		panic(err)
	}
	if renderer, err = render.NewRenderer(100); err != nil {
		panic(err)
	}

	if sheet, err = loaders.NewTexturePackerLoader().Load(
		"src/resources/spritesheet.json",
		core.SmoothingNearest,
	); err != nil {
		panic(err)
	}
	if textMapping, err = loaders.NewTextMapping(
		sheet,
		"numbered_squares_03",
	); err != nil {
		panic(err)
	}
	textMapping.Set('A', "numbered_squares_01")
	textMapping.Set('B', "numbered_squares_tall_16")
	if batchData, err = loaders.NewTextLoader().Load(
		textMapping,
		1,
		BATCH,
	); err != nil {
		panic(err)
	}

	spriteInstances = sprites.NewSpriteInstanceList(sheet, PixelsPerUnit)
	batchInstances = render.NewInstanceList()

	batchInstances.NewInstance()

	square = render.NewGeometryFromPoints(render.Square)

	textInstances = text.NewTextInstanceList(text.Config{
		TextureWidth:  512,
		TextureHeight: 512,
		PixelsPerUnit: PixelsPerUnit,
	})
	if framerate, err = util.NewFramerateRenderer(); err != nil {
		panic(err)
	}
	if camera, err = context.Camera(mgl32.Vec3{0, 0, 0}, mgl32.Vec3{6.4, 4.8, 2}); err != nil {
		panic(err)
	}
	if font, err = text.NewFontFace("src/resources/Roboto-Light.ttf", 24, fg, bg); err != nil {
		panic(err)
	}
	for _, s := range []Inst{
		Inst{Key: "This is text!", X: 0, Y: -1.0, R: 0},
		Inst{Key: "More text!", X: 1.0, Y: 1.0, R: 15},
	} {
		inst = textInstances.NewInstance()
		if err = textInstances.SetText(inst, s.Key, font); err != nil {
			panic(err)
		}
		inst.SetPosition(mgl32.Vec3{s.X, s.Y, 0})
		inst.SetRotation(s.R)
	}
	for _, s := range []Inst{
		Inst{Key: "numbered_squares_01", X: 0, Y: 0, R: 0},
		Inst{Key: "numbered_squares_02", X: -1.5, Y: -1.5, R: -15},
		Inst{Key: "numbered_squares_03", X: -2.0, Y: -2.0, R: -30},
	} {
		inst = spriteInstances.NewInstance()
		if err = spriteInstances.SetFrame(inst, s.Key); err != nil {
			panic(err)
		}
		inst.SetPosition(mgl32.Vec3{s.X, s.Y, 0})
		inst.SetRotation(s.R)
	}

	// fmt.Printf("Sheet: %v\n", sprites.Tiles)
	// fmt.Printf("BatchData: %v\n", batchData)
	// fmt.Printf("Framerate: %v\n", framerate)
	// fmt.Printf("TextLoader: %v\n", textLoader)
	// fmt.Printf("Font: %v\n", font)

	for !context.ShouldClose() {
		context.Events.Poll()
		context.Clear()

		renderer.Bind()
		sheet.Bind()

		renderer.Render(camera, sheet, batchData, batchInstances)
		renderer.Render(camera, sheet, square, spriteInstances)

		textInstances.Bind()
		renderer.Render(camera, textInstances.Sheet(), square, textInstances)
		textInstances.Unbind()

		renderer.Unbind()

		framerate.Bind()
		framerate.Render(camera)
		framerate.Unbind()

		context.SwapBuffers()

		if err = textInstances.SetText(
			textInstances.Head(),
			fmt.Sprintf("Rotation %v", rot%100),
			font,
		); err != nil {
			fmt.Printf("ERROR: %v\n", err)
			break
		}
		inst.SetRotation(float32(rot))
		rot += 1
	}
	if err = core.WritePNG("test-packed.png", textInstances.Sheet().Image()); err != nil {
		panic(err)
	}
	textInstances.Delete()
	glog.Flush()
}