craftworkgames / MonoGame.Extended

Extensions to make MonoGame more awesome
http://www.monogameextended.net/
Other
1.42k stars 324 forks source link

Sprite atlas Content Importer #62

Closed Felsir closed 8 years ago

Felsir commented 9 years ago

As briefly discussed here: http://gamedev.stackexchange.com/questions/108613/monogame-3-4-content-pipeline-extension-missing-reference/108616?noredirect=1#comment189059_108616

Basically a content importer that reads png files, places them into an atlas and makes the assets easy retrievable in the game code. The process works like this:

  1. Create a bunch of individual .png images that should end up in the game.
  2. Store the images in a directory.
  3. Create an xml file: the xml file contains path(s) to the directories containing the images. The xml file also contains a width and height for the atlas image (the "bin" size so to speak).
  4. The xml file is added to the content tool, with the spriteatlas importer.

The code contains a dictionary <string,spriteinfo> spriteinfo contains a struct like this:

struct SpriteInfo
{
    Texture2D Spritesheet;
    Rectangle SpriteRectangle;
}

In the code you could now draw sprites like this:

spriteBatch.Draw(Atlas("spritename").Spritesheet,new Vector2(x,y),Atlas("spritename").SpriteRectangle,Color.White);

The content processor uses Texture2D.FromFile to read the png files. The SetData<Color> method is used to pack sprites in their bins according to the packing algorithm (as I expected the draw methods not to be available during the content building process- I may be wrong though). the writer writes the textures and the dictionary to the XNB file.

This decouples the creation of the images from the atlas creation. I used this in my game "80s Racer"

https://www.youtube.com/watch?v=46-Mb2CMopw

The level editor references the same .png images for roadside objects. The content is then created and the game reads the level files and is able to pick the images from the atlas content without having to worry where the images were located.

craftworkgames commented 9 years ago

Thanks @Felsir. Cool looking game too.

WardBenjamin commented 9 years ago

@Felsir you beat me to the anwer :P

First of all, nice answer. It would have beaten mine in the amount of detail. I basically said "you need to import this Nuget package. See Dylan's website". Second of all, nice game and nice Pipeline extension. I like it.

craftworkgames commented 9 years ago

@Felsir you beat me to the freaking answer you scrub :P

Funny story about that. When I first looked at the question on stack it didn't have any answers. I must've been writing my answer at the same time as @Felsir. He submitted his answer just a few minutes before mine and it got upvoted (I assume by @WardBenjamin). Small world :)

WardBenjamin commented 9 years ago

Ouch. I come across as a total jerk... not quite what I meant.

Anyway, quite a small world, definitely. I was also writing my own answer with very similar content, but ended up deleting it when Felsir's popped up. Huh.

Felsir commented 9 years ago

@WardBenjamin nah, it is all a funny coincidence and weird timing :-)

The racegame is currently "on hold" I learned a few new techniques so it's up for a rewrite. In it's current state it works but it's not very modular. Happy you like it :-)

Back to the topic of Atlasses: another importer I'm looking to build is one for this (free) editor: http://darkfunction.com/editor/ it is really simple for sprite animations. I did some test animations with it and it works quite well. It moves the atlas creation to the tool; but it provides a complete sprite animation solution. It is less complex than Spriter Pro (or Spine) so may provide a solution for many entry level game developers. What do you think?

craftworkgames commented 9 years ago

What do you think?

I think that's a great idea. Having support for a few different free and commercial tools is a really great thing. The only downside is that we have to maintain the importers if the file format changes.

When I have time to take a closer look at the tool I'll add it to my list of content creation tools.

@Felsir can you raise a new issue for this? Something like "Support for darkFunction Editor" with relevant links and description. When we are done we should also remember to contact the developer and ask him for a link on his import libraries page