Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 3
0
func (m *TextMapping) Set(r rune, key string) (err error) {
	var (
		sprite *sprites.Sprite
	)
	if sprite, err = m.sheet.Sprite(key); err != nil {
		return
	}
	m.mapping[r] = sprite.Index()
	return
}
Exemplo n.º 4
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

}