Loading textureatlases

Hepp, figured it out =)

Someone in a year be like https://xkcd.com/979/


Haha, theres really an xkcd for everything.

It always amazes me how people find the right xkcd for the post. :slightly_smiling:

I’m I missing something here - Hepp, figured what out? What is the problem, what is the solution you figured out?

Haha, well maybe it’s a good idea to share what i found on how to draw sprites from an atlas.

I looked through the source and search around for somehat similar problems i encounterd and basically this is what you need to do:
call atlas to get a textureregion2d, call that one with Bounds to get a rectangle as source, crate a destination rectangle with that regions sizes, draw it with the regions texture like so:

TextureRegion2D region = atlas.GetRegion(regionid); Rectangle sourceRectangle = region.Bounds; Rectangle destinationRectangle = new Rectangle(position, region.Width, region.Height); spriteBatch.Draw(region.Texture, destinationRectangle, sourceRectangle, Color.White);

Before all this you have to create a atlas from texturepacker and load it of course.
The documentation on MGE is till a bit unfinished.

1 Like

There’s an extension method for TextureRegion2D so you can just call it directly. No need to mess around with getting the regions rectangle:

var region = atlas.GetRegion("some-region") spriteBatch.Draw(region, new Vector2(x, y), Color.White);

where x,y is the location where you want to draw your texture. Sure, if you need to resize it you’ll need to get the rectangle and do a source/destination drawing but you can also scale the drawing or something (depends on what you’re drawing).



Straight to the point I think…

Yeah these posts get me like… Was there supposed to be something here lol

How do you search xkcd anyway?

Also how to err image control?

I was trying to keep it ‘family friendly’ lol, there are young ones on here too you know.

Well, that is handy to know.

The more you know =D