Пример #1
0
func (l *TextInstanceList) repackImage() (err error) {
	var (
		newImage *sprites.PackedSheet
		instance *render.Instance
		sprite   *sprites.Sprite
	)
	if glog.V(1) {
		glog.Info("Repacking image")
	}
	newImage = sprites.NewPackedSheet(
		l.sheet.Width,
		l.sheet.Height,
	)
	instance = l.Head()
	for instance != nil {
		if err = newImage.Copy(instance.Key, l.sheet); err != nil {
			return
		}
		if sprite, err = newImage.Sheet.Sprite(instance.Key); err != nil {
			return
		}
		instance.Frame = sprite.Index()
		instance.MarkChanged()
		instance = instance.Next()
	}
	l.sheet = newImage
	if err = l.generateTexture(); err != nil {
		return
	}
	if glog.V(1) {
		glog.Info("Done repacking")
	}
	return
}
Пример #2
0
func (l *TextInstanceList) SetText(instance *render.Instance, text string, font *FontFace) (err error) {
	var (
		img    draw.Image
		sprite *sprites.Sprite
	)
	if instance == nil {
		return // No error.
	}
	if img, err = font.GetImage(text); err != nil {
		return
	}
	if err = l.sheet.Pack(text, img); err != nil {
		// Attempt to compact the texture.
		if err = l.repackImage(); err != nil {
			return
		}
		if err = l.sheet.Pack(text, img); err != nil {
			return
		}
	}
	if sprite, err = l.sheet.Sprite(text); err != nil {
		return
	}
	instance.Frame = sprite.Index()
	instance.SetScale(sprite.WorldDimensions(l.cfg.PixelsPerUnit).Vec3(1.0))
	instance.MarkChanged()
	instance.Key = text
	if err = l.generateTexture(); err != nil {
		return
	}
	return
}
Пример #3
0
func (l *SpriteInstanceList) SetFrame(instance *render.Instance, frame string) (err error) {
	var s *Sprite
	if instance == nil {
		return // No error
	}
	if s, err = l.sheet.Sprite(frame); err != nil {
		return
	}
	instance.Frame = s.Index()
	instance.SetScale(s.WorldDimensions(l.pixelsPerUnit).Vec3(1.0))
	instance.MarkChanged()
	instance.Key = frame
	return
}
Пример #4
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()
}