Last active
August 29, 2015 13:57
-
-
Save gdm85/9924314 to your computer and use it in GitHub Desktop.
This is a simple program that packs a Tiled tileset by removing all-black tiles, ideally you want to use this between Image2Map.py and MapWriter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Tiled tileset packer | |
Copyright (c) 2014, gdm85 - https://github.com/gdm85 | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
3. Neither the name of the author nor the names of its contributors may | |
be used to endorse or promote products derived from this software without | |
specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY gdm85 ''AS IS'' AND ANY EXPRESS OR IMPLIED | |
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
IN NO EVENT SHALL gdm85 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/* | |
this is a simple program that packs a Tiled tileset by removing all-black tiles | |
ideally you want to use this between Image2Map.py and MapWriter.py | |
NOTE: expects square tiles only | |
*/ | |
import std.stdio; | |
import dsfml.graphics; | |
import core.runtime; | |
import std.conv; | |
immutable int tileSz = 32; | |
void main() | |
{ | |
auto args = Runtime.cArgs; | |
if (args.argc < 3) { | |
writeln("Please specify input and output tilesets"); | |
return; | |
} | |
auto tileset = new Image(); | |
if(!tileset.loadFromFile(to!string(args.argv[1]))) | |
throw new Exception("Cannot load source tileset"); | |
// create destination tileset with maximum possible tileset size | |
// it will be later trimmed down | |
auto origSize = tileset.getSize(); | |
int origTilesCount = origSize.x * origSize.y / (tileSz * tileSz); | |
auto packedTileset = new Image(); | |
packedTileset.create(tileSz, origTilesCount * tileSz, Color(0, 0, 0)); | |
auto newTilesCount = 0; | |
for(int x=0;x < origSize.x;x+=tileSz){ | |
for(int y=0;y < origSize.y;y+=tileSz){ | |
// skip a fully black tile | |
if (isBlackTile(tileset, x, y)) | |
continue; | |
writeln("Now writing tile at " ~ to!string(x) ~ ", " ~ to!string(y)); | |
stdout.flush(); | |
// add tile to packed tileset | |
// not using SFML copyImage because of a SEGFAULT | |
//packedTileset.copyImage(tileset, 0, newTilesCount * tileSz, IntRect(x, y, tileSz, tileSz)); | |
copyTile(tileset, x, y, packedTileset, 0, newTilesCount * tileSz); | |
newTilesCount++; | |
} | |
} | |
writeln("Packed " ~ to!string(origTilesCount) ~ " tiles into " ~ to!string(newTilesCount)); | |
// create new tileset with correct height | |
auto finalTileset = new Image(); | |
finalTileset.create(tileSz, newTilesCount * tileSz, Color(0, 0, 0)); | |
finalTileset.copyImage(packedTileset, 0, 0, IntRect(0,0,tileSz,newTilesCount*tileSz)); | |
finalTileset.saveToFile(to!string(args.argv[2])); | |
} | |
bool isBlackTile(Image tileset, int x, int y) { | |
int w = x + tileSz; | |
int h = y + tileSz; | |
for(;x < w;x++){ | |
for(;y < h;y++){ | |
auto pixel = tileset.getPixel(x, y); | |
if (pixel.r + pixel.g + pixel.b != 0) | |
return false; | |
} | |
} | |
return true; | |
} | |
void copyTile(Image source, int srcX, int srcY, Image dest, int destX, int destY) { | |
for(int x = 0;x < tileSz;x++){ | |
for(int y = 0;y < tileSz;y++){ | |
auto pixel = source.getPixel(srcX + x, srcY + y); | |
dest.setPixel(destX + x, destY + y, pixel); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment