A sprite sheet loader for using Free Texture Packer with Monogame
Read in another language : English, Français
There are four main classes to look at :
SpriteFrame
: it represent one image inside of a spritesheetSpriteSheet
: it represent the a collection on spriteframeSpriteSheetLoader
: it is used to load the spritesheet generated from Free Texture PackerSpriteRender
: it draws a sprite to the screen
The other classes are used to store information about a SpriteFrame:
Pivot
stores the pivot point of a frameRect
store the (X,Y) position of an image inside of a sritesheet and that image size (Width, Height)Size
store the size (With, Height) of the image
You must have Monogame installed.
For this example, I'm using Monogame 3.8.1.303
You must also have Free Texture Packer installed
For this example, I'm using Free Texture Packer 0.6.7
You have two ways to install it in your project
-
Use the sources
Simply copy theFreeTexturePackerLoader
folder in your monogame project -
Get IT through Nuget
dotnet add package FreeTexturePacker.Lib --version 1.0.0
-
Open Free Texture Packer
-
Add the images in the
Examples/images
folder -
Set texture name to
chess
-
Set texture format to
png
-
Check the
Remove file ext
box -
For the packer use
OptimalPacker
-
Uncheck
Allow trim
box -
For the format, choose
custom
-
Click the little pencil next to it
-
Paste the following export template :
{ "name":"{{{config.imageName}}}", "frames":[ {{#rects}} { "filename":"{{{name}}}", "frame":{ "X":{{frame.x}}, "Y":{{frame.y}}, "Width":{{frame.w}}, "Height":{{frame.h}} }, "rotated":{{rotated}}, "trimmed":{{trimmed}}, "spriteSourceSize":{ "X":{{spriteSourceSize.x}}, "Y":{{spriteSourceSize.y}}, "Width":{{spriteSourceSize.w}}, "Height":{{spriteSourceSize.h}} }, "sourceSize":{ "Width":{{sourceSize.w}}, "Height":{{sourceSize.h}} }, "pivot":{ "X":0.5, "Y":0.5 } }{{^last}},{{/last}} {{/rects}} ], "meta":{ "app":"{{{appInfo.url}}}", "version":"{{{appInfo.version}}}", "image":"{{{config.imageName}}}", "format":"{{{config.format}}}", "size":{ "Width":{{config.imageWidth}}, "Height":{{config.imageHeight}} }, "scale":{{config.scale}} } }
-
Check
Allow trim
andAllow rotation
boxes -
For file extension, write
json
-
Click
Save
-
Click
Export
-
Locate the Content directory of your monogame project and click
Select Folder
It'll generate two files: chess.json
and chess.png
.
The first contains informations about the spritesheet such as the posistion and size all the images inside that spritesheet and the last file is the actual spritesheet.
-
Create a monogame desktop project
-
Copy the
FreeTexturePackerLoader
folder inside of your monogame project or add it through nuget -
At this using line at the top of your Game1.cs file
using FreeTexturePackerLoader;
-
Create an instance of the SpriteSheetLoader in the
LoadContent
methodvar spriteSheetLoader = new SpriteSheetLoader(Content);
-
Create aninstance of the SpriteRenderer
private SpriteRender spriteRender;
In the
LoadContent
method, add :spriteRender = new SpriteRender(_spriteBatch);
-
Load the spritesheet
private SpriteSheet spriteSheet;
In the
LoadContent
method, add :spriteSheet = spriteSheetLoader.Load("chess");
-
Draw a sprite from the spritesheet In the
Draw
method, add :spriteRender.Draw(spriteSheet.GetSprite("reine_blanc"), new Vector2(100, 100), Color.White);
You should see the white queen at the screen
-
Now try to show all the chess piece at the screen.
You can find the full project in the
Example/ChessBoard Final
Please read CODE_OF_CONDUCT.md for details on our code of conduct, and the process for submitting pull requests to us.
- Sesso Kosga - Initial work - senor16
This project is licensed under the MIT License - see the licence.md file for details