Skip to content

Conversation

@fisherevans
Copy link

text.NewAtlas does the work of rendering a font onto an Atlas sprite to optimize rendering. But this prevents the text package from drawing onto a canvas using a shared Atlas.

  • Atlas.PictureDataCopy allows you to make a copy of the atlas image, which you can then draw onto another shared atlas.
  • Atlas.CloneWithPictureData allows you to make a new text.Atlas, referencing a shared atlas.

With both of these functions, you can draw text to a pixel.Batch, sharing an atlas with other fonts, sprites, or any other picture data.

Usage would look something like this:

// normal text atlas
textOnlyAtlas := text.Atlas7x13
textAtlasImg := textOnlyAtlas.PictureDataCopy().Image()

// draw the text atlas to a large, shared picture atlas
x, y := 100, 700
textAtlasPlacement := image.Rect(x, y, x+textAtlasImg.Bounds().Dx(), y+textAtlasImg.Bounds().Dx())
sharedAtlasImage := image.NewRGBA(image.Rect(0, 0, 1000, 1000))
draw.Draw(sharedAtlasImage, textAtlasPlacement, textAtlasImg, sharedAtlasImage.Bounds().Min, draw.Over)
sharedAtlasPiture := pixel.PictureDataFromImage(sharedAtlasImage)
// ... add more sprites to the shared atlas

// create a new text drawer with the shared atlas
pixelPlacement := convertFromImageToPixelRect(textAtlasPlacement)
sharedTextAtlas := textOnlyAtlas.CloneWithPictureData(sharedAtlasPiture, pixelPlacement)
textDrawer := text.New(pixel.ZV, sharedTextAtlas)

// draw to a batch
batch := pixel.NewBatch(nil, sharedAtlasPiture)
textDrawer.WriteString("Hello, world!")
textDrawer.Draw(batch, pixel.IM)
// ... draw other things to the batch that aren't text, that use the same source atlas picture

batch.Draw(canvas)

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

Successfully merging this pull request may close these issues.

1 participant