From a722abca65d8afce3834939be1530001afd11903 Mon Sep 17 00:00:00 2001 From: underscorediscovery Date: Fri, 16 May 2014 18:15:56 -0230 Subject: [PATCH] Adding provisional getBytes for html5 and fixing Main.hx compile issues for SimpleOpenGL Although the PNG decoding is failing due to invalid header. --- lime/utils/Assets.hx | 68 +++++++++++++---------------- samples/SimpleOpenGL/Source/Main.hx | 28 +++++++++--- 2 files changed, 53 insertions(+), 43 deletions(-) diff --git a/lime/utils/Assets.hx b/lime/utils/Assets.hx index efab4b0a8..f7c0134ba 100644 --- a/lime/utils/Assets.hx +++ b/lime/utils/Assets.hx @@ -14,6 +14,7 @@ import lime.utils.ByteArray; import format.swf.lite.SWFLite; #else import format.SWF; + import lime.utils.UInt8Array; #end //js #end //swf @@ -69,45 +70,38 @@ import lime.utils.ByteArray; initialize(); - #if (tools && !display) - - if (AssetData.type.exists(id)) { - - #if flash - return Type.createInstance(AssetData.className.get(id), []); - #elseif js + if (AssetData.type.exists(id)) { + + #if lime_html5 + + var req = new haxe.Http(id); + var results : Dynamic = null; - #if !lime_html5 - var bytes:ByteArray = null; - var data = ApplicationMain.urlLoaders.get(AssetData.path.get(id)).data; - if (Std.is(data, String)) { - var bytes = new ByteArray(); - bytes.writeUTFBytes(data); - } else if (Std.is(data, ByteArray)) { - bytes = cast data; - } else { - bytes = null; - } + req.async = false; + req.onData = function(e) { + results = e; + } + req.request(); + req = null; - if (bytes != null) { - bytes.position = 0; - return bytes; - } else { - return null; - } - #end //lime_html5 - - #else //js or flash - - return ByteArray.readFile(AssetData.path.get(id)); - - #end - - } else { - trace("[lime.utils.Assets] There is no String or ByteArray asset with an ID of \"" + id + "\""); - } - - #end + var len : Int = results.length; + var bytearray : ByteArray = new ByteArray(); + for( i in 0 ... len ) { + bytearray.writeByte(results.charCodeAt(i)); + } + + bytearray.position = 0; + return bytearray; + + #else //lime_html5 + + return ByteArray.readFile(AssetData.path.get(id)); + + #end + + } else { + trace("[lime.utils.Assets] There is no String or ByteArray asset with an ID of \"" + id + "\""); + } return null; diff --git a/samples/SimpleOpenGL/Source/Main.hx b/samples/SimpleOpenGL/Source/Main.hx index f306e51ee..d3684421a 100644 --- a/samples/SimpleOpenGL/Source/Main.hx +++ b/samples/SimpleOpenGL/Source/Main.hx @@ -3,11 +3,15 @@ package; import format.png.Reader; import format.png.Tools; +import haxe.io.Bytes; +import haxe.io.BytesData; import haxe.io.BytesInput; import lime.gl.GL; import lime.gl.GLBuffer; import lime.gl.GLProgram; import lime.gl.GLTexture; +import lime.gl.GLUniformLocation; +import lime.utils.ByteArray; import lime.utils.Matrix3D; import lime.utils.Assets; import lime.utils.Float32Array; @@ -21,10 +25,10 @@ class Main { private var imageData:UInt8Array; private var imageHeight:Int; private var imageWidth:Int; - private var imageUniform:Int; + private var imageUniform:GLUniformLocation; private var lime:Lime; - private var modelViewMatrixUniform:Int; - private var projectionMatrixUniform:Int; + private var modelViewMatrixUniform:GLUniformLocation; + private var projectionMatrixUniform:GLUniformLocation; private var shaderProgram:GLProgram; private var texCoordAttribute:Int; private var texCoordBuffer:GLBuffer; @@ -146,13 +150,25 @@ class Main { } - + function arrayToBytes( array:ByteArray ):haxe.io.Bytes { + + if (array == null) return null; + var bytes:haxe.io.Bytes = haxe.io.Bytes.alloc(array.length); + for (n in 0 ... array.length) { + bytes.set(n, array.readByte()); + } + + return bytes; + + } + public function ready (lime:Lime):Void { this.lime = lime; - var bytes = Assets.getBytes ("assets/lime.png"); - var byteInput = new BytesInput (bytes, 0, bytes.length); + var bytes : ByteArray = Assets.getBytes ("assets/lime.png"); + var io_bytes : haxe.io.Bytes = arrayToBytes( bytes ); + var byteInput = new BytesInput ( io_bytes, 0, io_bytes.length); var png = new Reader (byteInput).read (); var data = Tools.extract32 (png); var header = Tools.getHeader (png);