From fe550ab764e83cedec0870d81aa02cd30651676c Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Wed, 18 Mar 2015 14:40:40 -0700 Subject: [PATCH] Add @:image embed for Lime --- lime/Assets.hx | 178 +++++++++++--------------- lime/graphics/Image.hx | 2 + lime/system/System.hx | 41 +----- templates/haxe/DefaultAssetLibrary.hx | 4 +- 4 files changed, 80 insertions(+), 145 deletions(-) diff --git a/lime/Assets.hx b/lime/Assets.hx index 1a5f90e01..8260acaf0 100644 --- a/lime/Assets.hx +++ b/lime/Assets.hx @@ -1,4 +1,5 @@ package lime; +import lime.graphics.ImageBuffer; #if !macro @@ -1268,111 +1269,6 @@ class Assets { } - macro public static function embedBitmap ():Array { - - #if (html5 && !openfl_html5_dom) - var fields = embedData (":bitmap", true); - #else - var fields = embedData (":bitmap"); - #end - - if (fields != null) { - - var constructor = macro { - - #if html5 - #if openfl_html5_dom - - super (width, height, transparent, fillRGBA); - - var currentType = Type.getClass (this); - - if (preload != null) { - - ___textureBuffer.width = Std.int (preload.width); - ___textureBuffer.height = Std.int (preload.height); - rect = new openfl.geom.Rectangle (0, 0, preload.width, preload.height); - setPixels(rect, preload.getPixels(rect)); - __buildLease(); - - } else { - - var byteArray = openfl.utils.ByteArray.fromBytes (haxe.Resource.getBytes(resourceName)); - - if (onload != null && !Std.is (onload, Bool)) { - - __loadFromBytes(byteArray, null, onload); - - } else { - - __loadFromBytes(byteArray); - - } - - } - - #else - - super (0, 0, transparent, fillRGBA); - - if (preload != null) { - - __sourceImage = preload; - width = __sourceImage.width; - height = __sourceImage.height; - - } else { - - __loadFromBase64 (haxe.Resource.getString(resourceName), resourceType, function (b) { - - if (preload == null) { - - preload = b.__sourceImage; - - } - - if (onload != null) { - - onload (b); - - } - - }); - - } - - #end - #else - - super (width, height, transparent, fillRGBA); - - var byteArray = openfl.utils.ByteArray.fromBytes (haxe.Resource.getBytes (resourceName)); - __loadFromBytes (byteArray); - - #end - - }; - - var args = [ { name: "width", opt: false, type: macro :Int, value: null }, { name: "height", opt: false, type: macro :Int, value: null }, { name: "transparent", opt: true, type: macro :Bool, value: macro true }, { name: "fillRGBA", opt: true, type: macro :Int, value: macro 0xFFFFFFFF } ]; - - #if html5 - args.push ({ name: "onload", opt: true, type: macro :Dynamic, value: null }); - #if openfl_html5_dom - fields.push ({ kind: FVar(macro :openfl.display.BitmapData, null), name: "preload", doc: null, meta: [], access: [ APublic, AStatic ], pos: Context.currentPos() }); - #else - fields.push ({ kind: FVar(macro :js.html.Image, null), name: "preload", doc: null, meta: [], access: [ APublic, AStatic ], pos: Context.currentPos() }); - #end - #end - - fields.push ({ name: "new", access: [ APublic ], kind: FFun({ args: args, expr: constructor, params: [], ret: null }), pos: Context.currentPos() }); - - } - - return fields; - - } - - private static function embedData (metaName:String, encode:Bool = false):Array { var classType = Context.getLocalClass().get(); @@ -1532,6 +1428,78 @@ class Assets { } + macro public static function embedImage ():Array { + + #if html5 + var fields = embedData (":image", true); + #else + var fields = embedData (":image"); + #end + + if (fields != null) { + + var constructor = macro { + + #if html5 + + super (); + + if (preload != null) { + + var buffer = new lime.graphics.ImageBuffer (); + buffer.__srcImage = preload; + buffer.width = preload.width; + buffer.width = preload.height; + + __fromImageBuffer (buffer); + + } else { + + __fromBase64 (haxe.Resource.getString (resourceName), resourceType, function (image) { + + if (preload == null) { + + preload = image.buffer.__srcImage; + + } + + if (onload != null) { + + onload (image); + + } + + }); + + } + + #else + + super (); + + var byteArray = lime.utils.ByteArray.fromBytes (haxe.Resource.getBytes (resourceName)); + __fromBytes (byteArray, null); + + #end + + }; + + var args = [ { name: "buffer", opt: true, type: macro :lime.graphics.ImageBuffer, value: null }, { name: "offsetX", opt: true, type: macro :Int, value: null }, { name: "offsetY", opt: true, type: macro :Int, value: null }, { name: "width", opt: true, type: macro :Int, value: null }, { name: "height", opt: true, type: macro :Int, value: null }, { name: "color", opt: true, type: macro :Null, value: null }, { name: "type", opt: true, type: macro :lime.graphics.ImageType, value: null } ]; + + #if html5 + args.push ({ name: "onload", opt: true, type: macro :Dynamic, value: null }); + fields.push ({ kind: FVar(macro :js.html.Image, null), name: "preload", doc: null, meta: [], access: [ APublic, AStatic ], pos: Context.currentPos() }); + #end + + fields.push ({ name: "new", access: [ APublic ], kind: FFun({ args: args, expr: constructor, params: [], ret: null }), pos: Context.currentPos() }); + + } + + return fields; + + } + + macro public static function embedSound ():Array { var fields = embedData (":sound"); diff --git a/lime/graphics/Image.hx b/lime/graphics/Image.hx index 52b5a20fa..0b0027539 100644 --- a/lime/graphics/Image.hx +++ b/lime/graphics/Image.hx @@ -46,6 +46,8 @@ import sys.io.File; @:access(lime.math.Rectangle) @:access(lime.math.Vector2) +@:autoBuild(lime.Assets.embedImage()) + class Image { diff --git a/lime/system/System.hx b/lime/system/System.hx index 0518c3121..40d89b732 100644 --- a/lime/system/System.hx +++ b/lime/system/System.hx @@ -1,5 +1,4 @@ package lime.system; -#if !macro #if flash @@ -163,7 +162,7 @@ class System { public static function load (library:String, method:String, args:Int = 0, lazy:Bool = false):Dynamic { - #if disable_cffi + #if (disable_cffi || macro) var disableCFFI = true; #end @@ -183,7 +182,7 @@ class System { } - #if !disable_cffi + #if (!disable_cffi && !macro) #if (sys && !html5) #if (iphone || emscripten || android || static_link) @@ -407,38 +406,4 @@ class System { #end -} - - -#else - - -import haxe.macro.Compiler; -import haxe.macro.Context; -import sys.FileSystem; - - -class System { - - - public static function includeTools () { - - var paths = Context.getClassPath (); - - for (path in paths) { - - if (FileSystem.exists (path + "/tools/CommandLineTools.hx")) { - - Compiler.addClassPath (path + "/tools"); - - } - - } - - } - - -} - - -#end +} \ No newline at end of file diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index 946ec5202..d1f9dce37 100644 --- a/templates/haxe/DefaultAssetLibrary.hx +++ b/templates/haxe/DefaultAssetLibrary.hx @@ -303,7 +303,7 @@ class DefaultAssetLibrary extends AssetLibrary { if (className.exists (id)) { var fontClass = className.get (id); - return Image.fromBytes (cast (Type.createInstance (fontClass, []), ByteArray)); + return cast (Type.createInstance (fontClass, []), Image); } else { @@ -694,7 +694,7 @@ class DefaultAssetLibrary extends AssetLibrary { #if (windows || mac || linux) ::if (assets != null):: -::foreach assets::::if (embed)::::if (type == "image")::@:file("::sourcePath::") class __ASSET__::flatName:: extends lime.utils.ByteArray {} +::foreach assets::::if (embed)::::if (type == "image")::@:image("::sourcePath::") class __ASSET__::flatName:: extends lime.graphics.Image {} ::elseif (type == "sound")::@:file("::sourcePath::") class __ASSET__::flatName:: extends lime.utils.ByteArray {} ::elseif (type == "music")::@:file("::sourcePath::") class __ASSET__::flatName:: extends lime.utils.ByteArray {} ::elseif (type == "font")::@:font("::sourcePath::") class __ASSET__::flatName:: extends lime.graphics.Font {}