Permalink
Browse files

Added js file downloading for export

  • Loading branch information...
hamaluik committed Oct 22, 2015
1 parent a6db89b commit 01332f60145ec38adef1fa1c34d0d8094a885f31
Showing with 107 additions and 4 deletions.
  1. +2 −0 jslibs/FileSaver.min.js
  2. +3 −0 jslibs/pako.min.js
  3. +12 −2 project.flow
  4. +12 −2 src/Main.hx
  5. +78 −0 src/js/BuildPNG.hx

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -18,19 +18,29 @@
app : {
name : 'AutoTerrainGen',
package : 'com.darekgreenly.AutoTerrainGen'
package : 'com.darekgreenly.AutoTerrainGen',
web : {
libs : {
FileSaver: 'FileSaver.min.js',
pako: 'pako.min.js'
}
}
},
build : {
dependencies : {
luxe : '*',
mint: '*',
format: '*',
FileSaver: '*',
pako: '*'
}
},
files : {
assets : 'input/'
assets : 'input/',
FileSaver : 'jslibs/FileSaver.min.js => FileSaver.min.js',
pako: 'jslibs/pako.min.js => pako.min.js'
}
}
@@ -725,9 +725,11 @@ class Main extends luxe.Game {
function prepare_export(){
#if !web
if(!sys.FileSystem.exists(sys.FileSystem.absolutePath('output/'))){
sys.FileSystem.createDirectory(sys.FileSystem.absolutePath('output/'));
}
#end
}
@@ -736,12 +738,16 @@ class Main extends luxe.Game {
prepare_export();
var data = format.png.Tools.build32BGRA( generator.w, generator.h, generator.output_pixels.toBytes() );
#if desktop
var data = format.png.Tools.build32BGRA( generator.w, generator.h, generator.output_pixels.toBytes() );
var out = sys.io.File.write( sys.FileSystem.absolutePath('output/output.png'), true);
new format.png.Writer(out).write(data);
add_log('File saved in output/output.png');
#elseif web
var data:js.html.Uint8Array = js.BuildPNG.build(generator.w, generator.h, generator.output_pixels.toBytes());
var blob:js.html.Blob = new js.html.Blob([data], {type: 'image/png'});
js.html.FileSaver.saveAs(blob, "output.png");
add_log("File saved as output.png");
#end
}
@@ -754,6 +760,10 @@ class Main extends luxe.Game {
var out = sys.io.File.write( sys.FileSystem.absolutePath('output/output.tsx'), false);
sys.io.File.saveContent("output/output.tsx", generator.tsx.toString());
add_log('File saved in output/output.tsx');
#elseif web
var blob:js.html.Blob = new js.html.Blob([generator.tsx.toString()], {type: 'text/xml;charset=utf8'});
js.html.FileSaver.saveAs(blob, "output.tsx");
add_log('File saved as output.tsx');
#end
}
@@ -0,0 +1,78 @@
package js;
#if web
import haxe.io.Bytes;
import haxe.io.BytesData;
import haxe.io.BytesOutput;
import js.html.Uint8Array;
import js.pako.Pako;
class BuildPNG {
private static var o:BytesOutput;
// shamelessly stolen and adapted from https://github.com/HaxeFoundation/format/tree/master/format/png
public static function build(width:Int, height:Int, data:Bytes):Uint8Array {
// create a stream of bytes
o = new BytesOutput();
o.bigEndian = true;
// add the PNG front matter
for(b in [137,80,78,71,13,10,26,10])
o.writeByte(b);
// add the header
var b = new BytesOutput();
b.bigEndian = true;
b.writeInt32(width);
b.writeInt32(height);
b.writeByte(8);
b.writeByte(6);
b.writeByte(0);
b.writeByte(0);
b.writeByte(0);
writeChunk("IHDR", b.getBytes());
// format the data
var rgba = Bytes.alloc(width * height * 4 + height);
var w = 0, r = 0;
for(y in 0...height) {
rgba.set(w++, 0); // no filter for this scanline
for(x in 0...width) {
rgba.set(w++, data.get(r + 2)); // r
rgba.set(w++, data.get(r + 1)); // g
rgba.set(w++, data.get(r)); // b
rgba.set(w++, data.get(r + 3)); // a
r += 4;
}
}
// deflate the data
var bytesData:BytesData = rgba.getData();
var arrForm:Uint8Array = new Uint8Array(bytesData);
var deflated:Uint8Array = Pako.deflate(arrForm);
var d = Bytes.alloc(deflated.length);
for(i in 0...deflated.length) {
d.set(i, deflated[i]);
}
writeChunk("IDAT", d);
// write the end
writeChunk("IEND", Bytes.alloc(0));
// return the array of bytes!
return new Uint8Array(o.getBytes().getData());
}
private static function writeChunk(id:String, data:Bytes) {
o.writeInt32(data.length);
o.writeString(id);
o.write(data);
var crc = new haxe.crypto.Crc32();
for(i in 0...4)
crc.byte(id.charCodeAt(i));
crc.update(data, 0, data.length);
o.writeInt32(crc.get());
}
}
#end

0 comments on commit 01332f6

Please sign in to comment.