HaxeFlixel / flixel

Free, cross-platform 2D game engine powered by Haxe and OpenFL
https://haxeflixel.com/
MIT License
1.98k stars 439 forks source link

Improve spritesheet handling #434

Closed Beeblerox closed 9 years ago

Beeblerox commented 11 years ago

avoid creation of new bitmapDatas. this will be usefull for native targets

Eiyeron commented 10 years ago

Idea : Add margin/spacer. For instance when loading a tilemap with its tilesheet (with margins) from TIled will results in unspaced sprites/tiles and thus wrong result. Do you wan I give an example?

Beeblerox commented 10 years ago

@Eiyeron yes, example will be helpful to understand what you mean

Eiyeron commented 10 years ago

Taking Kenney's Platformer Art tileset at an example : spritesheet

Notice the 2px margin between the tiles. If editors like Tiled manage this, Haxeflixel won't take account of them and cut the tileset wrongly.

Beeblerox commented 10 years ago

actually it should be supported already, but with some helper object:

var cached:CachedGraphics = FlxG.bitmap.add("assets/colortiles.png");
// top left corner of the first tile
var startX:Int = 2;
var startY:Int = 2;
// tile size
var tileWidth:Int = 10;
var tileHeight:Int = 10;
// tile spacing
var spacingX:Int = 2;
var spacingY:Int = 2;
// end of tiles
var width:Int = cached.bitmap.width - startX;
var height:Int = cached.bitmap.height - startY;
// define region
var textureRegion:TextureRegion = new TextureRegion(cached, startX, startY, tileWidth, tileHeight, spacingX, spacingY, width, height);
// and finally load it into the tilemap
add(level.loadMap(Assets.getText("assets/slopemap.txt"), textureRegion, tileWidth, tileHeight));
Eiyeron commented 10 years ago

Thanks, I was searching and trying to edit HF to include spacer. Sorry for my poor level in Haxe and HF.

Gama11 commented 10 years ago

@Beeblerox That seems like a lot of extra code that is required to make that work.. I'm thinking there should be a built-in way to do this, so it works out of the box. With a Spacing:FlxPoint param for FlxTilemap.loadMap() and FlxSprite.loadGraphic() maybe? Not quite sure, those functions have too many parameters already...

Beeblerox commented 10 years ago

@Gama11 yeah, i know that i need to improve it a lot

Beeblerox commented 10 years ago

@Eiyeron this feature isn't well known (actually i think that almost noone knows about it). so no need to sorry, because it's my fault that this feature isn't documented :)

Eiyeron commented 10 years ago

Why not creating some helper classes which automatize some operations? loadGraphic("url", HelperClass.THIS_WAY(arguments)); ?

Gama11 commented 10 years ago

@Eiyeron Yeah, I've thought about this as well, seems like there's lots of repeated code across classes for graphic-loading-functionality. FlxBitmapUtil might be suitable.

MintPaw commented 9 years ago

How do you do this padding stuff now? CachedGraphics and TextureRegion are gone.

Gama11 commented 9 years ago

Not gone, just replaced by something else. I think you're supposed to use FlxTileFrames.fromBitmapAddSpacesAndBorders() now, but @Beeblerox is the expert.

Beeblerox commented 9 years ago

now you have to use FlxTileFrames class:

// top left corner of the first tile
var startX:Int = 2;
var startY:Int = 2;
// tile size
var tileSize:FlxPoint = new FlxPoint(10, 10);
// tile spacing
var tileSpacing:FlxPoint = new FlxPoint(2, 2);
// region of image to use for tiles (optional)
var region:FlxRect = new FlxRect(startX, startY);
var tiles:FlxTileFrames = FlxTileFrames.fromRectangle("assets/colortiles.png", tileSize, region, tileSpacing);
add(level.loadMap(Assets.getText("assets/slopemap.txt"), tiles));
Beeblerox commented 9 years ago

FlxTileFrames.fromBitmapAddSpacesAndBorders() takes spritesheet without spaces between tile images, then adds provided spaces between them and adds borders around tile images (just copies their border pixels). this method could help avoid tile tearing problem on native targets

vnavarro commented 9 years ago

Hi,

I can't get any of this samples working. The current docs don't mention FlxTileFrames and I don't now what kind of object level is so I can't figure what loadMap expects. Furthermore the previous example isn't too much helpful either, I'm kind of lost here :sob: Could anyone provide a more complete example?

Thanks :smile:

sruloart commented 9 years ago

@vnavarro If you're not using the dev (git clone) version of HaxeFlixel you can't use FlxTileFrames, and if you aren't (i.e you use HaxeFlixel 3.3.9) you need to specify what exactly are you trying to achieve (preferably in the Google group: http://haxeflixel.com/forum/).

If you're trying to add "borders" to your spritesheet (i.e "padding") then search the issues / forum, the solution is out there :)

MelonFunction commented 8 years ago

@vnavarro Most/all of the solutions are outdated now.