In cocos2d-x, what is difference between atlas and sprite-sheet? - cocos2d-x

I'm new to cocos2d-x game development engine
but I don't understand what is the difference between Atlas and sprite-sheet.
are they the same thing or not?

Atlas is like a generic object if you want to render fast a grouped of images from a single image file like, bitmap labels, spritesheet, multi-image particle, etc.
SpriteSheet is one of those objects that uses functionality of Atlas to render its images fast.

Related

How to make two or more different font files use a common Atlas in libgdx?

I use different fonts for different screens. some of the fonts are only few letters, so it would be inefficient to generate an atlas for each font.So is there a way to make all of my fonts use a single Atlas?
You can use a constructor that takes a TextureRegion. For example, if your font and image were named myFont.fnt and myFont.png, you can put myFont.png in with the rest of your sprite source images and pack it into the texture atlas. Put the .fnt files in with the rest of your assets. Then after loading the texture atlas:
myFont = new BitmapFont(Gdx.files.internal("myFont.fnt"), myTextureAtlas.findRegion("myFont"));
To use it in a Skin, you'll want to add it to a Skin before loading the Json file:
skin = new Skin(); // instantiate blank skin
skin.add(myFont, "myFont");
skin.load(Gdx.files.internal("mySkin.json"));
The Json file can reference the font by the name you use when adding it to the skin.
Although, I'd highly advise using AssetManager for everything. In this case, your skin Json could define the font normally. The only extra step you need beyond loading everything to the asset manager the usual way would be to add a BitmapFontLoader.BitmapFontParameter that specifies the TextureAtlas that contains the region. The AssetManager will use this to determine that the BitmapFont is dependent on the atlas, so it will load them in the correct order.
BitmapFontLoader.BitmapFontParameter fontParam = new BitmapFontLoader.BitmapFontParameter(){{
atlasName = "mySkin.pack";
}};
assetManager.load("myFont.fnt", BitmapFont.class, fontParam);
The atlas should be the same one the skin uses.

How to set a texture filter

How important is it to set a texture filter?
In the book Java Game Development with LibGDX in chapter 3 they set a texture filter.
When I load video assets with the assetmanager I can't convert a textureregion to a texture to set the texture filter.
But I can however set a texture filter on the entire spritesheet like so:
textureAtlas = assetManager.get("images/packed/game.pack.atlas") // all images are found in this global static variable
textureAtlas!!.findRegion("button").texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
How important is it to set a texture filter? Is this an ok solution? How can I get the textures from the atlas?
Textures always have a filter. If you don't set one, it will have the default filter of (Nearest, Nearest). This filter is appropriate for retro graphics (pixellated look). Otherwise, you'll most likely want to use (MipMapLinearLinear, Linear). If your game is mostly done and you've identified sprite drawing as a performance bottle-neck, then you can downgrade to (MipMapLinearNearest, Linear).
When creating an atlas using the TexturePacker, there is an option for texture filter, and if you set that you don't have to set it after you load the TextureAtlas in your game. You could also add a line at the top of your pack file like this:
filter: MipMapLinearLinear,Linear
Otherwise, if you want to set it on the atlas, it is fine with a single-page atlas to do what you did, and apply the filter using a texture reference from any of the texture regions, since they are all referencing the same Texture instance. But TextureAtlases can have multiple pages, so it would be more appropriate to do this:
for (Texture texture : textureAtlas.getTextures())
texture.setFilter(...);
Edit: To add settings to a TexturePacker build, put a text file named pack.json in the directory with the source images. You only have to add the settings that you want to change from the defaults. LibGDX can read simplified json that omits quotation marks for elements with no whitespace. So to just set the texture filter, this is all you need in the file:
{
filterMin: MipMapLinearLinear,
filterMag: Linear
}

libgdx TextureAtlas, and NinePatch's

I am making a simple game using libgdx. I have a TextureAtlas that has I ninepatch I am trying to use:
The image is saved as menu.9.png
I am using the following code:
Image bg = new Image(Room.iAtlas.findRegion("GUI/menu"));
bg.setBounds(guix-border,guiy-border,(border+radius)*2,(border+radius)*2);
batch.begin();
bg.draw(s,1);
batch.end();
The output is like this:
I really just have no idea what I am doing wrong, but it should be more like this(Except it would have the shapes on top of it, but I didn't add those):
(I created that by hand, i've never actually had 9patch working, and it doesn't have the ships because I didn't bother to edit those in)
It looks like your "nine patch" isn't being treated as a real nine path, and is being treated as a "degenerate" nine patch (I had a very similar problem earlier: Loading nine-patch image as a Libgdx Scene2d Button background looks awful, though I wasn't using a TextureAtlas which is supposed to be the solution.)
Basically, when Libgdx reads the nine-patch out of your atlas, its supposed to read all the meta-data that describes how to chop the image up into 9 tiles (see https://code.google.com/p/libgdx/wiki/TexturePacker#NinePatches). I see a couple places this could go wrong:
Your texture isn't a valid nine-patch, and the meta-data is being ignored. (Check with the Android draw9patch tool.)
Your texture packer isn't processing the .9.png file correctly. Check the contents of the .txt file for your atlas, and see if it has "split" entries associated with the "menu.9.png" entry.
The texture lookup is just returning a regular TextureRegion wrapper for the nine-patch region, and isn't wrapping it in a NinePatch object. Try using TextureRegion.createNinePatch to make that more explicit. (I'm under the impression that this isn't necessary, but maybe it is ...)

Spritesheet with mask or separate image files?

I'm making an animation of a 2D character that can walk, run, jump, bend,...
Would it be better to load one big 'spritesheet' with all the animations and just use a mask, or would loading separate files (walk, run,...) be better because you're not using a mask on such a big image every frame?
I'm not using the Stage3D features with a framework like Starling because I think the normal flash display API is fast enough and has much less bugs than the relatively new GPU frameworks.
Blitting just the character (using lock(),copyPixels(),unlock()) works pretty well.
private function updatePixels():void{
//update sprite sheet copy position based on the frame placements ons prite sheet
position.x = spriteSourceData[currentFrame].x + offset.x;
position.y = spriteSourceData[currentFrame].y + offset.y;
//draw into the bitmap displayed
displayData.lock();
displayData.fillRect(displayData.rect, 0x00FFFFFF);//clear
displayData.copyPixels(sourceData, spriteData[currentFrame], position);//copy new frame pixels
displayData.unlock();
}
//a bit about vars:
position:Point
spriteSourceData:Vector.<Rectangle> - from parsed Texture Packer data
offset:Point - front view and side view animations weren't always centred, so an offset was needed
displayData:BitmapData - pluging into a Bitmap object displayed
sourceData:BitmapData - the large sprite sheet
currentFrame:int - image index on the sprite sheet
I've done this on an older project writing a custom class loosely following what I've learned from Lee Brimelow's tutorial series Sprite Sheets and Blitting (Part 1,Part 2, Part 3)
In short, you'd use two BitmapData objects:
a large sprite sheet
a small image to display just the character (size of the largest character bounding box) to copy pixels into
In my project I had a character with front and side animations and for the sides I've used one set of animations and used the Matrix class to flip(scale and translate) the side animation accordingly. I've used TexturePacker to export the image sequence as a sprite sheet and the frame data as well as a JSON object. There is native JSON support now, so that's handy. Texture Packer isn't free but it's really worth the money (affordable, fast and does the job perfectly). I haven't used Flash CS6 yet but I imagine it's also possible to import your image sequence and export a spritesheet with the new feature.
In my experience the rule "the simpler the display list the better the performance" generally applies. Which means you should use the most specific display object that will do the job (don't use a Sprite when a Shape would be sufficient or favor Bitmaps over vectors where it makes sense).
The most extreme version of this is to only have one Bitmap display object on the stage and use copyPixels to draw all game objects into it every time you want to update the screen. It doesn't really matter what the source in the copyPixel call is, it could either be a large BitmapData acting as a sprite sheet or a small BitmapData objects representing a single frame in an animation. This method is really fast and you could easily have many hundreds of objects on screen at the same time. But using copyPixels means you can't scale or rotate a game object, so for those cases you would have to fall back to the much slower draw() method. Of course this single Bitmap method is not suitable for games where you need to attach mouse events to specfic objects in the game, but works well for shoot'em ups or platform games.
To answer your question, I think you will get better performance by using a single Bitmap display object to represent the player and a collection of BitmapData objects for all the animation frames. Then you can just change the Bitmap's bitmapData property to the frame you want to display. You can still load a large spritesheet png and then plit it up into a series of BitmapData objects during the initialization of the game.

AS3 - Using sprite sheets to optimize my games

So, I'm trying to make my flash "games" run more smoothly. I am using individual PNG files for each of my objects in order to create player animations.
I've heard from some places that using individual files like that is bad.
I heard about using sprite sheets in order to compress data and reduce memory usage.
Maybe I have it wrong, but is there a way to merge all of my PNG images (with transparency) together in such a way that flash can continue to use the images individually?
I am really looking for ways to make my programs run more smoothly in order to be able to have lots of images on screen without much lag. Any ideas on how I can make things run better?
Here is an example of a tile based game I'm trying to make that is having serious lag issues.
TexturePacker allows merge png files. It generates two files: png and config file. Png is just merged images and config file is txt file which you can load into your swf, parse and demerge your images using it. Config could be in various formats for different game engines.
Using Photoshop or similar software you would combine all of the animations frames into one file. The size and shape of the file can be whatever you want, but each of the 'frames' should be the same size, in the same order and with no space between them. For example, lets say each frame is 25x25px, your walk animation is 10 frames and you want the final .png to be one long strip. You would make a new .png with the dimensions of either 250X25 or 25X250 and then insert all of your frames into that one file in the order of the animation. It's up to you if you want to embed these as display object or files that get loaded, but once you have them you just need to use BitmapData to break up the input file into new BitmapData objects and then display them as needed. Going one step further, lets say that most if not all characters have a walk animation and an action animation, you would make a single class to deal with loading character animations and the first row of the image file would be the walk animation and the second would be the action animation.