Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v1.1.0] Incorrectly calculated coordinates #6

Open
Klebestreifen opened this issue Aug 12, 2022 · 0 comments
Open

[v1.1.0] Incorrectly calculated coordinates #6

Klebestreifen opened this issue Aug 12, 2022 · 0 comments

Comments

@Klebestreifen
Copy link

In "packer.imageRectToPixelRect" the coordinates for the sprite are translated incorrectly.

The rectangle from the image library has its origin at the top left while a pixel rectangle has its origin at the bottom left.

I have fixed it for me but am currently too lazy to fork.

But my packar/pack.go looks like this now:

package packer

import (
	"image"

	"github.com/dusk125/rectpack"
	"github.com/faiface/pixel"
)

type Packer struct {
	rectpack.Packer
	pic   pixel.Picture
	batch *pixel.Batch
}

func New() (pack *Packer) {
	pack = &Packer{
		Packer: *rectpack.NewPacker(rectpack.PackerCfg{}),
	}
	return
}

func (pack Packer) imageRectToPixelRect(r image.Rectangle) (pr pixel.Rect) {
	b := pack.pic.Bounds()
	return pixel.R(float64(r.Min.X), b.H() - float64(r.Min.Y), float64(r.Max.X), b.H() - float64(r.Max.Y))
}


func (pack *Packer) Pack() (err error) {
	if err = pack.Packer.Pack(); err != nil {
		return
	}

	pack.pic = pixel.PictureDataFromImage(pack.Packer.Image())
	pack.batch = pixel.NewBatch(&pixel.TrianglesData{}, pack.pic)

	return
}

func (pack *Packer) BoundsOf(id int) pixel.Rect {
	return pack.imageRectToPixelRect(pack.Get(id))
}

func (pack *Packer) Draw(t pixel.Target) {
	pack.batch.Draw(t)
}

func (pack *Packer) DrawSub(id int, m pixel.Matrix) {
	r := pack.imageRectToPixelRect(pack.Get(id))
	s := pixel.NewSprite(pack.pic, r)
	s.Draw(pack.batch, m)
}

func (pack *Packer) Picture() pixel.Picture {
	return pack.pic
}

func (pack *Packer) Bounds() pixel.Rect {
	return pack.pic.Bounds()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant